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

r/aws_globalaccelerator_listener: Add aws_globalaccelerator_listener resource #7003

Merged
merged 1 commit into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -473,6 +473,7 @@ func Provider() terraform.ResourceProvider {
"aws_glacier_vault": resourceAwsGlacierVault(),
"aws_glacier_vault_lock": resourceAwsGlacierVaultLock(),
"aws_globalaccelerator_accelerator": resourceAwsGlobalAcceleratorAccelerator(),
"aws_globalaccelerator_listener": resourceAwsGlobalAcceleratorListener(),
"aws_glue_catalog_database": resourceAwsGlueCatalogDatabase(),
"aws_glue_catalog_table": resourceAwsGlueCatalogTable(),
"aws_glue_classifier": resourceAwsGlueClassifier(),
Expand Down
270 changes: 270 additions & 0 deletions aws/resource_aws_globalaccelerator_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
package aws

import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/globalaccelerator"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsGlobalAcceleratorListener() *schema.Resource {
return &schema.Resource{
Create: resourceAwsGlobalAcceleratorListenerCreate,
Read: resourceAwsGlobalAcceleratorListenerRead,
Update: resourceAwsGlobalAcceleratorListenerUpdate,
Delete: resourceAwsGlobalAcceleratorListenerDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should likely remove these customizable timeouts since:

  • They do not seem to be determined by a scalable resource such as amount of data
  • They are currently undocumented 😉

Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"accelerator_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"client_affinity": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should switch this to Default: globalaccelerator.ClientAffinityNone, to match the API default and so we can properly perform drift detection when unconfigured.

Suggested change
Computed: true,
Default: globalaccelerator.ClientAffinityNone,

ValidateFunc: validation.StringInSlice([]string{
globalaccelerator.ClientAffinityNone,
globalaccelerator.ClientAffinitySourceIp,
}, false),
},
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
globalaccelerator.ProtocolTcp,
globalaccelerator.ProtocolUdp,
}, false),
},
"port_range": {
Type: schema.TypeList,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the ordering matter for these? If not, we should use schema.TypeSet. It would be nice to verify this either which way with an acceptance test that has multiple port_range configurations.

Required: true,
MinItems: 1,
MaxItems: 10,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"from_port": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add plan-time validation for this attribute and to_port below. 👍

ValidateFunc: validation.IntBetween(1, 65535),

Type: schema.TypeInt,
Optional: true,
},
"to_port": {
Type: schema.TypeInt,
Optional: true,
},
},
},
},
},
}
}

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

opts := &globalaccelerator.CreateListenerInput{
AcceleratorArn: aws.String(d.Get("accelerator_arn").(string)),
IdempotencyToken: aws.String(resource.UniqueId()),
Protocol: aws.String(d.Get("protocol").(string)),
PortRanges: resourceAwsGlobalAcceleratorListenerExpandPortRanges(d.Get("port_range").([]interface{})),
}

if v, ok := d.GetOk("client_affinity"); ok {
opts.ClientAffinity = aws.String(v.(string))
}

log.Printf("[DEBUG] Create Global Accelerator listener: %s", opts)

resp, err := conn.CreateListener(opts)
if err != nil {
return fmt.Errorf("Error creating Global Accelerator listener: %s", err)
}

d.SetId(*resp.Listener.ListenerArn)

// Creating a listener triggers the accelerator to change status to InPending
stateConf := &resource.StateChangeConf{
Pending: []string{globalaccelerator.AcceleratorStatusInProgress},
Target: []string{globalaccelerator.AcceleratorStatusDeployed},
Refresh: resourceAwsGlobalAcceleratorAcceleratorStateRefreshFunc(conn, d.Get("accelerator_arn").(string)),
Timeout: d.Timeout(schema.TimeoutCreate),
}

log.Printf("[DEBUG] Waiting for Global Accelerator listener (%s) availability", d.Id())
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Global Accelerator listener (%s) availability: %s", d.Id(), err)
}

return resourceAwsGlobalAcceleratorListenerRead(d, meta)
}

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

listener, err := resourceAwsGlobalAcceleratorListenerRetrieve(conn, d.Id())

if err != nil {
return fmt.Errorf("Error reading Global Accelerator listener: %s", err)
}

if listener == nil {
log.Printf("[WARN] Global Accelerator listener (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

acceleratorArn, err := resourceAwsGlobalAcceleratorListenerParseAcceleratorArn(d.Id())

if err != nil {
return err
}

d.Set("accelerator_arn", acceleratorArn)
d.Set("client_affinity", listener.ClientAffinity)
d.Set("protocol", listener.Protocol)
d.Set("port_range", resourceAwsGlobalAcceleratorListenerFlattenPortRanges(listener.PortRanges))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using d.Set() with aggregate types (TypeList, TypeSet, TypeMap), we should perform error checking to prevent issues where the code is not properly able to set the Terraform state. e.g.

Suggested change
d.Set("port_range", resourceAwsGlobalAcceleratorListenerFlattenPortRanges(listener.PortRanges))
if err := d.Set("port_range", resourceAwsGlobalAcceleratorListenerFlattenPortRanges(listener.PortRanges)); err != nil {
return fmt.Errorf("error setting routing_config: %s", err)
}


return nil
}

func resourceAwsGlobalAcceleratorListenerParseAcceleratorArn(listenerArn string) (string, error) {
parts := strings.Split(listenerArn, "/")
if len(parts) < 4 {
return "", fmt.Errorf("Unable to parse accelerator ARN from %s", listenerArn)
}
return strings.Join(parts[0:2], "/"), nil
}

func resourceAwsGlobalAcceleratorListenerExpandPortRanges(portRanges []interface{}) []*globalaccelerator.PortRange {
out := make([]*globalaccelerator.PortRange, len(portRanges))

for i, raw := range portRanges {
portRange := raw.(map[string]interface{})
m := globalaccelerator.PortRange{}

m.FromPort = aws.Int64(int64(portRange["from_port"].(int)))
m.ToPort = aws.Int64(int64(portRange["to_port"].(int)))

out[i] = &m
}

return out
}

func resourceAwsGlobalAcceleratorListenerFlattenPortRanges(portRanges []*globalaccelerator.PortRange) []interface{} {
out := make([]interface{}, len(portRanges))

for i, portRange := range portRanges {
m := make(map[string]interface{})

m["from_port"] = *portRange.FromPort
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent potential panics we should use the AWS Go SDK provided helpers with FromPort and ToPort:

		m["from_port"] = aws.Int64Value(portRange.FromPort)
		m["to_port"] = aws.Int64Value(portRange.ToPort)

m["to_port"] = *portRange.ToPort

out[i] = m
}

log.Printf("[DEBUG] Flatten port_range: %s", out)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Extraneous logging can be removed. 👍

return out
}

func resourceAwsGlobalAcceleratorListenerRetrieve(conn *globalaccelerator.GlobalAccelerator, listenerArn string) (*globalaccelerator.Listener, error) {
resp, err := conn.DescribeListener(&globalaccelerator.DescribeListenerInput{
ListenerArn: aws.String(listenerArn),
})

if err != nil {
if isAWSErr(err, globalaccelerator.ErrCodeListenerNotFoundException, "") {
return nil, nil
}
return nil, err
}

return resp.Listener, nil
}

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

opts := &globalaccelerator.UpdateListenerInput{
ListenerArn: aws.String(d.Id()),
Protocol: aws.String(d.Get("protocol").(string)),
PortRanges: resourceAwsGlobalAcceleratorListenerExpandPortRanges(d.Get("port_range").([]interface{})),
}

if v, ok := d.GetOk("client_affinity"); ok {
opts.ClientAffinity = aws.String(v.(string))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are switching client_affinity to have a default, this can be moved up into the above struct. 👍

}

log.Printf("[DEBUG] Update Global Accelerator listener: %s", opts)

_, err := conn.UpdateListener(opts)
if err != nil {
return fmt.Errorf("Error updating Global Accelerator listener: %s", err)
}

// Creating a listener triggers the accelerator to change status to InPending
stateConf := &resource.StateChangeConf{
Pending: []string{globalaccelerator.AcceleratorStatusInProgress},
Target: []string{globalaccelerator.AcceleratorStatusDeployed},
Refresh: resourceAwsGlobalAcceleratorAcceleratorStateRefreshFunc(conn, d.Get("accelerator_arn").(string)),
Timeout: d.Timeout(schema.TimeoutUpdate),
}

log.Printf("[DEBUG] Waiting for Global Accelerator listener (%s) availability", d.Id())
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Global Accelerator listener (%s) availability: %s", d.Id(), err)
}

return resourceAwsGlobalAcceleratorListenerRead(d, meta)
}

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

opts := &globalaccelerator.DeleteListenerInput{
ListenerArn: aws.String(d.Id()),
}

_, err := conn.DeleteListener(opts)
if err != nil {
if isAWSErr(err, globalaccelerator.ErrCodeListenerNotFoundException, "") {
return nil
}
return fmt.Errorf("Error deleting Global Accelerator listener: %s", err)
}

// Deleting a listener triggers the accelerator to change status to InPending
stateConf := &resource.StateChangeConf{
Pending: []string{globalaccelerator.AcceleratorStatusInProgress},
Target: []string{globalaccelerator.AcceleratorStatusDeployed},
Refresh: resourceAwsGlobalAcceleratorAcceleratorStateRefreshFunc(conn, d.Get("accelerator_arn").(string)),
Timeout: d.Timeout(schema.TimeoutDelete),
}

log.Printf("[DEBUG] Waiting for Global Accelerator listener (%s) deletion", d.Id())
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Global Accelerator listener (%s) deletion: %s", d.Id(), err)
}

return nil
}
Loading