Skip to content

Commit

Permalink
Adding machine data source (#140)
Browse files Browse the repository at this point in the history
Signed-off-by: Prativa Bawri <[email protected]>
  • Loading branch information
Prativa20 authored and markpeek committed Dec 18, 2019
1 parent a22c1eb commit b8f0234
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hil v0.0.0-20190212132231-97b3a9cdfa93 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.1.0
github.com/vmware/vra-sdk-go v0.2.4
github.com/vmware/vra-sdk-go v0.2.5
)

replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ github.com/vmware/vra-sdk-go v0.2.3 h1:oepqRZapum3bfzUAo+o7DYwTp97230f7yLgPu8gBV
github.com/vmware/vra-sdk-go v0.2.3/go.mod h1:dqUEePGPUHNYQ/p9DKZM1amqB2WP1uI9JkYvzF+auVo=
github.com/vmware/vra-sdk-go v0.2.4 h1:gIQvcT0XeFqMNX+hurvHWtGiJAEQYopzyilCm20bryg=
github.com/vmware/vra-sdk-go v0.2.4/go.mod h1:dqUEePGPUHNYQ/p9DKZM1amqB2WP1uI9JkYvzF+auVo=
github.com/vmware/vra-sdk-go v0.2.5 h1:VaBWKVW7dW/BRMNNLoDFuMaDRh9kvVsnl+S4sMqstMM=
github.com/vmware/vra-sdk-go v0.2.5/go.mod h1:dqUEePGPUHNYQ/p9DKZM1amqB2WP1uI9JkYvzF+auVo=
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.1.0 h1:uJwc9HiBOCpoKIObTQaLR+tsEXx1HBHnOsOOpcdhZgw=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
Expand Down
158 changes: 158 additions & 0 deletions vra/data_source_vra_machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package vra

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vmware/vra-sdk-go/pkg/client/compute"
"github.com/vmware/vra-sdk-go/pkg/models"
)

func dataSourceMachine() *schema.Resource {
return &schema.Resource{
Read: dataSourceMachineRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"filter": {
Type: schema.TypeString,
Optional: true,
},
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"cloud_account_ids": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"custom_properties": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"deployment_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"external_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"external_region_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"external_zone_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"links": linksSchema(),
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"org_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"owner": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"power_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"project_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"updated_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceMachineRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Reading the vra_machine data source with filter %s", d.Get("filter"))
apiClient := meta.(*Client).apiClient

var machine *models.Machine

id := d.Get("id").(string)
filter := d.Get("filter").(string)

if id == "" && filter == "" {
return fmt.Errorf("one of id or filter is required")
}

if id != "" {
getResp, err := apiClient.Compute.GetMachine(compute.NewGetMachineParams().WithID(id))

if err != nil {
return err
}
machine = getResp.GetPayload()
} else {
getResp, err := apiClient.Compute.GetMachines(compute.NewGetMachinesParams().WithDollarFilter(withString(filter)))
if err != nil {
return err
}

machines := *getResp.Payload
if len(machines.Content) > 1 {
return fmt.Errorf("vra_machine must filter to a machine")
}
if len(machines.Content) == 0 {
return fmt.Errorf("vra_machine filter did not match any machine")
}

machine = machines.Content[0]
}

d.SetId(*machine.ID)
d.Set("address", machine.Address)
d.Set("cloud_account_ids", machine.CloudAccountIds)
d.Set("created_at", machine.CreatedAt)
d.Set("custom_properties", machine.CustomProperties)
d.Set("deployment_id", machine.DeploymentID)
d.Set("description", machine.Description)
d.Set("external_zone_id", machine.ExternalZoneID)
d.Set("external_region_id", machine.ExternalRegionID)
d.Set("external_id", machine.ExternalID)
d.Set("name", machine.Name)
d.Set("org_id", machine.OrgID)
d.Set("owner", machine.Owner)
d.Set("power_state", machine.PowerState)
d.Set("project_id", machine.ProjectID)
d.Set("updated_at", machine.UpdatedAt)

if err := d.Set("tags", flattenTags(machine.Tags)); err != nil {
return fmt.Errorf("error setting machine tags - error: %v", err)
}

if err := d.Set("links", flattenLinks(machine.Links)); err != nil {
return fmt.Errorf("error setting machine links - error: %#v", err)
}

log.Printf("Finished reading the vra_machine data source with filter %s", d.Get("filter"))
return nil
}
70 changes: 70 additions & 0 deletions vra/data_source_vra_machine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package vra

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"

"regexp"
"testing"
)

func TestAccDataSourceVRAMachine(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "vra_machine.my_machine"
dataSourceName := "data.vra_machine.this"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckMachine(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVRAMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVRAMachineNotFound(rInt),
ExpectError: regexp.MustCompile("vra_machine filter did not match any machine"),
},
{
Config: testAccDataSourceVRAMachineNameFilter(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "id"),
resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceName, "description"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "image", dataSourceName, "image"),
resource.TestCheckResourceAttrPair(resourceName, "flavor", dataSourceName, "flavor"),
),
},
{
Config: testAccDataSourceVRAMachineByID(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "id"),
resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceName, "description"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "image", dataSourceName, "image"),
resource.TestCheckResourceAttrPair(resourceName, "flavor", dataSourceName, "flavor"),
),
},
},
})
}

func testAccDataSourceVRAMachineNotFound(rInt int) string {
return testAccCheckVRAMachineConfig(rInt) + fmt.Sprintf(`
data "vra_machine" "this" {
filter = "name eq 'foobar'"
}`)
}

func testAccDataSourceVRAMachineNameFilter(rInt int) string {
return testAccCheckVRAMachineConfig(rInt) + fmt.Sprintf(`
data "vra_machine" "this" {
filter = "name eq '${vra_machine.my_machine.name}'"
}`)
}

func testAccDataSourceVRAMachineByID(rInt int) string {
return testAccCheckVRAMachineConfig(rInt) + fmt.Sprintf(`
data "vra_machine" "this" {
id = vra_machine.my_machine.id
}`)
}
1 change: 1 addition & 0 deletions vra/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Provider() *schema.Provider {
"vra_deployment": dataSourceDeployment(),
"vra_fabric_network": dataSourceFabricNetwork(),
"vra_image": dataSourceImage(),
"vra_machine": dataSourceMachine(),
"vra_network": dataSourceNetwork(),
"vra_network_domain": dataSourceNetworkDomain(),
"vra_network_profile": dataSourceNetworkProfile(),
Expand Down

0 comments on commit b8f0234

Please sign in to comment.