github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/cloudstack/resource_cloudstack_secondary_ipaddress_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 TestAccCloudStackSecondaryIPAddress_basic(t *testing.T) { 13 var ip cloudstack.AddIpToNicResponse 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckCloudStackSecondaryIPAddressDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCloudStackSecondaryIPAddress_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckCloudStackSecondaryIPAddressExists( 24 "cloudstack_secondary_ipaddress.foo", &ip), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccCloudStackSecondaryIPAddress_fixedIP(t *testing.T) { 32 var ip cloudstack.AddIpToNicResponse 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 CheckDestroy: testAccCheckCloudStackSecondaryIPAddressDestroy, 38 Steps: []resource.TestStep{ 39 resource.TestStep{ 40 Config: testAccCloudStackSecondaryIPAddress_fixedIP, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckCloudStackSecondaryIPAddressExists( 43 "cloudstack_secondary_ipaddress.foo", &ip), 44 testAccCheckCloudStackSecondaryIPAddressAttributes(&ip), 45 resource.TestCheckResourceAttr( 46 "cloudstack_secondary_ipaddress.foo", "ip_address", CLOUDSTACK_NETWORK_1_IPADDRESS1), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func testAccCheckCloudStackSecondaryIPAddressExists( 54 n string, ip *cloudstack.AddIpToNicResponse) resource.TestCheckFunc { 55 return func(s *terraform.State) error { 56 rs, ok := s.RootModule().Resources[n] 57 if !ok { 58 return fmt.Errorf("Not found: %s", n) 59 } 60 61 if rs.Primary.ID == "" { 62 return fmt.Errorf("No IP address ID is set") 63 } 64 65 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 66 67 virtualmachine, ok := rs.Primary.Attributes["virtual_machine_id"] 68 if !ok { 69 virtualmachine, ok = rs.Primary.Attributes["virtual_machine"] 70 } 71 72 // Retrieve the virtual_machine ID 73 virtualmachineid, e := retrieveID(cs, "virtual_machine", virtualmachine) 74 if e != nil { 75 return e.Error() 76 } 77 78 // Get the virtual machine details 79 vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid) 80 if err != nil { 81 if count == 0 { 82 return fmt.Errorf("Instance not found") 83 } 84 return err 85 } 86 87 nicid, ok := rs.Primary.Attributes["nic_id"] 88 if !ok { 89 nicid, ok = rs.Primary.Attributes["nicid"] 90 } 91 if !ok { 92 nicid = vm.Nic[0].Id 93 } 94 95 p := cs.Nic.NewListNicsParams(virtualmachineid) 96 p.SetNicid(nicid) 97 98 l, err := cs.Nic.ListNics(p) 99 if err != nil { 100 return err 101 } 102 103 if l.Count == 0 { 104 return fmt.Errorf("NIC not found") 105 } 106 107 if l.Count > 1 { 108 return fmt.Errorf("Found more then one possible result: %v", l.Nics) 109 } 110 111 for _, sip := range l.Nics[0].Secondaryip { 112 if sip.Id == rs.Primary.ID { 113 ip.Ipaddress = sip.Ipaddress 114 ip.Nicid = l.Nics[0].Id 115 return nil 116 } 117 } 118 119 return fmt.Errorf("IP address not found") 120 } 121 } 122 123 func testAccCheckCloudStackSecondaryIPAddressAttributes( 124 ip *cloudstack.AddIpToNicResponse) resource.TestCheckFunc { 125 return func(s *terraform.State) error { 126 127 if ip.Ipaddress != CLOUDSTACK_NETWORK_1_IPADDRESS1 { 128 return fmt.Errorf("Bad IP address: %s", ip.Ipaddress) 129 } 130 return nil 131 } 132 } 133 134 func testAccCheckCloudStackSecondaryIPAddressDestroy(s *terraform.State) error { 135 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 136 137 for _, rs := range s.RootModule().Resources { 138 if rs.Type != "cloudstack_secondary_ipaddress" { 139 continue 140 } 141 142 if rs.Primary.ID == "" { 143 return fmt.Errorf("No IP address ID is set") 144 } 145 146 virtualmachine, ok := rs.Primary.Attributes["virtual_machine_id"] 147 if !ok { 148 virtualmachine, ok = rs.Primary.Attributes["virtual_machine"] 149 } 150 151 // Retrieve the virtual_machine ID 152 virtualmachineid, e := retrieveID(cs, "virtual_machine", virtualmachine) 153 if e != nil { 154 return e.Error() 155 } 156 157 // Get the virtual machine details 158 vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid) 159 if err != nil { 160 if count == 0 { 161 return nil 162 } 163 return err 164 } 165 166 nicid, ok := rs.Primary.Attributes["nic_id"] 167 if !ok { 168 nicid, ok = rs.Primary.Attributes["nicid"] 169 } 170 if !ok { 171 nicid = vm.Nic[0].Id 172 } 173 174 p := cs.Nic.NewListNicsParams(virtualmachineid) 175 p.SetNicid(nicid) 176 177 l, err := cs.Nic.ListNics(p) 178 if err != nil { 179 return err 180 } 181 182 if l.Count == 0 { 183 return fmt.Errorf("NIC not found") 184 } 185 186 if l.Count > 1 { 187 return fmt.Errorf("Found more then one possible result: %v", l.Nics) 188 } 189 190 for _, sip := range l.Nics[0].Secondaryip { 191 if sip.Id == rs.Primary.ID { 192 return fmt.Errorf("IP address %s still exists", rs.Primary.ID) 193 } 194 } 195 196 return nil 197 } 198 199 return nil 200 } 201 202 var testAccCloudStackSecondaryIPAddress_basic = fmt.Sprintf(` 203 resource "cloudstack_instance" "foobar" { 204 name = "terraform-test" 205 service_offering= "%s" 206 network_id = "%s" 207 template = "%s" 208 zone = "%s" 209 expunge = true 210 } 211 212 resource "cloudstack_secondary_ipaddress" "foo" { 213 virtual_machine_id = "${cloudstack_instance.foobar.id}" 214 } 215 `, 216 CLOUDSTACK_SERVICE_OFFERING_1, 217 CLOUDSTACK_NETWORK_1, 218 CLOUDSTACK_TEMPLATE, 219 CLOUDSTACK_ZONE) 220 221 var testAccCloudStackSecondaryIPAddress_fixedIP = fmt.Sprintf(` 222 resource "cloudstack_instance" "foobar" { 223 name = "terraform-test" 224 service_offering= "%s" 225 network_id = "%s" 226 template = "%s" 227 zone = "%s" 228 expunge = true 229 } 230 231 resource "cloudstack_secondary_ipaddress" "foo" { 232 ip_address = "%s" 233 virtual_machine_id = "${cloudstack_instance.foobar.id}" 234 }`, 235 CLOUDSTACK_SERVICE_OFFERING_1, 236 CLOUDSTACK_NETWORK_1, 237 CLOUDSTACK_TEMPLATE, 238 CLOUDSTACK_ZONE, 239 CLOUDSTACK_NETWORK_1_IPADDRESS1)