-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_dli_datasource_connection_associate.go
231 lines (197 loc) · 7.89 KB
/
resource_huaweicloud_dli_datasource_connection_associate.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
package dli
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/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 DLI POST /v2.0/{project_id}/datasource/enhanced-connections/{connection_id}/associate-queue
// @API DLI GET /v2.0/{project_id}/datasource/enhanced-connections/{connection_id}
// @API DLI POST /v2.0/{project_id}/datasource/enhanced-connections/{connection_id}/disassociate-queue
func ResourceDatasourceConnectionAssociate() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDatasourceConnectionAssociateCreate,
ReadContext: resourceDatasourceConnectionAssociateRead,
UpdateContext: resourceDatasourceConnectionAssociateUpdate,
DeleteContext: resourceDatasourceConnectionAssociateDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The region where the datasource enhanced connection and elastic resource pools are located.`,
},
"connection_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The ID of the datasource enhanced connection to be associated.`,
},
"elastic_resource_pools": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The list of the elastic resource pool names.`,
},
},
}
}
func buildCreateDatasourceConnectionAssociateBodyParams(poolNames *schema.Set) map[string]interface{} {
return map[string]interface{}{
"elastic_resource_pools": utils.ExpandToStringListBySet(poolNames),
}
}
func associateResourcePoolsToConnection(client *golangsdk.ServiceClient, connectionId string, poolNames *schema.Set) error {
httpUrl := "v2.0/{project_id}/datasource/enhanced-connections/{connection_id}/associate-queue"
createPath := client.Endpoint + httpUrl
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
createPath = strings.ReplaceAll(createPath, "{connection_id}", connectionId)
createOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: utils.RemoveNil(buildCreateDatasourceConnectionAssociateBodyParams(poolNames)),
}
requestResp, err := client.Request("POST", createPath, &createOpt)
if err != nil {
return fmt.Errorf("error associating the elastic resource pools to the enhanced connection: %s", err)
}
respBody, err := utils.FlattenResponse(requestResp)
if err != nil {
return err
}
if !utils.PathSearch("is_success", respBody, true).(bool) {
return fmt.Errorf("unable to associate the elastic resource pools to the enhanced connection: %s",
utils.PathSearch("message", respBody, "Message Not Found"))
}
return nil
}
func resourceDatasourceConnectionAssociateCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
connectionId = d.Get("connection_id").(string)
)
client, err := cfg.NewServiceClient("dli", region)
if err != nil {
return diag.Errorf("error creating DLI client: %s", err)
}
err = associateResourcePoolsToConnection(client, connectionId, d.Get("elastic_resource_pools").(*schema.Set))
if err != nil {
return diag.FromErr(err)
}
d.SetId(connectionId)
return resourceDatasourceConnectionAssociateRead(ctx, d, meta)
}
func GetDatasourceConnectionAssociatedPoolNames(client *golangsdk.ServiceClient, connectionId string) ([]interface{}, error) {
respBody, err := getConnectionById(client, connectionId)
if err != nil {
return nil, err
}
associatedPoolNames := utils.PathSearch("elastic_resource_pools[*].name", respBody, make([]interface{}, 0)).([]interface{})
if len(associatedPoolNames) < 1 {
return nil, golangsdk.ErrDefault404{}
}
return associatedPoolNames, nil
}
func resourceDatasourceConnectionAssociateRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
connectionId = d.Id()
)
client, err := cfg.NewServiceClient("dli", region)
if err != nil {
return diag.Errorf("error creating DLI client: %s", err)
}
associatedPoolNames, err := GetDatasourceConnectionAssociatedPoolNames(client, connectionId)
if err != nil {
return common.CheckDeletedDiag(d, err, "DLI Enhanced connection associate elastic resource pools")
}
mErr := multierror.Append(
d.Set("region", region),
d.Set("connection_id", connectionId),
d.Set("elastic_resource_pools", associatedPoolNames),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func buildCreateDatasourceConnectionDisassociateBodyParams(poolNames *schema.Set) map[string]interface{} {
return map[string]interface{}{
"elastic_resource_pools": utils.ExpandToStringListBySet(poolNames),
}
}
func disassociateResourcePoolsToConnection(client *golangsdk.ServiceClient, connectionId string, poolNames *schema.Set) error {
httpUrl := "v2.0/{project_id}/datasource/enhanced-connections/{connection_id}/disassociate-queue"
createPath := client.Endpoint + httpUrl
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
createPath = strings.ReplaceAll(createPath, "{connection_id}", connectionId)
createOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: utils.RemoveNil(buildCreateDatasourceConnectionDisassociateBodyParams(poolNames)),
}
requestResp, err := client.Request("POST", createPath, &createOpt)
if err != nil {
return fmt.Errorf("error disassociating the elastic resource pools from the enhanced connection: %s", err)
}
respBody, err := utils.FlattenResponse(requestResp)
if err != nil {
return err
}
if !utils.PathSearch("is_success", respBody, true).(bool) {
return fmt.Errorf("unable to disassociate the elastic resource pools from the enhanced connection: %s",
utils.PathSearch("message", respBody, "Message Not Found"))
}
return nil
}
func resourceDatasourceConnectionAssociateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
connectionId = d.Get("connection_id").(string)
)
client, err := cfg.NewServiceClient("dli", region)
if err != nil {
return diag.Errorf("error creating DLI client: %s", err)
}
oldRaws, newRaws := d.GetChange("elastic_resource_pools")
associatedPoolNames := newRaws.(*schema.Set).Difference(oldRaws.(*schema.Set))
disassociatedPoolNames := oldRaws.(*schema.Set).Difference(newRaws.(*schema.Set))
if disassociatedPoolNames.Len() > 0 {
err = disassociateResourcePoolsToConnection(client, connectionId, disassociatedPoolNames)
if err != nil {
return diag.FromErr(err)
}
}
if associatedPoolNames.Len() > 0 {
err = associateResourcePoolsToConnection(client, connectionId, associatedPoolNames)
if err != nil {
return diag.FromErr(err)
}
}
return resourceDatasourceConnectionAssociateRead(ctx, d, meta)
}
func resourceDatasourceConnectionAssociateDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
connectionId = d.Id()
)
client, err := cfg.NewServiceClient("dli", region)
if err != nil {
return diag.Errorf("error creating DLI client: %s", err)
}
disassociatedPoolNames, _ := d.GetChange("elastic_resource_pools")
err = disassociateResourcePoolsToConnection(client, connectionId, disassociatedPoolNames.(*schema.Set))
if err != nil {
return diag.FromErr(err)
}
return nil
}