github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_eip_association_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/denverdino/aliyungo/ecs"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"time"
    11  )
    12  
    13  func TestAccAlicloudEIPAssociation(t *testing.T) {
    14  	var asso ecs.EipAddressSetType
    15  	var inst ecs.InstanceAttributesType
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck: func() {
    19  			testAccPreCheck(t)
    20  		},
    21  
    22  		// module name
    23  		IDRefreshName: "alicloud_eip_association.foo",
    24  
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckEIPAssociationDestroy,
    27  		Steps: []resource.TestStep{
    28  			resource.TestStep{
    29  				Config: testAccEIPAssociationConfig,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckInstanceExists(
    32  						"alicloud_instance.instance", &inst),
    33  					testAccCheckEIPExists(
    34  						"alicloud_eip.eip", &asso),
    35  					testAccCheckEIPAssociationExists(
    36  						"alicloud_eip_association.foo", &inst, &asso),
    37  				),
    38  			},
    39  		},
    40  	})
    41  
    42  }
    43  
    44  func testAccCheckEIPAssociationExists(n string, instance *ecs.InstanceAttributesType, eip *ecs.EipAddressSetType) resource.TestCheckFunc {
    45  	return func(s *terraform.State) error {
    46  		rs, ok := s.RootModule().Resources[n]
    47  		if !ok {
    48  			return fmt.Errorf("Not found: %s", n)
    49  		}
    50  
    51  		if rs.Primary.ID == "" {
    52  			return fmt.Errorf("No EIP Association ID is set")
    53  		}
    54  
    55  		client := testAccProvider.Meta().(*AliyunClient)
    56  		return resource.Retry(3*time.Minute, func() *resource.RetryError {
    57  			d, err := client.DescribeEipAddress(rs.Primary.Attributes["allocation_id"])
    58  
    59  			if err != nil {
    60  				return resource.NonRetryableError(err)
    61  			}
    62  
    63  			if d != nil {
    64  				if d.Status != ecs.EipStatusInUse {
    65  					return resource.RetryableError(fmt.Errorf("Eip is in associating - trying again while it associates"))
    66  				} else if d.InstanceId == instance.InstanceId {
    67  					*eip = *d
    68  					return nil
    69  				}
    70  			}
    71  
    72  			return resource.NonRetryableError(fmt.Errorf("EIP Association not found"))
    73  		})
    74  	}
    75  }
    76  
    77  func testAccCheckEIPAssociationDestroy(s *terraform.State) error {
    78  	client := testAccProvider.Meta().(*AliyunClient)
    79  
    80  	for _, rs := range s.RootModule().Resources {
    81  		if rs.Type != "alicloud_eip_association" {
    82  			continue
    83  		}
    84  
    85  		if rs.Primary.ID == "" {
    86  			return fmt.Errorf("No EIP Association ID is set")
    87  		}
    88  
    89  		// Try to find the EIP
    90  		eips, _, err := client.ecsconn.DescribeEipAddresses(&ecs.DescribeEipAddressesArgs{
    91  			RegionId:     client.Region,
    92  			AllocationId: rs.Primary.Attributes["allocation_id"],
    93  		})
    94  
    95  		for _, eip := range eips {
    96  			if eip.Status != ecs.EipStatusAvailable {
    97  				return fmt.Errorf("Error EIP Association still exist")
    98  			}
    99  		}
   100  
   101  		// Verify the error is what we want
   102  		if err != nil {
   103  			return err
   104  		}
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  const testAccEIPAssociationConfig = `
   111  data "alicloud_zones" "default" {
   112    "available_resource_creation"= "VSwitch"
   113  }
   114  
   115  resource "alicloud_vpc" "main" {
   116    cidr_block = "10.1.0.0/21"
   117  }
   118  
   119  resource "alicloud_vswitch" "main" {
   120    vpc_id = "${alicloud_vpc.main.id}"
   121    cidr_block = "10.1.1.0/24"
   122    availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   123    depends_on = [
   124      "alicloud_vpc.main"]
   125  }
   126  
   127  resource "alicloud_instance" "instance" {
   128    # cn-beijing
   129    vswitch_id = "${alicloud_vswitch.main.id}"
   130    image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   131  
   132    # series II
   133    instance_type = "ecs.n1.medium"
   134    io_optimized = "optimized"
   135    system_disk_category = "cloud_efficiency"
   136  
   137    security_groups = ["${alicloud_security_group.group.id}"]
   138    instance_name = "test_foo"
   139  
   140    tags {
   141      Name = "TerraformTest-instance"
   142    }
   143  }
   144  
   145  resource "alicloud_eip" "eip" {
   146  }
   147  
   148  resource "alicloud_eip_association" "foo" {
   149    allocation_id = "${alicloud_eip.eip.id}"
   150    instance_id = "${alicloud_instance.instance.id}"
   151  }
   152  
   153  resource "alicloud_security_group" "group" {
   154    name = "terraform-test-group"
   155    description = "New security group"
   156    vpc_id = "${alicloud_vpc.main.id}"
   157  }
   158  `