-
Notifications
You must be signed in to change notification settings - Fork 65
/
schema_variable.go
165 lines (143 loc) · 3.95 KB
/
schema_variable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func expandVariable(d *schema.ResourceData) *octopusdeploy.Variable {
name := d.Get("name").(string)
variable := octopusdeploy.NewVariable(name)
if v, ok := d.GetOk("description"); ok {
variable.Description = v.(string)
}
if v, ok := d.GetOk("is_editable"); ok {
variable.IsEditable = v.(bool)
}
if v, ok := d.GetOk("is_sensitive"); ok {
variable.IsSensitive = v.(bool)
}
if v, ok := d.GetOk("type"); ok {
variable.Type = v.(string)
}
if v, ok := d.GetOk("scope"); ok {
variable.Scope = expandVariableScope(v)
}
if variable.IsSensitive {
variable.Type = "Sensitive"
variable.Value = d.Get("sensitive_value").(string)
} else {
variable.Value = d.Get("value").(string)
}
variable.ID = d.Id()
varPrompt, ok := d.GetOk("prompt")
if ok {
tfPromptSettings := varPrompt.(*schema.Set)
if len(tfPromptSettings.List()) == 1 {
tfPromptList := tfPromptSettings.List()[0].(map[string]interface{})
newPrompt := octopusdeploy.VariablePromptOptions{
Description: tfPromptList["description"].(string),
Label: tfPromptList["label"].(string),
Required: tfPromptList["is_required"].(bool),
}
variable.Prompt = &newPrompt
}
}
return variable
}
func getVariableDataSchema() map[string]*schema.Schema {
dataSchema := getVariableSchema()
setDataSchema(&dataSchema)
return map[string]*schema.Schema{
"id": getDataSchemaID(),
"variables": {
Computed: true,
Description: "A list of variables that match the filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Type: schema.TypeList,
},
}
}
func getVariableSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"description": getDescriptionSchema(),
"encrypted_value": {
Computed: true,
Type: schema.TypeString,
},
"is_editable": {
Default: true,
Description: "Indicates whether or not this variable is considered editable.",
Optional: true,
Type: schema.TypeBool,
},
"is_sensitive": getIsSensitiveSchema(),
"key_fingerprint": {
Computed: true,
Type: schema.TypeString,
},
"name": getNameSchema(true),
"owner_id": {
ConflictsWith: []string{"project_id"},
Optional: true,
Type: schema.TypeString,
},
"pgp_key": {
ForceNew: true,
Optional: true,
Sensitive: true,
Type: schema.TypeString,
},
"project_id": {
ConflictsWith: []string{"owner_id"},
Deprecated: "This attribute is deprecated; please use owner_id instead.",
Optional: true,
Type: schema.TypeString,
},
"prompt": {
Elem: &schema.Resource{Schema: getVariablePromptOptionsSchema()},
MaxItems: 1,
Optional: true,
Type: schema.TypeSet,
},
"scope": {
Elem: &schema.Resource{Schema: getVariableScopeSchema()},
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
"sensitive_value": {
ConflictsWith: []string{"value"},
Optional: true,
Sensitive: true,
Type: schema.TypeString,
},
"type": getVariableTypeSchema(),
"value": {
ConflictsWith: []string{"sensitive_value"},
Optional: true,
Type: schema.TypeString,
},
}
}
func setVariable(ctx context.Context, d *schema.ResourceData, variable *octopusdeploy.Variable) error {
if d == nil || variable == nil {
return fmt.Errorf("error setting scope")
}
d.Set("description", variable.Description)
d.Set("is_editable", variable.IsEditable)
d.Set("is_sensitive", variable.IsSensitive)
d.Set("name", variable.Name)
d.Set("type", variable.Type)
if variable.IsSensitive {
d.Set("value", nil)
} else {
d.Set("value", variable.Value)
}
if err := d.Set("scope", flattenVariableScope(variable.Scope)); err != nil {
return fmt.Errorf("error setting scope: %s", err)
}
d.SetId(variable.GetID())
return nil
}