github.com/aiven/aiven-go-client@v1.36.0/accounts.go (about) 1 package aiven 2 3 import ( 4 "errors" 5 "time" 6 ) 7 8 type ( 9 // AccountsHandler Aiven go-client handler for Accounts 10 AccountsHandler struct { 11 client *Client 12 } 13 14 // AccountsResponse represents Accounts (list of accounts) response 15 AccountsResponse struct { 16 APIResponse 17 Accounts []Account `json:"accounts"` 18 } 19 20 // AccountResponse represents a Account response 21 AccountResponse struct { 22 APIResponse 23 Account Account `json:"account"` 24 } 25 26 // Account represents account 27 Account struct { 28 Id string `json:"account_id,omitempty"` 29 Name string `json:"account_name"` 30 OwnerTeamId string `json:"account_owner_team_id,omitempty"` 31 CreateTime *time.Time `json:"create_time,omitempty"` 32 UpdateTime *time.Time `json:"update_time,omitempty"` 33 BillingEnabled bool `json:"account_billing_enabled,omitempty"` 34 TenantId string `json:"tenant_id,omitempty"` 35 PrimaryBillingGroupId string `json:"primary_billing_group_id,omitempty"` 36 IsAccountOwner bool `json:"is_account_owner,omitempty"` 37 ParentAccountId string `json:"parent_account_id,omitempty"` 38 OrganizationId string `json:"organization_id,omitempty"` 39 } 40 ) 41 42 // List returns a list of all existing accounts 43 func (h AccountsHandler) List() (*AccountsResponse, error) { 44 path := buildPath("account") 45 bts, err := h.client.doGetRequest(path, nil) 46 if err != nil { 47 return nil, err 48 } 49 50 var rsp AccountsResponse 51 if errR := checkAPIResponse(bts, &rsp); errR != nil { 52 return nil, errR 53 } 54 55 return &rsp, nil 56 } 57 58 // Get retrieves account by id 59 func (h AccountsHandler) Get(id string) (*AccountResponse, error) { 60 if id == "" { 61 return nil, errors.New("cannot get account by empty account id") 62 } 63 64 path := buildPath("account", id) 65 bts, err := h.client.doGetRequest(path, nil) 66 if err != nil { 67 return nil, err 68 } 69 70 var rsp AccountResponse 71 if errR := checkAPIResponse(bts, &rsp); errR != nil { 72 return nil, errR 73 } 74 75 return &rsp, nil 76 } 77 78 // Delete deletes an existing account by id 79 func (h AccountsHandler) Delete(id string) error { 80 if id == "" { 81 return errors.New("cannot delete account by empty account id") 82 } 83 84 path := buildPath("account", id) 85 bts, err := h.client.doDeleteRequest(path, nil) 86 if err != nil { 87 return err 88 } 89 90 return checkAPIResponse(bts, nil) 91 } 92 93 // Update updates an existing account 94 func (h AccountsHandler) Update(id string, account Account) (*AccountResponse, error) { 95 if id == "" { 96 return nil, errors.New("cannot update account by empty account id") 97 } 98 99 path := buildPath("account", id) 100 bts, err := h.client.doPutRequest(path, account) 101 if err != nil { 102 return nil, err 103 } 104 105 var rsp AccountResponse 106 if errR := checkAPIResponse(bts, &rsp); errR != nil { 107 return nil, errR 108 } 109 110 return &rsp, nil 111 } 112 113 // Create creates new account 114 func (h AccountsHandler) Create(account Account) (*AccountResponse, error) { 115 path := buildPath("account") 116 bts, err := h.client.doPostRequest(path, account) 117 if err != nil { 118 return nil, err 119 } 120 121 var rsp AccountResponse 122 if errR := checkAPIResponse(bts, &rsp); errR != nil { 123 return nil, errR 124 } 125 126 return &rsp, nil 127 }