github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/clc/resource_clc_public_ip_test.go (about) 1 package clc 2 3 import ( 4 "fmt" 5 "testing" 6 7 clc "github.com/CenturyLinkCloud/clc-sdk" 8 "github.com/CenturyLinkCloud/clc-sdk/server" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 // things to test: 14 // maps to internal specified ip 15 // port range 16 // update existing rule 17 // CIDR restriction 18 19 func TestAccPublicIPBasic(t *testing.T) { 20 var resp server.PublicIP 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckPublicIPDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: testAccCheckPublicIPConfigBasic, 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckPublicIPExists("clc_public_ip.acc_test_public_ip", &resp), 30 testAccCheckPublicIPNIC("clc_public_ip.acc_test_public_ip", &resp), 31 testAccCheckPublicIPPortRange("clc_public_ip.acc_test_public_ip", &resp), 32 testAccCheckPublicIPBlockCIDR("clc_public_ip.acc_test_public_ip", &resp), 33 //testAccCheckPublicIPUpdated("clc_public_ip.eip", &resp), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func testAccCheckPublicIPDestroy(s *terraform.State) error { 41 client := testAccProvider.Meta().(*clc.Client) 42 for _, rs := range s.RootModule().Resources { 43 if rs.Type != "clc_public_ip" { 44 continue 45 } 46 sid := rs.Primary.Attributes["server_id"] 47 _, err := client.Server.GetPublicIP(sid, rs.Primary.ID) 48 if err == nil { 49 return fmt.Errorf("IP still exists") 50 } 51 } 52 return nil 53 54 } 55 56 func testAccCheckPublicIPExists(n string, resp *server.PublicIP) resource.TestCheckFunc { 57 return func(s *terraform.State) error { 58 rs, ok := s.RootModule().Resources[n] 59 if !ok { 60 return fmt.Errorf("Not found: %s", n) 61 } 62 if rs.Primary.ID == "" { 63 return fmt.Errorf("No PublicIP ID is set") 64 } 65 client := testAccProvider.Meta().(*clc.Client) 66 sid := rs.Primary.Attributes["server_id"] 67 p, err := client.Server.GetPublicIP(sid, rs.Primary.ID) 68 if err != nil { 69 return err 70 } 71 *resp = *p 72 return nil 73 } 74 } 75 76 func testAccCheckPublicIPPortRange(n string, resp *server.PublicIP) resource.TestCheckFunc { 77 return func(s *terraform.State) error { 78 // check the passed port range made it through 79 var spec server.Port 80 for _, p := range resp.Ports { 81 if p.Protocol == "UDP" { 82 spec = p 83 break 84 } 85 } 86 if spec.Port != 53 || spec.PortTo != 55 { 87 return fmt.Errorf("Expected udp ports from 53-55 but found: %v", spec) 88 } 89 return nil 90 } 91 } 92 func testAccCheckPublicIPBlockCIDR(n string, resp *server.PublicIP) resource.TestCheckFunc { 93 return func(s *terraform.State) error { 94 // check the passed port range made it through 95 spec := resp.SourceRestrictions[0] 96 if spec.CIDR != "108.19.67.15/32" { 97 return fmt.Errorf("Expected cidr restriction but found: %v", spec) 98 } 99 return nil 100 } 101 } 102 103 func testAccCheckPublicIPNIC(n string, resp *server.PublicIP) resource.TestCheckFunc { 104 return func(s *terraform.State) error { 105 rs, ok := s.RootModule().Resources[n] 106 if !ok { 107 return fmt.Errorf("Not found: %s", n) 108 } 109 sid := rs.Primary.Attributes["server_id"] 110 nic := rs.Primary.Attributes["internal_ip_address"] 111 112 client := testAccProvider.Meta().(*clc.Client) 113 srv, err := client.Server.Get(sid) 114 if err != nil { 115 return fmt.Errorf("Failed fetching server? %v", err) 116 } 117 first := srv.Details.IPaddresses[0].Internal 118 if nic != first { 119 return fmt.Errorf("Expected public ip to be mapped to %s but found: %s", first, nic) 120 } 121 return nil 122 } 123 } 124 125 var testAccCheckPublicIPConfigBasic = ` 126 variable "dc" { default = "IL1" } 127 128 resource "clc_group" "acc_test_group_ip" { 129 location_id = "${var.dc}" 130 name = "acc_test_group_ip" 131 parent = "Default Group" 132 } 133 134 resource "clc_server" "acc_test_server" { 135 name_template = "test" 136 source_server_id = "UBUNTU-14-64-TEMPLATE" 137 group_id = "${clc_group.acc_test_group_ip.id}" 138 cpu = 1 139 memory_mb = 1024 140 password = "Green123$" 141 } 142 143 resource "clc_public_ip" "acc_test_public_ip" { 144 server_id = "${clc_server.acc_test_server.id}" 145 internal_ip_address = "${clc_server.acc_test_server.private_ip_address}" 146 source_restrictions 147 { cidr = "108.19.67.15/32" } 148 ports 149 { 150 protocol = "TCP" 151 port = 80 152 } 153 ports 154 { 155 protocol = "UDP" 156 port = 53 157 port_to = 55 158 } 159 } 160 `