Merge pull request #2281 from svanharmelen/f-provider-cloudstack

provider/cloudstack: updating and tweaking tests and docs
This commit is contained in:
Sander van Harmelen 2015-06-09 12:44:22 +02:00
commit aed44622b1
26 changed files with 311 additions and 310 deletions

View File

@ -158,7 +158,7 @@ func testAccCheckAzureDataDiskDestroy(s *terraform.State) error {
_, err = virtualmachinedisk.NewClient(mc).GetDataDisk(vm, vm, vm, lun)
if err == nil {
return fmt.Errorf("Resource %s still exists", rs.Primary.ID)
return fmt.Errorf("Data disk %s still exists", rs.Primary.ID)
}
if !management.IsResourceNotFoundError(err) {

View File

@ -297,7 +297,7 @@ func testAccCheckAzureInstanceDestroy(s *terraform.State) error {
_, err := hostedservice.NewClient(mc).GetHostedService(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Resource %s still exists", rs.Primary.ID)
return fmt.Errorf("Instance %s still exists", rs.Primary.ID)
}
if !management.IsResourceNotFoundError(err) {

View File

@ -217,7 +217,7 @@ func testAccCheckAzureSecurityGroupDestroy(s *terraform.State) error {
_, err := networksecuritygroup.NewClient(mc).GetNetworkSecurityGroup(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Resource %s still exists", rs.Primary.ID)
return fmt.Errorf("Network Security Group %s still exists", rs.Primary.ID)
}
if !management.IsResourceNotFoundError(err) {

View File

@ -190,7 +190,7 @@ func testAccCheckAzureVirtualNetworkDestroy(s *terraform.State) error {
for _, n := range nc.Configuration.VirtualNetworkSites {
if n.Name == rs.Primary.ID {
return fmt.Errorf("Resource %s still exists", rs.Primary.ID)
return fmt.Errorf("Virtual Network %s still exists", rs.Primary.ID)
}
}
}

View File

@ -39,7 +39,7 @@ func testSetValueOnResourceData(t *testing.T) {
}
}
func testSetUuidOnResourceData(t *testing.T) {
func testSetUUIDOnResourceData(t *testing.T) {
d := schema.ResourceData{}
d.Set("id", "54711781-274e-41b2-83c0-17194d0108f7")
@ -60,35 +60,116 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("CLOUDSTACK_SECRET_KEY"); v == "" {
t.Fatal("CLOUDSTACK_SECRET_KEY must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_2ND_NIC_IPADDRESS"); v == "" {
t.Fatal("CLOUDSTACK_2ND_NIC_IPADDRESS must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_2ND_NIC_NETWORK"); v == "" {
t.Fatal("CLOUDSTACK_2ND_NIC_NETWORK must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_DISK_OFFERING_1"); v == "" {
t.Fatal("CLOUDSTACK_DISK_OFFERING_1 must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_DISK_OFFERING_2"); v == "" {
t.Fatal("CLOUDSTACK_DISK_OFFERING_2 must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_HYPERVISOR"); v == "" {
t.Fatal("CLOUDSTACK_HYPERVISOR must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_SERVICE_OFFERING_1"); v == "" {
t.Fatal("CLOUDSTACK_SERVICE_OFFERING_1 must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_SERVICE_OFFERING_2"); v == "" {
t.Fatal("CLOUDSTACK_SERVICE_OFFERING_2 must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_NETWORK_1"); v == "" {
t.Fatal("CLOUDSTACK_NETWORK_1 must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_NETWORK_1_IPADDRESS"); v == "" {
t.Fatal("CLOUDSTACK_NETWORK_1_IPADDRESS must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_NETWORK_2"); v == "" {
t.Fatal("CLOUDSTACK_NETWORK_2 must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_NETWORK_2_CIDR"); v == "" {
t.Fatal("CLOUDSTACK_NETWORK_2_CIDR must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_NETWORK_2_OFFERING"); v == "" {
t.Fatal("CLOUDSTACK_NETWORK_2_OFFERING must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_NETWORK_2_IPADDRESS"); v == "" {
t.Fatal("CLOUDSTACK_NETWORK_2_IPADDRESS must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_VPC_CIDR_1"); v == "" {
t.Fatal("CLOUDSTACK_VPC_CIDR_1 must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_VPC_CIDR_2"); v == "" {
t.Fatal("CLOUDSTACK_VPC_CIDR_2 must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_VPC_OFFERING"); v == "" {
t.Fatal("CLOUDSTACK_VPC_OFFERING must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_VPC_NETWORK_CIDR"); v == "" {
t.Fatal("CLOUDSTACK_VPC_NETWORK_CIDR must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_VPC_NETWORK_OFFERING"); v == "" {
t.Fatal("CLOUDSTACK_VPC_NETWORK_OFFERING must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_PUBLIC_IPADDRESS"); v == "" {
t.Fatal("CLOUDSTACK_PUBLIC_IPADDRESS must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_SSH_KEYPAIR"); v == "" {
t.Fatal("CLOUDSTACK_SSH_KEYPAIR must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_SSH_PUBLIC_KEY"); v == "" {
t.Fatal("CLOUDSTACK_SSH_PUBLIC_KEY must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_TEMPLATE"); v == "" {
t.Fatal("CLOUDSTACK_TEMPLATE must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_TEMPLATE_FORMAT"); v == "" {
t.Fatal("CLOUDSTACK_TEMPLATE_FORMAT must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_TEMPLATE_URL"); v == "" {
t.Fatal("CLOUDSTACK_TEMPLATE_URL must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_TEMPLATE_OS_TYPE"); v == "" {
t.Fatal("CLOUDSTACK_TEMPLATE_OS_TYPE must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_PROJECT_NAME"); v == "" {
t.Fatal("CLOUDSTACK_PROJECT_NAME must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_PROJECT_NETWORK"); v == "" {
t.Fatal("CLOUDSTACK_PROJECT_NETWORK must be set for acceptance tests")
}
if v := os.Getenv("CLOUDSTACK_ZONE"); v == "" {
t.Fatal("CLOUDSTACK_ZONE must be set for acceptance tests")
}
}
// SET THESE VALUES IN ORDER TO RUN THE ACC TESTS!!
var CLOUDSTACK_2ND_NIC_IPADDRESS = ""
var CLOUDSTACK_2ND_NIC_NETWORK = ""
var CLOUDSTACK_DISK_OFFERING_1 = ""
var CLOUDSTACK_DISK_OFFERING_2 = ""
var CLOUDSTACK_HYPERVISOR = ""
var CLOUDSTACK_SERVICE_OFFERING_1 = ""
var CLOUDSTACK_SERVICE_OFFERING_2 = ""
var CLOUDSTACK_NETWORK_1 = ""
var CLOUDSTACK_NETWORK_1_IPADDRESS = ""
var CLOUDSTACK_NETWORK_2 = ""
var CLOUDSTACK_NETWORK_2_CIDR = ""
var CLOUDSTACK_NETWORK_2_OFFERING = ""
var CLOUDSTACK_NETWORK_2_IPADDRESS = ""
var CLOUDSTACK_VPC_CIDR_1 = ""
var CLOUDSTACK_VPC_CIDR_2 = ""
var CLOUDSTACK_VPC_OFFERING = ""
var CLOUDSTACK_VPC_NETWORK_CIDR = ""
var CLOUDSTACK_VPC_NETWORK_OFFERING = ""
var CLOUDSTACK_PUBLIC_IPADDRESS = ""
var CLOUDSTACK_SSH_KEYPAIR = ""
var CLOUDSTACK_SSH_PUBLIC_KEY = ""
var CLOUDSTACK_TEMPLATE = ""
var CLOUDSTACK_TEMPLATE_FORMAT = ""
var CLOUDSTACK_TEMPLATE_URL = ""
var CLOUDSTACK_TEMPLATE_OS_TYPE = ""
var CLOUDSTACK_ZONE = ""
var CLOUDSTACK_PROJECT_NAME = ""
var CLOUDSTACK_PROJECT_ID = ""
var CLOUDSTACK_2ND_NIC_IPADDRESS = os.Getenv("CLOUDSTACK_2ND_NIC_IPADDRESS")
var CLOUDSTACK_2ND_NIC_NETWORK = os.Getenv("CLOUDSTACK_2ND_NIC_NETWORK")
var CLOUDSTACK_DISK_OFFERING_1 = os.Getenv("CLOUDSTACK_DISK_OFFERING_1")
var CLOUDSTACK_DISK_OFFERING_2 = os.Getenv("CLOUDSTACK_DISK_OFFERING_2")
var CLOUDSTACK_HYPERVISOR = os.Getenv("CLOUDSTACK_HYPERVISOR")
var CLOUDSTACK_SERVICE_OFFERING_1 = os.Getenv("CLOUDSTACK_SERVICE_OFFERING_1")
var CLOUDSTACK_SERVICE_OFFERING_2 = os.Getenv("CLOUDSTACK_SERVICE_OFFERING_2")
var CLOUDSTACK_NETWORK_1 = os.Getenv("CLOUDSTACK_NETWORK_1")
var CLOUDSTACK_NETWORK_1_IPADDRESS = os.Getenv("CLOUDSTACK_NETWORK_1_IPADDRESS")
var CLOUDSTACK_NETWORK_2 = os.Getenv("CLOUDSTACK_NETWORK_2")
var CLOUDSTACK_NETWORK_2_CIDR = os.Getenv("CLOUDSTACK_NETWORK_2_CIDR")
var CLOUDSTACK_NETWORK_2_OFFERING = os.Getenv("CLOUDSTACK_NETWORK_2_OFFERING")
var CLOUDSTACK_NETWORK_2_IPADDRESS = os.Getenv("CLOUDSTACK_NETWORK_2_IPADDRESS")
var CLOUDSTACK_VPC_CIDR_1 = os.Getenv("CLOUDSTACK_VPC_CIDR_1")
var CLOUDSTACK_VPC_CIDR_2 = os.Getenv("CLOUDSTACK_VPC_CIDR_2")
var CLOUDSTACK_VPC_OFFERING = os.Getenv("CLOUDSTACK_VPC_OFFERING")
var CLOUDSTACK_VPC_NETWORK_CIDR = os.Getenv("CLOUDSTACK_VPC_NETWORK_CIDR")
var CLOUDSTACK_VPC_NETWORK_OFFERING = os.Getenv("CLOUDSTACK_VPC_NETWORK_OFFERING")
var CLOUDSTACK_PUBLIC_IPADDRESS = os.Getenv("CLOUDSTACK_PUBLIC_IPADDRESS")
var CLOUDSTACK_SSH_PUBLIC_KEY = os.Getenv("CLOUDSTACK_SSH_PUBLIC_KEY")
var CLOUDSTACK_TEMPLATE = os.Getenv("CLOUDSTACK_TEMPLATE")
var CLOUDSTACK_TEMPLATE_FORMAT = os.Getenv("CLOUDSTACK_TEMPLATE_FORMAT")
var CLOUDSTACK_TEMPLATE_URL = os.Getenv("CLOUDSTACK_TEMPLATE_URL")
var CLOUDSTACK_TEMPLATE_OS_TYPE = os.Getenv("CLOUDSTACK_TEMPLATE_OS_TYPE")
var CLOUDSTACK_PROJECT_NAME = os.Getenv("CLOUDSTACK_PROJECT_NAME")
var CLOUDSTACK_PROJECT_NETWORK = os.Getenv("CLOUDSTACK_PROJECT_NETWORK")
var CLOUDSTACK_ZONE = os.Getenv("CLOUDSTACK_ZONE")

View File

@ -35,7 +35,7 @@ func TestAccCloudStackDisk_device(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackDiskDestroyAdvanced,
CheckDestroy: testAccCheckCloudStackDiskDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackDisk_device,
@ -57,7 +57,7 @@ func TestAccCloudStackDisk_update(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackDiskDestroyAdvanced,
CheckDestroy: testAccCheckCloudStackDiskDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackDisk_update,
@ -151,57 +151,9 @@ func testAccCheckCloudStackDiskDestroy(s *terraform.State) error {
return fmt.Errorf("No disk ID is set")
}
p := cs.Volume.NewDeleteVolumeParams(rs.Primary.ID)
_, err := cs.Volume.DeleteVolume(p)
if err != nil {
return fmt.Errorf(
"Error deleting disk (%s): %s",
rs.Primary.ID, err)
}
}
return nil
}
func testAccCheckCloudStackDiskDestroyAdvanced(s *terraform.State) error {
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudstack_disk" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("No disk ID is set")
}
p := cs.Volume.NewDeleteVolumeParams(rs.Primary.ID)
_, err := cs.Volume.DeleteVolume(p)
if err != nil {
return fmt.Errorf(
"Error deleting disk (%s): %s",
rs.Primary.ID, err)
}
}
for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudstack_instance" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}
p := cs.VirtualMachine.NewDestroyVirtualMachineParams(rs.Primary.ID)
_, err := cs.VirtualMachine.DestroyVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error deleting instance (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.Volume.GetVolumeByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Disk %s still exists", rs.Primary.ID)
}
}

View File

@ -154,11 +154,9 @@ func testAccCheckCloudStackEgressFirewallDestroy(s *terraform.State) error {
continue
}
p := cs.Firewall.NewDeleteEgressFirewallRuleParams(uuid)
_, err := cs.Firewall.DeleteEgressFirewallRule(p)
if err != nil {
return err
_, _, err := cs.Firewall.GetEgressFirewallRuleByID(uuid)
if err == nil {
return fmt.Errorf("Egress rule %s still exists", rs.Primary.ID)
}
}
}

View File

@ -148,11 +148,9 @@ func testAccCheckCloudStackFirewallDestroy(s *terraform.State) error {
continue
}
p := cs.Firewall.NewDeleteFirewallRuleParams(uuid)
_, err := cs.Firewall.DeleteFirewallRule(p)
if err != nil {
return err
_, _, err := cs.Firewall.GetFirewallRuleByID(uuid)
if err == nil {
return fmt.Errorf("Firewall rule %s still exists", rs.Primary.ID)
}
}
}

View File

@ -56,6 +56,12 @@ func resourceCloudStackInstance() *schema.Resource {
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"zone": &schema.Schema{
Type: schema.TypeString,
Required: true,
@ -87,13 +93,6 @@ func resourceCloudStackInstance() *schema.Resource {
Optional: true,
Default: false,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
@ -148,6 +147,18 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
p.SetIpaddress(ipaddres.(string))
}
// If there is a project supplied, we retreive and set the project id
if project, ok := d.GetOk("project"); ok {
// Retrieve the project UUID
projectid, e := retrieveUUID(cs, "project", project.(string))
if e != nil {
return e.Error()
}
// Set the default project ID
p.SetProjectid(projectid)
}
// If a keypair is supplied, add it to the parameter struct
if keypair, ok := d.GetOk("keypair"); ok {
p.SetKeypair(keypair.(string))
}
@ -164,16 +175,6 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
p.SetUserdata(ud)
}
// If project contains any info, we retreive the project id
if project, ok := d.GetOk("project"); ok {
projectid, e := retrieveUUID(cs, "project", project.(string))
if e != nil {
return e.Error()
}
log.Printf("[DEBUG] project id %s", projectid)
p.SetProjectid(projectid)
}
// Create the new instance
r, err := cs.VirtualMachine.DeployVirtualMachine(p)
if err != nil {
@ -218,7 +219,7 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er
setValueOrUUID(d, "service_offering", vm.Serviceofferingname, vm.Serviceofferingid)
setValueOrUUID(d, "template", vm.Templatename, vm.Templateid)
setValueOrUUID(d, "project", vm.Project, vm.Projectid)
return nil
}

View File

@ -24,13 +24,7 @@ func TestAccCloudStackInstance_basic(t *testing.T) {
"cloudstack_instance.foobar", &instance),
testAccCheckCloudStackInstanceAttributes(&instance),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar",
"user_data",
"0cf3dcdc356ec8369494cb3991985ecd5296cdd5"),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar",
"keypair",
CLOUDSTACK_SSH_KEYPAIR),
"cloudstack_instance.foobar", "user_data", "0cf3dcdc356ec8369494cb3991985ecd5296cdd5"),
),
},
},
@ -52,13 +46,7 @@ func TestAccCloudStackInstance_update(t *testing.T) {
"cloudstack_instance.foobar", &instance),
testAccCheckCloudStackInstanceAttributes(&instance),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar",
"user_data",
"0cf3dcdc356ec8369494cb3991985ecd5296cdd5"),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar",
"keypair",
CLOUDSTACK_SSH_KEYPAIR),
"cloudstack_instance.foobar", "user_data", "0cf3dcdc356ec8369494cb3991985ecd5296cdd5"),
),
},
@ -99,7 +87,7 @@ func TestAccCloudStackInstance_fixedIP(t *testing.T) {
})
}
func TestAccCloudStackInstance_Projectname(t *testing.T) {
func TestAccCloudStackInstance_keyPair(t *testing.T) {
var instance cloudstack.VirtualMachine
resource.Test(t, resource.TestCase{
@ -108,19 +96,19 @@ func TestAccCloudStackInstance_Projectname(t *testing.T) {
CheckDestroy: testAccCheckCloudStackInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackInstance_projectname,
Config: testAccCloudStackInstance_keyPair,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackInstanceExists(
"cloudstack_instance.foobar", &instance),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar", "project", CLOUDSTACK_PROJECT_NAME),
"cloudstack_instance.foobar", "keypair", "terraform-test-keypair"),
),
},
},
})
}
func TestAccCloudStackInstance_Projectid(t *testing.T) {
func TestAccCloudStackInstance_project(t *testing.T) {
var instance cloudstack.VirtualMachine
resource.Test(t, resource.TestCase{
@ -129,12 +117,12 @@ func TestAccCloudStackInstance_Projectid(t *testing.T) {
CheckDestroy: testAccCheckCloudStackInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudStackInstance_projectid,
Config: testAccCloudStackInstance_project,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackInstanceExists(
"cloudstack_instance.foobar", &instance),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar", "project", CLOUDSTACK_PROJECT_ID),
"cloudstack_instance.foobar", "project", CLOUDSTACK_PROJECT_NAME),
),
},
},
@ -226,13 +214,9 @@ func testAccCheckCloudStackInstanceDestroy(s *terraform.State) error {
return fmt.Errorf("No instance ID is set")
}
p := cs.VirtualMachine.NewDestroyVirtualMachineParams(rs.Primary.ID)
_, err := cs.VirtualMachine.DestroyVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error deleting instance (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.VirtualMachine.GetVirtualMachineByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Virtual Machine %s still exists", rs.Primary.ID)
}
}
@ -247,15 +231,13 @@ resource "cloudstack_instance" "foobar" {
network = "%s"
template = "%s"
zone = "%s"
keypair = "%s"
user_data = "foobar\nfoo\nbar"
expunge = true
}`,
CLOUDSTACK_SERVICE_OFFERING_1,
CLOUDSTACK_NETWORK_1,
CLOUDSTACK_TEMPLATE,
CLOUDSTACK_ZONE,
CLOUDSTACK_SSH_KEYPAIR)
CLOUDSTACK_ZONE)
var testAccCloudStackInstance_renameAndResize = fmt.Sprintf(`
resource "cloudstack_instance" "foobar" {
@ -265,15 +247,13 @@ resource "cloudstack_instance" "foobar" {
network = "%s"
template = "%s"
zone = "%s"
keypair = "%s"
user_data = "foobar\nfoo\nbar"
expunge = true
}`,
CLOUDSTACK_SERVICE_OFFERING_2,
CLOUDSTACK_NETWORK_1,
CLOUDSTACK_TEMPLATE,
CLOUDSTACK_ZONE,
CLOUDSTACK_SSH_KEYPAIR)
CLOUDSTACK_ZONE)
var testAccCloudStackInstance_fixedIP = fmt.Sprintf(`
resource "cloudstack_instance" "foobar" {
@ -291,38 +271,42 @@ resource "cloudstack_instance" "foobar" {
CLOUDSTACK_NETWORK_1_IPADDRESS,
CLOUDSTACK_TEMPLATE,
CLOUDSTACK_ZONE)
var testAccCloudStackInstance_projectname = fmt.Sprintf(`
var testAccCloudStackInstance_keyPair = fmt.Sprintf(`
resource "cloudstack_ssh_keypair" "foo" {
name = "terraform-test-keypair"
}
resource "cloudstack_instance" "foobar" {
name = "terraform-test"
display_name = "terraform"
service_offering= "%s"
network = "%s"
ipaddress = "%s"
template = "%s"
zone = "%s"
keypair = "${cloudstack_ssh_keypair.foo.name}"
expunge = true
project = "%s"
}`,
CLOUDSTACK_SERVICE_OFFERING_1,
CLOUDSTACK_NETWORK_1,
CLOUDSTACK_NETWORK_1_IPADDRESS,
CLOUDSTACK_TEMPLATE,
CLOUDSTACK_ZONE,
CLOUDSTACK_PROJECT_NAME)
CLOUDSTACK_ZONE)
var testAccCloudStackInstance_projectid = fmt.Sprintf(`
var testAccCloudStackInstance_project = fmt.Sprintf(`
resource "cloudstack_instance" "foobar" {
name = "terraform-test"
display_name = "terraform"
service_offering= "%s"
network = "%s"
network = "%s"
template = "%s"
project = "%s"
zone = "%s"
expunge = true
project = "%s"
}`,
CLOUDSTACK_SERVICE_OFFERING_1,
CLOUDSTACK_NETWORK_1,
CLOUDSTACK_PROJECT_NETWORK,
CLOUDSTACK_TEMPLATE,
CLOUDSTACK_ZONE,
CLOUDSTACK_PROJECT_ID)
CLOUDSTACK_PROJECT_NAME,
CLOUDSTACK_ZONE)

View File

@ -136,7 +136,7 @@ func resourceCloudStackIPAddressDelete(d *schema.ResourceData, meta interface{})
return nil
}
return fmt.Errorf("Error deleting network ACL list %s: %s", d.Get("name").(string), err)
return fmt.Errorf("Error disassociating IP address %s: %s", d.Get("name").(string), err)
}
return nil

View File

@ -103,13 +103,9 @@ func testAccCheckCloudStackIPAddressDestroy(s *terraform.State) error {
return fmt.Errorf("No IP address ID is set")
}
p := cs.Address.NewDisassociateIpAddressParams(rs.Primary.ID)
_, err := cs.Address.DisassociateIpAddress(p)
if err != nil {
return fmt.Errorf(
"Error disassociating IP address (%s): %s",
rs.Primary.ID, err)
ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
if err == nil && ip.Associatednetworkid != "" {
return fmt.Errorf("Public IP %s still associated", rs.Primary.ID)
}
}

View File

@ -160,11 +160,9 @@ func testAccCheckCloudStackNetworkACLRuleDestroy(s *terraform.State) error {
continue
}
p := cs.NetworkACL.NewDeleteNetworkACLParams(uuid)
_, err := cs.NetworkACL.DeleteNetworkACL(p)
if err != nil {
return err
_, _, err := cs.NetworkACL.GetNetworkACLByID(uuid)
if err == nil {
return fmt.Errorf("Network ACL rule %s still exists", rs.Primary.ID)
}
}
}

View File

@ -86,13 +86,9 @@ func testAccCheckCloudStackNetworkACLDestroy(s *terraform.State) error {
return fmt.Errorf("No network ACL ID is set")
}
p := cs.NetworkACL.NewDeleteNetworkACLListParams(rs.Primary.ID)
_, err := cs.NetworkACL.DeleteNetworkACLList(p)
if err != nil {
return fmt.Errorf(
"Error deleting network ACL (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.NetworkACL.GetNetworkACLListByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Network ACl list %s still exists", rs.Primary.ID)
}
}

View File

@ -140,13 +140,9 @@ func testAccCheckCloudStackNetworkDestroy(s *terraform.State) error {
return fmt.Errorf("No network ID is set")
}
p := cs.Network.NewDeleteNetworkParams(rs.Primary.ID)
_, err := cs.Network.DeleteNetwork(p)
if err != nil {
return fmt.Errorf(
"Error deleting network (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.Network.GetNetworkByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Network %s still exists", rs.Primary.ID)
}
}

View File

@ -140,13 +140,9 @@ func testAccCheckCloudStackNICDestroy(s *terraform.State) error {
return fmt.Errorf("No instance ID is set")
}
p := cs.VirtualMachine.NewDestroyVirtualMachineParams(rs.Primary.ID)
_, err := cs.VirtualMachine.DestroyVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error deleting instance (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.VirtualMachine.GetVirtualMachineByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Virtual Machine %s still exists", rs.Primary.ID)
}
}

View File

@ -140,11 +140,9 @@ func testAccCheckCloudStackPortForwardDestroy(s *terraform.State) error {
continue
}
p := cs.Firewall.NewDeletePortForwardingRuleParams(uuid)
_, err := cs.Firewall.DeletePortForwardingRule(p)
if err != nil {
return err
_, _, err := cs.Firewall.GetPortForwardingRuleByID(uuid)
if err == nil {
return fmt.Errorf("Port forward %s still exists", rs.Primary.ID)
}
}
}

View File

@ -2,10 +2,12 @@ package cloudstack
import (
"fmt"
"io/ioutil"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/go-homedir"
"github.com/xanzy/go-cloudstack/cloudstack"
)
@ -48,54 +50,59 @@ func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}
publicKey := d.Get("public_key").(string)
if publicKey != "" {
//register key supplied
p := cs.SSH.NewRegisterSSHKeyPairParams(name, publicKey)
r, err := cs.SSH.RegisterSSHKeyPair(p)
// Register supplied key
keyPath, err := homedir.Expand(publicKey)
if err != nil {
return fmt.Errorf("Error expanding the public key path: %v", err)
}
key, err := ioutil.ReadFile(keyPath)
if err != nil {
return fmt.Errorf("Error reading the public key: %v", err)
}
p := cs.SSH.NewRegisterSSHKeyPairParams(name, string(key))
_, err = cs.SSH.RegisterSSHKeyPair(p)
if err != nil {
return err
}
log.Printf("[DEBUG] RegisterSSHKeyPair response: %+v\n", r)
log.Printf("[DEBUG] Key pair successfully registered at Cloudstack")
d.SetId(name)
} else {
//no key supplied, must create one and return the private key
// No key supplied, must create one and return the private key
p := cs.SSH.NewCreateSSHKeyPairParams(name)
r, err := cs.SSH.CreateSSHKeyPair(p)
if err != nil {
return err
}
log.Printf("[DEBUG] CreateSSHKeyPair response: %+v\n", r)
log.Printf("[DEBUG] Key pair successfully generated at Cloudstack")
log.Printf("[DEBUG] Private key returned: %s", r.Privatekey)
d.Set("private_key", r.Privatekey)
d.SetId(name)
}
log.Printf("[DEBUG] Key pair successfully generated at Cloudstack")
d.SetId(name)
return resourceCloudStackSSHKeyPairRead(d, meta)
}
func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
log.Printf("[DEBUG] looking for ssh key %s with name %s", d.Id(), d.Get("name").(string))
log.Printf("[DEBUG] looking for key pair with name %s", d.Id())
p := cs.SSH.NewListSSHKeyPairsParams()
p.SetName(d.Get("name").(string))
p.SetName(d.Id())
r, err := cs.SSH.ListSSHKeyPairs(p)
if err != nil {
return err
}
if r.Count == 0 {
log.Printf("[DEBUG] Key pair %s does not exist", d.Get("name").(string))
d.Set("name", "")
log.Printf("[DEBUG] Key pair %s does not exist", d.Id())
d.SetId("")
return nil
}
//SSHKeyPair name is unique in a cloudstack account so dont need to check for multiple
d.Set("name", r.SSHKeyPairs[0].Name)
d.Set("fingerprint", r.SSHKeyPairs[0].Fingerprint)
log.Printf("[DEBUG] Read ssh key pair %+v\n", d)
return nil
}
@ -104,18 +111,18 @@ func resourceCloudStackSSHKeyPairDelete(d *schema.ResourceData, meta interface{}
cs := meta.(*cloudstack.CloudStackClient)
// Create a new parameter struct
p := cs.SSH.NewDeleteSSHKeyPairParams(d.Get("name").(string))
p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
// Remove the SSH Keypair
_, err := cs.SSH.DeleteSSHKeyPair(p)
if err != nil {
// This is a very poor way to be told the UUID does no longer exist :(
if strings.Contains(err.Error(), fmt.Sprintf(
"A key pair with name '%s' does not exist for account", d.Get("name").(string))) {
"A key pair with name '%s' does not exist for account", d.Id())) {
return nil
}
return fmt.Errorf("Error deleting SSH Keypair: %s", err)
return fmt.Errorf("Error deleting key pair: %s", err)
}
return nil

View File

@ -2,7 +2,6 @@ package cloudstack
import (
"fmt"
"log"
"strings"
"testing"
@ -24,7 +23,7 @@ func TestAccCloudStackSSHKeyPair_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackSSHKeyPairExists("cloudstack_ssh_keypair.foo", &sshkey),
testAccCheckCloudStackSSHKeyPairAttributes(&sshkey),
testAccCheckCloudStackSSHKeyPairCreateAttributes("cloudstack_ssh_keypair.foo"),
testAccCheckCloudStackSSHKeyPairCreateAttributes("terraform-test-keypair"),
),
},
},
@ -61,64 +60,69 @@ func testAccCheckCloudStackSSHKeyPairExists(n string, sshkey *cloudstack.SSHKeyP
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.Attributes["name"] == "" {
return fmt.Errorf("No ssh key name is set")
if rs.Primary.ID == "" {
return fmt.Errorf("No key pair ID is set")
}
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
p := cs.SSH.NewListSSHKeyPairsParams()
p.SetName(rs.Primary.Attributes["name"])
list, err := cs.SSH.ListSSHKeyPairs(p)
p.SetName(rs.Primary.ID)
list, err := cs.SSH.ListSSHKeyPairs(p)
if err != nil {
return err
}
if list.Count == 1 && list.SSHKeyPairs[0].Name == rs.Primary.Attributes["name"] {
//ssh key exists
*sshkey = *list.SSHKeyPairs[0]
return nil
if list.Count != 1 || list.SSHKeyPairs[0].Name != rs.Primary.ID {
return fmt.Errorf("Key pair not found")
}
return fmt.Errorf("SSH key not found")
*sshkey = *list.SSHKeyPairs[0]
return nil
}
}
func testAccCheckCloudStackSSHKeyPairAttributes(
sshkey *cloudstack.SSHKeyPair) resource.TestCheckFunc {
keypair *cloudstack.SSHKeyPair) resource.TestCheckFunc {
return func(s *terraform.State) error {
fingerprintLen := len(sshkey.Fingerprint)
if fingerprintLen != 47 {
return fmt.Errorf(
"SSH key: Attribute private_key expected length 47, got %d",
fingerprintLen)
fpLen := len(keypair.Fingerprint)
if fpLen != 47 {
return fmt.Errorf("SSH key: Attribute private_key expected length 47, got %d", fpLen)
}
return nil
}
}
func testAccCheckCloudStackSSHKeyPairCreateAttributes(
name string) resource.TestCheckFunc {
func testAccCheckCloudStackSSHKeyPairCreateAttributes(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()
rs, ok := ms.Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
found := false
for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudstack_ssh_keypair" {
continue
}
if rs.Primary.ID != name {
continue
}
if !strings.Contains(rs.Primary.Attributes["private_key"], "PRIVATE KEY") {
return fmt.Errorf(
"SSH key: Attribute private_key expected 'PRIVATE KEY' to be present, got %s",
rs.Primary.Attributes["private_key"])
}
found = true
break
}
is := rs.Primary
if is == nil {
return fmt.Errorf("No primary instance: %s", name)
if !found {
return fmt.Errorf("Could not find key pair %s", name)
}
log.Printf("Private key calculated: %s", is.Attributes["private_key"])
if !strings.Contains(is.Attributes["private_key"], "PRIVATE KEY") {
return fmt.Errorf(
"SSH key: Attribute private_key expected 'PRIVATE KEY' to be present, got %s",
is.Attributes["private_key"])
}
return nil
}
}
@ -131,17 +135,23 @@ func testAccCheckCloudStackSSHKeyPairDestroy(s *terraform.State) error {
continue
}
if rs.Primary.Attributes["name"] == "" {
return fmt.Errorf("No ssh key name is set")
if rs.Primary.ID == "" {
return fmt.Errorf("No key pair ID is set")
}
p := cs.SSH.NewDeleteSSHKeyPairParams(rs.Primary.Attributes["name"])
_, err := cs.SSH.DeleteSSHKeyPair(p)
p := cs.SSH.NewListSSHKeyPairsParams()
p.SetName(rs.Primary.ID)
list, err := cs.SSH.ListSSHKeyPairs(p)
if err != nil {
return fmt.Errorf(
"Error deleting ssh key (%s): %s",
rs.Primary.Attributes["name"], err)
return err
}
if list.Count != 1 {
return fmt.Errorf("Found more Key pair %s still exists", rs.Primary.ID)
}
if list.SSHKeyPairs[0].Name == rs.Primary.ID {
return fmt.Errorf("Key pair %s still exists", rs.Primary.ID)
}
}
@ -150,11 +160,11 @@ func testAccCheckCloudStackSSHKeyPairDestroy(s *terraform.State) error {
var testAccCloudStackSSHKeyPair_create = fmt.Sprintf(`
resource "cloudstack_ssh_keypair" "foo" {
name = "terraform-testacc"
name = "terraform-test-keypair"
}`)
var testAccCloudStackSSHKeyPair_register = fmt.Sprintf(`
resource "cloudstack_ssh_keypair" "foo" {
name = "terraform-testacc"
name = "terraform-test-keypair"
public_key = "%s"
}`, CLOUDSTACK_SSH_PUBLIC_KEY)

View File

@ -149,13 +149,9 @@ func testAccCheckCloudStackTemplateDestroy(s *terraform.State) error {
return fmt.Errorf("No template ID is set")
}
p := cs.Template.NewDeleteTemplateParams(rs.Primary.ID)
_, err := cs.Template.DeleteTemplate(p)
if err != nil {
return fmt.Errorf(
"Error deleting template (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.Template.GetTemplateByID(rs.Primary.ID, "executable")
if err == nil {
return fmt.Errorf("Template %s still exists", rs.Primary.ID)
}
}

View File

@ -92,13 +92,9 @@ func testAccCheckCloudStackVPCDestroy(s *terraform.State) error {
return fmt.Errorf("No VPC ID is set")
}
p := cs.VPC.NewDeleteVPCParams(rs.Primary.ID)
_, err := cs.VPC.DeleteVPC(p)
if err != nil {
return fmt.Errorf(
"Error deleting VPC (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.VPC.GetVPCByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("VPC %s still exists", rs.Primary.ID)
}
}

View File

@ -71,13 +71,9 @@ func testAccCheckCloudStackVPNConnectionDestroy(s *terraform.State) error {
return fmt.Errorf("No VPN Connection ID is set")
}
p := cs.VPN.NewDeleteVpnConnectionParams(rs.Primary.ID)
_, err := cs.VPN.DeleteVpnConnection(p)
if err != nil {
return fmt.Errorf(
"Error deleting VPN Connection (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.VPN.GetVpnConnectionByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("VPN Connection %s still exists", rs.Primary.ID)
}
}

View File

@ -163,13 +163,9 @@ func testAccCheckCloudStackVPNCustomerGatewayDestroy(s *terraform.State) error {
return fmt.Errorf("No VPN Customer Gateway ID is set")
}
p := cs.VPN.NewDeleteVpnCustomerGatewayParams(rs.Primary.ID)
_, err := cs.VPN.DeleteVpnCustomerGateway(p)
if err != nil {
return fmt.Errorf(
"Error deleting VPN Customer Gateway (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.VPN.GetVpnCustomerGatewayByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("VPN Customer Gateway %s still exists", rs.Primary.ID)
}
}

View File

@ -71,13 +71,9 @@ func testAccCheckCloudStackVPNGatewayDestroy(s *terraform.State) error {
return fmt.Errorf("No VPN Gateway ID is set")
}
p := cs.VPN.NewDeleteVpnGatewayParams(rs.Primary.ID)
_, err := cs.VPN.DeleteVpnGateway(p)
if err != nil {
return fmt.Errorf(
"Error deleting VPN Gateway (%s): %s",
rs.Primary.ID, err)
_, _, err := cs.VPN.GetVpnGatewayByID(rs.Primary.ID)
if err == nil {
return fmt.Errorf("VPN Gateway %s still exists", rs.Primary.ID)
}
}

View File

@ -32,23 +32,29 @@ The following arguments are supported:
* `display_name` - (Optional) The display name of the instance.
* `service_offering` - (Required) The name or ID of the service offering used for this instance.
* `service_offering` - (Required) The name or ID of the service offering used
for this instance.
* `network` - (Optional) The name or ID of the network to connect this instance to.
Changing this forces a new resource to be created.
* `network` - (Optional) The name or ID of the network to connect this instance
to. Changing this forces a new resource to be created.
* `ipaddress` - (Optional) The IP address to assign to this instance. Changing
this forces a new resource to be created.
* `template` - (Required) The name or ID of the template used for this instance.
Changing this forces a new resource to be created.
* `template` - (Required) The name or ID of the template used for this
instance. Changing this forces a new resource to be created.
* `project` - (Optional) The name or ID of the project to deploy this
instance to. Changing this forces a new resource to be created.
* `zone` - (Required) The name of the zone where this instance will be created.
Changing this forces a new resource to be created.
* `user_data` - (Optional) The user data to provide when launching the instance.
* `user_data` - (Optional) The user data to provide when launching the
instance.
* `keypair` - (Optional) The name of the SSH keypair that will be used to access this instance.
* `keypair` - (Optional) The name of the SSH key pair that will be used to
access this instance.
* `expunge` - (Optional) This determines if the instance is expunged when it is
destroyed (defaults false)

View File

@ -3,17 +3,17 @@ layout: "cloudstack"
page_title: "CloudStack: cloudstack_ssh_keypair"
sidebar_current: "docs-cloudstack-resource-ssh-keypair"
description: |-
Creates or registers an SSH keypair.
Creates or registers an SSH key pair.
---
# cloudstack\_ssh\_keypair
Creates or registers an SSH keypair.
Creates or registers an SSH key pair.
## Example Usage
```
resource "cloudstack_ssh_keypair" "myKey" {
resource "cloudstack_ssh_keypair" "default" {
name = "myKey"
}
```
@ -22,15 +22,19 @@ resource "cloudstack_ssh_keypair" "myKey" {
The following arguments are supported:
* `name` - (Required) The name to give the SSH keypair. This is a unique value within a Cloudstack account.
* `name` - (Required) The name of the SSH key pair. This is a unique value
within a CloudStack account. Changing this forces a new resource to be
created.
* `public_key` - (Optional) The full public key text of this keypair. If this is omitted, Cloudstack
will generate a new keypair.
* `public_key` - (Optional) The path to a public key that will be uploaded
the remote machine. If this is omitted, CloudStack will generate a new
key pair. Changing this forces a new resource to be created.
## Attributes Reference
The following attributes are exported:
* `id` - The keypair ID. This is set to the keypair `name` argument.
* `fingerprint` - The fingerprint of the public key specified or calculated.
* `private_key` - This is returned only if Cloudstack generated the keypair.
* `id` - The key pair ID.
* `fingerprint` - The fingerprint of the public key specified or created.
* `private_key` - The private key generated by CloudStack. Only available
if CloudStack generated the key pair.