provider/aws: Add support for aws_lightsail_static_ip (#13175)

This commit is contained in:
Radek Simko 2017-03-30 14:59:28 +01:00 committed by GitHub
parent 5ba7aa8296
commit 7d8a6f8533
5 changed files with 276 additions and 0 deletions

View File

@ -337,6 +337,7 @@ func Provider() terraform.ResourceProvider {
"aws_lightsail_domain": resourceAwsLightsailDomain(),
"aws_lightsail_instance": resourceAwsLightsailInstance(),
"aws_lightsail_key_pair": resourceAwsLightsailKeyPair(),
"aws_lightsail_static_ip": resourceAwsLightsailStaticIp(),
"aws_lb_cookie_stickiness_policy": resourceAwsLBCookieStickinessPolicy(),
"aws_load_balancer_policy": resourceAwsLoadBalancerPolicy(),
"aws_load_balancer_backend_server_policy": resourceAwsLoadBalancerBackendServerPolicies(),

View File

@ -0,0 +1,98 @@
package aws
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsLightsailStaticIp() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLightsailStaticIpCreate,
Read: resourceAwsLightsailStaticIpRead,
Delete: resourceAwsLightsailStaticIpDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"support_code": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsLightsailStaticIpCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
name := d.Get("name").(string)
log.Printf("[INFO] Allocating Lightsail Static IP: %q", name)
out, err := conn.AllocateStaticIp(&lightsail.AllocateStaticIpInput{
StaticIpName: aws.String(name),
})
if err != nil {
return err
}
log.Printf("[INFO] Lightsail Static IP allocated: %s", *out)
d.SetId(name)
return resourceAwsLightsailStaticIpRead(d, meta)
}
func resourceAwsLightsailStaticIpRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
name := d.Get("name").(string)
log.Printf("[INFO] Reading Lightsail Static IP: %q", name)
out, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
StaticIpName: aws.String(name),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NotFoundException" {
log.Printf("[WARN] Lightsail Static IP (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
}
return err
}
log.Printf("[INFO] Received Lightsail Static IP: %s", *out)
d.Set("arn", out.StaticIp.Arn)
d.Set("ip_address", out.StaticIp.IpAddress)
d.Set("support_code", out.StaticIp.SupportCode)
return nil
}
func resourceAwsLightsailStaticIpDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
name := d.Get("name").(string)
log.Printf("[INFO] Deleting Lightsail Static IP: %q", name)
out, err := conn.ReleaseStaticIp(&lightsail.ReleaseStaticIpInput{
StaticIpName: aws.String(name),
})
if err != nil {
return err
}
log.Printf("[INFO] Deleted Lightsail Static IP: %s", *out)
return nil
}

View File

@ -0,0 +1,138 @@
package aws
import (
"errors"
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSLightsailStaticIp_basic(t *testing.T) {
var staticIp lightsail.StaticIp
staticIpName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLightsailStaticIpDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLightsailStaticIpConfig_basic(staticIpName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLightsailStaticIpExists("aws_lightsail_static_ip.test", &staticIp),
),
},
},
})
}
func TestAccAWSLightsailStaticIp_disappears(t *testing.T) {
var staticIp lightsail.StaticIp
staticIpName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
staticIpDestroy := func(*terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).lightsailconn
_, err := conn.ReleaseStaticIp(&lightsail.ReleaseStaticIpInput{
StaticIpName: aws.String(staticIpName),
})
if err != nil {
return fmt.Errorf("Error deleting Lightsail Static IP in disapear test")
}
return nil
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLightsailStaticIpDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLightsailStaticIpConfig_basic(staticIpName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLightsailStaticIpExists("aws_lightsail_static_ip.test", &staticIp),
staticIpDestroy,
),
ExpectNonEmptyPlan: true,
},
},
})
}
func testAccCheckAWSLightsailStaticIpExists(n string, staticIp *lightsail.StaticIp) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return errors.New("No Lightsail Static IP ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).lightsailconn
resp, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
StaticIpName: aws.String(rs.Primary.ID),
})
if err != nil {
return err
}
if resp == nil || resp.StaticIp == nil {
return fmt.Errorf("Static IP (%s) not found", rs.Primary.ID)
}
*staticIp = *resp.StaticIp
return nil
}
}
func testAccCheckAWSLightsailStaticIpDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_lightsail_static_ip" {
continue
}
conn := testAccProvider.Meta().(*AWSClient).lightsailconn
resp, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
StaticIpName: aws.String(rs.Primary.ID),
})
if err == nil {
if resp.StaticIp != nil {
return fmt.Errorf("Lightsail Static IP %q still exists", rs.Primary.ID)
}
}
// Verify the error
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NotFoundException" {
return nil
}
}
return err
}
return nil
}
func testAccAWSLightsailStaticIpConfig_basic(staticIpName string) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}
resource "aws_lightsail_static_ip" "test" {
name = "%s"
}
`, staticIpName)
}

View File

@ -0,0 +1,35 @@
---
layout: "aws"
page_title: "AWS: aws_lightsail_static_ip"
sidebar_current: "docs-aws-resource-lightsail-static-ip"
description: |-
Provides an Lightsail Static IP
---
# aws\_lightsail\_static\_ip
Allocates a static IP address.
~> **Note:** Lightsail is currently only supported in `us-east-1` region.
## Example Usage
```
resource "aws_lightsail_static_ip" "test" {
name = "example"
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name for the allocated static IP
## Attributes Reference
The following attributes are exported in addition to the arguments listed above:
* `arn` - The ARN of the Lightsail static IP
* `ip_address` - The allocated static IP address
* `support_code` - The support code.

View File

@ -881,6 +881,10 @@
<a href="/docs/providers/aws/r/lightsail_key_pair.html">aws_lightsail_key_pair</a>
</li>
<li<%= sidebar_current("docs-aws-resource-lightsail-static-ip") %>>
<a href="/docs/providers/aws/r/lightsail_static_ip.html">aws_lightsail_static_ip</a>
</li>
</ul>
</li>