forked from nextgenhealthcare/cloudhealth-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaws_accounts.go
More file actions
165 lines (140 loc) · 5.23 KB
/
Copy pathaws_accounts.go
File metadata and controls
165 lines (140 loc) · 5.23 KB
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
package cloudhealth
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
)
// AwsAccounts represents all AWS Accounts enabled in CloudHealth with their configurations.
type AwsAccounts struct {
AwsAccounts []AwsAccount `json:"aws_accounts"`
}
// AwsAccount represents the configuration of an AWS Account enabled in CloudHealth.
type AwsAccount struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
OwnerID string `json:"owner_id,omitempty"`
HidePublicFields bool `json:"hide_public_fields,omitempty"`
Region string `json:"region,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
AccountType string `json:"account_type,omitempty"`
VpcOnly bool `json:"vpc_only,omitempty"`
ClusterName string `json:"cluster_name,omitempty"`
Status AwsAccountStatus `json:"status,omitempty"`
Authentication AwsAccountAuthentication `json:"authentication,omitempty"`
Tags []map[string]string `json:"tags,omitempty"`
}
// AwsAccountStatus represents the status details for AWS integration.
type AwsAccountStatus struct {
Level string `json:"level"`
LastUpdate time.Time `json:"last_update,omitempty"`
}
// AwsAccountAuthentication represents the authentication details for AWS integration.
type AwsAccountAuthentication struct {
Protocol string `json:"protocol,omitempty"`
AccessKey string `json:"access_key,omitempty"`
SecreyKey string `json:"secret_key,omitempty"`
AssumeRoleArn string `json:"assume_role_arn,omitempty"`
AssumeRoleExternalID string `json:"assume_role_external_id,omitempty"`
}
// AwsExternalID is used to enable integration with AWS via IAM Roles.
type AwsExternalID struct {
ExternalID string `json:"generated_external_id,omitempty"`
}
// GetAwsAccount gets the AWS Account with the specified CloudHealth Account ID. (deprecated, will be removed in future, kept only to not break anything)
func (s *Client) GetAwsAccount(id int) (*AwsAccount, error) {
return s.GetSingleAwsAccount(id)
}
// GetSingleAwsAccount gets the AWS Account with the specified CloudHealth Account ID.
func (s *Client) GetSingleAwsAccount(id int) (*AwsAccount, error) {
relativeURL, _ := url.Parse(fmt.Sprintf("aws_accounts/%d?api_key=%s", id, s.APIKey))
responseBody, err := getResponsePage(s, relativeURL)
if err != nil {
return nil, err
}
var account = new(AwsAccount)
err = json.Unmarshal(responseBody, &account)
if err != nil {
return nil, err
}
return account, nil
}
// GetAwsAccounts gets all AWS Accounts enabled in CloudHealth.
func (s *Client) GetAwsAccounts() (*AwsAccounts, error) {
awsaccounts := new(AwsAccounts)
page := 1
for {
params := url.Values{"page": {strconv.Itoa(page)}, "per_page": {"100"}, "api_key": {s.APIKey}}
relativeURL, _ := url.Parse(fmt.Sprintf("aws_accounts/?%s", params.Encode()))
responseBody, err := getResponsePage(s, relativeURL)
if err != nil {
return nil, err
}
var acts = new(AwsAccounts)
err = json.Unmarshal(responseBody, &acts)
if err != nil {
return nil, err
}
for _, p := range acts.AwsAccounts {
awsaccounts.AwsAccounts = append(awsaccounts.AwsAccounts, p)
}
if len(acts.AwsAccounts) < 100 {
break
}
page++
}
return awsaccounts, nil
}
// CreateAwsAccount enables a new AWS Account in CloudHealth.
func (s *Client) CreateAwsAccount(account AwsAccount) (*AwsAccount, error) {
relativeURL, _ := url.Parse(fmt.Sprintf("aws_accounts?api_key=%s", s.APIKey))
responseBody, err := createResource(s, relativeURL, account)
if err != nil {
return nil, err
}
var returnedAccount = new(AwsAccount)
err = json.Unmarshal(responseBody, &returnedAccount)
if err != nil {
return nil, err
}
return returnedAccount, nil
}
// UpdateAwsAccount updates an existing AWS Account in CloudHealth.
func (s *Client) UpdateAwsAccount(account AwsAccount) (*AwsAccount, error) {
relativeURL, _ := url.Parse(fmt.Sprintf("aws_accounts/%d?api_key=%s", account.ID, s.APIKey))
responseBody, err := updateResource(s, relativeURL, account)
if err != nil {
return nil, err
}
var returnedAccount = new(AwsAccount)
err = json.Unmarshal(responseBody, &returnedAccount)
if err != nil {
return nil, err
}
return returnedAccount, nil
}
// DeleteAwsAccount removes the AWS Account with the specified CloudHealth ID.
func (s *Client) DeleteAwsAccount(id int) error {
relativeURL, _ := url.Parse(fmt.Sprintf("aws_accounts/%d?api_key=%s", id, s.APIKey))
_, err := deleteResource(s, relativeURL)
if err != nil {
return err
}
return nil
}
// GetAwsExternalID gets the AWS External ID tied to the CloudHealth Account.
func (s *Client) GetAwsExternalID(id int) (*AwsExternalID, error) {
relativeURL, _ := url.Parse(fmt.Sprintf("aws_accounts/%d/generate_external_id?api_key=%s", id, s.APIKey))
responseBody, err := getResponsePage(s, relativeURL)
if err != nil {
return nil, err
}
var extid = new(AwsExternalID)
err = json.Unmarshal(responseBody, &extid)
if err != nil {
return nil, err
}
return extid, nil
}