forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_google_compute_addresses.go
187 lines (171 loc) · 5.72 KB
/
data_source_google_compute_addresses.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
package google
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/compute/v1"
)
func dataSourceGoogleComputeAddresses() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGoogleComputeAddressesRead,
Schema: map[string]*schema.Schema{
"addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the IP address.`,
},
"address": {
Type: schema.TypeString,
Computed: true,
Description: `The IP address.`,
},
"address_type": {
Type: schema.TypeString,
Computed: true,
Description: `The IP address type.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"self_link": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"filter": {
Type: schema.TypeString,
Description: `Filter sets the optional parameter "filter": A filter expression that
filters resources listed in the response. The expression must specify
the field name, an operator, and the value that you want to use for
filtering. The value must be a string, a number, or a boolean. The
operator must be either "=", "!=", ">", "<", "<=", ">=" or ":". For
example, if you are filtering Compute Engine instances, you can
exclude instances named "example-instance" by specifying "name !=
example-instance". The ":" operator can be used with string fields to
match substrings. For non-string fields it is equivalent to the "="
operator. The ":*" comparison can be used to test whether a key has
been defined. For example, to find all objects with "owner" label
use: """ labels.owner:* """ You can also filter nested fields. For
example, you could specify "scheduling.automaticRestart = false" to
include instances only if they are not scheduled for automatic
restarts. You can use filtering on nested fields to filter based on
resource labels. To filter on multiple expressions, provide each
separate expression within parentheses. For example: """
(scheduling.automaticRestart = true) (cpuPlatform = "Intel Skylake")
""" By default, each expression is an "AND" expression. However, you
can include "AND" and "OR" expressions explicitly. For example: """
(cpuPlatform = "Intel Skylake") OR (cpuPlatform = "Intel Broadwell")
AND (scheduling.automaticRestart = true) """`,
Optional: true,
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: `Region that should be considered to search addresses. All regions are considered if missing.`,
},
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `The google project in which addresses are listed. Defaults to provider's configuration if missing.`,
},
},
}
}
func dataSourceGoogleComputeAddressesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return diag.FromErr(err)
}
project, err := getProject(d, config)
if err != nil {
return diag.FromErr(err)
}
allAddresses := make([]map[string]interface{}, 0)
client := config.NewComputeClient(userAgent).Addresses
if region, has_region := d.GetOk("region"); has_region {
request := client.List(project, region.(string))
if filter, has_filter := d.GetOk("filter"); has_filter {
request = request.Filter(filter.(string))
}
err = request.Pages(context, func(addresses *compute.AddressList) error {
for _, address := range addresses.Items {
allAddresses = append(allAddresses, generateTfAddress(address))
}
return nil
})
} else {
request := client.AggregatedList(project)
if filter, has_filter := d.GetOk("filter"); has_filter {
request = request.Filter(filter.(string))
}
err = request.Pages(context, func(addresses *compute.AddressAggregatedList) error {
for _, items := range addresses.Items {
for _, address := range items.Addresses {
allAddresses = append(allAddresses, generateTfAddress(address))
}
}
return nil
})
}
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("addresses", allAddresses); err != nil {
return diag.FromErr(fmt.Errorf("error setting addresses: %s", err))
}
if err := d.Set("project", project); err != nil {
return diag.FromErr(fmt.Errorf("error setting project: %s", err))
}
d.SetId(computeId(project, d))
return nil
}
func generateTfAddress(address *compute.Address) map[string]interface{} {
return map[string]interface{}{
"name": address.Name,
"address": address.Address,
"address_type": address.AddressType,
"description": address.Description,
"region": regionFromUrl(address.Region),
"status": address.Status,
"self_link": address.SelfLink,
}
}
func computeId(project string, d *schema.ResourceData) string {
region := "ALL"
filter := "ALL"
if p_region, has_region := d.GetOk("region"); has_region {
region = p_region.(string)
}
if p_filter, has_filter := d.GetOk("filter"); has_filter {
filter = p_filter.(string)
}
return fmt.Sprintf("%s-%s-%s", project, region, filter)
}
func regionFromUrl(url string) string {
parts := strings.Split(url, "/")
if count := len(parts); count > 0 {
return parts[count-1]
}
return ""
}