forked from hashicorp/terraform-provider-aws
/
resource_aws_gamelift_build.go
190 lines (168 loc) · 4.81 KB
/
resource_aws_gamelift_build.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package aws
import (
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/gamelift"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsGameliftBuild() *schema.Resource {
return &schema.Resource{
Create: resourceAwsGameliftBuildCreate,
Read: resourceAwsGameliftBuildRead,
Update: resourceAwsGameliftBuildUpdate,
Delete: resourceAwsGameliftBuildDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"operating_system": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
gamelift.OperatingSystemAmazonLinux,
gamelift.OperatingSystemWindows2012,
}, false),
},
"storage_location": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"role_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
},
},
},
"version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
},
}
}
func resourceAwsGameliftBuildCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
sl := expandGameliftStorageLocation(d.Get("storage_location").([]interface{}))
input := gamelift.CreateBuildInput{
Name: aws.String(d.Get("name").(string)),
OperatingSystem: aws.String(d.Get("operating_system").(string)),
StorageLocation: sl,
}
if v, ok := d.GetOk("version"); ok {
input.Version = aws.String(v.(string))
}
log.Printf("[INFO] Creating Gamelift Build: %s", input)
var out *gamelift.CreateBuildOutput
err := resource.Retry(30*time.Second, func() *resource.RetryError {
var err error
out, err = conn.CreateBuild(&input)
if err != nil {
if isAWSErr(err, gamelift.ErrCodeInvalidRequestException, "Provided build is not accessible.") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return err
}
d.SetId(*out.Build.BuildId)
stateConf := resource.StateChangeConf{
Pending: []string{gamelift.BuildStatusInitialized},
Target: []string{gamelift.BuildStatusReady},
Timeout: 1 * time.Minute,
Refresh: func() (interface{}, string, error) {
out, err := conn.DescribeBuild(&gamelift.DescribeBuildInput{
BuildId: aws.String(d.Id()),
})
if err != nil {
return 42, "", err
}
return out, *out.Build.Status, nil
},
}
_, err = stateConf.WaitForState()
if err != nil {
return err
}
return resourceAwsGameliftBuildRead(d, meta)
}
func resourceAwsGameliftBuildRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
log.Printf("[INFO] Reading Gamelift Build: %s", d.Id())
out, err := conn.DescribeBuild(&gamelift.DescribeBuildInput{
BuildId: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, gamelift.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Gamelift Build (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
b := out.Build
d.Set("name", b.Name)
d.Set("operating_system", b.OperatingSystem)
d.Set("version", b.Version)
return nil
}
func resourceAwsGameliftBuildUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
log.Printf("[INFO] Updating Gamelift Build: %s", d.Id())
input := gamelift.UpdateBuildInput{
BuildId: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
}
if v, ok := d.GetOk("version"); ok {
input.Version = aws.String(v.(string))
}
_, err := conn.UpdateBuild(&input)
if err != nil {
return err
}
return resourceAwsGameliftBuildRead(d, meta)
}
func resourceAwsGameliftBuildDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
log.Printf("[INFO] Deleting Gamelift Build: %s", d.Id())
_, err := conn.DeleteBuild(&gamelift.DeleteBuildInput{
BuildId: aws.String(d.Id()),
})
if err != nil {
return err
}
return nil
}
func expandGameliftStorageLocation(cfg []interface{}) *gamelift.S3Location {
loc := cfg[0].(map[string]interface{})
return &gamelift.S3Location{
Bucket: aws.String(loc["bucket"].(string)),
Key: aws.String(loc["key"].(string)),
RoleArn: aws.String(loc["role_arn"].(string)),
}
}