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