Skip to content

Commit

Permalink
fx issue #165 in catalystcenter_assign_templates_to_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-mazurkiewicz committed Jan 14, 2025
1 parent cd6be1b commit 19c8b8b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/guides/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gen/definitions/assign_templates_to_tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions templates/guides/changelog.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19c8b8b

Please sign in to comment.