-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Prativa Bawri <[email protected]>
- Loading branch information
Showing
5 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters