Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gamelift Game Session Queue #6335

Merged
merged 15 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ func Provider() terraform.ResourceProvider {
"aws_gamelift_alias": resourceAwsGameliftAlias(),
"aws_gamelift_build": resourceAwsGameliftBuild(),
"aws_gamelift_fleet": resourceAwsGameliftFleet(),
"aws_gamelift_game_session_queue": resourceAwsGameliftGameSessionQueue(),
"aws_glacier_vault": resourceAwsGlacierVault(),
"aws_glue_catalog_database": resourceAwsGlueCatalogDatabase(),
"aws_glue_catalog_table": resourceAwsGlueCatalogTable(),
Expand Down
204 changes: 204 additions & 0 deletions aws/resource_aws_gamelift_game_session_queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
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,

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 err
Hinidu marked this conversation as resolved.
Show resolved Hide resolved
}

d.SetId(*out.GameSessionQueue.GameSessionQueueArn)
Hinidu marked this conversation as resolved.
Show resolved Hide resolved
d.Set("name", out.GameSessionQueue.Name)
Hinidu marked this conversation as resolved.
Show resolved Hide resolved

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.Get("name"))
limit := int64(1)
out, err := conn.DescribeGameSessionQueues(&gamelift.DescribeGameSessionQueuesInput{
Names: aws.StringSlice([]string{d.Get("name").(string)}),
Limit: &limit,
})
if err != nil {
if isAWSErr(err, gamelift.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Gamelift Session Queues (%s) not found, removing from state", d.Get("name"))
d.SetId("")
return nil
}
return err
Hinidu marked this conversation as resolved.
Show resolved Hide resolved
}
sessionQueues := out.GameSessionQueues

if len(sessionQueues) < 1 {
log.Printf("[WARN] Gamelift Session Queue (%s) not found, removing from state", d.Get("name"))
d.SetId("")
return nil
}
if len(sessionQueues) != 1 {
return fmt.Errorf("expected exactly 1 Gamelift Session Queues, found %d under %q",
len(sessionQueues), d.Get("name"))
}
sessionQueue := sessionQueues[0]

d.Set("arn", sessionQueue.GameSessionQueueArn)
d.Set("name", sessionQueue.Name)
d.Set("timeout_in_seconds", sessionQueue.TimeoutInSeconds)
d.Set("destinations", sessionQueue.Destinations)
Hinidu marked this conversation as resolved.
Show resolved Hide resolved
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 flattenGameliftPlayerLatencyPolicies(playerLatencyPolicies []*gamelift.PlayerLatencyPolicy) []interface{} {
lst := []interface{}{}
for _, policy := range playerLatencyPolicies {
m := map[string]interface{}{
"maximum_individual_player_latency_milliseconds": aws.Int64Value(policy.MaximumIndividualPlayerLatencyMilliseconds),
"policy_duration_seconds": aws.Int64Value(policy.PolicyDurationSeconds),
}
lst = append(lst, m)
}
return lst
}

func resourceAwsGameliftGameSessionQueueUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn

name := d.Get("name").(string)

log.Printf("[INFO] Updating Gamelift Session Queue: %s", name)

input := gamelift.UpdateGameSessionQueueInput{
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))),
}

_, err := conn.UpdateGameSessionQueue(&input)
if err != nil {
return err
Hinidu marked this conversation as resolved.
Show resolved Hide resolved
}

return resourceAwsGameliftGameSessionQueueRead(d, meta)
}

func resourceAwsGameliftGameSessionQueueDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).gameliftconn
name := d.Get("name").(string)
log.Printf("[INFO] Deleting Gamelift Session Queue: %s", name)
_, err := conn.DeleteGameSessionQueue(&gamelift.DeleteGameSessionQueueInput{
Name: aws.String(name),
})
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
}
Loading