· 5 years ago · Feb 25, 2020, 04:30 PM
1package cloudhealth
2
3import (
4 "strconv"
5
6 "./cloudhealth-sdk-go"
7 "github.com/hashicorp/terraform/helper/schema"
8)
9
10func resourceCloudHealthAwsAccount() *schema.Resource {
11 return &schema.Resource{
12 Create: resourceCloudHealthAwsAccountCreate,
13 Read: resourceCloudHealthAwsAccountRead,
14 Update: resourceCloudHealthAwsAccountUpdate,
15 Delete: resourceCloudHealthAwsAccountDelete,
16 Importer: &schema.ResourceImporter{
17 State: schema.ImportStatePassthrough,
18 },
19
20 Schema: map[string]*schema.Schema{
21 "name": {
22 Type: schema.TypeString,
23 Required: true,
24 },
25 "region": {
26 Type: schema.TypeString,
27 Optional: true,
28 Default: "global",
29 },
30 "billingbucket": {
31 Type: schema.TypeString,
32 Optional: true,
33 },
34 "authentication": {
35 Type: schema.TypeList,
36 Required: true,
37 MaxItems: 1,
38 Elem: &schema.Resource{
39 Schema: map[string]*schema.Schema{
40 "protocol": {
41 Type: schema.TypeString,
42 Required: true,
43 },
44 "access_key": {
45 Type: schema.TypeString,
46 Optional: true,
47 ConflictsWith: []string{"authentication.assume_role_arn", "authentication.assume_role_external_id"},
48 },
49 "secret_key": {
50 Type: schema.TypeString,
51 Optional: true,
52 ConflictsWith: []string{"authentication.assume_role_arn", "authentication.assume_role_external_id"},
53 DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
54 return true
55 },
56 },
57 "assume_role_arn": {
58 Type: schema.TypeString,
59 Optional: true,
60 ConflictsWith: []string{"authentication.access_key", "authentication.secret_key"},
61 },
62 "assume_role_external_id": {
63 Type: schema.TypeString,
64 Optional: true,
65 ConflictsWith: []string{"authentication.access_key", "authentication.secret_key"},
66 },
67 },
68 },
69 },
70 "cloudtrail": {
71 Type: schema.TypeList,
72 Optional: true,
73 MaxItems: 1,
74 Elem: &schema.Resource{
75 Schema: map[string]*schema.Schema{
76 "enabled": {
77 Type: schema.TypeBool,
78 Optional: true,
79 Default: false,
80 },
81 "bucket_name": {
82 Type: schema.TypeString,
83 Optional: true,
84 },
85 },
86 },
87 },
88 "config": {
89 Type: schema.TypeList,
90 Optional: true,
91 MaxItems: 1,
92 Elem: &schema.Resource{
93 Schema: map[string]*schema.Schema{
94 "enabled": {
95 Type: schema.TypeBool,
96 Optional: true,
97 Default: false,
98 },
99 "bucket_name": {
100 Type: schema.TypeString,
101 Optional: true,
102 },
103 "prefix": {
104 Type: schema.TypeString,
105 Optional: true,
106 },
107 },
108 },
109 },
110 },
111 }
112}
113
114func resourceCloudHealthAwsAccountCreate(d *schema.ResourceData, m interface{}) error {
115 client := m.(*cloudhealth.Client)
116
117 account, err := client.CreateAwsAccount(cloudhealth.AwsAccount{
118 Name: d.Get("name").(string),
119 Region: d.Get("region").(string),
120 Billing: cloudhealth.AwsBilling{
121 BillingBucket: d.Get("billingbucket").(string),
122 },
123 Authentication: cloudhealth.AwsAccountAuthentication{
124 Protocol: d.Get("authentication.0.protocol").(string),
125 AssumeRoleArn: d.Get("authentication.0.assume_role_arn").(string),
126 AssumeRoleExternalID: d.Get("authentication.0.assume_role_external_id").(string),
127 AccessKey: d.Get("authentication.0.access_key").(string),
128 SecretKey: d.Get("authentication.0.secret_key").(string),
129 },
130 Cloudtrail: cloudhealth.AwsCloudtrail{
131 Enabled: d.Get("cloudtrail.0.enabled").(bool),
132 CloudtrailBucket: d.Get("cloudtrail.0.bucket_name").(string),
133 },
134 Config: cloudhealth.AwsConfig{
135 Enabled: d.Get("config.0.enabled").(bool),
136 ConfigBucket: d.Get("config.0.bucket_name").(string),
137 Prefix: d.Get("config.0.prefix").(string),
138 },
139 })
140 if err != nil {
141 return err
142 }
143
144 d.SetId(strconv.Itoa(account.ID))
145
146 return resourceCloudHealthAwsAccountUpdate(d, m)
147}
148
149func resourceCloudHealthAwsAccountRead(d *schema.ResourceData, m interface{}) error {
150 client := m.(*cloudhealth.Client)
151
152 id, _ := strconv.Atoi(d.Id())
153 account, err := client.GetAwsAccount(id)
154 if err == cloudhealth.ErrAwsAccountNotFound {
155 d.SetId("")
156 return nil
157 }
158 if err != nil {
159 return err
160 }
161
162 d.Set("name", account.Name)
163 d.Set("region", account.Region)
164 auth := make(map[string]interface{})
165 authList := make([]map[string]interface{}, 0, 1)
166 auth["protocol"] = account.Authentication.Protocol
167 auth["assume_role_arn"] = account.Authentication.AssumeRoleArn
168 auth["assume_role_external_id"] = account.Authentication.AssumeRoleExternalID
169 auth["access_key"] = account.Authentication.AccessKey
170 authList = append(authList, auth)
171 d.Set("authentication", authList)
172 config := make(map[string]interface{})
173 configList := make([]map[string]interface{}, 0, 1)
174 config["enabled"] = account.Config.Enabled
175 config["bucket"] = account.Config.ConfigBucket
176 config["prefix"] = account.Config.Prefix
177 d.Set("config", configList)
178 cloudtrail := make(map[string]interface{})
179 cloudtrailList := make([]map[string]interface{}, 0, 1)
180 cloudtrail["enabled"] = account.Cloudtrail.Enabled
181 cloudtrail["bucket"] = account.Cloudtrail.CloudtrailBucket
182 d.Set("cloudtrail", cloudtrailList)
183
184 return nil
185}
186
187func resourceCloudHealthAwsAccountUpdate(d *schema.ResourceData, m interface{}) error {
188 client := m.(*cloudhealth.Client)
189
190 id, _ := strconv.Atoi(d.Id())
191 account := cloudhealth.AwsAccount{
192 ID: id,
193 Name: d.Get("name").(string),
194 Region: d.Get("region").(string),
195 Billing: cloudhealth.AwsBilling{
196 BillingBucket: d.Get("billingbucket").(string),
197 },
198 Authentication: cloudhealth.AwsAccountAuthentication{
199 Protocol: d.Get("authentication.0.protocol").(string),
200 AssumeRoleArn: d.Get("authentication.0.assume_role_arn").(string),
201 AssumeRoleExternalID: d.Get("authentication.0.assume_role_external_id").(string),
202 AccessKey: d.Get("authentication.0.access_key").(string),
203 SecretKey: d.Get("authentication.0.secret_key").(string),
204 },
205 Cloudtrail: cloudhealth.AwsCloudtrail{
206 Enabled: d.Get("cloudtrail.0.enabled").(bool),
207 CloudtrailBucket: d.Get("cloudtrail.0.bucket_name").(string),
208 },
209 Config: cloudhealth.AwsConfig{
210 Enabled: d.Get("config.0.enabled").(bool),
211 ConfigBucket: d.Get("config.0.bucket_name").(string),
212 Prefix: d.Get("config.0.prefix").(string),
213 },
214 }
215
216 updatedAccount, err := client.UpdateAwsAccount(account)
217 if err != nil {
218 return err
219 }
220
221 d.SetId(strconv.Itoa(updatedAccount.ID))
222
223 return resourceCloudHealthAwsAccountRead(d, m)
224}
225
226func resourceCloudHealthAwsAccountDelete(d *schema.ResourceData, m interface{}) error {
227 client := m.(*cloudhealth.Client)
228
229 id, _ := strconv.Atoi(d.Id())
230 err := client.DeleteAwsAccount(id)
231 if err != nil {
232 return err
233 }
234
235 d.SetId("")
236
237 return nil
238}