github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_codedeploy_app.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/codedeploy" 13 ) 14 15 func resourceAwsCodeDeployApp() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsCodeDeployAppCreate, 18 Read: resourceAwsCodeDeployAppRead, 19 Update: resourceAwsCodeDeployUpdate, 20 Delete: resourceAwsCodeDeployAppDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "name": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 // The unique ID is set by AWS on create. 30 "unique_id": &schema.Schema{ 31 Type: schema.TypeString, 32 Optional: true, 33 Computed: true, 34 }, 35 }, 36 } 37 } 38 39 func resourceAwsCodeDeployAppCreate(d *schema.ResourceData, meta interface{}) error { 40 conn := meta.(*AWSClient).codedeployconn 41 42 application := d.Get("name").(string) 43 log.Printf("[DEBUG] Creating CodeDeploy application %s", application) 44 45 resp, err := conn.CreateApplication(&codedeploy.CreateApplicationInput{ 46 ApplicationName: aws.String(application), 47 }) 48 if err != nil { 49 return err 50 } 51 log.Printf("[DEBUG] CodeDeploy application %s created", *resp.ApplicationId) 52 53 // Despite giving the application a unique ID, AWS doesn't actually use 54 // it in API calls. Use it and the app name to identify the resource in 55 // the state file. This allows us to reliably detect both when the TF 56 // config file changes and when the user deletes the app without removing 57 // it first from the TF config. 58 d.SetId(fmt.Sprintf("%s:%s", *resp.ApplicationId, application)) 59 60 return resourceAwsCodeDeployAppRead(d, meta) 61 } 62 63 func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) error { 64 conn := meta.(*AWSClient).codedeployconn 65 66 _, application := resourceAwsCodeDeployAppParseId(d.Id()) 67 log.Printf("[DEBUG] Reading CodeDeploy application %s", application) 68 resp, err := conn.GetApplication(&codedeploy.GetApplicationInput{ 69 ApplicationName: aws.String(application), 70 }) 71 if err != nil { 72 if codedeployerr, ok := err.(awserr.Error); ok && codedeployerr.Code() == "ApplicationDoesNotExistException" { 73 d.SetId("") 74 return nil 75 } else { 76 log.Printf("[ERROR] Error finding CodeDeploy application: %s", err) 77 return err 78 } 79 } 80 81 d.Set("name", resp.Application.ApplicationName) 82 83 return nil 84 } 85 86 func resourceAwsCodeDeployUpdate(d *schema.ResourceData, meta interface{}) error { 87 conn := meta.(*AWSClient).codedeployconn 88 89 o, n := d.GetChange("name") 90 91 _, err := conn.UpdateApplication(&codedeploy.UpdateApplicationInput{ 92 ApplicationName: aws.String(o.(string)), 93 NewApplicationName: aws.String(n.(string)), 94 }) 95 if err != nil { 96 return err 97 } 98 log.Printf("[DEBUG] CodeDeploy application %s updated", n) 99 100 d.Set("name", n) 101 102 return nil 103 } 104 105 func resourceAwsCodeDeployAppDelete(d *schema.ResourceData, meta interface{}) error { 106 conn := meta.(*AWSClient).codedeployconn 107 108 _, err := conn.DeleteApplication(&codedeploy.DeleteApplicationInput{ 109 ApplicationName: aws.String(d.Get("name").(string)), 110 }) 111 if err != nil { 112 if cderr, ok := err.(awserr.Error); ok && cderr.Code() == "InvalidApplicationNameException" { 113 d.SetId("") 114 return nil 115 } else { 116 log.Printf("[ERROR] Error deleting CodeDeploy application: %s", err) 117 return err 118 } 119 } 120 121 return nil 122 } 123 124 func resourceAwsCodeDeployAppParseId(id string) (string, string) { 125 parts := strings.SplitN(id, ":", 2) 126 return parts[0], parts[1] 127 }