github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/oneandone/resource_oneandone_private_network_test.go (about)

     1  package oneandone
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/1and1/oneandone-cloudserver-sdk-go"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"os"
    11  	"time"
    12  )
    13  
    14  func TestAccOneandonePrivateNetwork_Basic(t *testing.T) {
    15  	var net oneandone.PrivateNetwork
    16  
    17  	name := "test"
    18  	name_updated := "test1"
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck: func() {
    22  			testAccPreCheck(t)
    23  		},
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckOneandonePrivateNetworkDestroyCheck,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: fmt.Sprintf(testAccCheckOneandonePrivateNetwork_basic, name),
    29  				Check: resource.ComposeTestCheckFunc(
    30  					func(*terraform.State) error {
    31  						time.Sleep(10 * time.Second)
    32  						return nil
    33  					},
    34  					testAccCheckOneandonePrivateNetworkExists("oneandone_private_network.pn", &net),
    35  					testAccCheckOneandonePrivateNetworkAttributes("oneandone_private_network.pn", name),
    36  					resource.TestCheckResourceAttr("oneandone_private_network.pn", "name", name),
    37  				),
    38  			},
    39  			resource.TestStep{
    40  				Config: fmt.Sprintf(testAccCheckOneandonePrivateNetwork_basic, name_updated),
    41  				Check: resource.ComposeTestCheckFunc(
    42  					func(*terraform.State) error {
    43  						time.Sleep(10 * time.Second)
    44  						return nil
    45  					},
    46  					testAccCheckOneandonePrivateNetworkExists("oneandone_private_network.pn", &net),
    47  					testAccCheckOneandonePrivateNetworkAttributes("oneandone_private_network.pn", name_updated),
    48  					resource.TestCheckResourceAttr("oneandone_private_network.pn", "name", name_updated),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCheckOneandonePrivateNetworkDestroyCheck(s *terraform.State) error {
    56  	for _, rs := range s.RootModule().Resources {
    57  		if rs.Type != "oneandone_private_network" {
    58  			continue
    59  		}
    60  
    61  		api := oneandone.New(os.Getenv("ONEANDONE_TOKEN"), oneandone.BaseUrl)
    62  
    63  		_, err := api.GetPrivateNetwork(rs.Primary.ID)
    64  
    65  		if err == nil {
    66  			return fmt.Errorf("PrivateNetwork still exists %s %s", rs.Primary.ID, err.Error())
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  func testAccCheckOneandonePrivateNetworkAttributes(n string, reverse_dns string) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		rs, ok := s.RootModule().Resources[n]
    75  		if !ok {
    76  			return fmt.Errorf("Not found: %s", n)
    77  		}
    78  		if rs.Primary.Attributes["name"] != reverse_dns {
    79  			return fmt.Errorf("Bad name: expected %s : found %s ", reverse_dns, rs.Primary.Attributes["name"])
    80  		}
    81  
    82  		return nil
    83  	}
    84  }
    85  
    86  func testAccCheckOneandonePrivateNetworkExists(n string, server *oneandone.PrivateNetwork) resource.TestCheckFunc {
    87  	return func(s *terraform.State) error {
    88  		rs, ok := s.RootModule().Resources[n]
    89  
    90  		if !ok {
    91  			return fmt.Errorf("Not found: %s", n)
    92  		}
    93  
    94  		if rs.Primary.ID == "" {
    95  			return fmt.Errorf("No Record ID is set")
    96  		}
    97  
    98  		api := oneandone.New(os.Getenv("ONEANDONE_TOKEN"), oneandone.BaseUrl)
    99  
   100  		found_server, err := api.GetPrivateNetwork(rs.Primary.ID)
   101  
   102  		if err != nil {
   103  			return fmt.Errorf("Error occured while fetching PrivateNetwork: %s", rs.Primary.ID)
   104  		}
   105  		if found_server.Id != rs.Primary.ID {
   106  			return fmt.Errorf("Record not found")
   107  		}
   108  		server = found_server
   109  
   110  		return nil
   111  	}
   112  }
   113  
   114  const testAccCheckOneandonePrivateNetwork_basic = `
   115  resource "oneandone_server" "server1" {
   116    name = "server_private_net_01"
   117    description = "ttt"
   118    image = "CoreOS_Stable_64std"
   119    datacenter = "US"
   120    vcores = 1
   121    cores_per_processor = 1
   122    ram = 2
   123    password = "Kv40kd8PQb"
   124    hdds = [
   125      {
   126        disk_size = 60
   127        is_main = true
   128      }
   129    ]
   130  }
   131  
   132  resource "oneandone_server" "server2" {
   133    name = "server_private_net_02"
   134    description = "ttt"
   135    image = "CoreOS_Stable_64std"
   136    datacenter = "US"
   137    vcores = 1
   138    cores_per_processor = 1
   139    ram = 2
   140    password = "${oneandone_server.server1.password}"
   141    hdds = [
   142      {
   143        disk_size = 60
   144        is_main = true
   145      }
   146    ]
   147  }
   148  
   149  resource "oneandone_private_network" "pn" {
   150    name = "%s",
   151    description = "new private net"
   152    datacenter = "US"
   153    network_address = "192.168.7.0"
   154    subnet_mask = "255.255.255.0"
   155      server_ids = [
   156        "${oneandone_server.server1.id}",
   157        "${oneandone_server.server2.id}"
   158      ]
   159  }
   160  `