@@ -2,6 +2,7 @@ package provider
2
2
3
3
import (
4
4
"context"
5
+ "time"
5
6
6
7
"github.com/hashicorp/terraform-plugin-log/tflog"
7
8
@@ -18,6 +19,9 @@ func resourceVdbGroup() *schema.Resource {
18
19
ReadContext : resourceVdbGroupRead ,
19
20
UpdateContext : resourceVdbGroupUpdate ,
20
21
DeleteContext : resourceVdbGroupDelete ,
22
+ Importer : & schema.ResourceImporter {
23
+ StateContext : schema .ImportStatePassthroughContext ,
24
+ },
21
25
22
26
Schema : map [string ]* schema.Schema {
23
27
"id" : {
@@ -38,6 +42,7 @@ func resourceVdbGroup() *schema.Resource {
38
42
"tags" : {
39
43
Type : schema .TypeList ,
40
44
Optional : true ,
45
+ Computed : true ,
41
46
Elem : & schema.Resource {
42
47
Schema : map [string ]* schema.Schema {
43
48
"key" : {
@@ -105,24 +110,39 @@ func resourceVdbGroupRead(ctx context.Context, d *schema.ResourceData, meta inte
105
110
106
111
func resourceVdbGroupUpdate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
107
112
client := meta .(* apiClient ).client
108
-
109
113
vdbGroupId := d .Id ()
110
114
111
- if d .HasChange ("name" ) || d .HasChange ("vdb_ids" ) {
112
- updateVdbGroupReq := * dctapi .NewUpdateVDBGroupParameters ()
113
- if d .HasChange ("name" ) {
114
- updateVdbGroupReq .SetName (d .Get ("name" ).(string ))
115
+ // Existing update logic
116
+ updateVdbGroupReq := * dctapi .NewUpdateVDBGroupParameters ()
117
+ if d .HasChange ("name" ) {
118
+ updateVdbGroupReq .SetName (d .Get ("name" ).(string ))
119
+ }
120
+ if d .HasChange ("vdb_ids" ) {
121
+ updateVdbGroupReq .SetVdbIds (toStringArray (d .Get ("vdb_ids" )))
122
+ }
123
+
124
+ _ , httpRes , err := client .VDBGroupsAPI .UpdateVdbGroupById (ctx , vdbGroupId ).UpdateVDBGroupParameters (updateVdbGroupReq ).Execute ()
125
+ if diags := apiErrorResponseHelper (ctx , nil , httpRes , err ); diags != nil {
126
+ return diags
127
+ }
128
+
129
+ // Polling logic for name/vdb_ids update
130
+ maxAttempts := 10
131
+ for attempt := 1 ; attempt <= maxAttempts ; attempt ++ {
132
+ status , err := PollVdbGroupStatus (ctx , client , vdbGroupId )
133
+ if err != nil {
134
+ return diag .FromErr (err )
115
135
}
116
- if d . HasChange ( "vdb_ids" ) {
117
- updateVdbGroupReq . SetVdbIds ( toStringArray ( d . Get ( "vdb_ids" )))
136
+ if status == "RUNNING" {
137
+ break
118
138
}
119
-
120
- _ , httpRes , err := client .VDBGroupsAPI .UpdateVdbGroupById (ctx , vdbGroupId ).UpdateVDBGroupParameters (updateVdbGroupReq ).Execute ()
121
- if diags := apiErrorResponseHelper (ctx , nil , httpRes , err ); diags != nil {
122
- return diags
139
+ if status == "FAILED" || status == "CANCELED" {
140
+ return diag .Errorf ("VDB group update failed with status: %s" , status )
123
141
}
142
+ time .Sleep (time .Duration (STATUS_POLL_SLEEP_TIME ) * time .Second )
124
143
}
125
144
145
+ // Tag update logic
126
146
if d .HasChange ("tags" ) {
127
147
oldTags , newTags := d .GetChange ("tags" )
128
148
oldTagList := oldTags .([]interface {})
@@ -152,65 +172,85 @@ func resourceVdbGroupUpdate(ctx context.Context, d *schema.ResourceData, meta in
152
172
newTagMap [key ][value ] = true
153
173
}
154
174
155
- // Delete removed tags
156
- for key , oldValues := range oldTagMap {
157
- newValues , exists := newTagMap [key ]
158
- if ! exists {
159
- // Key doesn't exist in new tags, delete all values for this key
175
+ // If newTagList is empty, delete all existing tags
176
+ if len (newTagList ) == 0 {
177
+ for key := range oldTagMap {
160
178
deleteTag := * dctapi .NewDeleteTag ()
161
179
deleteTag .SetKey (key )
162
180
httpRes , err := client .VDBGroupsAPI .DeleteVdbGroupTags (ctx , vdbGroupId ).DeleteTag (deleteTag ).Execute ()
163
181
if diags := apiErrorResponseHelper (ctx , nil , httpRes , err ); diags != nil {
164
182
return diags
165
183
}
166
- } else {
167
- // Key exists, delete only values that are not in new tags
168
- for oldValue := range oldValues {
169
- if ! newValues [oldValue ] {
170
- deleteTag := * dctapi .NewDeleteTag ()
171
- deleteTag .SetKey (key )
172
- deleteTag .SetValue (oldValue )
173
- httpRes , err := client .VDBGroupsAPI .DeleteVdbGroupTags (ctx , vdbGroupId ).DeleteTag (deleteTag ).Execute ()
174
- if diags := apiErrorResponseHelper (ctx , nil , httpRes , err ); diags != nil {
175
- return diags
184
+ }
185
+ } else {
186
+ // Delete removed tags
187
+ for key , oldValues := range oldTagMap {
188
+ newValues , exists := newTagMap [key ]
189
+ if ! exists {
190
+ // Key doesn't exist in new tags, delete all values for this key
191
+ deleteTag := * dctapi .NewDeleteTag ()
192
+ deleteTag .SetKey (key )
193
+ httpRes , err := client .VDBGroupsAPI .DeleteVdbGroupTags (ctx , vdbGroupId ).DeleteTag (deleteTag ).Execute ()
194
+ if diags := apiErrorResponseHelper (ctx , nil , httpRes , err ); diags != nil {
195
+ return diags
196
+ }
197
+ } else {
198
+ // Key exists, delete only values that are not in new tags
199
+ for oldValue := range oldValues {
200
+ if ! newValues [oldValue ] {
201
+ deleteTag := * dctapi .NewDeleteTag ()
202
+ deleteTag .SetKey (key )
203
+ deleteTag .SetValue (oldValue )
204
+ httpRes , err := client .VDBGroupsAPI .DeleteVdbGroupTags (ctx , vdbGroupId ).DeleteTag (deleteTag ).Execute ()
205
+ if diags := apiErrorResponseHelper (ctx , nil , httpRes , err ); diags != nil {
206
+ return diags
207
+ }
176
208
}
177
209
}
178
210
}
179
211
}
180
- }
181
212
182
- // Create new tags
183
- var tags []dctapi.Tag
184
- for key , newValues := range newTagMap {
185
- oldValues , exists := oldTagMap [key ]
186
- if ! exists {
187
- // Key doesn't exist in old tags, create all values
188
- for value := range newValues {
189
- tag := * dctapi .NewTag (key , value )
190
- tags = append (tags , tag )
191
- }
192
- } else {
193
- // Key exists, create only new values
194
- for value := range newValues {
195
- if ! oldValues [value ] {
213
+ // Create new tags
214
+ var tags []dctapi.Tag
215
+ for key , newValues := range newTagMap {
216
+ oldValues , exists := oldTagMap [key ]
217
+ if ! exists {
218
+ // Key doesn't exist in old tags, create all values
219
+ for value := range newValues {
196
220
tag := * dctapi .NewTag (key , value )
197
221
tags = append (tags , tag )
198
222
}
223
+ } else {
224
+ // Key exists, create only new values
225
+ for value := range newValues {
226
+ if ! oldValues [value ] {
227
+ tag := * dctapi .NewTag (key , value )
228
+ tags = append (tags , tag )
229
+ }
230
+ }
199
231
}
200
232
}
201
- }
202
- if len (tags ) > 0 {
203
- tagsRequest := * dctapi . NewTagsRequest ( tags )
204
- _ , httpRes , err := client . VDBGroupsAPI . CreateVdbGroupsTags (ctx , vdbGroupId ). TagsRequest ( tagsRequest ). Execute ()
205
- if diags := apiErrorResponseHelper ( ctx , nil , httpRes , err ); diags != nil {
206
- return diags
233
+ if len ( tags ) > 0 {
234
+ tagsRequest := * dctapi . NewTagsRequest (tags )
235
+ _ , httpRes , err := client . VDBGroupsAPI . CreateVdbGroupsTags ( ctx , vdbGroupId ). TagsRequest ( tagsRequest ). Execute ( )
236
+ if diags := apiErrorResponseHelper (ctx , nil , httpRes , err ); diags != nil {
237
+ return diags
238
+ }
207
239
}
208
240
}
209
241
}
210
242
211
243
return resourceVdbGroupRead (ctx , d , meta )
212
244
}
213
245
246
+ func PollVdbGroupStatus (ctx context.Context , client * dctapi.APIClient , vdbGroupId string ) (string , error ) {
247
+ res , _ , err := client .VDBGroupsAPI .GetVdbGroup (ctx , vdbGroupId ).Execute ()
248
+ if err != nil {
249
+ return "" , err
250
+ }
251
+ return res .GetStatus (), nil
252
+ }
253
+
214
254
func resourceVdbGroupDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
215
255
client := meta .(* apiClient ).client
216
256
0 commit comments