Skip to content

Commit

Permalink
fix: data source variables not working (#564)
Browse files Browse the repository at this point in the history
closes #372
  • Loading branch information
domenicsim1 authored Oct 24, 2023
1 parent 62d4b66 commit 872d79f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 28 deletions.
28 changes: 10 additions & 18 deletions octopusdeploy/data_source_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,28 @@ func dataSourceVariable() *schema.Resource {
}

func dataSourceVariableReadByName(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
ownerID := d.Get("owner_id")
name := d.Get("name")
scope := variables.VariableScope{}
spaceID := d.Get("space_id").(string)

if v, ok := d.GetOk("scope"); ok {
scope = expandVariableScope(v)
}
ownerID := d.Get("owner_id").(string)

var spaceID string
if v, ok := d.GetOk("space_id"); ok {
spaceID = v.(string)
}
name := d.Get("name").(string)

scope := expandVariableScope(d.Get("scope"))

client := m.(*client.Client)
variables, err := variables.GetByName(client, spaceID, ownerID.(string), name.(string), &scope)
variables, err := variables.GetByName(client, spaceID, ownerID, name, &scope)
if err != nil {
return diag.Errorf("error reading variable with owner ID %s with name %s: %s", ownerID, name, err.Error())
}
if variables == nil {
return nil
}
if len(variables) > 1 {
return diag.Errorf("found %v variables with owner ID %s with name %s, should match exactly 1", len(variables), ownerID, name)

if len(variables) != 1 {
return diag.Errorf("error could not find variable by name, expected to find 1 variable but got %d", len(variables))
}

d.SetId(variables[0].ID)
d.Set("name", variables[0].Name)
d.Set("type", variables[0].Type)
d.Set("value", variables[0].Value)
d.Set("description", variables[0].Description)
setVariable(ctx, d, variables[0])

return nil
}
108 changes: 98 additions & 10 deletions octopusdeploy/schema_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,107 @@ func expandVariable(d *schema.ResourceData) *variables.Variable {
}

func getVariableDataSchema() map[string]*schema.Schema {
dataSchema := getVariableSchema()
setDataSchema(&dataSchema)

return map[string]*schema.Schema{
"id": getDataSchemaID(),
"space_id": getQuerySpaceID(),
"ids": getQueryIDs(),
"variables": {
"id": {
Computed: true,
Description: "A list of variables that match the filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Description: "The identifier of the variable to find.",
Type: schema.TypeString,
},
"name": {
Required: true,
Description: "The name of variable to find.",
Type: schema.TypeString,
},
"owner_id": {
Required: true,
Description: "Owner ID for the variable to find.",
Type: schema.TypeString,
},
"scope": {
Description: "As variable names can appear more than once under different scopes, a VariableScope must also be provided",
Elem: &schema.Resource{Schema: getVariableScopeSchema()},
Required: true,
Type: schema.TypeList,
MaxItems: 1,
},
"space_id": getQuerySpaceID(),
"description": {
Description: "The description of this variable.",
Computed: true,
Type: schema.TypeString,
},
"is_editable": {
Description: "Indicates whether or not this variable is considered editable.",
Computed: true,
Type: schema.TypeBool,
},
"is_sensitive": {
Description: "Indicates whether or not this resource is considered sensitive and should be kept secret.",
Computed: true,
Type: schema.TypeBool,
},
"type": {
Description: "The type of variable represented by this resource. Valid types are `AmazonWebServicesAccount`, `AzureAccount`, `GoogleCloudAccount`, `Certificate`, `Sensitive`, `String`, or `WorkerPool`.",
Computed: true,
Type: schema.TypeString,
},
"sensitive_value": {
Computed: true,
Sensitive: true,
Type: schema.TypeString,
},
"value": {
Computed: true,
Type: schema.TypeString,
},
"prompt": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": getDescriptionSchema("variable prompt option"),
"display_settings": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"control_type": {
Description: "The type of control for rendering this prompted variable. Valid types are `SingleLineText`, `MultiLineText`, `Checkbox`, `Select`.",
Required: true,
Type: schema.TypeString,
},
"select_option": {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Description: "The select value",
Required: true,
Type: schema.TypeString,
},
"display_name": {
Description: "The display name for the select value",
Required: true,
Type: schema.TypeString,
},
},
},
Description: "If the `control_type` is `Select`, then this value defines an option.",
Optional: true,
Type: schema.TypeList,
},
},
},
Optional: true,
Type: schema.TypeList,
},
"is_required": {
Type: schema.TypeBool,
Optional: true,
},
"label": {
Type: schema.TypeString,
Optional: true,
},
},
},
Computed: true,
Type: schema.TypeList,
},
}
}
Expand Down

0 comments on commit 872d79f

Please sign in to comment.