/
data_source_load_balancer_backend_services.go
271 lines (238 loc) · 7.96 KB
/
data_source_load_balancer_backend_services.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package gcp
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
googleComputeClient "google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
var (
_ datasource.DataSource = &LbBackendServicesDataSource{}
_ datasource.DataSourceWithConfigure = &LbBackendServicesDataSource{}
)
// NewLbBackendServicesDataSource
func NewLbBackendServicesDataSource() datasource.DataSource {
return &LbBackendServicesDataSource{}
}
// LbBackendServicesDataSource
type LbBackendServicesDataSource struct {
project string
client *googleComputeClient.Service
}
// LbBackendServicesDataSourceModel
type LbBackendServicesDataSourceModel struct {
ClientConfig *clientConfig `tfsdk:"client_config"`
Name types.String `tfsdk:"name"`
Tags types.Map `tfsdk:"tags"`
Items []*lbBackendServicesItemModel `tfsdk:"items"`
}
type lbBackendServicesItemModel struct {
ID types.Int64 `tfsdk:"id"`
Tags types.Map `tfsdk:"tags"`
}
type clientConfig struct {
Project types.String `tfsdk:"project"`
Credentials types.String `tfsdk:"credentials"`
}
// Metadata returns the data source backend services type name.
func (d *LbBackendServicesDataSource) Metadata(_ context.Context,
req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_load_balancer_backend_services"
}
// Schema defines the schema for the backend services data source .
func (d *LbBackendServicesDataSource) Schema(_ context.Context,
_ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "This data source provides the load balancer backend services on Google Cloud.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Name of backend service to be filtered.",
Optional: true,
},
"tags": schema.MapAttribute{
Description: "Tags of backend service to be filtered.",
ElementType: types.StringType,
Optional: true,
},
"items": schema.ListNestedAttribute{
Description: "List of queried load balancer backend services.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Description: "ID of backend service.",
Computed: true,
},
"tags": schema.MapAttribute{
Description: "Tags of backend service.",
ElementType: types.StringType,
Computed: true,
},
},
},
},
},
Blocks: map[string]schema.Block{
"client_config": schema.SingleNestedBlock{
Description: "Config to override default client created in Provider. " +
"This block will not be recorded in state file.",
Attributes: map[string]schema.Attribute{
"project": schema.StringAttribute{
Description: "Project Name for Google Cloud API. Default " +
"to use project configured in the provider.",
Optional: true,
},
"credentials": schema.StringAttribute{
Description: "The credentials of service account in JSON format " +
" Default to use credentials configured in the provider.",
Optional: true,
Sensitive: true,
},
},
},
},
}
}
// Configure adds the provider configured client to the data source.
func (d *LbBackendServicesDataSource) Configure(_ context.Context,
req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.project = req.ProviderData.(*gcpClients).project
d.client = req.ProviderData.(*gcpClients).computeClient
}
// Read backend services data source information
func (d *LbBackendServicesDataSource) Read(ctx context.Context,
req datasource.ReadRequest, resp *datasource.ReadResponse) {
var plan *LbBackendServicesDataSourceModel
diags := req.Config.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if plan.ClientConfig == nil {
plan.ClientConfig = &clientConfig{}
}
initClient := false
project := plan.ClientConfig.Project.ValueString()
credentials := plan.ClientConfig.Credentials.ValueString()
if project != "" || credentials != "" {
initClient = true
}
if initClient {
err := d.initClient(ctx, project, credentials, resp)
if err != nil {
return
}
}
// Initialize input into state
state := &LbBackendServicesDataSourceModel{}
state.Items = []*lbBackendServicesItemModel{}
// Get list of backend services
// if backendService.Description != "" {
// Convert service description (tags) to Map
// Convert Map to types.Map
// If the input name is not empty, then compare the input name with
// the Google backend service name. Continue to next backend service
// if the name is not matched.
// If the input tag is not empty, then compare the input tag with
// the Google backend service tags (extracted from description).
// Tags comparison.
// If the key is not found or the tag value is not matched,
// then break the checking and continue to next backend service.
// }
err := d.runBackendServices(ctx, resp, plan, state)
if err != nil {
return
}
state.Name = plan.Name
state.Tags = plan.Tags
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (d *LbBackendServicesDataSource) runBackendServices(ctx context.Context,
resp *datasource.ReadResponse, plan *LbBackendServicesDataSourceModel,
state *LbBackendServicesDataSourceModel) error {
responseByList := d.client.BackendServices.List(d.project)
if err := responseByList.Pages(
ctx,
func(page *googleComputeClient.BackendServiceList) error {
for _, backendService := range page.Items {
slbTags := make(map[string]attr.Value)
slbTagsTfType := types.MapNull(types.StringType)
if backendService.Description != "" {
tags := strings.Split(backendService.Description, "|")
for _, tag := range tags {
t := strings.Split(tag, ":")
slbTags[t[0]] = types.StringValue(t[1])
}
var convertMapDiags diag.Diagnostics
slbTagsTfType, convertMapDiags = types.MapValue(types.StringType, slbTags)
resp.Diagnostics.Append(convertMapDiags...)
if resp.Diagnostics.HasError() {
return fmt.Errorf("[INTERNAL ERROR] Failed to convert description to tags")
}
}
serviceItem := &lbBackendServicesItemModel{
ID: types.Int64Value(int64(backendService.Id)),
Tags: slbTagsTfType,
}
if !(plan.Name.IsUnknown() || plan.Name.IsNull()) && plan.Name.ValueString() != backendService.Name {
continue
}
if !(plan.Tags.IsUnknown() || plan.Tags.IsNull()) {
matched := true
goInputMap := plan.Tags.Elements()
for inputKey, inputValue := range goInputMap {
value, ok := slbTags[inputKey]
if !ok || value != inputValue {
matched = false
break
}
}
if !matched {
continue
}
}
state.Items = append(state.Items, serviceItem)
}
return nil
},
); err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to list load balancer backend services.",
err.Error(),
)
return err
}
return nil
}
func (d *LbBackendServicesDataSource) initClient(ctx context.Context,
project string, credentials string, resp *datasource.ReadResponse) error {
if project != "" {
d.project = project
}
if credentials != "" {
googleClientOption := option.WithCredentialsJSON([]byte(credentials))
var err error
d.client, err = googleComputeClient.NewService(ctx, googleClientOption)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Reinitialize Google Cloud client",
"Please make sure the credentials is valid.\n"+
"Additional error message: "+err.Error(),
)
return err
}
}
return nil
}