github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/profitbricks/resource_profitbricks_nic_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 TestAccProfitBricksNic_Basic(t *testing.T) {
    13  	var nic profitbricks.Nic
    14  	volumeName := "volume"
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckDProfitBricksNicDestroyCheck,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: fmt.Sprintf(testAccCheckProfitbricksNicConfig_basic, volumeName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckProfitBricksNICExists("profitbricks_nic.database_nic", &nic),
    27  					testAccCheckProfitBricksNicAttributes("profitbricks_nic.database_nic", volumeName),
    28  					resource.TestCheckResourceAttr("profitbricks_nic.database_nic", "name", volumeName),
    29  				),
    30  			},
    31  			resource.TestStep{
    32  				Config: testAccCheckProfitbricksNicConfig_update,
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckProfitBricksNicAttributes("profitbricks_nic.database_nic", "updated"),
    35  					resource.TestCheckResourceAttr("profitbricks_nic.database_nic", "name", "updated"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckDProfitBricksNicDestroyCheck(s *terraform.State) error {
    43  	for _, rs := range s.RootModule().Resources {
    44  		if rs.Type != "profitbricks_nic" {
    45  			continue
    46  		}
    47  
    48  		resp := profitbricks.GetNic(rs.Primary.Attributes["datacenter_id"], rs.Primary.Attributes["nic_id"], rs.Primary.ID)
    49  
    50  		if resp.StatusCode < 299 {
    51  			return fmt.Errorf("NIC still exists %s %s", rs.Primary.ID, resp.Response)
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func testAccCheckProfitBricksNicAttributes(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("testAccCheckProfitBricksNicAttributes: 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 testAccCheckProfitBricksNICExists(n string, nic *profitbricks.Nic) 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  		foundNic := profitbricks.GetNic(rs.Primary.Attributes["datacenter_id"], rs.Primary.Attributes["server_id"], rs.Primary.ID)
    85  
    86  		if foundNic.StatusCode != 200 {
    87  			return fmt.Errorf("Error occured while fetching Volume: %s", rs.Primary.ID)
    88  		}
    89  		if foundNic.Id != rs.Primary.ID {
    90  			return fmt.Errorf("Record not found")
    91  		}
    92  
    93  		nic = &foundNic
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  const testAccCheckProfitbricksNicConfig_basic = `
   100  resource "profitbricks_datacenter" "foobar" {
   101  	name       = "nic-test"
   102  	location = "us/las"
   103  }
   104  
   105  resource "profitbricks_server" "webserver" {
   106    name = "webserver"
   107    datacenter_id = "${profitbricks_datacenter.foobar.id}"
   108    cores = 1
   109    ram = 1024
   110    availability_zone = "ZONE_1"
   111    cpu_family = "AMD_OPTERON"
   112    volume {
   113      name = "system"
   114      size = 5
   115      disk_type = "SSD"
   116      image_name ="ubuntu-16.04"
   117      image_password = "K3tTj8G14a3EgKyNeeiY"
   118  }
   119    nic {
   120      lan = "1"
   121      dhcp = true
   122      firewall_active = true
   123      firewall {
   124        protocol = "TCP"
   125        name = "SSH"
   126        port_range_start = 22
   127        port_range_end = 22
   128      }
   129    }
   130  }
   131  
   132  resource "profitbricks_nic" "database_nic" {
   133    datacenter_id = "${profitbricks_datacenter.foobar.id}"
   134    server_id = "${profitbricks_server.webserver.id}"
   135    lan = 2
   136    dhcp = true
   137    firewall_active = true
   138    name = "%s"
   139  }`
   140  
   141  const testAccCheckProfitbricksNicConfig_update = `
   142  resource "profitbricks_datacenter" "foobar" {
   143  	name       = "nic-test"
   144  	location = "us/las"
   145  }
   146  
   147  resource "profitbricks_server" "webserver" {
   148    name = "webserver"
   149    datacenter_id = "${profitbricks_datacenter.foobar.id}"
   150    cores = 1
   151    ram = 1024
   152    availability_zone = "ZONE_1"
   153    cpu_family = "AMD_OPTERON"
   154    volume {
   155      name = "system"
   156      size = 5
   157      disk_type = "SSD"
   158      image_name ="ubuntu-16.04"
   159      image_password = "K3tTj8G14a3EgKyNeeiY"
   160  }
   161    nic {
   162      lan = "1"
   163      dhcp = true
   164      firewall_active = true
   165      firewall {
   166        protocol = "TCP"
   167        name = "SSH"
   168        port_range_start = 22
   169        port_range_end = 22
   170      }
   171    }
   172  }
   173  
   174  resource "profitbricks_nic" "database_nic" {
   175    datacenter_id = "${profitbricks_datacenter.foobar.id}"
   176    server_id = "${profitbricks_server.webserver.id}"
   177    lan = 2
   178    dhcp = true
   179    firewall_active = true
   180    name = "updated"
   181  }
   182  `