|
3 | 3 | package google |
4 | 4 |
|
5 | 5 | 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" |
11 | 7 | transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
12 | | - |
13 | | - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
14 | 8 | ) |
15 | 9 |
|
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) |
214 | 12 | } |
0 commit comments