diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d5605b..569cd3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.19 (unreleased) + +- Fix issue with tags removal from templates in `catalystcenter_assign_templates_to_tag` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/165) + ## 0.1.18 - Add `catalystcenter_fabric_l3_virtual_network` resource and data source diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index 751777b..2d0726a 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -7,6 +7,10 @@ description: |- # Changelog +## 0.1.19 (unreleased) + +- Fix issue with tags removal from templates in `catalystcenter_assign_templates_to_tag` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/165) + ## 0.1.18 - Add `catalystcenter_fabric_l3_virtual_network` resource and data source diff --git a/gen/definitions/assign_templates_to_tag.yaml b/gen/definitions/assign_templates_to_tag.yaml index a41de28..b3eaeaf 100644 --- a/gen/definitions/assign_templates_to_tag.yaml +++ b/gen/definitions/assign_templates_to_tag.yaml @@ -3,7 +3,7 @@ name: Assign Templates to Tag rest_endpoint: /dna/intent/api/v1/tag/%v/member get_extra_query_params: '?memberType=template' get_no_id: true -# Manual updates in Delete function to handle removal of templates from tag in resource file, and fromBody, updateFromBody functions in model file +# Manual updates in Update and Delete function to handle removal of templates from tag in resource file, and fromBody, updateFromBody functions in model file res_description: 'This resource is responsible for assigning templates to a specified tag during creation and removing the template from the tag during destroy operation.' post_update: true diff --git a/internal/provider/resource_catalystcenter_assign_templates_to_tag.go b/internal/provider/resource_catalystcenter_assign_templates_to_tag.go index 7881863..a410fe3 100644 --- a/internal/provider/resource_catalystcenter_assign_templates_to_tag.go +++ b/internal/provider/resource_catalystcenter_assign_templates_to_tag.go @@ -163,7 +163,6 @@ func (r *AssignTemplatesToTagResource) Read(ctx context.Context, req resource.Re // End of section. //template:end read -// Section below is generated&owned by "gen/generator.go". //template:begin update func (r *AssignTemplatesToTagResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { var plan, state AssignTemplatesToTag @@ -182,6 +181,37 @@ func (r *AssignTemplatesToTagResource) Update(ctx context.Context, req resource. tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString())) + // Handle removed template IDs + stateTemplateIds := make(map[string]struct{}) + planTemplateIds := make(map[string]struct{}) + + if !state.TemplateIds.IsNull() { + var stateValues []string + state.TemplateIds.ElementsAs(ctx, &stateValues, false) + for _, id := range stateValues { + stateTemplateIds[id] = struct{}{} + } + } + + if !plan.TemplateIds.IsNull() { + var planValues []string + plan.TemplateIds.ElementsAs(ctx, &planValues, false) + for _, id := range planValues { + planTemplateIds[id] = struct{}{} + } + } + + for id := range stateTemplateIds { + if _, exists := planTemplateIds[id]; !exists { + _, err := r.client.Delete(plan.getPath() + "/" + url.QueryEscape(id)) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete template (%s), got error: %s", id, err)) + return + } + } + } + // + body := plan.toBody(ctx, state) params := "" res, err := r.client.Post(plan.getPath()+params, body) @@ -196,8 +226,6 @@ func (r *AssignTemplatesToTagResource) Update(ctx context.Context, req resource. resp.Diagnostics.Append(diags...) } -// End of section. //template:end update - func (r *AssignTemplatesToTagResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { var state AssignTemplatesToTag diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index 751777b..2d0726a 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -7,6 +7,10 @@ description: |- # Changelog +## 0.1.19 (unreleased) + +- Fix issue with tags removal from templates in `catalystcenter_assign_templates_to_tag` resource, [link](https://github.com/CiscoDevNet/terraform-provider-catalystcenter/issues/165) + ## 0.1.18 - Add `catalystcenter_fabric_l3_virtual_network` resource and data source