forked from hashicorp/terraform-provider-aws
/
resource_aws_codedeploy_app.go
175 lines (142 loc) · 4.92 KB
/
resource_aws_codedeploy_app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package aws
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/codedeploy"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsCodeDeployApp() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCodeDeployAppCreate,
Read: resourceAwsCodeDeployAppRead,
Update: resourceAwsCodeDeployUpdate,
Delete: resourceAwsCodeDeployAppDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), ":")
if len(idParts) == 2 {
return []*schema.ResourceData{d}, nil
}
applicationName := d.Id()
conn := meta.(*AWSClient).codedeployconn
input := &codedeploy.GetApplicationInput{
ApplicationName: aws.String(applicationName),
}
log.Printf("[DEBUG] Reading CodeDeploy Application: %s", input)
output, err := conn.GetApplication(input)
if err != nil {
return []*schema.ResourceData{}, err
}
if output == nil || output.Application == nil {
return []*schema.ResourceData{}, fmt.Errorf("error reading CodeDeploy Application (%s): empty response", applicationName)
}
d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(output.Application.ApplicationId), applicationName))
d.Set("name", applicationName)
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"compute_platform": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
codedeploy.ComputePlatformEcs,
codedeploy.ComputePlatformLambda,
codedeploy.ComputePlatformServer,
}, false),
Default: codedeploy.ComputePlatformServer,
},
// The unique ID is set by AWS on create.
"unique_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceAwsCodeDeployAppCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codedeployconn
application := d.Get("name").(string)
computePlatform := d.Get("compute_platform").(string)
log.Printf("[DEBUG] Creating CodeDeploy application %s", application)
resp, err := conn.CreateApplication(&codedeploy.CreateApplicationInput{
ApplicationName: aws.String(application),
ComputePlatform: aws.String(computePlatform),
})
if err != nil {
return err
}
log.Printf("[DEBUG] CodeDeploy application %s created", *resp.ApplicationId)
// Despite giving the application a unique ID, AWS doesn't actually use
// it in API calls. Use it and the app name to identify the resource in
// the state file. This allows us to reliably detect both when the TF
// config file changes and when the user deletes the app without removing
// it first from the TF config.
d.SetId(fmt.Sprintf("%s:%s", *resp.ApplicationId, application))
return resourceAwsCodeDeployAppRead(d, meta)
}
func resourceAwsCodeDeployAppRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codedeployconn
application := resourceAwsCodeDeployAppParseId(d.Id())
log.Printf("[DEBUG] Reading CodeDeploy application %s", application)
resp, err := conn.GetApplication(&codedeploy.GetApplicationInput{
ApplicationName: aws.String(application),
})
if err != nil {
if codedeployerr, ok := err.(awserr.Error); ok && codedeployerr.Code() == "ApplicationDoesNotExistException" {
d.SetId("")
return nil
} else {
log.Printf("[ERROR] Error finding CodeDeploy application: %s", err)
return err
}
}
d.Set("compute_platform", resp.Application.ComputePlatform)
d.Set("name", resp.Application.ApplicationName)
return nil
}
func resourceAwsCodeDeployUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codedeployconn
o, n := d.GetChange("name")
_, err := conn.UpdateApplication(&codedeploy.UpdateApplicationInput{
ApplicationName: aws.String(o.(string)),
NewApplicationName: aws.String(n.(string)),
})
if err != nil {
return err
}
log.Printf("[DEBUG] CodeDeploy application %s updated", n)
d.Set("name", n)
return nil
}
func resourceAwsCodeDeployAppDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codedeployconn
_, err := conn.DeleteApplication(&codedeploy.DeleteApplicationInput{
ApplicationName: aws.String(d.Get("name").(string)),
})
if err != nil {
if cderr, ok := err.(awserr.Error); ok && cderr.Code() == "InvalidApplicationNameException" {
return nil
} else {
log.Printf("[ERROR] Error deleting CodeDeploy application: %s", err)
return err
}
}
return nil
}
func resourceAwsCodeDeployAppParseId(id string) string {
parts := strings.SplitN(id, ":", 2)
// We currently omit the application ID as it is not currently used anywhere
return parts[1]
}