2014-12-10 22:20:52 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccCloudStackNetwork_basic(t *testing.T) {
|
|
|
|
var network cloudstack.Network
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackNetworkDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackNetwork_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackNetworkExists(
|
|
|
|
"cloudstack_network.foo", &network),
|
|
|
|
testAccCheckCloudStackNetworkBasicAttributes(&network),
|
2016-01-22 05:45:51 +01:00
|
|
|
testAccCheckNetworkTags(&network, "terraform-tag", "true"),
|
2014-12-10 22:20:52 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-05 11:37:23 +02:00
|
|
|
func TestAccCloudStackNetwork_vpc(t *testing.T) {
|
2014-12-10 22:20:52 +01:00
|
|
|
var network cloudstack.Network
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackNetworkDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-05-05 11:37:23 +02:00
|
|
|
Config: testAccCloudStackNetwork_vpc,
|
2014-12-10 22:20:52 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackNetworkExists(
|
|
|
|
"cloudstack_network.foo", &network),
|
2015-05-05 11:37:23 +02:00
|
|
|
testAccCheckCloudStackNetworkVPCAttributes(&network),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_network.foo", "vpc", "terraform-vpc"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackNetworkExists(
|
|
|
|
n string, network *cloudstack.Network) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No network ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
|
|
|
ntwrk, _, err := cs.Network.GetNetworkByID(rs.Primary.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ntwrk.Id != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Network not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*network = *ntwrk
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackNetworkBasicAttributes(
|
|
|
|
network *cloudstack.Network) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if network.Name != "terraform-network" {
|
|
|
|
return fmt.Errorf("Bad name: %s", network.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if network.Displaytext != "terraform-network" {
|
|
|
|
return fmt.Errorf("Bad display name: %s", network.Displaytext)
|
|
|
|
}
|
|
|
|
|
2015-05-05 11:37:23 +02:00
|
|
|
if network.Cidr != CLOUDSTACK_NETWORK_2_CIDR {
|
2016-01-22 05:45:51 +01:00
|
|
|
return fmt.Errorf("Bad CIDR: %s", network.Cidr)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
2015-05-05 11:37:23 +02:00
|
|
|
if network.Networkofferingname != CLOUDSTACK_NETWORK_2_OFFERING {
|
2016-01-22 05:45:51 +01:00
|
|
|
return fmt.Errorf("Bad network offering: %s", network.Networkofferingname)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 05:45:51 +01:00
|
|
|
func testAccCheckNetworkTags(
|
|
|
|
n *cloudstack.Network, key string, value string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
tags := make(map[string]string)
|
|
|
|
for item := range n.Tags {
|
|
|
|
tags[n.Tags[item].Key] = n.Tags[item].Value
|
|
|
|
}
|
|
|
|
return testAccCheckTags(tags, key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 11:37:23 +02:00
|
|
|
func testAccCheckCloudStackNetworkVPCAttributes(
|
2014-12-10 22:20:52 +01:00
|
|
|
network *cloudstack.Network) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if network.Name != "terraform-network" {
|
|
|
|
return fmt.Errorf("Bad name: %s", network.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if network.Displaytext != "terraform-network" {
|
|
|
|
return fmt.Errorf("Bad display name: %s", network.Displaytext)
|
|
|
|
}
|
|
|
|
|
|
|
|
if network.Cidr != CLOUDSTACK_VPC_NETWORK_CIDR {
|
2015-05-05 11:37:23 +02:00
|
|
|
return fmt.Errorf("Bad CIDR: %s", network.Cidr)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if network.Networkofferingname != CLOUDSTACK_VPC_NETWORK_OFFERING {
|
2015-05-05 11:37:23 +02:00
|
|
|
return fmt.Errorf("Bad network offering: %s", network.Networkofferingname)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackNetworkDestroy(s *terraform.State) error {
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "cloudstack_network" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No network ID is set")
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
_, _, err := cs.Network.GetNetworkByID(rs.Primary.ID)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Network %s still exists", rs.Primary.ID)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccCloudStackNetwork_basic = fmt.Sprintf(`
|
|
|
|
resource "cloudstack_network" "foo" {
|
|
|
|
name = "terraform-network"
|
|
|
|
cidr = "%s"
|
|
|
|
network_offering = "%s"
|
|
|
|
zone = "%s"
|
2016-01-22 05:45:51 +01:00
|
|
|
tags = {
|
|
|
|
terraform-tag = "true"
|
|
|
|
}
|
2014-12-10 22:20:52 +01:00
|
|
|
}`,
|
2015-05-05 11:37:23 +02:00
|
|
|
CLOUDSTACK_NETWORK_2_CIDR,
|
|
|
|
CLOUDSTACK_NETWORK_2_OFFERING,
|
2014-12-10 22:20:52 +01:00
|
|
|
CLOUDSTACK_ZONE)
|
|
|
|
|
2015-05-05 11:37:23 +02:00
|
|
|
var testAccCloudStackNetwork_vpc = fmt.Sprintf(`
|
2014-12-10 22:20:52 +01:00
|
|
|
resource "cloudstack_vpc" "foobar" {
|
|
|
|
name = "terraform-vpc"
|
|
|
|
cidr = "%s"
|
|
|
|
vpc_offering = "%s"
|
|
|
|
zone = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "cloudstack_network" "foo" {
|
|
|
|
name = "terraform-network"
|
|
|
|
cidr = "%s"
|
|
|
|
network_offering = "%s"
|
|
|
|
vpc = "${cloudstack_vpc.foobar.name}"
|
|
|
|
zone = "${cloudstack_vpc.foobar.zone}"
|
|
|
|
}`,
|
2015-03-09 14:00:29 +01:00
|
|
|
CLOUDSTACK_VPC_CIDR_1,
|
2014-12-10 22:20:52 +01:00
|
|
|
CLOUDSTACK_VPC_OFFERING,
|
|
|
|
CLOUDSTACK_ZONE,
|
|
|
|
CLOUDSTACK_VPC_NETWORK_CIDR,
|
|
|
|
CLOUDSTACK_VPC_NETWORK_OFFERING)
|