/
resource_huaweicloud_aom_cmdb_environment.go
247 lines (209 loc) · 7.74 KB
/
resource_huaweicloud_aom_cmdb_environment.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product AOM
// ---------------------------------------------------------------
package aom
import (
"context"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"
"github.com/chnsz/golangsdk"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API AOM POST /v1/environments
// @API AOM DELETE /v1/environments/{environment_id}
// @API AOM GET /v1/environments/{environment_id}
// @API AOM PUT /v1/environments/{environment_id}
func ResourceCmdbEnvironment() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCmdbEnvironmentCreate,
ReadContext: resourceCmdbEnvironmentRead,
UpdateContext: resourceCmdbEnvironmentUpdate,
DeleteContext: resourceCmdbEnvironmentDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"component_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"os_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
// attributes
"enterprise_project_id": {
Type: schema.TypeString,
Computed: true,
},
"register_type": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func buildCreateEnvironmentBodyParams(d *schema.ResourceData, region string) map[string]interface{} {
bodyParams := map[string]interface{}{
"region": region,
"component_id": d.Get("component_id"),
"env_name": d.Get("name"),
"env_type": d.Get("type"),
"os_type": d.Get("os_type"),
"description": utils.ValueIngoreEmpty(d.Get("description")),
}
return bodyParams
}
func resourceCmdbEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
product := "aom"
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating AOM client: %s", err)
}
createEnvironmentHttpUrl := "v1/environments"
createEnvironmentPath := client.Endpoint + createEnvironmentHttpUrl
createEnvironmentOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"Content-Type": "application/json"},
}
createEnvironmentOpt.JSONBody = utils.RemoveNil(buildCreateEnvironmentBodyParams(d, region))
createEnvironmentResp, err := client.Request("POST", createEnvironmentPath, &createEnvironmentOpt)
if err != nil {
return diag.Errorf("error creating CMDB environment: %s", err)
}
createEnvironmentRespBody, err := utils.FlattenResponse(createEnvironmentResp)
if err != nil {
return diag.FromErr(err)
}
id, err := jmespath.Search("id", createEnvironmentRespBody)
if err != nil {
return diag.Errorf("error creating CMDB environment: ID is not found in API response")
}
d.SetId(id.(string))
return resourceCmdbEnvironmentRead(ctx, d, meta)
}
func resourceCmdbEnvironmentRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
product := "aom"
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient(product, cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating AOM client: %s", err)
}
getEnvironmentHttpUrl := "v1/environments/{environment_id}"
getEnvironmentPath := client.Endpoint + getEnvironmentHttpUrl
getEnvironmentPath = strings.ReplaceAll(getEnvironmentPath, "{environment_id}", d.Id())
getEnvironmentOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"Content-Type": "application/json"},
}
getEnvironmentResp, err := client.Request("GET", getEnvironmentPath, &getEnvironmentOpt)
if err != nil {
if hasErrorCode(err, EnvNotExistsCode) {
err = golangsdk.ErrDefault404{}
}
return common.CheckDeletedDiag(d, err, "error retrieving CMDB environment")
}
getEnvironmentRespBody, err := utils.FlattenResponse(getEnvironmentResp)
if err != nil {
return diag.FromErr(err)
}
mErr := multierror.Append(nil,
d.Set("region", region),
d.Set("component_id", utils.PathSearch("component_id", getEnvironmentRespBody, nil)),
d.Set("name", utils.PathSearch("env_name", getEnvironmentRespBody, nil)),
d.Set("type", utils.PathSearch("env_type", getEnvironmentRespBody, nil)),
d.Set("os_type", utils.PathSearch("os_type", getEnvironmentRespBody, nil)),
d.Set("register_type", utils.PathSearch("register_type", getEnvironmentRespBody, nil)),
d.Set("description", utils.PathSearch("description", getEnvironmentRespBody, nil)),
d.Set("enterprise_project_id", utils.PathSearch("eps_id", getEnvironmentRespBody, nil)),
d.Set("created_at", utils.PathSearch("create_time", getEnvironmentRespBody, nil)),
)
if err := mErr.ErrorOrNil(); err != nil {
return diag.Errorf("error setting CMDB environment fields: %s", err)
}
return nil
}
func buildUpdateEnvironmentBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"component_id": d.Get("component_id"),
"os_type": d.Get("os_type"),
"env_name": d.Get("name"),
"env_type": d.Get("type"),
"description": d.Get("description"),
}
return bodyParams
}
func resourceCmdbEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
product := "aom"
client, err := cfg.NewServiceClient(product, cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating AOM client: %s", err)
}
updateEnvironmentHttpUrl := "v1/environments/{environment_id}"
updateEnvironmentPath := client.Endpoint + updateEnvironmentHttpUrl
updateEnvironmentPath = strings.ReplaceAll(updateEnvironmentPath, "{environment_id}", d.Id())
updateEnvironmentOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"Content-Type": "application/json"},
}
updateEnvironmentOpt.JSONBody = utils.RemoveNil(buildUpdateEnvironmentBodyParams(d))
_, err = client.Request("PUT", updateEnvironmentPath, &updateEnvironmentOpt)
if err != nil {
return diag.Errorf("error updating CMDB environment: %s", err)
}
return resourceCmdbEnvironmentRead(ctx, d, meta)
}
func resourceCmdbEnvironmentDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
product := "aom"
client, err := cfg.NewServiceClient(product, cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating AOM client: %s", err)
}
deleteEnvironmentHttpUrl := "v1/environments/{environment_id}"
deleteEnvironmentPath := client.Endpoint + deleteEnvironmentHttpUrl
deleteEnvironmentPath = strings.ReplaceAll(deleteEnvironmentPath, "{environment_id}", d.Id())
deleteEnvironmentOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"Content-Type": "application/json"},
}
_, err = client.Request("DELETE", deleteEnvironmentPath, &deleteEnvironmentOpt)
if err != nil && !hasErrorCode(err, EnvNotExistsCode) {
return diag.Errorf("error deleting CMDB environment: %s", err)
}
return nil
}