From 305994036db82eb117cd2a8b4e15c8a0e933c1f3 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Thu, 10 Jul 2014 14:15:58 -0400 Subject: [PATCH] providers/aws: add createlaunchocnfiguration --- .../aws/resource_aws_launch_configuration.go | 180 ++++++++++++++++++ builtin/providers/aws/resources.go | 7 + builtin/providers/aws/structure.go | 11 +- 3 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 builtin/providers/aws/resource_aws_launch_configuration.go diff --git a/builtin/providers/aws/resource_aws_launch_configuration.go b/builtin/providers/aws/resource_aws_launch_configuration.go new file mode 100644 index 000000000..1a4392198 --- /dev/null +++ b/builtin/providers/aws/resource_aws_launch_configuration.go @@ -0,0 +1,180 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform/flatmap" + "github.com/hashicorp/terraform/helper/diff" + "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/goamz/autoscaling" +) + +func resource_aws_launch_configuration_create( + s *terraform.ResourceState, + d *terraform.ResourceDiff, + meta interface{}) (*terraform.ResourceState, error) { + p := meta.(*ResourceProvider) + autoscalingconn := p.autoscalingconn + + // Merge the diff into the state so that we have all the attributes + // properly. + rs := s.MergeDiff(d) + + var err error + createLaunchConfigurationOpts := autoscaling.CreateLaunchConfiguration{} + + if rs.Attributes["image_id"] != "" { + createLaunchConfigurationOpts.ImageId = rs.Attributes["image_id"] + } + + if rs.Attributes["instance_type"] != "" { + createLaunchConfigurationOpts.InstanceType = rs.Attributes["instance_type"] + } + + if rs.Attributes["instance_id"] != "" { + createLaunchConfigurationOpts.InstanceId = rs.Attributes["instance_id"] + } + + if rs.Attributes["key_name"] != "" { + createLaunchConfigurationOpts.KeyName = rs.Attributes["key_name"] + } + + if err != nil { + return nil, fmt.Errorf("Error parsing configuration: %s", err) + } + + if _, ok := rs.Attributes["security_groups.#"]; ok { + createLaunchConfigurationOpts.SecurityGroups = expandStringList(flatmap.Expand( + rs.Attributes, "security_groups").([]interface{})) + } + + createLaunchConfigurationOpts.Name = rs.Attributes["name"] + + log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts) + _, err = autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts) + if err != nil { + return nil, fmt.Errorf("Error creating launch configuration: %s", err) + } + + rs.ID = rs.Attributes["name"] + + log.Printf("[INFO] launch configuration ID: %s", rs.ID) + + g, err := resource_aws_launch_configuration_retrieve(rs.ID, autoscalingconn) + if err != nil { + return rs, err + } + + return resource_aws_launch_configuration_update_state(rs, g) +} + +func resource_aws_launch_configuration_update( + s *terraform.ResourceState, + d *terraform.ResourceDiff, + meta interface{}) (*terraform.ResourceState, error) { + + rs := s.MergeDiff(d) + log.Printf("ResourceDiff: %s", d) + log.Printf("ResourceState: %s", s) + log.Printf("Merged: %s", rs) + + return nil, fmt.Errorf("Did not update") +} + +func resource_aws_launch_configuration_destroy( + s *terraform.ResourceState, + meta interface{}) error { + // p := meta.(*ResourceProvider) + // autoscalingconn := p.autoscalingconn + + log.Printf("[DEBUG] launch configuration destroy: %v", s.ID) + + return nil +} + +func resource_aws_launch_configuration_refresh( + s *terraform.ResourceState, + meta interface{}) (*terraform.ResourceState, error) { + p := meta.(*ResourceProvider) + autoscalingconn := p.autoscalingconn + + g, err := resource_aws_launch_configuration_retrieve(s.ID, autoscalingconn) + + if err != nil { + return s, err + } + + return resource_aws_launch_configuration_update_state(s, g) +} + +func resource_aws_launch_configuration_diff( + s *terraform.ResourceState, + c *terraform.ResourceConfig, + meta interface{}) (*terraform.ResourceDiff, error) { + + b := &diff.ResourceBuilder{ + Attrs: map[string]diff.AttrType{ + "image_id": diff.AttrTypeCreate, + "instance_id": diff.AttrTypeCreate, + "instance_type": diff.AttrTypeCreate, + "key_name": diff.AttrTypeCreate, + "name": diff.AttrTypeCreate, + "security_groups": diff.AttrTypeCreate, + }, + + ComputedAttrs: []string{}, + } + + return b.Diff(s, c) +} + +func resource_aws_launch_configuration_update_state( + s *terraform.ResourceState, + lc *autoscaling.LaunchConfiguration) (*terraform.ResourceState, error) { + + s.Attributes["image_id"] = lc.ImageId + s.Attributes["instance_type"] = lc.InstanceType + s.Attributes["key_name"] = lc.KeyName + s.Attributes["name"] = lc.Name + + // Flatten our group values + toFlatten := make(map[string]interface{}) + + if len(lc.SecurityGroups) > 0 && lc.SecurityGroups[0].SecurityGroup != "" { + toFlatten["security_groups"] = flattenAutoscalingSecurityGroups(lc.SecurityGroups) + } + + for k, v := range flatmap.Flatten(toFlatten) { + s.Attributes[k] = v + } + + return s, nil +} + +// Returns a single group by it's ID +func resource_aws_launch_configuration_retrieve(id string, autoscalingconn *autoscaling.AutoScaling) (*autoscaling.LaunchConfiguration, error) { + describeOpts := autoscaling.DescribeLaunchConfigurations{ + Names: []string{id}, + } + + log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts) + + describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts) + + if err != nil { + return nil, fmt.Errorf("Error retrieving launch configuration: %s", err) + } + + // Verify AWS returned our launch configuration + if len(describConfs.LaunchConfigurations) != 1 || + describConfs.LaunchConfigurations[0].Name != id { + if err != nil { + return nil, fmt.Errorf("Unable to find launch configuration: %#v", describConfs.LaunchConfigurations) + } + } + + l := describConfs.LaunchConfigurations[0] + + return &l, nil +} diff --git a/builtin/providers/aws/resources.go b/builtin/providers/aws/resources.go index d7ed1474c..9e16b89ae 100644 --- a/builtin/providers/aws/resources.go +++ b/builtin/providers/aws/resources.go @@ -48,6 +48,13 @@ func init() { Refresh: resource_aws_internet_gateway_refresh, }, + "aws_launch_configuration": resource.Resource{ + Create: resource_aws_launch_configuration_create, + Destroy: resource_aws_launch_configuration_destroy, + Diff: resource_aws_launch_configuration_diff, + Refresh: resource_aws_launch_configuration_refresh, + }, + "aws_route_table": resource.Resource{ ConfigValidator: &config.Validator{ Required: []string{ diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 82484c3ef..e4a83c933 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -84,7 +84,7 @@ func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} { return result } -// Flattens an array of SecurityGroups into a []string +// Flattens an array of UserSecurityGroups into a []string func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string { result := make([]string, 0, len(list)) for _, g := range list { @@ -93,6 +93,15 @@ func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string { return result } +// Flattens an array of SecurityGroups into a []string +func flattenAutoscalingSecurityGroups(list []autoscaling.SecurityGroup) []string { + result := make([]string, 0, len(list)) + for _, g := range list { + result = append(result, g.SecurityGroup) + } + return result +} + // Flattens an array of AvailabilityZones into a []string func flattenAvailabilityZones(list []autoscaling.AvailabilityZone) []string { result := make([]string, 0, len(list))