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