forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new datasource for fetching the available alloydb locations (G…
…oogleCloudPlatform#7678) * Added validation for "type" in cloud_sql_user_resource for preventing user from setting "password" or "host" for CLOUD_IAM_USER and CLOUD_IAM_SERVICE_ACCOUNT user types. * Removed validation and added documentation to prevent setting of host or password field for CLOUD_IAM_USER and CLOUD_IAM_SERVICE_ACCOUNT * Added a new data source for fetching the details of available alloydb locations * using go client libraries to call the apis * using go client library to call the apis * using go client library to call the apis * Using v1 and v1beta go client for ga and beta versions respectively * Using v1 and v1beta go client for ga and beta versions respectively * Revert "Added a new data source for fetching the details of available alloydb locations" This reverts commit a6791a3. * calling http calls using magic modules send request insted of alloydb go client * calling http calls using magic modules send request insted of alloydb go client
- Loading branch information
1 parent
b6ce2b6
commit d805856
Showing
4 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
mmv1/third_party/terraform/data_sources/data_source_alloydb_locations.go
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,140 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceAlloydbLocations() *schema.Resource { | ||
|
||
return &schema.Resource{ | ||
Read: dataSourceAlloydbLocationsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: `Project ID of the project.`, | ||
}, | ||
"locations": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
Description: `Resource name for the location, which may vary between implementations. For example: "projects/example-project/locations/us-east1`, | ||
}, | ||
"location_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
Description: `The canonical id for this location. For example: "us-east1".`, | ||
}, | ||
"display_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
Description: `The friendly name for this location, typically a nearby city name. For example, "Tokyo".`, | ||
}, | ||
"labels": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Optional: true, | ||
Description: `Cross-service attributes for the location. For example {"cloud.googleapis.com/region": "us-east1"}`, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"metadata": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Optional: true, | ||
Description: `Service-specific metadata. For example the available capacity at the given location.`, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAlloydbLocationsRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
userAgent, err := generateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
billingProject := "" | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching project: %s", err) | ||
} | ||
billingProject = project | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := getBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
url, err := ReplaceVars(d, config, "{{AlloydbBasePath}}projects/{{project}}/locations") | ||
if err != nil { | ||
return fmt.Errorf("Error setting api endpoint") | ||
} | ||
res, err := SendRequest(config, "GET", billingProject, url, userAgent, nil) | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Locations %q", d.Id())) | ||
} | ||
var locations []map[string]interface{} | ||
for { | ||
fetchedLocations := res["locations"].([]interface{}) | ||
for _, loc := range fetchedLocations { | ||
locationDetails := make(map[string]interface{}) | ||
l := loc.(map[string]interface{}) | ||
if l["name"] != nil { | ||
locationDetails["name"] = l["name"].(string) | ||
} | ||
if l["locationId"] != nil { | ||
locationDetails["location_id"] = l["locationId"].(string) | ||
} | ||
if l["displayName"] != nil { | ||
locationDetails["display_id"] = l["displayName"].(string) | ||
} | ||
if l["labels"] != nil { | ||
labels := make(map[string]string) | ||
for k, v := range l["labels"].(map[string]interface{}) { | ||
labels[k] = v.(string) | ||
} | ||
locationDetails["labels"] = labels | ||
} | ||
if l["metadata"] != nil { | ||
metadata := make(map[string]string) | ||
for k, v := range l["metadata"].(map[interface{}]interface{}) { | ||
metadata[k.(string)] = v.(string) | ||
} | ||
locationDetails["metadata"] = metadata | ||
} | ||
locations = append(locations, locationDetails) | ||
} | ||
if res["nextPageToken"] == nil || res["nextPageToken"].(string) == "" { | ||
break | ||
} | ||
url, err = ReplaceVars(d, config, "{{AlloydbBasePath}}projects/{{project}}/locations?pageToken="+res["nextPageToken"].(string)) | ||
if err != nil { | ||
return fmt.Errorf("Error setting api endpoint") | ||
} | ||
res, err = SendRequest(config, "GET", billingProject, url, userAgent, nil) | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Locations %q", d.Id())) | ||
} | ||
} | ||
|
||
if err := d.Set("locations", locations); err != nil { | ||
return fmt.Errorf("Error setting locations: %s", err) | ||
} | ||
d.SetId(fmt.Sprintf("projects/%s/locations", project)) | ||
return nil | ||
} |
70 changes: 70 additions & 0 deletions
70
mmv1/third_party/terraform/tests/data_source_alloydb_locations_test.go
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 google | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccDataSourceAlloydbLocations_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"random_suffix": RandString(t, 10), | ||
} | ||
|
||
VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { AccTestPreCheck(t) }, | ||
Providers: TestAccProviders, | ||
CheckDestroy: testAccSqlDatabaseDestroyProducer(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAlloydbLocations_basic(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
validateAlloydbLocationsResult( | ||
"data.google_alloydb_locations.qa", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAlloydbLocations_basic(context map[string]interface{}) string { | ||
return Nprintf(` | ||
data "google_alloydb_locations" "qa" { | ||
} | ||
`, context) | ||
} | ||
|
||
func validateAlloydbLocationsResult(dataSourceName string) func(*terraform.State) error { | ||
return func(s *terraform.State) error { | ||
ds, ok := s.RootModule().Resources[dataSourceName] | ||
if !ok { | ||
return fmt.Errorf("can't find %s in state", dataSourceName) | ||
} | ||
var dsAttr map[string]string | ||
dsAttr = ds.Primary.Attributes | ||
|
||
totalFlags, err := strconv.Atoi(dsAttr["locations.#"]) | ||
if err != nil { | ||
return errors.New("Couldn't convert length of flags list to integer") | ||
} | ||
if totalFlags == 0 { | ||
return errors.New("No locations are fetched") | ||
} | ||
for i := 0; i < totalFlags; i++ { | ||
if dsAttr["locations."+strconv.Itoa(i)+".name"] == "" { | ||
return errors.New("name parameter is not set for the location") | ||
} | ||
if dsAttr["locations."+strconv.Itoa(i)+".location_id"] == "" { | ||
return errors.New("location_id parameter is not set for the location") | ||
} | ||
} | ||
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
41 changes: 41 additions & 0 deletions
41
mmv1/third_party/terraform/website/docs/d/alloydb_locations.html.markdown
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,41 @@ | ||
--- | ||
subcategory: "Alloydb" | ||
description: |- | ||
Fetches the details of available locations. | ||
--- | ||
|
||
# google\_alloydb\_locations | ||
|
||
Use this data source to get information about the available locations. For more details refer the [API docs](https://cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations). | ||
|
||
## Example Usage | ||
|
||
|
||
```hcl | ||
data "google_alloydb_locations" "qa" { | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `project` - (optional) The ID of the project. | ||
|
||
## Attributes Reference | ||
|
||
In addition to the arguments listed above, the following computed attributes are exported: | ||
|
||
* `locations` - Contains a list of `location`, which contains the details about a particular location. | ||
|
||
A `location` object would contain the following fields:- | ||
|
||
* `name` - Resource name for the location, which may vary between implementations. For example: "projects/example-project/locations/us-east1". | ||
|
||
* `location_id` - The canonical id for this location. For example: "us-east1".. | ||
|
||
* `display_name` - The friendly name for this location, typically a nearby city name. For example, "Tokyo". | ||
|
||
* `labels` - Cross-service attributes for the location. For example `{"cloud.googleapis.com/region": "us-east1"}`. | ||
|
||
* `metadata` - Service-specific metadata. For example the available capacity at the given location. |