Skip to content

Commit

Permalink
Added a computed field called template_ids to the library variable se…
Browse files Browse the repository at this point in the history
…t, and ensured that it has all the keys based on the template names to be created. (#649)
  • Loading branch information
mcasperson committed Jun 19, 2024
1 parent e8b26b8 commit 4598c09
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions octopusdeploy/resource_library_variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,28 @@ func resourceLibraryVariableSet() *schema.Resource {
ReadContext: resourceLibraryVariableSetRead,
Schema: getLibraryVariableSetSchema(),
UpdateContext: resourceLibraryVariableSetUpdate,
CustomizeDiff: fixTemplateIds,
}
}

// fixTemplateIds uses the suggestion from https://github.com/hashicorp/terraform/issues/18863
// to ensure that the template_ids field has keys to match the list of template names.
func fixTemplateIds(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
templates := d.Get("template")
templateIds := map[string]string{}
if templates != nil {
for _, t := range templates.([]interface{}) {
template := t.(map[string]interface{})
templateIds[template["name"].(string)] = template["id"].(string)
}
}
if err := d.SetNew("template_ids", templateIds); err != nil {
return err
}

return nil
}

func resourceLibraryVariableSetCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
libraryVariableSet := expandLibraryVariableSet(d)

Expand Down
8 changes: 8 additions & 0 deletions octopusdeploy/schema_action_template_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func flattenActionTemplateParameters(actionTemplateParameters []actiontemplates.
return flattenedActionTemplateParameters
}

func mapTemplateNamesToIds(actionTemplateParameters []actiontemplates.ActionTemplateParameter) map[string]string {
templateNameIds := map[string]string{}
for _, actionTemplateParameter := range actionTemplateParameters {
templateNameIds[actionTemplateParameter.Name] = actionTemplateParameter.ID
}
return templateNameIds
}

func getActionTemplateParameterSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"default_value": {
Expand Down
15 changes: 15 additions & 0 deletions octopusdeploy/schema_library_variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func getLibraryVariableSetSchema() map[string]*schema.Schema {
Elem: &schema.Resource{Schema: getActionTemplateParameterSchema()},
Type: schema.TypeList,
},
// This field is based on the suggestion at
// https://discuss.hashicorp.com/t/custom-provider-how-to-reference-computed-attribute-of-typemap-list-set-defined-as-nested-block/22898/2
"template_ids": {
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Optional: false,
},
"variable_set_id": {
Computed: true,
Type: schema.TypeString,
Expand All @@ -94,11 +104,16 @@ func setLibraryVariableSet(ctx context.Context, d *schema.ResourceData, libraryV
d.Set("name", libraryVariableSet.Name)
d.Set("space_id", libraryVariableSet.SpaceID)
d.Set("variable_set_id", libraryVariableSet.VariableSetID)
d.Set("template_ids", nil)

if err := d.Set("template", flattenActionTemplateParameters(libraryVariableSet.Templates)); err != nil {
return fmt.Errorf("error setting template: %s", err)
}

if err := d.Set("template_ids", mapTemplateNamesToIds(libraryVariableSet.Templates)); err != nil {
return fmt.Errorf("error setting template_ids: %s", err)
}

d.SetId(libraryVariableSet.GetID())

return nil
Expand Down

0 comments on commit 4598c09

Please sign in to comment.