forked from hashicorp/terraform-provider-aws
/
resource_aws_gamelift_game_session_queue.go
218 lines (194 loc) · 7.26 KB
/
resource_aws_gamelift_game_session_queue.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package aws
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/gamelift"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"log"
)
func resourceAwsGameliftGameSessionQueue() *schema.Resource {
return &schema.Resource{
Create: resourceAwsGameliftGameSessionQueueCreate,
Read: resourceAwsGameliftGameSessionQueueRead,
Update: resourceAwsGameliftGameSessionQueueUpdate,
Delete: resourceAwsGameliftGameSessionQueueDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"destinations": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"player_latency_policy": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"maximum_individual_player_latency_milliseconds": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntAtLeast(0),
},
"policy_duration_seconds": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(0),
},
},
},
},
"timeout_in_seconds": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(10, 600),
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsGameliftGameSessionQueueCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
input := gamelift.CreateGameSessionQueueInput{
Name: aws.String(d.Get("name").(string)),
Destinations: expandGameliftGameSessionQueueDestinations(d.Get("destinations").([]interface{})),
PlayerLatencyPolicies: expandGameliftGameSessionPlayerLatencyPolicies(d.Get("player_latency_policy").([]interface{})),
TimeoutInSeconds: aws.Int64(int64(d.Get("timeout_in_seconds").(int))),
}
log.Printf("[INFO] Creating Gamelift Session Queue: %s", input)
out, err := conn.CreateGameSessionQueue(&input)
if err != nil {
return fmt.Errorf("error creating Gamelift Game Session Queue: %s", err)
}
d.SetId(*out.GameSessionQueue.Name)
return resourceAwsGameliftGameSessionQueueRead(d, meta)
}
func resourceAwsGameliftGameSessionQueueRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
log.Printf("[INFO] Describing Gamelift Session Queues: %s", d.Id())
limit := int64(1)
out, err := conn.DescribeGameSessionQueues(&gamelift.DescribeGameSessionQueuesInput{
Names: aws.StringSlice([]string{d.Id()}),
Limit: &limit,
})
if err != nil {
if isAWSErr(err, gamelift.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Gamelift Session Queues (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading Gamelift Game Session Queue (%s): %s", d.Id(), err)
}
sessionQueues := out.GameSessionQueues
if len(sessionQueues) < 1 {
log.Printf("[WARN] Gamelift Session Queue (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if len(sessionQueues) != 1 {
return fmt.Errorf("expected exactly 1 Gamelift Session Queues, found %d under %q",
len(sessionQueues), d.Id())
}
sessionQueue := sessionQueues[0]
d.Set("arn", sessionQueue.GameSessionQueueArn)
d.Set("name", sessionQueue.Name)
d.Set("timeout_in_seconds", sessionQueue.TimeoutInSeconds)
if err := d.Set("destinations", flattenGameliftGameSessionQueueDestinations(sessionQueue.Destinations)); err != nil {
return fmt.Errorf("error setting destinations: %s", err)
}
if err := d.Set("player_latency_policy", flattenGameliftPlayerLatencyPolicies(sessionQueue.PlayerLatencyPolicies)); err != nil {
return fmt.Errorf("error setting player_latency_policy: %s", err)
}
return nil
}
func flattenGameliftGameSessionQueueDestinations(destinations []*gamelift.GameSessionQueueDestination) []interface{} {
l := make([]interface{}, 0)
for _, destination := range destinations {
if destination == nil {
continue
}
l = append(l, aws.StringValue(destination.DestinationArn))
}
return l
}
func flattenGameliftPlayerLatencyPolicies(playerLatencyPolicies []*gamelift.PlayerLatencyPolicy) []interface{} {
l := make([]interface{}, 0)
for _, policy := range playerLatencyPolicies {
m := map[string]interface{}{
"maximum_individual_player_latency_milliseconds": aws.Int64Value(policy.MaximumIndividualPlayerLatencyMilliseconds),
"policy_duration_seconds": aws.Int64Value(policy.PolicyDurationSeconds),
}
l = append(l, m)
}
return l
}
func resourceAwsGameliftGameSessionQueueUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
log.Printf("[INFO] Updating Gamelift Session Queue: %s", d.Id())
input := gamelift.UpdateGameSessionQueueInput{
Name: aws.String(d.Id()),
Destinations: expandGameliftGameSessionQueueDestinations(d.Get("destinations").([]interface{})),
PlayerLatencyPolicies: expandGameliftGameSessionPlayerLatencyPolicies(d.Get("player_latency_policy").([]interface{})),
TimeoutInSeconds: aws.Int64(int64(d.Get("timeout_in_seconds").(int))),
}
_, err := conn.UpdateGameSessionQueue(&input)
if err != nil {
return fmt.Errorf("error updating Gamelift Game Session Queue (%s): %s", d.Id(), err)
}
return resourceAwsGameliftGameSessionQueueRead(d, meta)
}
func resourceAwsGameliftGameSessionQueueDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
log.Printf("[INFO] Deleting Gamelift Session Queue: %s", d.Id())
_, err := conn.DeleteGameSessionQueue(&gamelift.DeleteGameSessionQueueInput{
Name: aws.String(d.Id()),
})
if isAWSErr(err, gamelift.ErrCodeNotFoundException, "") {
return nil
}
if err != nil {
return fmt.Errorf("error deleting Gamelift Game Session Queue (%s): %s", d.Id(), err)
}
return nil
}
func expandGameliftGameSessionQueueDestinations(destinationsMap []interface{}) []*gamelift.GameSessionQueueDestination {
if len(destinationsMap) < 1 {
return nil
}
var destinations []*gamelift.GameSessionQueueDestination
for _, destination := range destinationsMap {
destinations = append(
destinations,
&gamelift.GameSessionQueueDestination{
DestinationArn: aws.String(destination.(string)),
})
}
return destinations
}
func expandGameliftGameSessionPlayerLatencyPolicies(destinationsPlayerLatencyPolicyMap []interface{}) []*gamelift.PlayerLatencyPolicy {
if len(destinationsPlayerLatencyPolicyMap) < 1 {
return nil
}
var playerLatencyPolicies []*gamelift.PlayerLatencyPolicy
for _, playerLatencyPolicy := range destinationsPlayerLatencyPolicyMap {
item := playerLatencyPolicy.(map[string]interface{})
playerLatencyPolicies = append(
playerLatencyPolicies,
&gamelift.PlayerLatencyPolicy{
MaximumIndividualPlayerLatencyMilliseconds: aws.Int64(int64(item["maximum_individual_player_latency_milliseconds"].(int))),
PolicyDurationSeconds: aws.Int64(int64(item["policy_duration_seconds"].(int))),
})
}
return playerLatencyPolicies
}