-
Notifications
You must be signed in to change notification settings - Fork 30
/
awscredentials.go
141 lines (119 loc) · 3.21 KB
/
awscredentials.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
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"
"io/ioutil"
"net/http"
)
type Credentials struct {
IamRole *string `json:"iamRole,omitempty"`
AccountId *string `json:"accountId,omitempty"`
forceSendFields []string
nullFields []string
}
func (o Credentials) MarshalJSON() ([]byte, error) {
type noMethod Credentials
raw := noMethod(o)
return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields)
}
func (o *Credentials) SetIamRole(v *string) *Credentials {
if o.IamRole = v; o.IamRole == nil {
o.nullFields = append(o.nullFields, "IamRole")
}
return o
}
func (o *Credentials) SetAccountId(v *string) *Credentials {
if o.AccountId = v; o.AccountId == nil {
o.nullFields = append(o.nullFields, "AccountId")
}
return o
}
type SetCredentialsInput struct {
Credentials *Credentials `json:"credentials,omitempty"`
}
type SetCredentialsOutput struct {
Credentials *Credentials `json:"Credentials,omitempty"`
}
func (s *ServiceOp) Credentials(ctx context.Context, input *SetCredentialsInput) (*SetCredentialsOutput, error) {
r := client.NewRequest(http.MethodPost, "/setup/credentials/aws")
if input != nil {
r.Params.Set("accountId", spotinst.StringValue(input.Credentials.AccountId))
}
input.Credentials.AccountId = nil
r.Obj = input
resp, err := client.RequireOK(s.Client.DoOrg(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
gs, err := credentialsFromHttpResponse(resp)
if err != nil {
return nil, err
}
output := new(SetCredentialsOutput)
if len(gs) > 0 {
output.Credentials = gs[0]
}
return output, nil
}
type ReadCredentialsInput struct {
AccountId *string `json:"accountId,omitempty"`
}
type ReadCredentialsOutput struct {
Credentials *Credentials `json:"Credentials,omitempty"`
}
func (s *ServiceOp) ReadCredentials(ctx context.Context, input *ReadCredentialsInput) (*ReadCredentialsOutput, error) {
r := client.NewRequest(http.MethodGet, "/setup/credentials/aws")
if input != nil {
r.Params.Set("accountId", spotinst.StringValue(input.AccountId))
}
resp, err := client.RequireOK(s.Client.DoOrg(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
gs, err := credentialsFromHttpResponse(resp)
if err != nil {
return nil, err
}
output := new(ReadCredentialsOutput)
if len(gs) > 0 {
output.Credentials = gs[0]
}
return output, nil
}
func credentialsFromHttpResponse(resp *http.Response) ([]*Credentials, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return credentialsFromJSON(body)
}
func credentialsFromJSON(in []byte) ([]*Credentials, error) {
var rw client.Response
if err := json.Unmarshal(in, &rw); err != nil {
return nil, err
}
out := make([]*Credentials, len(rw.Response.Items))
if len(out) == 0 {
return out, nil
}
for i, rb := range rw.Response.Items {
b, err := credentialFromJSON(rb)
if err != nil {
return nil, err
}
out[i] = b
}
return out, nil
}
func credentialFromJSON(in []byte) (*Credentials, error) {
b := new(Credentials)
if err := json.Unmarshal(in, b); err != nil {
return nil, err
}
return b, nil
}