github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_elastic_beanstalk_configuration_template.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/elasticbeanstalk" 11 ) 12 13 func resourceAwsElasticBeanstalkConfigurationTemplate() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsElasticBeanstalkConfigurationTemplateCreate, 16 Read: resourceAwsElasticBeanstalkConfigurationTemplateRead, 17 Update: resourceAwsElasticBeanstalkConfigurationTemplateUpdate, 18 Delete: resourceAwsElasticBeanstalkConfigurationTemplateDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 "application": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 "description": &schema.Schema{ 32 Type: schema.TypeString, 33 Optional: true, 34 }, 35 "environment_id": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 ForceNew: true, 39 }, 40 "setting": &schema.Schema{ 41 Type: schema.TypeSet, 42 Optional: true, 43 Computed: true, 44 Elem: resourceAwsElasticBeanstalkOptionSetting(), 45 Set: optionSettingValueHash, 46 }, 47 "solution_stack_name": &schema.Schema{ 48 Type: schema.TypeString, 49 Optional: true, 50 ForceNew: true, 51 }, 52 }, 53 } 54 } 55 56 func resourceAwsElasticBeanstalkConfigurationTemplateCreate(d *schema.ResourceData, meta interface{}) error { 57 conn := meta.(*AWSClient).elasticbeanstalkconn 58 59 // Get the relevant properties 60 name := d.Get("name").(string) 61 appName := d.Get("application").(string) 62 63 optionSettings := gatherOptionSettings(d) 64 65 opts := elasticbeanstalk.CreateConfigurationTemplateInput{ 66 ApplicationName: aws.String(appName), 67 TemplateName: aws.String(name), 68 OptionSettings: optionSettings, 69 } 70 71 if attr, ok := d.GetOk("description"); ok { 72 opts.Description = aws.String(attr.(string)) 73 } 74 75 if attr, ok := d.GetOk("environment_id"); ok { 76 opts.EnvironmentId = aws.String(attr.(string)) 77 } 78 79 if attr, ok := d.GetOk("solution_stack_name"); ok { 80 opts.SolutionStackName = aws.String(attr.(string)) 81 } 82 83 log.Printf("[DEBUG] Elastic Beanstalk configuration template create opts: %s", opts) 84 if _, err := conn.CreateConfigurationTemplate(&opts); err != nil { 85 return fmt.Errorf("Error creating Elastic Beanstalk configuration template: %s", err) 86 } 87 88 d.SetId(name) 89 90 return resourceAwsElasticBeanstalkConfigurationTemplateRead(d, meta) 91 } 92 93 func resourceAwsElasticBeanstalkConfigurationTemplateRead(d *schema.ResourceData, meta interface{}) error { 94 conn := meta.(*AWSClient).elasticbeanstalkconn 95 96 log.Printf("[DEBUG] Elastic Beanstalk configuration template read: %s", d.Get("name").(string)) 97 98 resp, err := conn.DescribeConfigurationSettings(&elasticbeanstalk.DescribeConfigurationSettingsInput{ 99 TemplateName: aws.String(d.Id()), 100 ApplicationName: aws.String(d.Get("application").(string)), 101 }) 102 103 if err != nil { 104 return err 105 } 106 107 // if len(resp.ConfigurationSettings) > 1 { 108 109 // settings := make(map[string]map[string]string) 110 // for _, setting := range resp.ConfigurationSettings { 111 // k := fmt.Sprintf("%s.%s", setting.) 112 // } 113 // } 114 115 if len(resp.ConfigurationSettings) != 1 { 116 log.Printf("[DEBUG] Elastic Beanstalk unexpected describe configuration template response: %+v", resp) 117 return fmt.Errorf("Error reading application properties: found %d applications, expected 1", len(resp.ConfigurationSettings)) 118 } 119 120 d.Set("description", resp.ConfigurationSettings[0].Description) 121 return nil 122 } 123 124 func resourceAwsElasticBeanstalkConfigurationTemplateUpdate(d *schema.ResourceData, meta interface{}) error { 125 conn := meta.(*AWSClient).elasticbeanstalkconn 126 127 log.Printf("[DEBUG] Elastic Beanstalk configuration template update: %s", d.Get("name").(string)) 128 129 if d.HasChange("description") { 130 if err := resourceAwsElasticBeanstalkConfigurationTemplateDescriptionUpdate(conn, d); err != nil { 131 return err 132 } 133 } 134 135 if d.HasChange("setting") { 136 if err := resourceAwsElasticBeanstalkConfigurationTemplateOptionSettingsUpdate(conn, d); err != nil { 137 return err 138 } 139 } 140 141 return resourceAwsElasticBeanstalkConfigurationTemplateRead(d, meta) 142 } 143 144 func resourceAwsElasticBeanstalkConfigurationTemplateDescriptionUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error { 145 _, err := conn.UpdateConfigurationTemplate(&elasticbeanstalk.UpdateConfigurationTemplateInput{ 146 ApplicationName: aws.String(d.Get("application").(string)), 147 TemplateName: aws.String(d.Get("name").(string)), 148 Description: aws.String(d.Get("description").(string)), 149 }) 150 151 return err 152 } 153 154 func resourceAwsElasticBeanstalkConfigurationTemplateOptionSettingsUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error { 155 if d.HasChange("setting") { 156 _, err := conn.ValidateConfigurationSettings(&elasticbeanstalk.ValidateConfigurationSettingsInput{ 157 ApplicationName: aws.String(d.Get("application").(string)), 158 TemplateName: aws.String(d.Get("name").(string)), 159 OptionSettings: gatherOptionSettings(d), 160 }) 161 if err != nil { 162 return err 163 } 164 165 o, n := d.GetChange("setting") 166 if o == nil { 167 o = new(schema.Set) 168 } 169 if n == nil { 170 n = new(schema.Set) 171 } 172 173 os := o.(*schema.Set) 174 ns := o.(*schema.Set) 175 176 remove := extractOptionSettings(os.Difference(ns)) 177 add := extractOptionSettings(ns.Difference(os)) 178 179 req := &elasticbeanstalk.UpdateConfigurationTemplateInput{ 180 ApplicationName: aws.String(d.Get("application").(string)), 181 TemplateName: aws.String(d.Get("name").(string)), 182 OptionSettings: add, 183 } 184 185 for _, elem := range remove { 186 req.OptionsToRemove = append(req.OptionsToRemove, &elasticbeanstalk.OptionSpecification{ 187 Namespace: elem.Namespace, 188 OptionName: elem.OptionName, 189 }) 190 } 191 192 if _, err := conn.UpdateConfigurationTemplate(req); err != nil { 193 return err 194 } 195 } 196 197 return nil 198 } 199 200 func resourceAwsElasticBeanstalkConfigurationTemplateDelete(d *schema.ResourceData, meta interface{}) error { 201 conn := meta.(*AWSClient).elasticbeanstalkconn 202 203 application := d.Get("application").(string) 204 205 _, err := conn.DeleteConfigurationTemplate(&elasticbeanstalk.DeleteConfigurationTemplateInput{ 206 ApplicationName: aws.String(application), 207 TemplateName: aws.String(d.Id()), 208 }) 209 210 return err 211 } 212 213 func gatherOptionSettings(d *schema.ResourceData) []*elasticbeanstalk.ConfigurationOptionSetting { 214 optionSettingsSet, ok := d.Get("setting").(*schema.Set) 215 if !ok || optionSettingsSet == nil { 216 optionSettingsSet = new(schema.Set) 217 } 218 219 return extractOptionSettings(optionSettingsSet) 220 }