github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_static_nat_test.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/xanzy/go-cloudstack/cloudstack" 10 ) 11 12 func TestAccCloudStackStaticNAT_basic(t *testing.T) { 13 var ipaddr cloudstack.PublicIpAddress 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckCloudStackStaticNATDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCloudStackStaticNAT_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckCloudStackStaticNATExists( 24 "cloudstack_static_nat.foo", &ipaddr), 25 testAccCheckCloudStackStaticNATAttributes(&ipaddr), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func testAccCheckCloudStackStaticNATExists( 33 n string, ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc { 34 return func(s *terraform.State) error { 35 rs, ok := s.RootModule().Resources[n] 36 if !ok { 37 return fmt.Errorf("Not found: %s", n) 38 } 39 40 if rs.Primary.ID == "" { 41 return fmt.Errorf("No static NAT ID is set") 42 } 43 44 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 45 ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID) 46 47 if err != nil { 48 return err 49 } 50 51 if ip.Id != rs.Primary.ID { 52 return fmt.Errorf("Static NAT not found") 53 } 54 55 if !ip.Isstaticnat { 56 return fmt.Errorf("Static NAT not enabled") 57 } 58 59 *ipaddr = *ip 60 61 return nil 62 } 63 } 64 65 func testAccCheckCloudStackStaticNATAttributes( 66 ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc { 67 return func(s *terraform.State) error { 68 69 if ipaddr.Associatednetworkid != CLOUDSTACK_NETWORK_1 { 70 return fmt.Errorf("Bad network ID: %s", ipaddr.Associatednetworkid) 71 } 72 73 return nil 74 } 75 } 76 77 func testAccCheckCloudStackStaticNATDestroy(s *terraform.State) error { 78 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 79 80 for _, rs := range s.RootModule().Resources { 81 if rs.Type != "cloudstack_static_nat" { 82 continue 83 } 84 85 if rs.Primary.ID == "" { 86 return fmt.Errorf("No static NAT ID is set") 87 } 88 89 ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID) 90 if err == nil && ip.Isstaticnat { 91 return fmt.Errorf("Static NAT %s still enabled", rs.Primary.ID) 92 } 93 } 94 95 return nil 96 } 97 98 var testAccCloudStackStaticNAT_basic = fmt.Sprintf(` 99 resource "cloudstack_instance" "foobar" { 100 name = "terraform-test" 101 display_name = "terraform-test" 102 service_offering= "%s" 103 network_id = "%s" 104 template = "%s" 105 zone = "%s" 106 user_data = "foobar\nfoo\nbar" 107 expunge = true 108 } 109 110 resource "cloudstack_ipaddress" "foo" { 111 network_id = "${cloudstack_instance.foobar.network_id}" 112 } 113 114 resource "cloudstack_static_nat" "foo" { 115 ip_address_id = "${cloudstack_ipaddress.foo.id}" 116 network_id = "${cloudstack_ipaddress.foo.network_id}" 117 virtual_machine_id = "${cloudstack_instance.foobar.id}" 118 }`, 119 CLOUDSTACK_SERVICE_OFFERING_1, 120 CLOUDSTACK_NETWORK_1, 121 CLOUDSTACK_TEMPLATE, 122 CLOUDSTACK_ZONE, 123 )