Skip to content

Commit

Permalink
Added test to check reboots in case of guest customisation is used (r…
Browse files Browse the repository at this point in the history
…emove CDrom)
  • Loading branch information
yannickstruyf3 committed Mar 19, 2020
1 parent 7bdf302 commit a324e1a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
44 changes: 16 additions & 28 deletions nutanix/data_source_nutanix_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package nutanix

import (
"fmt"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
"strconv"
)

func dataSourceNutanixCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceNutanixClusterRead,
Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
// Required: true,
Optional: true,
Type: schema.TypeString,
Computed: true,
Optional: true,
ConflictsWith: []string{"name"},
},
"metadata": {
Type: schema.TypeMap,
Expand Down Expand Up @@ -99,9 +101,10 @@ func dataSourceNutanixCluster() *schema.Resource {
Computed: true,
},
"name": {
Type: schema.TypeString,
// Computed: true,
Optional: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"cluster_id"},
},

// COMPUTED
Expand Down Expand Up @@ -607,24 +610,6 @@ func dataSourceNutanixClusterRead(d *schema.ResourceData, meta interface{}) erro
if err != nil {
return err
}
// clusterEntitiesMetadata := &v3.DSMetadata{}
// allClusters, err := conn.V3.ListCluster(clusterEntitiesMetadata)
// if err != nil {
// return fmt.Errorf("Error occured: %s", err)
// }
// for _, cluster := range allClusters.Entities {
// if *cluster.Status.Name == n.(string) {
// v = &v3.ClusterIntentResponse{
// Status: cluster.Status,
// Spec: cluster.Spec,
// Metadata: cluster.Metadata,
// APIVersion: cluster.APIVersion,
// }
// }
// }
// if v == nil {
// return fmt.Errorf("Did not find cluster with name %s", n.(string))
// }
}

m, c := setRSEntityMetadata(v.Metadata)
Expand Down Expand Up @@ -1057,8 +1042,12 @@ func dataSourceNutanixClusterRead(d *schema.ResourceData, meta interface{}) erro
return err
}

d.SetId(utils.StringValue(v.Metadata.UUID))
cUUID := utils.StringValue(v.Metadata.UUID)
if err := d.Set("cluster_id", cUUID); err != nil {
return err
}

d.SetId(cUUID)
return nil
}

Expand Down Expand Up @@ -1087,9 +1076,8 @@ func findClusterByName(conn *v3.Client, name string) (*v3.ClusterIntentResponse,
}

if len(found) == 0 {
return nil, fmt.Errorf("Did not find cluster with name %s", name)
return nil, fmt.Errorf("did not find cluster with name %s", name)
}

return found[0], nil

}
38 changes: 31 additions & 7 deletions nutanix/data_source_nutanix_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,47 @@ func TestAccNutanixClusterDataSource_basic(t *testing.T) {
})
}

func TestAccNutanixClusterByNameDataSource_basic(t *testing.T) {
func TestAccNutanixClusterDataSource_ByName(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccClusterByNameDataSourceConfig,
Config: testAccClusterDataSourceConfigByName,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.nutanix_cluster.cluster", "id"),
resource.TestCheckResourceAttrSet(
"data.nutanix_cluster.cluster", "name"),
resource.TestCheckResourceAttrSet(
"data.nutanix_cluster.cluster", "cluster_id"),
),
},
},
})
}

func TestAccNutanixClusterByNameNotExistingDataSource_basic(t *testing.T) {
func TestAccNutanixClusterDataSource_ByNameNotExisting(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccClusterByNameNotExistingDataSourceConfig,
ExpectError: regexp.MustCompile("Did not find cluster with name *"),
Config: testAccClusterDataSourceConfigByNameNotExisting,
ExpectError: regexp.MustCompile("did not find cluster with name *"),
},
},
})
}

func TestAccNutanixClusterDataSource_NameUuidConflict(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccClusterDataSourceConfigNameUUIDConflict,
ExpectError: regexp.MustCompile(" * conflicts with *"),
},
},
})
Expand All @@ -62,18 +77,27 @@ data "nutanix_cluster" "cluster" {
cluster_id = data.nutanix_clusters.clusters.entities.0.metadata.uuid
}`

const testAccClusterByNameDataSourceConfig = `
const testAccClusterDataSourceConfigByName = `
data "nutanix_clusters" "clusters" {}
data "nutanix_cluster" "cluster" {
name = data.nutanix_clusters.clusters.entities.0.name
}`

const testAccClusterByNameNotExistingDataSourceConfig = `
const testAccClusterDataSourceConfigByNameNotExisting = `
data "nutanix_clusters" "clusters" {}
data "nutanix_cluster" "cluster" {
name = "ThisDoesNotExist"
}`

const testAccClusterDataSourceConfigNameUUIDConflict = `
data "nutanix_clusters" "clusters" {}
data "nutanix_cluster" "cluster" {
cluster_id = data.nutanix_clusters.clusters.entities.0.metadata.uuid
name = data.nutanix_clusters.clusters.entities.0.name
}`
8 changes: 6 additions & 2 deletions nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,10 +1183,16 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{

if d.HasChange("disk_list") {
preCdromCount, err := CountDiskListCdrom(res.DiskList)
if err != nil {
return err
}
if res.DiskList, err = expandDiskList(d, false); err != nil {
return err
}
postCdromCount, err := CountDiskListCdrom(res.DiskList)
if err != nil {
return err
}
if preCdromCount != postCdromCount {
hotPlugChange = false
}
Expand Down Expand Up @@ -1924,13 +1930,11 @@ func waitForIPRefreshFunc(client *v3.Client, vmUUID string) resource.StateRefres
}

func CountDiskListCdrom(dl []*v3.VMDisk) (int, error) {
log.Printf("=====")
counter := 0
for _, v := range dl {
if *v.DeviceProperties.DeviceType == "CDROM" {
counter++
}
}
log.Printf("=====")
return counter, nil
}

0 comments on commit a324e1a

Please sign in to comment.