-
Notifications
You must be signed in to change notification settings - Fork 66
/
schema_script_modules.go
155 lines (131 loc) · 4.01 KB
/
schema_script_modules.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
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func expandScriptModule(d *schema.ResourceData) *variables.ScriptModule {
name := d.Get("name").(string)
scriptModule := variables.NewScriptModule(name)
scriptModule.ID = d.Id()
if v, ok := d.GetOk("description"); ok {
scriptModule.Description = v.(string)
}
if v, ok := d.GetOk("script"); ok {
scripts := v.(*schema.Set).List()
for _, script := range scripts {
rawScript := script.(map[string]interface{})
if rawScript["body"] != nil {
scriptModule.ScriptBody = rawScript["body"].(string)
}
if rawScript["syntax"] != nil {
scriptModule.Syntax = rawScript["syntax"].(string)
}
}
}
if v, ok := d.GetOk("space_id"); ok {
scriptModule.SpaceID = v.(string)
}
if v, ok := d.GetOk("variable_set_id"); ok {
scriptModule.VariableSetID = v.(string)
}
return scriptModule
}
func flattenScript(scriptModule *variables.ScriptModule) []interface{} {
if scriptModule == nil {
return nil
}
flattenedScriptModules := make([]interface{}, 1)
flattenedScriptModules[0] = map[string]interface{}{
"body": scriptModule.ScriptBody,
"syntax": scriptModule.Syntax,
}
return flattenedScriptModules
}
func flattenScriptModule(scriptModule *variables.ScriptModule) map[string]interface{} {
if scriptModule == nil {
return nil
}
return map[string]interface{}{
"description": scriptModule.Description,
"id": scriptModule.GetID(),
"name": scriptModule.Name,
"script": flattenScript(scriptModule),
"space_id": scriptModule.SpaceID,
"variable_set_id": scriptModule.VariableSetID,
}
}
func getScriptModuleDataSchema() map[string]*schema.Schema {
dataSchema := getScriptModuleSchema()
setDataSchema(&dataSchema)
return map[string]*schema.Schema{
"id": getDataSchemaID(),
"space_id": getQuerySpaceID(),
"ids": getQueryIDs(),
"script_modules": {
Computed: true,
Description: "A list of script modules that match the filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Type: schema.TypeList,
},
"partial_name": getQueryPartialName(),
"skip": getQuerySkip(),
"take": getQueryTake(),
}
}
func getScriptModuleSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"description": getDescriptionSchema("script module"),
"id": getIDSchema(),
"name": getNameSchema(true),
"script": {
Description: "The script associated with this script module.",
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"body": {
Description: "The body of this script module.",
Required: true,
Type: schema.TypeString,
},
"syntax": {
Description: "The syntax of the script. Valid types are `Bash`, `CSharp`, `FSharp`, `PowerShell`, or `Python`.",
Required: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{
"Bash",
"CSharp",
"FSharp",
"PowerShell",
"Python",
}, false)),
},
},
},
MaxItems: 1,
MinItems: 1,
Type: schema.TypeSet,
},
"space_id": getSpaceIDSchema(),
"variable_set_id": {
Computed: true,
Description: "The variable set ID for this script module.",
Optional: true,
Type: schema.TypeString,
},
}
}
func setScriptModule(ctx context.Context, d *schema.ResourceData, scriptModule *variables.ScriptModule) error {
d.Set("description", scriptModule.Description)
d.Set("name", scriptModule.Name)
if err := d.Set("script", flattenScript(scriptModule)); err != nil {
return fmt.Errorf("error setting script: %s", err)
}
d.Set("space_id", scriptModule.SpaceID)
d.Set("variable_set_id", scriptModule.VariableSetID)
d.SetId(scriptModule.GetID())
return nil
}