@@ -35,18 +35,38 @@ func resourceVdbGroup() *schema.Resource {
35
35
Type : schema .TypeString ,
36
36
},
37
37
},
38
+ "tags" : {
39
+ Type : schema .TypeList ,
40
+ Optional : true ,
41
+ Elem : & schema.Resource {
42
+ Schema : map [string ]* schema.Schema {
43
+ "key" : {
44
+ Type : schema .TypeString ,
45
+ Required : true ,
46
+ },
47
+ "value" : {
48
+ Type : schema .TypeString ,
49
+ Required : true ,
50
+ },
51
+ },
52
+ },
53
+ },
38
54
},
39
55
}
40
56
}
41
57
42
58
func resourceVdbGroupCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
43
-
44
59
var diags diag.Diagnostics
45
60
46
61
client := meta .(* apiClient ).client
47
62
48
63
vdbGroupCreateReq := * dctapi .NewCreateVDBGroupRequest (d .Get ("name" ).(string ))
49
64
vdbGroupCreateReq .SetVdbIds (toStringArray (d .Get ("vdb_ids" )))
65
+
66
+ if v , has_v := d .GetOk ("tags" ); has_v {
67
+ vdbGroupCreateReq .SetTags (toTagArray (v ))
68
+ }
69
+
50
70
apiRes , httpRes , err := client .VDBGroupsAPI .CreateVdbGroup (ctx ).CreateVDBGroupRequest (vdbGroupCreateReq ).Execute ()
51
71
52
72
if diags := apiErrorResponseHelper (ctx , apiRes , httpRes , err ); diags != nil {
@@ -64,7 +84,6 @@ func resourceVdbGroupCreate(ctx context.Context, d *schema.ResourceData, meta in
64
84
}
65
85
66
86
func resourceVdbGroupRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
67
-
68
87
client := meta .(* apiClient ).client
69
88
70
89
var diags diag.Diagnostics
@@ -80,12 +99,116 @@ func resourceVdbGroupRead(ctx context.Context, d *schema.ResourceData, meta inte
80
99
81
100
d .Set ("name" , apiRes .GetName ())
82
101
d .Set ("vdb_ids" , apiRes .GetVdbIds ())
102
+ d .Set ("tags" , flattenTags (apiRes .GetTags ()))
83
103
return diags
84
104
}
85
105
86
106
func resourceVdbGroupUpdate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
107
+ client := meta .(* apiClient ).client
108
+
109
+ vdbGroupId := d .Id ()
110
+
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
+ }
116
+ if d .HasChange ("vdb_ids" ) {
117
+ updateVdbGroupReq .SetVdbIds (toStringArray (d .Get ("vdb_ids" )))
118
+ }
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
123
+ }
124
+ }
125
+
126
+ if d .HasChange ("tags" ) {
127
+ oldTags , newTags := d .GetChange ("tags" )
128
+ oldTagList := oldTags .([]interface {})
129
+ newTagList := newTags .([]interface {})
130
+
131
+ // Create a map of old tags for easy lookup
132
+ oldTagMap := make (map [string ]map [string ]bool )
133
+ for _ , tag := range oldTagList {
134
+ tagMap := tag .(map [string ]interface {})
135
+ key := tagMap ["key" ].(string )
136
+ value := tagMap ["value" ].(string )
137
+ if _ , exists := oldTagMap [key ]; ! exists {
138
+ oldTagMap [key ] = make (map [string ]bool )
139
+ }
140
+ oldTagMap [key ][value ] = true
141
+ }
142
+
143
+ // Create a map of new tags for easy lookup
144
+ newTagMap := make (map [string ]map [string ]bool )
145
+ for _ , tag := range newTagList {
146
+ tagMap := tag .(map [string ]interface {})
147
+ key := tagMap ["key" ].(string )
148
+ value := tagMap ["value" ].(string )
149
+ if _ , exists := newTagMap [key ]; ! exists {
150
+ newTagMap [key ] = make (map [string ]bool )
151
+ }
152
+ newTagMap [key ][value ] = true
153
+ }
154
+
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
160
+ deleteTag := * dctapi .NewDeleteTag ()
161
+ deleteTag .SetKey (key )
162
+ httpRes , err := client .VDBGroupsAPI .DeleteVdbGroupTags (ctx , vdbGroupId ).DeleteTag (deleteTag ).Execute ()
163
+ if diags := apiErrorResponseHelper (ctx , nil , httpRes , err ); diags != nil {
164
+ return diags
165
+ }
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
176
+ }
177
+ }
178
+ }
179
+ }
180
+ }
181
+
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 ] {
196
+ tag := * dctapi .NewTag (key , value )
197
+ tags = append (tags , tag )
198
+ }
199
+ }
200
+ }
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
207
+ }
208
+ }
209
+ }
87
210
88
- return diag . Errorf ( "not implemented" )
211
+ return resourceVdbGroupRead ( ctx , d , meta )
89
212
}
90
213
91
214
func resourceVdbGroupDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
0 commit comments