|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// ---------------------------------------------------------------------------- |
| 4 | +// |
| 5 | +// *** AUTO GENERATED CODE *** Type: Handwritten *** |
| 6 | +// |
| 7 | +// ---------------------------------------------------------------------------- |
| 8 | +// |
| 9 | +// This code is generated by Magic Modules using the following: |
| 10 | +// |
| 11 | +// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/compute/data_source_google_compute_interconnect_location.go |
| 12 | +// |
| 13 | +// DO NOT EDIT this file directly. Any changes made to this file will be |
| 14 | +// overwritten during the next generation cycle. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | +package compute |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "regexp" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 25 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 26 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + computeInterconnectLocationIdTemplate = "projects/%s/global/interconnectlocations/%s" |
| 31 | + computeInterconnectLocationLinkRegex = regexp.MustCompile(`projects/(.+)/global/interconnectlocations/(.+)$`) |
| 32 | +) |
| 33 | + |
| 34 | +type ComputeInterconnectLocationId struct { |
| 35 | + Project string |
| 36 | + Name string |
| 37 | +} |
| 38 | + |
| 39 | +func (s ComputeInterconnectLocationId) CanonicalId() string { |
| 40 | + return fmt.Sprintf(computeInterconnectLocationIdTemplate, s.Project, s.Name) |
| 41 | +} |
| 42 | + |
| 43 | +// ParseComputeInterconnectLocationId parses IDs of the form: |
| 44 | +// - projects/{project}/global/interconnectlocations/{name} |
| 45 | +// - {project}/{name} |
| 46 | +// - {name} (requires config.Project) |
| 47 | +func ParseComputeInterconnectLocationId(id string, config *transport_tpg.Config) (*ComputeInterconnectLocationId, error) { |
| 48 | + var parts []string |
| 49 | + if computeInterconnectLocationLinkRegex.MatchString(id) { |
| 50 | + parts = computeInterconnectLocationLinkRegex.FindStringSubmatch(id) |
| 51 | + return &ComputeInterconnectLocationId{ |
| 52 | + Project: parts[1], |
| 53 | + Name: parts[2], |
| 54 | + }, nil |
| 55 | + } else { |
| 56 | + parts = strings.Split(id, "/") |
| 57 | + } |
| 58 | + if len(parts) == 2 { |
| 59 | + return &ComputeInterconnectLocationId{ |
| 60 | + Project: parts[0], |
| 61 | + Name: parts[1], |
| 62 | + }, nil |
| 63 | + } else if len(parts) == 1 { |
| 64 | + if config.Project == "" { |
| 65 | + return nil, fmt.Errorf("The default project for the provider must be set when using the `{name}` id format.") |
| 66 | + } |
| 67 | + return &ComputeInterconnectLocationId{ |
| 68 | + Project: config.Project, |
| 69 | + Name: parts[0], |
| 70 | + }, nil |
| 71 | + } |
| 72 | + return nil, fmt.Errorf("Invalid interconnect location id. Expecting resource link, `{project}/{name}` or `{name}` format.") |
| 73 | +} |
| 74 | +func DataSourceGoogleComputeInterconnectLocation() *schema.Resource { |
| 75 | + return &schema.Resource{ |
| 76 | + Read: dataSourceGoogleComputeInterconnectLocationRead, |
| 77 | + Schema: map[string]*schema.Schema{ |
| 78 | + "name": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Required: true, |
| 81 | + }, |
| 82 | + "project": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Optional: true, |
| 85 | + Computed: true, |
| 86 | + }, |
| 87 | + "self_link": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "description": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "peeringdb_facility_id": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + "address": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + "facility_provider": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + }, |
| 107 | + "facility_provider_facility_id": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + }, |
| 111 | + "continent": { |
| 112 | + Type: schema.TypeString, |
| 113 | + Computed: true, |
| 114 | + }, |
| 115 | + "city": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Computed: true, |
| 118 | + }, |
| 119 | + "availability_zone": { |
| 120 | + Type: schema.TypeString, |
| 121 | + Computed: true, |
| 122 | + }, |
| 123 | + "status": { |
| 124 | + Type: schema.TypeString, |
| 125 | + Computed: true, |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | +} |
| 130 | +func dataSourceGoogleComputeInterconnectLocationRead(d *schema.ResourceData, meta interface{}) error { |
| 131 | + config := meta.(*transport_tpg.Config) |
| 132 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + project, err := tpgresource.GetProject(d, config) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + name := d.Get("name").(string) |
| 141 | + id := fmt.Sprintf("projects/%s/global/interconnectlocations/%s", project, name) |
| 142 | + location, err := config.NewComputeClient(userAgent).InterconnectLocations.Get(project, name).Do() |
| 143 | + if err != nil { |
| 144 | + return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("InterconnectLocation Not Found : %s", name), id) |
| 145 | + } |
| 146 | + d.SetId(location.Name) |
| 147 | + if err := d.Set("project", project); err != nil { |
| 148 | + return fmt.Errorf("Error setting project: %s", err) |
| 149 | + } |
| 150 | + if err := d.Set("self_link", location.SelfLink); err != nil { |
| 151 | + return fmt.Errorf("Error setting self_link: %s", err) |
| 152 | + } |
| 153 | + if err := d.Set("description", location.Description); err != nil { |
| 154 | + return fmt.Errorf("Error setting description: %s", err) |
| 155 | + } |
| 156 | + if err := d.Set("peeringdb_facility_id", location.PeeringdbFacilityId); err != nil { |
| 157 | + return fmt.Errorf("Error setting peeringdb_facility_id: %s", err) |
| 158 | + } |
| 159 | + if err := d.Set("address", location.Address); err != nil { |
| 160 | + return fmt.Errorf("Error setting address: %s", err) |
| 161 | + } |
| 162 | + if err := d.Set("facility_provider", location.FacilityProvider); err != nil { |
| 163 | + return fmt.Errorf("Error setting facility_provider: %s", err) |
| 164 | + } |
| 165 | + if err := d.Set("facility_provider_facility_id", location.FacilityProviderFacilityId); err != nil { |
| 166 | + return fmt.Errorf("Error setting facility_provider_facility_id: %s", err) |
| 167 | + } |
| 168 | + if err := d.Set("continent", location.Continent); err != nil { |
| 169 | + return fmt.Errorf("Error setting continent: %s", err) |
| 170 | + } |
| 171 | + if err := d.Set("city", location.City); err != nil { |
| 172 | + return fmt.Errorf("Error setting city: %s", err) |
| 173 | + } |
| 174 | + if err := d.Set("availability_zone", location.AvailabilityZone); err != nil { |
| 175 | + return fmt.Errorf("Error setting availability_zone: %s", err) |
| 176 | + } |
| 177 | + if err := d.Set("status", location.Status); err != nil { |
| 178 | + return fmt.Errorf("Error setting status: %s", err) |
| 179 | + } |
| 180 | + d.SetId(id) |
| 181 | + return nil |
| 182 | +} |
0 commit comments