Skip to content

Commit 19c8b8b

Browse files
fx issue #165 in catalystcenter_assign_templates_to_tag
1 parent cd6be1b commit 19c8b8b

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.19 (unreleased)
2+
3+
- Fix issue with tags removal from templates in `catalystcenter_assign_templates_to_tag` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/165)
4+
15
## 0.1.18
26

37
- Add `catalystcenter_fabric_l3_virtual_network` resource and data source

docs/guides/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ description: |-
77

88
# Changelog
99

10+
## 0.1.19 (unreleased)
11+
12+
- Fix issue with tags removal from templates in `catalystcenter_assign_templates_to_tag` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/165)
13+
1014
## 0.1.18
1115

1216
- Add `catalystcenter_fabric_l3_virtual_network` resource and data source

gen/definitions/assign_templates_to_tag.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Assign Templates to Tag
33
rest_endpoint: /dna/intent/api/v1/tag/%v/member
44
get_extra_query_params: '?memberType=template'
55
get_no_id: true
6-
# Manual updates in Delete function to handle removal of templates from tag in resource file, and fromBody, updateFromBody functions in model file
6+
# Manual updates in Update and Delete function to handle removal of templates from tag in resource file, and fromBody, updateFromBody functions in model file
77
res_description: 'This resource is responsible for assigning templates to a specified tag during creation
88
and removing the template from the tag during destroy operation.'
99
post_update: true

internal/provider/resource_catalystcenter_assign_templates_to_tag.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ func (r *AssignTemplatesToTagResource) Read(ctx context.Context, req resource.Re
163163

164164
// End of section. //template:end read
165165

166-
// Section below is generated&owned by "gen/generator.go". //template:begin update
167166
func (r *AssignTemplatesToTagResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
168167
var plan, state AssignTemplatesToTag
169168

@@ -182,6 +181,37 @@ func (r *AssignTemplatesToTagResource) Update(ctx context.Context, req resource.
182181

183182
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString()))
184183

184+
// Handle removed template IDs
185+
stateTemplateIds := make(map[string]struct{})
186+
planTemplateIds := make(map[string]struct{})
187+
188+
if !state.TemplateIds.IsNull() {
189+
var stateValues []string
190+
state.TemplateIds.ElementsAs(ctx, &stateValues, false)
191+
for _, id := range stateValues {
192+
stateTemplateIds[id] = struct{}{}
193+
}
194+
}
195+
196+
if !plan.TemplateIds.IsNull() {
197+
var planValues []string
198+
plan.TemplateIds.ElementsAs(ctx, &planValues, false)
199+
for _, id := range planValues {
200+
planTemplateIds[id] = struct{}{}
201+
}
202+
}
203+
204+
for id := range stateTemplateIds {
205+
if _, exists := planTemplateIds[id]; !exists {
206+
_, err := r.client.Delete(plan.getPath() + "/" + url.QueryEscape(id))
207+
if err != nil {
208+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete template (%s), got error: %s", id, err))
209+
return
210+
}
211+
}
212+
}
213+
//
214+
185215
body := plan.toBody(ctx, state)
186216
params := ""
187217
res, err := r.client.Post(plan.getPath()+params, body)
@@ -196,8 +226,6 @@ func (r *AssignTemplatesToTagResource) Update(ctx context.Context, req resource.
196226
resp.Diagnostics.Append(diags...)
197227
}
198228

199-
// End of section. //template:end update
200-
201229
func (r *AssignTemplatesToTagResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
202230
var state AssignTemplatesToTag
203231

templates/guides/changelog.md.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ description: |-
77

88
# Changelog
99

10+
## 0.1.19 (unreleased)
11+
12+
- Fix issue with tags removal from templates in `catalystcenter_assign_templates_to_tag` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/165)
13+
1014
## 0.1.18
1115

1216
- Add `catalystcenter_fabric_l3_virtual_network` resource and data source

0 commit comments

Comments
 (0)