-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_rms_resource_aggregation_authorization.go
180 lines (152 loc) · 6.3 KB
/
resource_huaweicloud_rms_resource_aggregation_authorization.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product RMS
// ---------------------------------------------------------------
package rms
import (
"context"
"fmt"
"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 Config PUT /v1/resource-manager/domains/{domain_id}/aggregators/aggregation-authorization
// @API Config GET /v1/resource-manager/domains/{domain_id}/aggregators/aggregation-authorization
// @API Config DELETE /v1/resource-manager/domains/{domain_id}/aggregators/aggregation-authorization/{id}
func ResourceAggregationAuthorization() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAggregationAuthCreate,
ReadContext: resourceAggregationAuthRead,
DeleteContext: resourceAggregationAuthDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the ID of the resource aggregation account to be authorized.`,
},
"urn": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the authorization identifier of the resource aggregation account.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the time when the resource aggregation account was authorized.`,
},
},
}
}
func buildCreateAggregationAuthBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"authorized_account_id": d.Get("account_id"),
}
return bodyParams
}
func resourceAggregationAuthCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
createAggregationAuthHttpUrl = "v1/resource-manager/domains/{domain_id}/aggregators/aggregation-authorization"
createAggregationAuthProduct = "rms"
)
cfg := meta.(*config.Config)
createAggregationAuthClient, err := cfg.NewServiceClient(createAggregationAuthProduct, cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating RMS Client: %s", err)
}
createAggregationAuthPath := createAggregationAuthClient.Endpoint + createAggregationAuthHttpUrl
createAggregationAuthPath = strings.ReplaceAll(createAggregationAuthPath, "{domain_id}", cfg.DomainID)
createAggregationAuthOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
createAggregationAuthOpt.JSONBody = buildCreateAggregationAuthBodyParams(d)
createAggregationAuthResp, err := createAggregationAuthClient.Request("PUT", createAggregationAuthPath, &createAggregationAuthOpt)
if err != nil {
return diag.Errorf("error creating aggregation authorization: %s", err)
}
createAggregationAuthRespBody, err := utils.FlattenResponse(createAggregationAuthResp)
if err != nil {
return diag.FromErr(err)
}
id, err := jmespath.Search("authorized_account_id", createAggregationAuthRespBody)
if err != nil {
return diag.Errorf("error creating aggregation authorization: ID is not found in API response")
}
d.SetId(id.(string))
return resourceAggregationAuthRead(ctx, d, meta)
}
func resourceAggregationAuthRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
getAggregationAuthHttpUrl = "v1/resource-manager/domains/{domain_id}/aggregators/aggregation-authorization"
getAggregationAuthProduct = "rms"
)
cfg := meta.(*config.Config)
getAggregationAuthClient, err := cfg.NewServiceClient(getAggregationAuthProduct, cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating RMS Client: %s", err)
}
getAggregationAuthPath := getAggregationAuthClient.Endpoint + getAggregationAuthHttpUrl
getAggregationAuthPath = strings.ReplaceAll(getAggregationAuthPath, "{domain_id}", cfg.DomainID)
getAggregationAuthPath += fmt.Sprintf("?account_id=%s", d.Id())
getAggregationAuthOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
getAggregationAuthResp, err := getAggregationAuthClient.Request("GET", getAggregationAuthPath, &getAggregationAuthOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving aggregation authorization")
}
respBody, err := utils.FlattenResponse(getAggregationAuthResp)
if err != nil {
return diag.FromErr(err)
}
item := utils.PathSearch("aggregation_authorizations[0]", respBody, nil)
if item == nil {
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "error retrieving aggregation authorization")
}
mErr := multierror.Append(nil,
d.Set("urn", utils.PathSearch("aggregation_authorization_urn", item, nil)),
d.Set("account_id", utils.PathSearch("authorized_account_id", item, nil)),
d.Set("created_at", utils.PathSearch("created_at", item, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func resourceAggregationAuthDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
deleteAggregationAuthHttpUrl = "v1/resource-manager/domains/{domain_id}/aggregators/aggregation-authorization/{id}"
deleteAggregationAuthProduct = "rms"
)
cfg := meta.(*config.Config)
deleteAggregationAuthClient, err := cfg.NewServiceClient(deleteAggregationAuthProduct, cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating RMS Client: %s", err)
}
deleteAggregationAuthPath := deleteAggregationAuthClient.Endpoint + deleteAggregationAuthHttpUrl
deleteAggregationAuthPath = strings.ReplaceAll(deleteAggregationAuthPath, "{domain_id}", cfg.DomainID)
deleteAggregationAuthPath = strings.ReplaceAll(deleteAggregationAuthPath, "{id}", d.Id())
deleteAggregationAuthOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
204,
},
}
_, err = deleteAggregationAuthClient.Request("DELETE", deleteAggregationAuthPath, &deleteAggregationAuthOpt)
if err != nil {
return diag.Errorf("error deleting aggregation authorization: %s", err)
}
return nil
}