Skip to content

Commit 3cd6605

Browse files
Move resources to the service packages (#8126) (#5770)
Signed-off-by: Modular Magician <[email protected]>
1 parent 2da581c commit 3cd6605

File tree

61 files changed

+2917
-2771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2917
-2771
lines changed

.changelog/.txt

Whitespace-only changes.

google-beta/data_source_google_compute_address.go

Lines changed: 3 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -3,212 +3,10 @@
33
package google
44

55
import (
6-
"fmt"
7-
"regexp"
8-
"strings"
9-
10-
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
6+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/compute"
117
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
12-
13-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
148
)
159

16-
var (
17-
computeAddressIdTemplate = "projects/%s/regions/%s/addresses/%s"
18-
computeAddressLinkRegex = regexp.MustCompile("projects/(.+)/regions/(.+)/addresses/(.+)$")
19-
)
20-
21-
func DataSourceGoogleComputeAddress() *schema.Resource {
22-
return &schema.Resource{
23-
Read: dataSourceGoogleComputeAddressRead,
24-
25-
Schema: map[string]*schema.Schema{
26-
"name": {
27-
Type: schema.TypeString,
28-
Required: true,
29-
},
30-
31-
"address": {
32-
Type: schema.TypeString,
33-
Computed: true,
34-
},
35-
36-
"address_type": {
37-
Type: schema.TypeString,
38-
Computed: true,
39-
},
40-
41-
"network": {
42-
Type: schema.TypeString,
43-
Computed: true,
44-
},
45-
46-
"network_tier": {
47-
Type: schema.TypeString,
48-
Computed: true,
49-
},
50-
51-
"prefix_length": {
52-
Type: schema.TypeInt,
53-
Computed: true,
54-
},
55-
56-
"purpose": {
57-
Type: schema.TypeString,
58-
Computed: true,
59-
},
60-
61-
"subnetwork": {
62-
Type: schema.TypeString,
63-
Computed: true,
64-
},
65-
66-
"users": {
67-
Type: schema.TypeString,
68-
Computed: true,
69-
},
70-
71-
"status": {
72-
Type: schema.TypeString,
73-
Computed: true,
74-
},
75-
76-
"self_link": {
77-
Type: schema.TypeString,
78-
Computed: true,
79-
},
80-
81-
"region": {
82-
Type: schema.TypeString,
83-
Computed: true,
84-
Optional: true,
85-
},
86-
87-
"project": {
88-
Type: schema.TypeString,
89-
Computed: true,
90-
Optional: true,
91-
},
92-
},
93-
}
94-
}
95-
96-
func dataSourceGoogleComputeAddressRead(d *schema.ResourceData, meta interface{}) error {
97-
config := meta.(*transport_tpg.Config)
98-
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
99-
if err != nil {
100-
return err
101-
}
102-
103-
project, err := tpgresource.GetProject(d, config)
104-
if err != nil {
105-
return err
106-
}
107-
region, err := tpgresource.GetRegion(d, config)
108-
if err != nil {
109-
return err
110-
}
111-
name := d.Get("name").(string)
112-
113-
address, err := config.NewComputeClient(userAgent).Addresses.Get(project, region, name).Do()
114-
if err != nil {
115-
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Address Not Found : %s", name))
116-
}
117-
118-
if err := d.Set("address", address.Address); err != nil {
119-
return fmt.Errorf("Error setting address: %s", err)
120-
}
121-
if err := d.Set("address_type", address.AddressType); err != nil {
122-
return fmt.Errorf("Error setting address_type: %s", err)
123-
}
124-
if err := d.Set("network", address.Network); err != nil {
125-
return fmt.Errorf("Error setting network: %s", err)
126-
}
127-
if err := d.Set("network_tier", address.NetworkTier); err != nil {
128-
return fmt.Errorf("Error setting network_tier: %s", err)
129-
}
130-
if err := d.Set("prefix_length", address.PrefixLength); err != nil {
131-
return fmt.Errorf("Error setting prefix_length: %s", err)
132-
}
133-
if err := d.Set("purpose", address.Purpose); err != nil {
134-
return fmt.Errorf("Error setting purpose: %s", err)
135-
}
136-
if err := d.Set("subnetwork", address.Subnetwork); err != nil {
137-
return fmt.Errorf("Error setting subnetwork: %s", err)
138-
}
139-
if err := d.Set("status", address.Status); err != nil {
140-
return fmt.Errorf("Error setting status: %s", err)
141-
}
142-
if err := d.Set("self_link", address.SelfLink); err != nil {
143-
return fmt.Errorf("Error setting self_link: %s", err)
144-
}
145-
if err := d.Set("project", project); err != nil {
146-
return fmt.Errorf("Error setting project: %s", err)
147-
}
148-
if err := d.Set("region", region); err != nil {
149-
return fmt.Errorf("Error setting region: %s", err)
150-
}
151-
152-
d.SetId(fmt.Sprintf("projects/%s/regions/%s/addresses/%s", project, region, name))
153-
return nil
154-
}
155-
156-
type computeAddressId struct {
157-
Project string
158-
Region string
159-
Name string
160-
}
161-
162-
func (s computeAddressId) canonicalId() string {
163-
return fmt.Sprintf(computeAddressIdTemplate, s.Project, s.Region, s.Name)
164-
}
165-
166-
func parseComputeAddressId(id string, config *transport_tpg.Config) (*computeAddressId, error) {
167-
var parts []string
168-
if computeAddressLinkRegex.MatchString(id) {
169-
parts = computeAddressLinkRegex.FindStringSubmatch(id)
170-
171-
return &computeAddressId{
172-
Project: parts[1],
173-
Region: parts[2],
174-
Name: parts[3],
175-
}, nil
176-
} else {
177-
parts = strings.Split(id, "/")
178-
}
179-
180-
if len(parts) == 3 {
181-
return &computeAddressId{
182-
Project: parts[0],
183-
Region: parts[1],
184-
Name: parts[2],
185-
}, nil
186-
} else if len(parts) == 2 {
187-
// Project is optional.
188-
if config.Project == "" {
189-
return nil, fmt.Errorf("The default project for the provider must be set when using the `{region}/{name}` id format.")
190-
}
191-
192-
return &computeAddressId{
193-
Project: config.Project,
194-
Region: parts[0],
195-
Name: parts[1],
196-
}, nil
197-
} else if len(parts) == 1 {
198-
// Project and region is optional
199-
if config.Project == "" {
200-
return nil, fmt.Errorf("The default project for the provider must be set when using the `{name}` id format.")
201-
}
202-
if config.Region == "" {
203-
return nil, fmt.Errorf("The default region for the provider must be set when using the `{name}` id format.")
204-
}
205-
206-
return &computeAddressId{
207-
Project: config.Project,
208-
Region: config.Region,
209-
Name: parts[0],
210-
}, nil
211-
}
212-
213-
return nil, fmt.Errorf("Invalid compute address id. Expecting resource link, `{project}/{region}/{name}`, `{region}/{name}` or `{name}` format.")
10+
func parseComputeAddressId(id string, config *transport_tpg.Config) (*compute.ComputeAddressId, error) {
11+
return compute.ParseComputeAddressId(id, config)
21412
}

google-beta/data_source_google_compute_address_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/compute"
1011
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
1112
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
1213

@@ -56,7 +57,7 @@ func TestComputeAddressIdParsing(t *testing.T) {
5657
}
5758

5859
for tn, tc := range cases {
59-
addressId, err := parseComputeAddressId(tc.ImportId, tc.Config)
60+
addressId, err := compute.ParseComputeAddressId(tc.ImportId, tc.Config)
6061

6162
if tc.ExpectedError && err == nil {
6263
t.Fatalf("bad: %s, expected an error", tn)
@@ -69,8 +70,8 @@ func TestComputeAddressIdParsing(t *testing.T) {
6970
t.Fatalf("bad: %s, err: %#v", tn, err)
7071
}
7172

72-
if addressId.canonicalId() != tc.ExpectedCanonicalId {
73-
t.Fatalf("bad: %s, expected canonical id to be `%s` but is `%s`", tn, tc.ExpectedCanonicalId, addressId.canonicalId())
73+
if addressId.CanonicalId() != tc.ExpectedCanonicalId {
74+
t.Fatalf("bad: %s, expected canonical id to be `%s` but is `%s`", tn, tc.ExpectedCanonicalId, addressId.CanonicalId())
7475
}
7576
}
7677
}
@@ -156,7 +157,7 @@ func testAccCheckDataSourceComputeAddressDestroy(t *testing.T, name string) reso
156157

157158
config := GoogleProviderConfig(t)
158159

159-
addressId, err := parseComputeAddressId(rs.Primary.ID, nil)
160+
addressId, err := compute.ParseComputeAddressId(rs.Primary.ID, nil)
160161
if err != nil {
161162
return err
162163
}

0 commit comments

Comments
 (0)