forked from hashicorp/terraform-provider-aws
/
resource_aws_cloud9_environment_ec2.go
218 lines (190 loc) · 5.39 KB
/
resource_aws_cloud9_environment_ec2.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
217
218
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloud9"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsCloud9EnvironmentEc2() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloud9EnvironmentEc2Create,
Read: resourceAwsCloud9EnvironmentEc2Read,
Update: resourceAwsCloud9EnvironmentEc2Update,
Delete: resourceAwsCloud9EnvironmentEc2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"instance_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"automatic_stop_time_minutes": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"owner_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsCloud9EnvironmentEc2Create(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn
params := &cloud9.CreateEnvironmentEC2Input{
InstanceType: aws.String(d.Get("instance_type").(string)),
Name: aws.String(d.Get("name").(string)),
ClientRequestToken: aws.String(resource.UniqueId()),
}
if v, ok := d.GetOk("automatic_stop_time_minutes"); ok {
params.AutomaticStopTimeMinutes = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("description"); ok {
params.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("owner_arn"); ok {
params.OwnerArn = aws.String(v.(string))
}
if v, ok := d.GetOk("subnet_id"); ok {
params.SubnetId = aws.String(v.(string))
}
out, err := conn.CreateEnvironmentEC2(params)
if err != nil {
return err
}
d.SetId(*out.EnvironmentId)
stateConf := resource.StateChangeConf{
Pending: []string{
cloud9.EnvironmentStatusConnecting,
cloud9.EnvironmentStatusCreating,
},
Target: []string{
cloud9.EnvironmentStatusReady,
},
Timeout: 10 * time.Minute,
Refresh: func() (interface{}, string, error) {
out, err := conn.DescribeEnvironmentStatus(&cloud9.DescribeEnvironmentStatusInput{
EnvironmentId: aws.String(d.Id()),
})
if err != nil {
return 42, "", err
}
status := *out.Status
var sErr error
if status == cloud9.EnvironmentStatusError && out.Message != nil {
sErr = fmt.Errorf("Reason: %s", *out.Message)
}
return out, status, sErr
},
}
_, err = stateConf.WaitForState()
if err != nil {
return err
}
return resourceAwsCloud9EnvironmentEc2Read(d, meta)
}
func resourceAwsCloud9EnvironmentEc2Read(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn
log.Printf("[INFO] Reading Cloud9 Environment EC2 %s", d.Id())
out, err := conn.DescribeEnvironments(&cloud9.DescribeEnvironmentsInput{
EnvironmentIds: []*string{aws.String(d.Id())},
})
if err != nil {
if isAWSErr(err, cloud9.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Cloud9 Environment EC2 (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
if len(out.Environments) == 0 {
log.Printf("[WARN] Cloud9 Environment EC2 (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
env := out.Environments[0]
d.Set("arn", env.Arn)
d.Set("description", env.Description)
d.Set("name", env.Name)
d.Set("owner_arn", env.OwnerArn)
d.Set("type", env.Type)
log.Printf("[DEBUG] Received Cloud9 Environment EC2: %s", env)
return nil
}
func resourceAwsCloud9EnvironmentEc2Update(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn
input := cloud9.UpdateEnvironmentInput{
Description: aws.String(d.Get("description").(string)),
EnvironmentId: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
}
log.Printf("[INFO] Updating Cloud9 Environment EC2: %s", input)
out, err := conn.UpdateEnvironment(&input)
if err != nil {
return err
}
log.Printf("[DEBUG] Cloud9 Environment EC2 updated: %s", out)
return resourceAwsCloud9EnvironmentEc2Read(d, meta)
}
func resourceAwsCloud9EnvironmentEc2Delete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn
_, err := conn.DeleteEnvironment(&cloud9.DeleteEnvironmentInput{
EnvironmentId: aws.String(d.Id()),
})
if err != nil {
return err
}
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
out, err := conn.DescribeEnvironments(&cloud9.DescribeEnvironmentsInput{
EnvironmentIds: []*string{aws.String(d.Id())},
})
if err != nil {
if isAWSErr(err, cloud9.ErrCodeNotFoundException, "") {
return nil
}
// :'-(
if isAWSErr(err, "AccessDeniedException", "is not authorized to access this resource") {
return nil
}
return resource.NonRetryableError(err)
}
if len(out.Environments) == 0 {
return nil
}
return resource.RetryableError(fmt.Errorf("Cloud9 EC2 Environment %q still exists", d.Id()))
})
if err != nil {
return err
}
return err
}