-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource_environment.go
154 lines (119 loc) · 3.81 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
package main
import (
"github.com/hashicorp/terraform/helper/schema"
"log"
"octopus"
)
const (
resourceKeyEnvironmentName = "name"
resourceKeyEnvironmentDescription = "description"
resourceKeyEnvironmentProjectGroups = "project_groups"
)
func resourceEnvironment() *schema.Resource {
return &schema.Resource{
Create: resourceEnvironmentCreate,
Read: resourceEnvironmentRead,
Update: resourceEnvironmentUpdate,
Delete: resourceEnvironmentDelete,
Exists: resourceEnvironmentExists,
Schema: map[string]*schema.Schema{
resourceKeyEnvironmentName: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The environment name.",
},
resourceKeyEnvironmentDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The environment description.",
},
resourceKeyEnvironmentProjectGroups: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Optional: true,
Default: nil,
Description: "The Ids of project groups associated with the environment.",
},
},
}
}
// Create an environment resource.
func resourceEnvironmentCreate(data *schema.ResourceData, provider interface{}) error {
name := data.Get(resourceKeyEnvironmentName).(string)
description := data.Get(resourceKeyEnvironmentDescription).(string)
log.Printf("Create environment named '%s'.", name)
client := provider.(*octopus.Client)
environment, err := client.CreateEnvironment(name, description, 0)
if err != nil {
return err
}
data.SetId(environment.ID)
return nil
}
// Read an environment resource.
func resourceEnvironmentRead(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
name := data.Get(resourceKeyEnvironmentName).(string)
log.Printf("Read environment '%s' (name = '%s').", id, name)
client := provider.(*octopus.Client)
environment, err := client.GetEnvironment(id)
if err != nil {
return err
}
if environment == nil {
// Environment has been deleted.
data.SetId("")
return nil
}
data.Set(resourceKeyEnvironmentName, environment.Name)
data.Set(resourceKeyEnvironmentDescription, environment.Description)
return nil
}
// Update an environment resource.
func resourceEnvironmentUpdate(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
log.Printf("Update environment '%s'.", id)
if !(data.HasChange(resourceKeyEnvironmentName) || data.HasChange(resourceKeyEnvironmentDescription)) {
return nil // Nothing to do.
}
client := provider.(*octopus.Client)
environment, err := client.GetEnvironment(id)
if err != nil {
return err
}
if environment != nil {
// Environment has been deleted.
data.SetId("")
return nil
}
if data.HasChange(resourceKeyEnvironmentName) {
environment.Name = data.Get(resourceKeyEnvironmentName).(string)
}
if data.HasChange(resourceKeyEnvironmentDescription) {
environment.Description = data.Get(resourceKeyEnvironmentDescription).(string)
}
_, err = client.UpdateEnvironment(environment)
return err
}
// Delete an environment resource.
func resourceEnvironmentDelete(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
name := data.Get(resourceKeyEnvironmentName).(string)
log.Printf("Delete Environment '%s' (name = '%s').", id, name)
client := provider.(*octopus.Client)
return client.DeleteEnvironment(id)
}
// Determine whether an environment resource exists.
func resourceEnvironmentExists(data *schema.ResourceData, provider interface{}) (exists bool, err error) {
id := data.Id()
log.Printf("Check if environment '%s' exists.", id)
client := provider.(*octopus.Client)
var environment *octopus.Environment
environment, err = client.GetEnvironment(id)
exists = environment != nil
return
}