Skip to content

Commit

Permalink
Add support for sub-accounts (#329)
Browse files Browse the repository at this point in the history
* Add support for sub-account route actions

* Add sub-account service handler to client

* Fix a type and clarify extra ID field
  • Loading branch information
optik-aper authored Oct 3, 2024
1 parent a39baf5 commit 6305d72
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
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
}

0 comments on commit 6305d72

Please sign in to comment.