Skip to content

Commit

Permalink
Merge pull request #386 from vmware/fix-data-source-region
Browse files Browse the repository at this point in the history
Several fixes for the data source region
  • Loading branch information
frodenas authored Sep 3, 2021
2 parents 4250a3b + d1dba66 commit 25bb1d4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 41 deletions.
91 changes: 60 additions & 31 deletions vra/data_source_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vra
import (
"fmt"
"log"
"strings"

"github.com/vmware/vra-sdk-go/pkg/client/location"
"github.com/vmware/vra-sdk-go/pkg/models"
Expand All @@ -19,46 +18,56 @@ func dataSourceRegion() *schema.Resource {

Schema: map[string]*schema.Schema{
"cloud_account_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The id of the cloud account the region belongs to.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Date when the entity was created. The date is in ISO 8601 and UTC.",
},
"external_region_id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Unique identifier of region on the provider side.",
},
"filter": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Description: "Search criteria to narrow down Regions.",
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The id of the region instance.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Name of region on the provider side. In vSphere, the name of the region is different from its id.",
},
"org_id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "The id of the organization this entity belongs to.",
},
"owner": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Email of the user that owns the entity.",
},
"region": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Description: "The specific region associated with the cloud account.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Date when the entity was last updated. The date is ISO 8601 and UTC.",
},
},
}
Expand Down Expand Up @@ -91,7 +100,12 @@ func dataSourceRegionRead(d *schema.ResourceData, meta interface{}) error {
// config includes id, using id to get region details
getResp, err := apiClient.Location.GetRegion(location.NewGetRegionParams().WithID(id.(string)))
if err != nil {
return err
switch err.(type) {
case *location.GetRegionNotFound:
return fmt.Errorf("region %s not found", id.(string))
default:
return err
}
}

setFields(getResp.Payload)
Expand Down Expand Up @@ -146,20 +160,35 @@ func dataSourceRegionRead(d *schema.ResourceData, meta interface{}) error {
if cloudAccountIDOk && regionOk {
getResp, err := apiClient.CloudAccount.GetCloudAccount(cloud_account.NewGetCloudAccountParams().WithID(cloudAccountID.(string)))
if err != nil {
return err
switch err.(type) {
case *cloud_account.GetCloudAccountNotFound:
return fmt.Errorf("cloud account %s not found", cloudAccountID.(string))
default:
return err
}
}

var id string
cloudAccount := getResp.Payload
for i, enabledRegion := range cloudAccount.EnabledRegionIds {
if enabledRegion == region {
d.SetId(strings.TrimPrefix(cloudAccount.Links["regions"].Hrefs[i], "/iaas/api/regions/"))
return nil
for _, enabledRegion := range cloudAccount.EnabledRegions {
if enabledRegion.Name == region {
id = *enabledRegion.ID
break
}
}

resp, err := apiClient.Location.GetRegion(location.NewGetRegionParams().WithID(id.(string)))
if id == "" {
return fmt.Errorf("region %s not found", region)
}

resp, err := apiClient.Location.GetRegion(location.NewGetRegionParams().WithID(id))
if err != nil {
return err
switch err.(type) {
case *location.GetRegionNotFound:
return fmt.Errorf("region %s not found", id)
default:
return err
}
}

setFields(resp.Payload)
Expand Down
20 changes: 10 additions & 10 deletions website/docs/d/vra_region.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ data "vra_region" "this" {
The region data source supports the following arguments:

## Argument Reference
* `cloud_account_id` - (Optional) The Cloud Account ID.
* `cloud_account_id` - (Optional) The id of the cloud account the region belongs to.

* `filter` - (Optional) Search criteria to narrow down Images.
* `filter` - (Optional) Search criteria to narrow down Regions.

* `id` - (Optional) The ID of the region to find.
* `id` - (Optional) The id of the region instance,

* `region` - (Optional) The specific region associated with the cloud account.

* `name` - (Optional) Name of the region from the associated vCenter.
* `name` - (Optional) Name of region on the provider side. In vSphere, the name of the region is different from its id.

## Attribute Reference
* `created_at` - Date when entity was created. Date and time format is ISO 8601 and UTC.
* `created_at` - Date when the entity was created. The date is in ISO 8601 and UTC.

* `org_id` - ID of organization that entity belongs to.
* `org_id` - The id of the organization this entity belongs to.

* `owner` - Email of entity owner.
* `owner` - Email of the user that owns the entity.

* `external_region_id` - External regionId of region.
* `external_region_id` - Unique identifier of region on the provider side.

* `id` - The ID of the given region within the cloud account.
* `id` - The id of this resource instance.

* `updated_at` - Date when entity was updated. Date and time format is ISO 8601 and UTC.
* `updated_at` - Date when the entity was last updated. The date is ISO 8601 and UTC.

0 comments on commit 25bb1d4

Please sign in to comment.