github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/profitbricks/resource_profitbricks_volume_test.go (about) 1 package profitbricks 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/profitbricks/profitbricks-sdk-go" 10 ) 11 12 func TestAccProfitBricksVolume_Basic(t *testing.T) { 13 var volume profitbricks.Volume 14 volumeName := "volume" 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { 18 testAccPreCheck(t) 19 }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckDProfitBricksVolumeDestroyCheck, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: fmt.Sprintf(testAccCheckProfitbricksVolumeConfig_basic, volumeName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckProfitBricksVolumeExists("profitbricks_volume.database_volume", &volume), 27 testAccCheckProfitBricksVolumeAttributes("profitbricks_volume.database_volume", volumeName), 28 resource.TestCheckResourceAttr("profitbricks_volume.database_volume", "name", volumeName), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccCheckProfitbricksVolumeConfig_update, 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckProfitBricksVolumeAttributes("profitbricks_volume.database_volume", "updated"), 35 resource.TestCheckResourceAttr("profitbricks_volume.database_volume", "name", "updated"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func testAccCheckDProfitBricksVolumeDestroyCheck(s *terraform.State) error { 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "profitbricks_datacenter" { 45 continue 46 } 47 48 resp := profitbricks.GetVolume(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID) 49 50 if resp.StatusCode < 299 { 51 return fmt.Errorf("Volume still exists %s %s", rs.Primary.ID, resp.Response) 52 } 53 } 54 55 return nil 56 } 57 58 func testAccCheckProfitBricksVolumeAttributes(n string, name string) resource.TestCheckFunc { 59 return func(s *terraform.State) error { 60 rs, ok := s.RootModule().Resources[n] 61 if !ok { 62 return fmt.Errorf("testAccCheckProfitBricksVolumeAttributes: Not found: %s", n) 63 } 64 if rs.Primary.Attributes["name"] != name { 65 return fmt.Errorf("Bad name: %s", rs.Primary.Attributes["name"]) 66 } 67 68 return nil 69 } 70 } 71 72 func testAccCheckProfitBricksVolumeExists(n string, volume *profitbricks.Volume) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 rs, ok := s.RootModule().Resources[n] 75 76 if !ok { 77 return fmt.Errorf("testAccCheckProfitBricksVolumeExists: Not found: %s", n) 78 } 79 80 if rs.Primary.ID == "" { 81 return fmt.Errorf("No Record ID is set") 82 } 83 84 foundServer := profitbricks.GetVolume(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID) 85 86 if foundServer.StatusCode != 200 { 87 return fmt.Errorf("Error occured while fetching Volume: %s", rs.Primary.ID) 88 } 89 if foundServer.Id != rs.Primary.ID { 90 return fmt.Errorf("Record not found") 91 } 92 93 volume = &foundServer 94 95 return nil 96 } 97 } 98 99 const testAccCheckProfitbricksVolumeConfig_basic = ` 100 resource "profitbricks_datacenter" "foobar" { 101 name = "volume-test" 102 location = "us/las" 103 } 104 105 resource "profitbricks_lan" "webserver_lan" { 106 datacenter_id = "${profitbricks_datacenter.foobar.id}" 107 public = true 108 name = "public" 109 } 110 111 resource "profitbricks_server" "webserver" { 112 name = "webserver" 113 datacenter_id = "${profitbricks_datacenter.foobar.id}" 114 cores = 1 115 ram = 1024 116 availability_zone = "ZONE_1" 117 cpu_family = "AMD_OPTERON" 118 volume { 119 name = "system" 120 size = 5 121 disk_type = "SSD" 122 image_name ="ubuntu-16.04" 123 image_password = "K3tTj8G14a3EgKyNeeiY" 124 } 125 nic { 126 lan = "${profitbricks_lan.webserver_lan.id}" 127 dhcp = true 128 firewall_active = true 129 firewall { 130 protocol = "TCP" 131 name = "SSH" 132 port_range_start = 22 133 port_range_end = 22 134 } 135 } 136 } 137 138 resource "profitbricks_volume" "database_volume" { 139 datacenter_id = "${profitbricks_datacenter.foobar.id}" 140 server_id = "${profitbricks_server.webserver.id}" 141 licence_type = "OTHER" 142 name = "%s" 143 size = 5 144 disk_type = "SSD" 145 bus = "VIRTIO" 146 }` 147 148 const testAccCheckProfitbricksVolumeConfig_update = ` 149 resource "profitbricks_datacenter" "foobar" { 150 name = "volume-test" 151 location = "us/las" 152 } 153 154 resource "profitbricks_lan" "webserver_lan" { 155 datacenter_id = "${profitbricks_datacenter.foobar.id}" 156 public = true 157 name = "public" 158 } 159 160 resource "profitbricks_server" "webserver" { 161 name = "webserver" 162 datacenter_id = "${profitbricks_datacenter.foobar.id}" 163 cores = 1 164 ram = 1024 165 availability_zone = "ZONE_1" 166 cpu_family = "AMD_OPTERON" 167 volume { 168 name = "system" 169 size = 5 170 disk_type = "SSD" 171 image_name ="ubuntu-16.04" 172 image_password = "K3tTj8G14a3EgKyNeeiY" 173 } 174 nic { 175 lan = "${profitbricks_lan.webserver_lan.id}" 176 dhcp = true 177 firewall_active = true 178 firewall { 179 protocol = "TCP" 180 name = "SSH" 181 port_range_start = 22 182 port_range_end = 22 183 } 184 } 185 } 186 187 resource "profitbricks_volume" "database_volume" { 188 datacenter_id = "${profitbricks_datacenter.foobar.id}" 189 server_id = "${profitbricks_server.webserver.id}" 190 licence_type = "OTHER" 191 name = "updated" 192 size = 5 193 disk_type = "SSD" 194 bus = "VIRTIO" 195 }`