-
Notifications
You must be signed in to change notification settings - Fork 662
/
resource_ibm_enterprise.go
233 lines (212 loc) · 8.12 KB
/
resource_ibm_enterprise.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package enterprise
import (
"context"
"fmt"
"log"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/platform-services-go-sdk/enterprisemanagementv1"
)
func ResourceIBMEnterprise() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIbmEnterpriseCreate,
ReadContext: resourceIbmEnterpriseRead,
UpdateContext: resourceIbmEnterpriseUpdate,
DeleteContext: resourceIbmEnterpriseDelete,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"source_account_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the account that is used to create the enterprise.",
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the enterprise. This field must have 3 - 60 characters.",
ValidateFunc: validate.ValidateAllowedEnterpriseNameValue(),
},
"primary_contact_iam_id": {
Type: schema.TypeString,
Required: true,
Description: "The IAM ID of the enterprise primary contact, such as `IBMid-0123ABC`. The IAM ID must already exist.",
},
"domain": {
Type: schema.TypeString,
Optional: true,
Description: "A domain or subdomain for the enterprise, such as `example.com` or `my.example.com`.",
},
"url": {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the enterprise.",
},
"enterprise_account_id": {
Type: schema.TypeString,
Computed: true,
Description: "The enterprise account ID.",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The Cloud Resource Name (CRN) of the enterprise.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "The state of the enterprise.",
},
"primary_contact_email": {
Type: schema.TypeString,
Computed: true,
Description: "The email of the primary contact of the enterprise.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The time stamp at which the enterprise was created.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: "The IAM ID of the user or service that created the enterprise.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The time stamp at which the enterprise was last updated.",
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
Description: "The IAM ID of the user or service that updated the enterprise.",
},
},
}
}
func resourceIbmEnterpriseCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
enterpriseManagementClient, err := meta.(conns.ClientSession).EnterpriseManagementV1()
if err != nil {
return diag.FromErr(err)
}
createEnterpriseOptions := &enterprisemanagementv1.CreateEnterpriseOptions{}
createEnterpriseOptions.SetSourceAccountID(d.Get("source_account_id").(string))
createEnterpriseOptions.SetName(d.Get("name").(string))
createEnterpriseOptions.SetPrimaryContactIamID(d.Get("primary_contact_iam_id").(string))
if _, ok := d.GetOk("domain"); ok {
createEnterpriseOptions.SetDomain(d.Get("domain").(string))
}
createEnterpriseResponse, response, err := enterpriseManagementClient.CreateEnterpriseWithContext(context, createEnterpriseOptions)
if err != nil {
log.Printf("[DEBUG] CreateEnterpriseWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
d.SetId(*createEnterpriseResponse.EnterpriseID)
return resourceIbmEnterpriseRead(context, d, meta)
}
func resourceIbmEnterpriseRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
enterpriseManagementClient, err := meta.(conns.ClientSession).EnterpriseManagementV1()
if err != nil {
return diag.FromErr(err)
}
getEnterpriseOptions := &enterprisemanagementv1.GetEnterpriseOptions{}
getEnterpriseOptions.SetEnterpriseID(d.Id())
enterprise, response, err := enterpriseManagementClient.GetEnterpriseWithContext(context, getEnterpriseOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetEnterpriseWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
//if err = d.Set("source_account_id", enterprise.); err != nil {
// return diag.FromErr(fmt.Errorf("[ERROR] Error setting source_account_id: %s", err))
//}
if err = d.Set("name", enterprise.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
if err = d.Set("primary_contact_iam_id", enterprise.PrimaryContactIamID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting primary_contact_iam_id: %s", err))
}
if err = d.Set("domain", enterprise.Domain); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting domain: %s", err))
}
if err = d.Set("url", enterprise.URL); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting url: %s", err))
}
if err = d.Set("enterprise_account_id", enterprise.EnterpriseAccountID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting enterprise_account_id: %s", err))
}
if err = d.Set("crn", enterprise.CRN); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting crn: %s", err))
}
if err = d.Set("state", enterprise.State); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting state: %s", err))
}
if err = d.Set("primary_contact_email", enterprise.PrimaryContactEmail); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting primary_contact_email: %s", err))
}
if err = d.Set("created_at", enterprise.CreatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("created_by", enterprise.CreatedBy); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_by: %s", err))
}
if err = d.Set("updated_at", enterprise.UpdatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting updated_at: %s", err))
}
if err = d.Set("updated_by", enterprise.UpdatedBy); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting updated_by: %s", err))
}
return nil
}
func resourceIbmEnterpriseUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
enterpriseManagementClient, err := meta.(conns.ClientSession).EnterpriseManagementV1()
if err != nil {
return diag.FromErr(err)
}
updateEnterpriseOptions := &enterprisemanagementv1.UpdateEnterpriseOptions{}
updateEnterpriseOptions.SetEnterpriseID(d.Id())
hasChange := false
//if d.HasChange("source_account_id") {
//
// updateEnterpriseOptions.SetSourceAccountID(d.Get("source_account_id").(string))
// hasChange = true
//}
if d.HasChange("name") {
updateEnterpriseOptions.SetName(d.Get("name").(string))
hasChange = true
}
if d.HasChange("primary_contact_iam_id") {
updateEnterpriseOptions.SetPrimaryContactIamID(d.Get("primary_contact_iam_id").(string))
hasChange = true
}
if d.HasChange("domain") {
updateEnterpriseOptions.SetDomain(d.Get("domain").(string))
hasChange = true
}
if hasChange {
response, err := enterpriseManagementClient.UpdateEnterpriseWithContext(context, updateEnterpriseOptions)
if err != nil {
log.Printf("[DEBUG] UpdateEnterpriseWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
}
return resourceIbmEnterpriseRead(context, d, meta)
}
func resourceIbmEnterpriseDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.SetId("")
return nil
}