Skip to content

Commit

Permalink
New Data Source: aws_storagegateway_gateway_activation_key
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Jul 16, 2018
1 parent 3e71c3e commit f5d7664
Show file tree
Hide file tree
Showing 5 changed files with 441 additions and 99 deletions.
94 changes: 94 additions & 0 deletions aws/data_source_aws_storagegateway_gateway_activation_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package aws

import (
"fmt"
"log"
"net"
"net/http"
"time"

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

func dataSourceAwsStorageGatewayGatewayActivationKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsStorageGatewayGatewayActivationKeyRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"activation_key": {
Type: schema.TypeString,
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.SingleIP(),
},
},
}
}

func dataSourceAwsStorageGatewayGatewayActivationKeyRead(d *schema.ResourceData, meta interface{}) error {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: time.Second * 10,
}

var activationKey string
activationRegion := meta.(*AWSClient).region
ipAddress := d.Get("ip_address").(string)

requestURL := fmt.Sprintf("http://%s/?activationRegion=%s", ipAddress, activationRegion)
log.Printf("[DEBUG] Creating HTTP request: %s", requestURL)
request, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return fmt.Errorf("error creating HTTP request: %s", err)
}

err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
log.Printf("[DEBUG] Making HTTP request: %s", request.URL.String())
response, err := client.Do(request)
if err != nil {
if err, ok := err.(net.Error); ok {
errMessage := fmt.Errorf("error making HTTP request: %s", err)
log.Printf("[DEBUG] retryable %s", errMessage)
return resource.RetryableError(errMessage)
}
return resource.NonRetryableError(fmt.Errorf("error making HTTP request: %s", err))
}

log.Printf("[DEBUG] Received HTTP response: %#v", response)
if response.StatusCode != 302 {
return resource.NonRetryableError(fmt.Errorf("expected HTTP status code 302, received: %d", response.StatusCode))
}

redirectURL, err := response.Location()
if err != nil {
return resource.NonRetryableError(fmt.Errorf("error extracting HTTP Location header: %s", err))
}

activationKey = redirectURL.Query().Get("activationKey")

return nil
})
if err != nil {
return fmt.Errorf("error retrieving activation key from IP Address (%s): %s", ipAddress, err)
}
if activationKey == "" {
return fmt.Errorf("empty activationKey received from IP Address: %s", ipAddress)
}

d.SetId(activationKey)
d.Set("activation_key", activationKey)
d.Set("ip_address", d.Id())

return nil
}
203 changes: 203 additions & 0 deletions aws/data_source_aws_storagegateway_gateway_activation_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSStorageGatewayGatewayActivationKeyDataSource_FileGateway(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_storagegateway_gateway_activation_key.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSStorageGatewayGatewayActivationKeyDataSourceConfig_FileGateway(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "activation_key"),
),
},
},
})
}

func TestAccAWSStorageGatewayGatewayActivationKeyDataSource_TapeAndVolumeGateway(t *testing.T) {
t.Skip("Currently the EC2 instance webserver is never reachable, its likely an instance configuration error.")

rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_storagegateway_gateway_activation_key.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSStorageGatewayGatewayActivationKeyDataSourceConfig_TapeAndVolumeGateway(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "activation_key"),
),
},
},
})
}

// testAccAWSStorageGateway_VPCBase provides a publicly accessible subnet
// and security group, suitable for Storage Gateway EC2 instances of any type
func testAccAWSStorageGateway_VPCBase(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags {
Name = %q
}
}
resource "aws_subnet" "test" {
cidr_block = "10.0.0.0/24"
vpc_id = "${aws_vpc.test.id}"
tags {
Name = %q
}
}
resource "aws_internet_gateway" "test" {
vpc_id = "${aws_vpc.test.id}"
tags {
Name = %q
}
}
resource "aws_route_table" "test" {
vpc_id = "${aws_vpc.test.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.test.id}"
}
tags {
Name = %q
}
}
resource "aws_route_table_association" "test" {
subnet_id = "${aws_subnet.test.id}"
route_table_id = "${aws_route_table.test.id}"
}
resource "aws_security_group" "test" {
name = %q
vpc_id = "${aws_vpc.test.id}"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = %q
}
}
`, rName, rName, rName, rName, rName, rName)
}

// testAccAWSStorageGateway_FileGatewayBase uses the "thinstaller" Storage
// Gateway AMI for File Gateways
func testAccAWSStorageGateway_FileGatewayBase(rName string) string {
return testAccAWSStorageGateway_VPCBase(rName) + fmt.Sprintf(`
data "aws_ami" "aws-thinstaller" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["aws-thinstaller-*"]
}
}
resource "aws_instance" "test" {
depends_on = ["aws_internet_gateway.test"]
ami = "${data.aws_ami.aws-thinstaller.id}"
associate_public_ip_address = true
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.test.id}"]
subnet_id = "${aws_subnet.test.id}"
tags {
Name = %q
}
}
`, rName)
}

// testAccAWSStorageGateway_TapeAndVolumeGatewayBase uses the Storage Gateway
// AMI for either Tape or Volume Gateways
func testAccAWSStorageGateway_TapeAndVolumeGatewayBase(rName string) string {
return testAccAWSStorageGateway_VPCBase(rName) + fmt.Sprintf(`
data "aws_ami" "aws-storage-gateway-2" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["aws-storage-gateway-2.*"]
}
}
resource "aws_instance" "test" {
depends_on = ["aws_internet_gateway.test"]
ami = "${data.aws_ami.aws-storage-gateway-2.id}"
associate_public_ip_address = true
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.test.id}"]
subnet_id = "${aws_subnet.test.id}"
tags {
Name = %q
}
}
`, rName)
}

func testAccAWSStorageGatewayGatewayActivationKeyDataSourceConfig_FileGateway(rName string) string {
return testAccAWSStorageGateway_FileGatewayBase(rName) + `
data "aws_storagegateway_gateway_activation_key" "test" {
ip_address = "${aws_instance.test.public_ip}"
}
`
}

func testAccAWSStorageGatewayGatewayActivationKeyDataSourceConfig_TapeAndVolumeGateway(rName string) string {
return testAccAWSStorageGateway_TapeAndVolumeGatewayBase(rName) + `
data "aws_storagegateway_gateway_activation_key" "test" {
ip_address = "${aws_instance.test.public_ip}"
}
`
}
Loading

0 comments on commit f5d7664

Please sign in to comment.