Skip to content

Commit e194221

Browse files
Add Data Source Interconnect Location (#15065) (#10727)
[upstream:958af83dc34c3f8b48e0b43729211aa1803a3e57] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 62101a8 commit e194221

8 files changed

+626
-0
lines changed

.changelog/15065.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_compute_interconnect_location`
3+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ var handwrittenDatasources = map[string]*schema.Resource{
276276
"google_compute_instance_serial_port": compute.DataSourceGoogleComputeInstanceSerialPort(),
277277
"google_compute_instance_template": compute.DataSourceGoogleComputeInstanceTemplate(),
278278
"google_compute_instance_guest_attributes": compute.DataSourceGoogleComputeInstanceGuestAttributes(),
279+
"google_compute_interconnect_location": compute.DataSourceGoogleComputeInterconnectLocation(),
280+
"google_compute_interconnect_locations": compute.DataSourceGoogleComputeInterconnectLocations(),
279281
"google_compute_lb_ip_ranges": compute.DataSourceGoogleComputeLbIpRanges(),
280282
"google_compute_machine_types": compute.DataSourceGoogleComputeMachineTypes(),
281283
"google_compute_network": compute.DataSourceGoogleComputeNetwork(),
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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_test.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_test
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
24+
"github.com/hashicorp/terraform-plugin-testing/terraform"
25+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
26+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/compute"
27+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
28+
)
29+
30+
var interconnectLoc = "z2z-us-west8-zone2-ncphxk-z"
31+
32+
func testAccDataSourceCheckInterconnectLocation() func(s *terraform.State) error {
33+
return func(s *terraform.State) error {
34+
data_source_name := "data.google_compute_interconnect_location.my_location"
35+
ds, ok := s.RootModule().Resources[data_source_name]
36+
if !ok {
37+
return fmt.Errorf("root module has no resource called %s", data_source_name)
38+
}
39+
ds_attr := ds.Primary.Attributes
40+
expected := map[string]string{
41+
"name": interconnectLoc,
42+
"description": "Zakim-to-Zakim location",
43+
"facility_provider": "Google",
44+
}
45+
for attr, expect_value := range expected {
46+
if ds_attr[attr] != expect_value {
47+
return fmt.Errorf("%s is %s; want %s", attr, ds_attr[attr], expect_value)
48+
}
49+
}
50+
if ds_attr["self_link"] == "" {
51+
return fmt.Errorf("self_link is not set")
52+
}
53+
return nil
54+
}
55+
}
56+
func TestAccDataSourceGoogleComputeInterconnectLocation_basic(t *testing.T) {
57+
acctest.VcrTest(t, resource.TestCase{
58+
PreCheck: func() { acctest.AccTestPreCheck(t) },
59+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
60+
Steps: []resource.TestStep{
61+
{
62+
Config: testAccDataSourceGoogleComputeInterconnectLocationConfig(interconnectLoc),
63+
Check: resource.ComposeTestCheckFunc(
64+
testAccDataSourceCheckInterconnectLocation(),
65+
),
66+
},
67+
},
68+
})
69+
}
70+
func testAccDataSourceGoogleComputeInterconnectLocationConfig(locationName string) string {
71+
return fmt.Sprintf(`
72+
data "google_compute_interconnect_location" "my_location" {
73+
name = "%s"
74+
}
75+
`, locationName)
76+
}
77+
func TestParseComputeInterconnectLocationId(t *testing.T) {
78+
config := &transport.Config{Project: "my-project"}
79+
cases := []struct {
80+
id string
81+
wantProj string
82+
wantName string
83+
wantErr bool
84+
}{
85+
{"projects/my-project/global/interconnectlocations/z2z-us-west8-zone2-ncphxk-z", "my-project", interconnectLoc, false},
86+
{"my-project/z2z-us-west8-zone2-ncphxk-z", "my-project", interconnectLoc, false},
87+
{interconnectLoc, "my-project", interconnectLoc, false},
88+
{"invalid/format/extra", "", "", true},
89+
}
90+
for _, tc := range cases {
91+
got, err := compute.ParseComputeInterconnectLocationId(tc.id, config)
92+
if tc.wantErr {
93+
if err == nil {
94+
t.Errorf("ParseComputeInterconnectLocationId(%q) expected error, got nil", tc.id)
95+
}
96+
continue
97+
}
98+
if err != nil {
99+
t.Errorf("ParseComputeInterconnectLocationId(%q) unexpected error: %v", tc.id, err)
100+
continue
101+
}
102+
if got.Project != tc.wantProj || got.Name != tc.wantName {
103+
t.Errorf("ParseComputeInterconnectLocationId(%q) = (%q, %q), want (%q, %q)", tc.id, got.Project, got.Name, tc.wantProj, tc.wantName)
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)