github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_codedeploy_deployment_config.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/codedeploy" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceAwsCodeDeployDeploymentConfig() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsCodeDeployDeploymentConfigCreate, 16 Read: resourceAwsCodeDeployDeploymentConfigRead, 17 Delete: resourceAwsCodeDeployDeploymentConfigDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "deployment_config_name": { 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "minimum_healthy_hosts": { 27 Type: schema.TypeList, 28 Required: true, 29 ForceNew: true, 30 MaxItems: 1, 31 Elem: &schema.Resource{ 32 Schema: map[string]*schema.Schema{ 33 "type": { 34 Type: schema.TypeString, 35 Required: true, 36 ValidateFunc: validateMinimumHealtyHostsType, 37 }, 38 39 "value": { 40 Type: schema.TypeInt, 41 Optional: true, 42 }, 43 }, 44 }, 45 }, 46 47 "deployment_config_id": { 48 Type: schema.TypeString, 49 Computed: true, 50 }, 51 }, 52 } 53 } 54 55 func resourceAwsCodeDeployDeploymentConfigCreate(d *schema.ResourceData, meta interface{}) error { 56 conn := meta.(*AWSClient).codedeployconn 57 58 input := &codedeploy.CreateDeploymentConfigInput{ 59 DeploymentConfigName: aws.String(d.Get("deployment_config_name").(string)), 60 MinimumHealthyHosts: expandAwsCodeDeployConfigMinimumHealthHosts(d), 61 } 62 63 _, err := conn.CreateDeploymentConfig(input) 64 if err != nil { 65 return err 66 } 67 68 d.SetId(d.Get("deployment_config_name").(string)) 69 70 return resourceAwsCodeDeployDeploymentConfigRead(d, meta) 71 } 72 73 func resourceAwsCodeDeployDeploymentConfigRead(d *schema.ResourceData, meta interface{}) error { 74 conn := meta.(*AWSClient).codedeployconn 75 76 input := &codedeploy.GetDeploymentConfigInput{ 77 DeploymentConfigName: aws.String(d.Id()), 78 } 79 80 resp, err := conn.GetDeploymentConfig(input) 81 if err != nil { 82 if awsErr, ok := err.(awserr.Error); ok { 83 if "DeploymentConfigDoesNotExistException" == awsErr.Code() { 84 log.Printf("[DEBUG] CodeDeploy Deployment Config (%s) not found", d.Id()) 85 d.SetId("") 86 return nil 87 } 88 } 89 return err 90 } 91 92 if resp.DeploymentConfigInfo == nil { 93 return fmt.Errorf("[ERROR] Cannot find DeploymentConfig %q", d.Id()) 94 } 95 96 if err := d.Set("minimum_healthy_hosts", flattenAwsCodeDeployConfigMinimumHealthHosts(resp.DeploymentConfigInfo.MinimumHealthyHosts)); err != nil { 97 return err 98 } 99 d.Set("deployment_config_id", resp.DeploymentConfigInfo.DeploymentConfigId) 100 d.Set("deployment_config_name", resp.DeploymentConfigInfo.DeploymentConfigName) 101 102 return nil 103 } 104 105 func resourceAwsCodeDeployDeploymentConfigDelete(d *schema.ResourceData, meta interface{}) error { 106 conn := meta.(*AWSClient).codedeployconn 107 108 input := &codedeploy.DeleteDeploymentConfigInput{ 109 DeploymentConfigName: aws.String(d.Id()), 110 } 111 112 _, err := conn.DeleteDeploymentConfig(input) 113 if err != nil { 114 return err 115 } 116 117 return nil 118 } 119 120 func expandAwsCodeDeployConfigMinimumHealthHosts(d *schema.ResourceData) *codedeploy.MinimumHealthyHosts { 121 hosts := d.Get("minimum_healthy_hosts").([]interface{}) 122 host := hosts[0].(map[string]interface{}) 123 124 minimumHealthyHost := codedeploy.MinimumHealthyHosts{ 125 Type: aws.String(host["type"].(string)), 126 Value: aws.Int64(int64(host["value"].(int))), 127 } 128 129 return &minimumHealthyHost 130 } 131 132 func flattenAwsCodeDeployConfigMinimumHealthHosts(hosts *codedeploy.MinimumHealthyHosts) []map[string]interface{} { 133 result := make([]map[string]interface{}, 0) 134 135 item := make(map[string]interface{}) 136 137 item["type"] = *hosts.Type 138 item["value"] = *hosts.Value 139 140 result = append(result, item) 141 142 return result 143 } 144 145 func validateMinimumHealtyHostsType(v interface{}, k string) (ws []string, errors []error) { 146 value := v.(string) 147 if value != "FLEET_PERCENT" && value != "HOST_COUNT" { 148 errors = append(errors, fmt.Errorf( 149 "%q must be one of \"FLEET_PERCENT\" or \"HOST_COUNT\"", k)) 150 } 151 return 152 }