diff --git a/vra/data_source_region.go b/vra/data_source_region.go index 6665e9bf..df054318 100644 --- a/vra/data_source_region.go +++ b/vra/data_source_region.go @@ -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" @@ -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.", }, }, } @@ -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) @@ -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) diff --git a/website/docs/d/vra_region.html.markdown b/website/docs/d/vra_region.html.markdown index 8f002673..817f30d8 100644 --- a/website/docs/d/vra_region.html.markdown +++ b/website/docs/d/vra_region.html.markdown @@ -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.