-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmanaged_account.go
137 lines (119 loc) · 4.81 KB
/
managed_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package megaport
import (
"context"
"encoding/json"
"io"
)
type ManagedAccountService interface {
// ListManagedAccounts retrieves a list of managed accounts. Megaport Partners can use this command to list all the managed companies linked to their account.
ListManagedAccounts(ctx context.Context) ([]*ManagedAccount, error)
// CreateManagedAccount creates a new managed account. As a Megaport Partner, use this endpoint to create a new managed company.
CreateManagedAccount(ctx context.Context, req *ManagedAccountRequest) (*ManagedAccount, error)
// UpdateManagedAccount updates an existing managed account. As a Megaport Partner, use this endpoint to update an existing managed company. You identify the company by providing the companyUid as a parameter for the endpoint.
UpdateManagedAccount(ctx context.Context, companyUID string, req *ManagedAccountRequest) (*ManagedAccount, error)
// GetManagedAccount retrieves a managed account by name. As a Megaport Partner, use this endpoint to retrieve a managed company by name.
GetManagedAccount(ctx context.Context, companyUID string, managedAccountName string) (*ManagedAccount, error)
}
type ManagedAccountServiceOp struct {
Client *Client
}
type ManagedAccount struct {
AccountRef string `json:"accountRef"`
AccountName string `json:"accountName"`
CompanyUID string `json:"companyUid"`
}
type ManagedAccountRequest struct {
AccountName string `json:"accountName"` // A required string that specifies a unique, easily identifiable name for the account. The length can range from 1 to 128 characters.
AccountRef string `json:"accountRef"` // A required string that specifies a reference ID for the managed account. The accountRef is typically an identifier used in partner systems (for example, CRM or billing). This value is shown on the invoices as the Managed Account Reference. The accountRef also identifies the account in email notifications. (The accountRef value maps to the Managed Account UID in the Portal interface.)
}
type ManagedAccountAPIResponse struct {
Message string `json:"message"`
Terms string `json:"terms"`
Data *ManagedAccount `json:"data"`
}
type ManagedAccountListAPIResponse struct {
Message string `json:"message"`
Terms string `json:"terms"`
Data []*ManagedAccount `json:"data"`
}
// NewManagedAccountService creates a new instance of the ManagedAccount Service.
func NewManagedAccountService(c *Client) *ManagedAccountServiceOp {
return &ManagedAccountServiceOp{
Client: c,
}
}
func (svc *ManagedAccountServiceOp) ListManagedAccounts(ctx context.Context) ([]*ManagedAccount, error) {
path := "/v2/managedCompanies"
req, err := svc.Client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, req, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
var apiResponse *ManagedAccountListAPIResponse
if err := json.Unmarshal(body, &apiResponse); err != nil {
return nil, err
}
return apiResponse.Data, nil
}
func (svc *ManagedAccountServiceOp) CreateManagedAccount(ctx context.Context, req *ManagedAccountRequest) (*ManagedAccount, error) {
path := "/v2/managedCompanies"
clientReq, err := svc.Client.NewRequest(ctx, "POST", path, req)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var createManagedAccountResponse *ManagedAccountAPIResponse
if err := json.Unmarshal(body, &createManagedAccountResponse); err != nil {
return nil, err
}
return createManagedAccountResponse.Data, nil
}
func (svc *ManagedAccountServiceOp) UpdateManagedAccount(ctx context.Context, companyUID string, req *ManagedAccountRequest) (*ManagedAccount, error) {
path := "/v2/managedCompanies/" + companyUID
clientReq, err := svc.Client.NewRequest(ctx, "PUT", path, req)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var updateManagedAccountResponse *ManagedAccountAPIResponse
if err := json.Unmarshal(body, &updateManagedAccountResponse); err != nil {
return nil, err
}
return updateManagedAccountResponse.Data, nil
}
func (svc *ManagedAccountServiceOp) GetManagedAccount(ctx context.Context, companyUID string, managedAccountName string) (*ManagedAccount, error) {
accounts, err := svc.ListManagedAccounts(ctx)
if err != nil {
return nil, err
}
for _, account := range accounts {
if account.AccountName == managedAccountName {
return account, nil
}
}
return nil, ErrManagedAccountNotFound
}