generated from hashicorp/terraform-provider-scaffolding-framework
/
organizations_networks_datasource.go
297 lines (264 loc) · 10.8 KB
/
organizations_networks_datasource.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package provider
import (
"context"
"encoding/json"
"fmt"
"github.com/core-infra-svcs/terraform-provider-meraki/tools"
"strings"
"github.com/core-infra-svcs/terraform-provider-meraki/internal/provider/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-log/tflog"
openApiClient "github.com/meraki/dashboard-api-go/client"
)
// Ensure provider defined jsontypes fully satisfy framework interfaces
var _ datasource.DataSource = &OrganizationsNetworksDataSource{}
func NewOrganizationsNetworksDataSource() datasource.DataSource {
return &OrganizationsNetworksDataSource{}
}
type OrganizationsNetworksDataSourceModelTag string
func (t *OrganizationsNetworksDataSourceModelTag) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
*t = OrganizationsNetworksDataSourceModelTag(strings.Trim(s, `"`))
return nil
}
// OrganizationsNetworksDataSource defines the data source implementation.
type OrganizationsNetworksDataSource struct {
client *openApiClient.APIClient
}
// OrganizationsNetworksDataSourceModel describes the data source data model.
type OrganizationsNetworksDataSourceModel struct {
Id jsontypes.String `tfsdk:"id"`
OrgId jsontypes.String `tfsdk:"organization_id"`
ConfigTemplateId jsontypes.String `tfsdk:"config_template_id"`
IsBoundToConfigTemplate jsontypes.Bool `tfsdk:"is_bound_to_config_template"`
Tags jsontypes.Set[jsontypes.String] `tfsdk:"tags"`
TagsFilterType jsontypes.String `tfsdk:"tags_filter_type"`
List []OrganizationsNetworksDataSourceModelList `tfsdk:"list"`
}
type OrganizationsNetworksDataSourceModelList struct {
Id jsontypes.String `tfsdk:"network_id" json:"id"`
OrganizationId jsontypes.String `tfsdk:"organization_id" json:"organizationId"`
Name jsontypes.String `tfsdk:"name"`
ProductTypes []jsontypes.String `tfsdk:"product_types" json:"productTypes"`
TimeZone jsontypes.String `tfsdk:"timezone" json:"timeZone"`
Tags []jsontypes.String `tfsdk:"tags"`
EnrollmentString jsontypes.String `tfsdk:"enrollment_string" json:"enrollmentString"`
Url jsontypes.String `tfsdk:"url"`
Notes jsontypes.String `tfsdk:"notes"`
IsBoundToConfigTemplate jsontypes.Bool `tfsdk:"is_bound_to_config_template" json:"IsBoundToConfigTemplate"`
}
func (d *OrganizationsNetworksDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_organizations_networks"
}
func (d *OrganizationsNetworksDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
CustomType: jsontypes.StringType,
Computed: true,
},
"organization_id": schema.StringAttribute{
MarkdownDescription: "Organization ID",
CustomType: jsontypes.StringType,
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 31),
},
},
"config_template_id": schema.StringAttribute{
CustomType: jsontypes.StringType,
MarkdownDescription: "config_template_id",
Optional: true,
Computed: true,
},
"is_bound_to_config_template": schema.BoolAttribute{
CustomType: jsontypes.BoolType,
MarkdownDescription: "",
Optional: true,
Computed: true,
},
"tags": schema.SetAttribute{
CustomType: jsontypes.SetType[jsontypes.String](),
ElementType: jsontypes.StringType,
Description: "Network tags",
Computed: true,
Optional: true,
},
"tags_filter_type": schema.StringAttribute{
CustomType: jsontypes.StringType,
MarkdownDescription: "",
Optional: true,
Computed: true,
},
"list": schema.SetNestedAttribute{
Optional: true,
Computed: true,
Description: "",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"network_id": schema.StringAttribute{
MarkdownDescription: "Network ID",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 31),
},
},
"organization_id": schema.StringAttribute{
MarkdownDescription: "Organization ID",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 31),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Network name",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"product_types": schema.SetAttribute{
ElementType: jsontypes.StringType,
Optional: true,
Computed: true,
Validators: []validator.Set{
setvalidator.ValueStringsAre(
stringvalidator.OneOf([]string{"appliance", "switch", "wireless", "systemsManager", "camera", "cellularGateway", "sensor"}...),
stringvalidator.LengthAtLeast(5),
),
},
},
"timezone": schema.StringAttribute{
MarkdownDescription: "Timezone of the network",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"tags": schema.SetAttribute{
Description: "Network tags",
ElementType: jsontypes.StringType,
Computed: true,
Optional: true,
},
"enrollment_string": schema.StringAttribute{
MarkdownDescription: "A unique identifier which can be used for device enrollment or easy access through the Meraki SM Registration page or the Self Service Portal. Once enabled, a network enrollment strings can be changed but they cannot be deleted.",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"url": schema.StringAttribute{
MarkdownDescription: "URL to the network Dashboard UI",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"notes": schema.StringAttribute{
MarkdownDescription: "Notes for the network",
Optional: true,
Computed: true,
CustomType: jsontypes.StringType,
},
"is_bound_to_config_template": schema.BoolAttribute{
MarkdownDescription: "If the network is bound to a config template",
Optional: true,
Computed: true,
CustomType: jsontypes.BoolType,
},
},
},
},
},
}
}
func (d *OrganizationsNetworksDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*openApiClient.APIClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *OrganizationsNetworksDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data OrganizationsNetworksDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
/*
// Tags
tags := []string{""} // []string | An optional parameter to filter networks by tags. The filtering is case-sensitive. If tags are included, 'tagsFilterType' should also be included (see below). (optional)
if !data.Tags.IsUnknown() {
tags = append(tags, data.Tags.String())
}
// tagsFilterType
tagsFilterType := "" // string | An optional parameter of value 'withAnyTags' or 'withAllTags' to indicate whether to return networks which contain ANY or ALL of the included tags. If no type is included, 'withAnyTags' will be selected. (optional)
if !data.TagsFilterType.IsUnknown() {
tagsFilterType = data.TagsFilterType.ValueString()
}
// configTemplateId
configTemplateId := "" // string | An optional parameter that is the ID of a config template. Will return all networks bound to that template. (optional)
if !data.ConfigTemplateId.IsUnknown() {
configTemplateId = data.ConfigTemplateId.ValueString()
}
//
IsBoundToConfigTemplate := false // bool | An optional parameter to filter config template bound networks. If configTemplateId is set, this cannot be false. (optional)
if data.IsBoundToConfigTemplate.ValueBool() {
IsBoundToConfigTemplate = data.IsBoundToConfigTemplate.ValueBool()
}
*/
perPage := int32(100000) // int32 | The number of entries per page returned. Acceptable range is 3 - 100000. Default is 1000. (optional)
_, httpResp, err := d.client.OrganizationsApi.GetOrganizationNetworks(context.Background(),
data.OrgId.ValueString()).PerPage(perPage).Execute()
// .ConfigTemplateId(configTemplateId).IsBoundToConfigTemplate(IsBoundToConfigTemplate).Tags(tags).TagsFilterType(tagsFilterType)
if err != nil {
resp.Diagnostics.AddError(
"HTTP Client Failure",
tools.HttpDiagnostics(httpResp),
)
return
}
// Check for API success inlineResp code
if httpResp.StatusCode != 200 {
resp.Diagnostics.AddError(
"Unexpected HTTP Response Status Code",
fmt.Sprintf("%v", httpResp.StatusCode),
)
return
}
// Check for errors after diagnostics collected
if resp.Diagnostics.HasError() {
resp.Diagnostics.AddError("State Data", fmt.Sprintf("\n%v", data))
return
}
// Save data into Terraform state
if err = json.NewDecoder(httpResp.Body).Decode(&data.List); err != nil {
resp.Diagnostics.AddError(
"JSON decoding error",
fmt.Sprintf("%v\n", err.Error()),
)
return
}
data.Id = jsontypes.StringValue("example-id")
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
// Write logs using the tflog package
tflog.Trace(ctx, "read data source")
}