-
Notifications
You must be signed in to change notification settings - Fork 30
/
account.go
181 lines (153 loc) · 4.73 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package aws
import (
"context"
"encoding/json"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/client"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/jsonutil"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/uritemplates"
"io/ioutil"
"net/http"
"time"
)
type Account struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
OrganizationId *string `json:"organizationId,omitempty"`
AccountId *string `json:"accountId,omitempty"`
CloudProvider *string `json:"cloudProvider,omitempty"`
ProviderExternalId *string `json:"providerExternalId,omitempty"`
// Read-only fields.
CreatedAt *time.Time `json:"createdAt,omitempty"`
// forceSendFields is a list of field names (e.g. "Keys") to
// unconditionally include in API requests. By default, fields with
// empty values are omitted from API requests. However, any non-pointer,
// non-interface field appearing in ForceSendFields will be sent to the
// server regardless of whether the field is empty or not. This may be
// used to include empty fields in Patch requests.
forceSendFields []string
// nullFields is a list of field names (e.g. "Keys") to include in API
// requests with the JSON null value. By default, fields with empty
// values are omitted from API requests. However, any field with an
// empty value appearing in NullFields will be sent to the server as
// null. It is an error if a field in this list has a non-empty value.
// This may be used to include null fields in Patch requests.
nullFields []string
}
func (o *Account) SetId(v *string) *Account {
if o.ID = v; o.ID == nil {
o.nullFields = append(o.nullFields, "ID")
}
return o
}
func (o *Account) SetName(v *string) *Account {
if o.Name = v; o.Name == nil {
o.nullFields = append(o.nullFields, "Name")
}
return o
}
func (o Account) MarshalJSON() ([]byte, error) {
type noMethod Account
raw := noMethod(o)
return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields)
}
type CreateAccountInput struct {
Account *Account `json:"account,omitempty"`
}
type CreateAccountOutput struct {
Account *Account `json:"account,omitempty"`
}
func (s *ServiceOp) CreateAccount(ctx context.Context, input *CreateAccountInput) (*CreateAccountOutput, error) {
r := client.NewRequest(http.MethodPost, "/setup/account")
r.Obj = input
resp, err := client.RequireOK(s.Client.DoOrg(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
gs, err := accountsFromHttpResponse(resp)
if err != nil {
return nil, err
}
output := new(CreateAccountOutput)
if len(gs) > 0 {
output.Account = gs[0]
}
return output, nil
}
type ReadAccountInput struct {
AccountID *string `json:"account,omitempty"`
}
type ReadAccountOutput struct {
Account *Account `json:"account,omitempty"`
}
func (s *ServiceOp) ReadAccount(ctx context.Context, input *ReadAccountInput) (*ReadAccountOutput, error) {
path, err := uritemplates.Expand("/setup/account/{acctId}", uritemplates.Values{"acctId": spotinst.StringValue(input.AccountID)})
r := client.NewRequest(http.MethodGet, path)
r.Obj = input
resp, err := client.RequireOK(s.Client.DoOrg(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
gs, err := accountsFromHttpResponse(resp)
if err != nil {
return nil, err
}
output := new(ReadAccountOutput)
if len(gs) > 0 {
output.Account = gs[0]
}
return output, nil
}
func accountsFromHttpResponse(resp *http.Response) ([]*Account, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return accountsFromJSON(body)
}
func accountsFromJSON(in []byte) ([]*Account, error) {
var rw client.Response
if err := json.Unmarshal(in, &rw); err != nil {
return nil, err
}
out := make([]*Account, len(rw.Response.Items))
if len(out) == 0 {
return out, nil
}
for i, rb := range rw.Response.Items {
b, err := accountFromJSON(rb)
if err != nil {
return nil, err
}
out[i] = b
}
return out, nil
}
func accountFromJSON(in []byte) (*Account, error) {
b := new(Account)
if err := json.Unmarshal(in, b); err != nil {
return nil, err
}
return b, nil
}
type DeleteAccountInput struct {
AccountID *string `json:"accountId,omitempty"`
}
type DeleteAccountOutput struct{}
func (s *ServiceOp) DeleteAccount(ctx context.Context, input *DeleteAccountInput) (*DeleteAccountOutput, error) {
path, err := uritemplates.Expand("/setup/account/{accountId}", uritemplates.Values{
"accountId": spotinst.StringValue(input.AccountID),
})
if err != nil {
return nil, err
}
r := client.NewRequest(http.MethodDelete, path)
resp, err := client.RequireOK(s.Client.DoOrg(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return &DeleteAccountOutput{}, nil
}