|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +func DataSourceAlloydbLocations() *schema.Resource { |
| 10 | + |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceAlloydbLocationsRead, |
| 13 | + |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "project": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Optional: true, |
| 18 | + Description: `Project ID of the project.`, |
| 19 | + }, |
| 20 | + "locations": { |
| 21 | + Type: schema.TypeList, |
| 22 | + Computed: true, |
| 23 | + Elem: &schema.Resource{ |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "name": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + Optional: true, |
| 29 | + Description: `Resource name for the location, which may vary between implementations. For example: "projects/example-project/locations/us-east1`, |
| 30 | + }, |
| 31 | + "location_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + Optional: true, |
| 35 | + Description: `The canonical id for this location. For example: "us-east1".`, |
| 36 | + }, |
| 37 | + "display_name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + Optional: true, |
| 41 | + Description: `The friendly name for this location, typically a nearby city name. For example, "Tokyo".`, |
| 42 | + }, |
| 43 | + "labels": { |
| 44 | + Type: schema.TypeMap, |
| 45 | + Computed: true, |
| 46 | + Optional: true, |
| 47 | + Description: `Cross-service attributes for the location. For example {"cloud.googleapis.com/region": "us-east1"}`, |
| 48 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 49 | + }, |
| 50 | + "metadata": { |
| 51 | + Type: schema.TypeMap, |
| 52 | + Computed: true, |
| 53 | + Optional: true, |
| 54 | + Description: `Service-specific metadata. For example the available capacity at the given location.`, |
| 55 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func dataSourceAlloydbLocationsRead(d *schema.ResourceData, meta interface{}) error { |
| 65 | + config := meta.(*Config) |
| 66 | + userAgent, err := generateUserAgentString(d, config.UserAgent) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + billingProject := "" |
| 71 | + |
| 72 | + project, err := getProject(d, config) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("Error fetching project: %s", err) |
| 75 | + } |
| 76 | + billingProject = project |
| 77 | + |
| 78 | + // err == nil indicates that the billing_project value was found |
| 79 | + if bp, err := getBillingProject(d, config); err == nil { |
| 80 | + billingProject = bp |
| 81 | + } |
| 82 | + |
| 83 | + url, err := ReplaceVars(d, config, "{{AlloydbBasePath}}projects/{{project}}/locations") |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("Error setting api endpoint") |
| 86 | + } |
| 87 | + res, err := SendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 88 | + if err != nil { |
| 89 | + return handleNotFoundError(err, d, fmt.Sprintf("Locations %q", d.Id())) |
| 90 | + } |
| 91 | + var locations []map[string]interface{} |
| 92 | + for { |
| 93 | + fetchedLocations := res["locations"].([]interface{}) |
| 94 | + for _, loc := range fetchedLocations { |
| 95 | + locationDetails := make(map[string]interface{}) |
| 96 | + l := loc.(map[string]interface{}) |
| 97 | + if l["name"] != nil { |
| 98 | + locationDetails["name"] = l["name"].(string) |
| 99 | + } |
| 100 | + if l["locationId"] != nil { |
| 101 | + locationDetails["location_id"] = l["locationId"].(string) |
| 102 | + } |
| 103 | + if l["displayName"] != nil { |
| 104 | + locationDetails["display_id"] = l["displayName"].(string) |
| 105 | + } |
| 106 | + if l["labels"] != nil { |
| 107 | + labels := make(map[string]string) |
| 108 | + for k, v := range l["labels"].(map[string]interface{}) { |
| 109 | + labels[k] = v.(string) |
| 110 | + } |
| 111 | + locationDetails["labels"] = labels |
| 112 | + } |
| 113 | + if l["metadata"] != nil { |
| 114 | + metadata := make(map[string]string) |
| 115 | + for k, v := range l["metadata"].(map[interface{}]interface{}) { |
| 116 | + metadata[k.(string)] = v.(string) |
| 117 | + } |
| 118 | + locationDetails["metadata"] = metadata |
| 119 | + } |
| 120 | + locations = append(locations, locationDetails) |
| 121 | + } |
| 122 | + if res["nextPageToken"] == nil || res["nextPageToken"].(string) == "" { |
| 123 | + break |
| 124 | + } |
| 125 | + url, err = ReplaceVars(d, config, "{{AlloydbBasePath}}projects/{{project}}/locations?pageToken="+res["nextPageToken"].(string)) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("Error setting api endpoint") |
| 128 | + } |
| 129 | + res, err = SendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 130 | + if err != nil { |
| 131 | + return handleNotFoundError(err, d, fmt.Sprintf("Locations %q", d.Id())) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if err := d.Set("locations", locations); err != nil { |
| 136 | + return fmt.Errorf("Error setting locations: %s", err) |
| 137 | + } |
| 138 | + d.SetId(fmt.Sprintf("projects/%s/locations", project)) |
| 139 | + return nil |
| 140 | +} |
0 commit comments