-
Notifications
You must be signed in to change notification settings - Fork 66
/
schema_space.go
171 lines (144 loc) · 4.57 KB
/
schema_space.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
166
167
168
169
170
171
package octopusdeploy
import (
"context"
"fmt"
"strings"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const spaceManagersTeamIDPrefix = "teams-spacemanagers-"
func expandSpace(d *schema.ResourceData) *spaces.Space {
name := d.Get("name").(string)
space := spaces.NewSpace(name)
space.ID = d.Id()
if v, ok := d.GetOk("description"); ok {
space.Description = v.(string)
}
if v, ok := d.GetOk("is_default"); ok {
space.IsDefault = v.(bool)
}
if v, ok := d.GetOk("slug"); ok {
space.Slug = v.(string)
}
if v, ok := d.GetOk("space_managers_team_members"); ok {
space.SpaceManagersTeamMembers = getSliceFromTerraformTypeList(v)
}
if v, ok := d.GetOk("space_managers_teams"); ok {
space.SpaceManagersTeams = addSpaceManagers(space.GetID(), getSliceFromTerraformTypeList(v))
}
if v, ok := d.GetOk("is_task_queue_stopped"); ok {
space.TaskQueueStopped = v.(bool)
}
return space
}
func addSpaceManagers(spaceID string, teamIDs []string) []string {
var newSlice []string
if getStringOrEmpty(spaceID) != "" {
newSlice = append(newSlice, spaceManagersTeamIDPrefix+spaceID)
}
newSlice = append(newSlice, teamIDs...)
return newSlice
}
func flattenSpace(space *spaces.Space) map[string]interface{} {
if space == nil {
return nil
}
return map[string]interface{}{
"description": space.Description,
"id": space.GetID(),
"is_default": space.IsDefault,
"is_task_queue_stopped": space.TaskQueueStopped,
"name": space.Name,
"slug": space.Slug,
"space_managers_team_members": space.SpaceManagersTeamMembers,
"space_managers_teams": space.SpaceManagersTeams,
}
}
func getSpaceDataSourceSchema() map[string]*schema.Schema {
dataSchema := getSpaceSchema()
setDataSchema(&dataSchema)
dataSchema["name"] = getNameSchemaWithMaxLength(true, 20)
return dataSchema
}
func getSpacesDataSourceSchema() map[string]*schema.Schema {
dataSchema := getSpaceSchema()
setDataSchema(&dataSchema)
return map[string]*schema.Schema{
"id": getDataSchemaID(),
"ids": getQueryIDs(),
"partial_name": getQueryPartialName(),
"skip": getQuerySkip(),
"take": getQueryTake(),
"spaces": {
Computed: true,
Description: "A list of spaces that match the filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Type: schema.TypeList,
},
}
}
func getSpaceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"description": getDescriptionSchema("space"),
"id": getIDSchema(),
"is_default": {
Description: "Specifies if this space is the default space in Octopus.",
Optional: true,
Type: schema.TypeBool,
},
"name": getNameSchemaWithMaxLength(true, 20),
"slug": {
Computed: true,
Description: "The unique slug of this space.",
Optional: true,
Type: schema.TypeString,
},
"space_managers_team_members": {
Computed: true,
Description: "A list of user IDs designated to be managers of this space.",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeSet,
},
"space_managers_teams": {
Computed: true,
Description: "A list of team IDs designated to be managers of this space.",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeSet,
},
"is_task_queue_stopped": {
Description: "Specifies the status of the task queue for this space.",
Optional: true,
Type: schema.TypeBool,
},
}
}
func setSpace(ctx context.Context, d *schema.ResourceData, space *spaces.Space) error {
d.Set("description", space.Description)
d.Set("id", space.GetID())
d.Set("is_default", space.IsDefault)
d.Set("name", space.Name)
d.Set("slug", space.Slug)
if err := d.Set("space_managers_team_members", space.SpaceManagersTeamMembers); err != nil {
return fmt.Errorf("error setting space_managers_team_members: %s", err)
}
if err := d.Set("space_managers_teams", removeSpaceManagers(space.SpaceManagersTeams)); err != nil {
return fmt.Errorf("error setting space_managers_teams: %s", err)
}
d.Set("is_task_queue_stopped", space.TaskQueueStopped)
return nil
}
func removeSpaceManagers(teamIDs []string) []string {
if len(teamIDs) == 0 {
return teamIDs
}
var newSlice []string
for _, v := range teamIDs {
if !strings.Contains(v, spaceManagersTeamIDPrefix) {
newSlice = append(newSlice, v)
}
}
return newSlice
}