Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sub-accounts #329

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions govultr.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Client struct {
Snapshot SnapshotService
SSHKey SSHKeyService
StartupScript StartupScriptService
SubAccount SubAccountService
User UserService
VPC VPCService
VPC2 VPC2Service
Expand Down Expand Up @@ -141,6 +142,7 @@ func NewClient(httpClient *http.Client) *Client {
client.Snapshot = &SnapshotServiceHandler{client}
client.SSHKey = &SSHKeyServiceHandler{client}
client.StartupScript = &StartupScriptServiceHandler{client}
client.SubAccount = &SubAccountServiceHandler{client}
client.User = &UserServiceHandler{client}
client.VPC = &VPCServiceHandler{client}
client.VPC2 = &VPC2ServiceHandler{client}
Expand Down
75 changes: 75 additions & 0 deletions subaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package govultr

import (
"context"
"net/http"
)

// SubAccountService is the interface to interact with sub-accounts endpoint on the Vultr API
// Link : https://www.vultr.com/api/#tag/subaccount
type SubAccountService interface {
List(ctx context.Context, options *ListOptions) ([]SubAccount, *Meta, *http.Response, error)
Create(ctx context.Context, saReq *SubAccountReq) (*SubAccount, *http.Response, error)
}

// SubAccountServiceHandler handles interaction with the account methods for the Vultr API
type SubAccountServiceHandler struct {
client *Client
}

type subAccountsBase struct {
SubAccounts []SubAccount `json:"subaccounts"`
Meta *Meta `json:"meta"`
}

// SubAccount represents a Vultr sub-account
type SubAccount struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"subaccount_name"`
OtherID string `json:"subaccount_id"`
Activated bool `json:"activated"`
Balance int `json:"balance"`
PendingCharges int `json:"pending_charges"`
}

// SubAccountReq is the sub-account struct for create calls
type SubAccountReq struct {
Email string `json:"email"`
Name string `json:"subaccount_name,omitempty"`
OtherID string `json:"subaccount_id,omitempty"`
}

// List all sub-accounts
func (s *SubAccountServiceHandler) List(ctx context.Context, options *ListOptions) ([]SubAccount, *Meta, *http.Response, error) {
uri := "/v2/subaccounts"
req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}

sas := new(subAccountsBase)
resp, err := s.client.DoWithContext(ctx, req, sas)
if err != nil {
return nil, nil, resp, err
}

return sas.SubAccounts, sas.Meta, resp, nil
}

// Create a sub-account
func (s *SubAccountServiceHandler) Create(ctx context.Context, saReq *SubAccountReq) (*SubAccount, *http.Response, error) {
uri := "/v2/subaccounts"
req, err := s.client.NewRequest(ctx, http.MethodPost, uri, saReq)
if err != nil {
return nil, nil, err
}

sa := new(SubAccount)
resp, err := s.client.DoWithContext(ctx, req, sa)
if err != nil {
return nil, resp, err
}

return sa, resp, nil
}
Loading