forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_launch_configuration.go
173 lines (145 loc) · 4.82 KB
/
resource_aws_launch_configuration.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
package aws
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/autoscaling"
)
func resourceAwsLaunchConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLaunchConfigurationCreate,
Read: resourceAwsLaunchConfigurationRead,
Delete: resourceAwsLaunchConfigurationDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"image_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"iam_instance_profile": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"key_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"user_data": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
switch v.(type) {
case string:
hash := sha1.Sum([]byte(v.(string)))
return hex.EncodeToString(hash[:])
default:
return ""
}
},
},
"security_groups": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
},
"associate_public_ip_address": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
var createLaunchConfigurationOpts autoscaling.CreateLaunchConfiguration
createLaunchConfigurationOpts.Name = d.Get("name").(string)
createLaunchConfigurationOpts.IamInstanceProfile = d.Get("iam_instance_profile").(string)
createLaunchConfigurationOpts.ImageId = d.Get("image_id").(string)
createLaunchConfigurationOpts.InstanceType = d.Get("instance_type").(string)
createLaunchConfigurationOpts.KeyName = d.Get("key_name").(string)
createLaunchConfigurationOpts.UserData = d.Get("user_data").(string)
createLaunchConfigurationOpts.AssociatePublicIpAddress = d.Get("associate_public_ip_address").(bool)
if v, ok := d.GetOk("security_groups"); ok {
createLaunchConfigurationOpts.SecurityGroups = expandStringList(
v.(*schema.Set).List())
}
log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
_, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
if err != nil {
return fmt.Errorf("Error creating launch configuration: %s", err)
}
d.SetId(d.Get("name").(string))
log.Printf("[INFO] launch configuration ID: %s", d.Id())
// We put a Retry here since sometimes eventual consistency bites
// us and we need to retry a few times to get the LC to load properly
return resource.Retry(30*time.Second, func() error {
return resourceAwsLaunchConfigurationRead(d, meta)
})
}
func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
describeOpts := autoscaling.DescribeLaunchConfigurations{
Names: []string{d.Id()},
}
log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts)
describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts)
if err != nil {
return fmt.Errorf("Error retrieving launch configuration: %s", err)
}
if len(describConfs.LaunchConfigurations) == 0 {
d.SetId("")
return nil
}
// Verify AWS returned our launch configuration
if describConfs.LaunchConfigurations[0].Name != d.Id() {
return fmt.Errorf(
"Unable to find launch configuration: %#v",
describConfs.LaunchConfigurations)
}
lc := describConfs.LaunchConfigurations[0]
d.Set("key_name", lc.KeyName)
d.Set("iam_instance_profile", lc.IamInstanceProfile)
d.Set("image_id", lc.ImageId)
d.Set("instance_type", lc.InstanceType)
d.Set("name", lc.Name)
d.Set("security_groups", lc.SecurityGroups)
return nil
}
func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
log.Printf("[DEBUG] Launch Configuration destroy: %v", d.Id())
_, err := autoscalingconn.DeleteLaunchConfiguration(
&autoscaling.DeleteLaunchConfiguration{Name: d.Id()})
if err != nil {
autoscalingerr, ok := err.(*autoscaling.Error)
if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" {
return nil
}
return err
}
return nil
}