-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_environment.go
216 lines (178 loc) · 5.49 KB
/
resource_environment.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"log"
"regexp"
"sort"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/pactflow/terraform/broker"
"github.com/pactflow/terraform/client"
)
func environment() *schema.Resource {
return &schema.Resource{
Create: environmentCreate,
Update: environmentUpdate,
Read: environmentRead,
Delete: environmentDelete,
Importer: &schema.ResourceImporter{State: schema.ImportStatePassthrough},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ValidateFunc: validateAlphaNumeric,
Required: true,
Description: "Name of the Environment",
},
"display_name": {
Type: schema.TypeString,
Required: true,
Description: "Display name of the Environment",
},
"production": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Is this environment a production environment?",
},
"teams": {
Type: schema.TypeSet,
Optional: true,
Description: "A list of teams (as uuids) that may use the environment",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"uuid": {
Type: schema.TypeString,
Computed: true,
Description: "The UUID of environment",
},
},
}
}
func validateAlphaNumeric(v interface{}, k string) (warns []string, errs []error) {
return validation.All(
validation.StringLenBetween(1, 255),
validation.StringMatch(
regexp.MustCompile("[a-zA-Z0-9]+"),
"The environment name must consist of alphanumerics"),
)(v, k)
}
func environmentToCRUD(environment broker.Environment, teams []string) broker.EnvironmentCreateOrUpdateRequest {
return broker.EnvironmentCreateOrUpdateRequest{
DisplayName: environment.DisplayName,
Name: environment.Name,
Production: environment.Production,
Teams: teams,
UUID: environment.UUID,
}
}
func environmentFromCRUD(environment broker.EnvironmentCreateOrUpdateResponse) broker.Environment {
return broker.Environment{
DisplayName: environment.DisplayName,
Name: environment.Name,
Production: environment.Production,
CreatedAt: environment.CreatedAt,
UpdatedAt: environment.UpdatedAt,
UUID: environment.UUID,
Embedded: environment.Embedded,
}
}
func environmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
environment := getEnvironmentFromState(d)
teams := ExpandStringSet(d.Get("teams").(*schema.Set))
log.Println("[DEBUG] creating environment", environment, teams)
created, err := client.CreateEnvironment(environmentToCRUD(environment, teams))
if err != nil {
return err
}
d.SetId(created.UUID)
setEnvironmentState(d, environmentFromCRUD(*created))
return nil
}
func environmentUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
environment := getEnvironmentFromState(d)
teams := ExpandStringSet(d.Get("teams").(*schema.Set))
log.Println("[DEBUG] updating environment", environment)
updated, err := client.UpdateEnvironment(environmentToCRUD(environment, teams))
if err != nil {
return err
}
setEnvironmentState(d, environmentFromCRUD(*updated))
return err
}
func environmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
uuid := d.Id()
log.Println("[DEBUG] reading environment", uuid)
environment, err := client.ReadEnvironment(uuid)
if err == nil {
d.SetId(environment.UUID)
setEnvironmentState(d, *environment)
}
return err
}
func environmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
log.Println("[DEBUG] deleting environment", d.Id())
err := client.DeleteEnvironment(getEnvironmentFromState(d))
if err != nil {
d.SetId("")
return fmt.Errorf("unable to delete environment %s: %w", d.Id(), err)
}
return nil
}
func setEnvironmentState(d *schema.ResourceData, environment broker.Environment) error {
log.Printf("[DEBUG] setting environment state: %+v \n", environment)
if err := d.Set("name", environment.Name); err != nil {
log.Println("[ERROR] error setting key 'name'", err)
return err
}
if err := d.Set("display_name", environment.DisplayName); err != nil {
log.Println("[ERROR] error setting key 'displayName'", err)
return err
}
if err := d.Set("production", environment.Production); err != nil {
log.Println("[ERROR] error setting key 'production'", err)
return err
}
if err := d.Set("uuid", environment.UUID); err != nil {
log.Println("[ERROR] error setting key 'uuid'", err)
return err
}
if err := d.Set("teams", teamsFromEnvironment(environment)); err != nil {
log.Println("[ERROR] error setting key 'teams'", err)
return err
}
return nil
}
func teamsFromEnvironment(u broker.Environment) []string {
teams := make([]string, len(u.Embedded.Teams))
for i, r := range u.Embedded.Teams {
teams[i] = r.UUID
}
sort.Strings(teams)
return teams
}
func teamsFromStateChange(d *schema.ResourceData) []string {
_, after := d.GetChange("teams")
teams, ok := after.(*schema.Set)
if !ok {
return []string{}
}
return ExpandStringSet(teams)
}
func getEnvironmentFromState(d *schema.ResourceData) broker.Environment {
uuid := d.Id()
name := d.Get("name").(string)
displayName := d.Get("display_name").(string)
production := d.Get("production").(bool)
return broker.Environment{
UUID: uuid,
DisplayName: displayName,
Production: production,
Name: name,
}
}