generated from hashicorp/terraform-provider-scaffolding-framework
/
organizations_adaptive_policy_acls_data_source.go
213 lines (191 loc) · 7.63 KB
/
organizations_adaptive_policy_acls_data_source.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
package provider
import (
"context"
"encoding/json"
"fmt"
"github.com/core-infra-svcs/terraform-provider-meraki/internal/provider/jsontypes"
"github.com/core-infra-svcs/terraform-provider-meraki/tools"
"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 types fully satisfy framework interfaces
var _ datasource.DataSource = &OrganizationsAdaptivePolicyAclsDataSource{}
func NewOrganizationsAdaptivePolicyAclsDataSource() datasource.DataSource {
return &OrganizationsAdaptivePolicyAclsDataSource{}
}
// OrganizationsAdaptivePolicyAclsDataSource defines the data source implementation.
type OrganizationsAdaptivePolicyAclsDataSource struct {
client *openApiClient.APIClient
}
// OrganizationsAdaptivePolicyAclsDataSourceModel describes the data source data model.
type OrganizationsAdaptivePolicyAclsDataSourceModel struct {
Id jsontypes.String `tfsdk:"id"`
OrgId jsontypes.String `tfsdk:"organization_id"`
List []OrganizationsAdaptivePolicyAclsDataSourceModelList `tfsdk:"list"`
}
// OrganizationsAdaptivePolicyAclsDataSourceModelList describes the acl data source data model.
type OrganizationsAdaptivePolicyAclsDataSourceModelList struct {
AclId jsontypes.String `tfsdk:"acl_id" json:"AclId"`
Name jsontypes.String `tfsdk:"name"`
Description jsontypes.String `tfsdk:"description"`
IpVersion jsontypes.String `tfsdk:"ip_version" json:"IpVersion"`
Rules []OrganizationsAdaptivePolicyAclsDataSourceModelRules `tfsdk:"rules"`
CreatedAt jsontypes.String `tfsdk:"created_at" json:"createdAt"`
UpdatedAt jsontypes.String `tfsdk:"updated_at" json:"updatedAt"`
}
type OrganizationsAdaptivePolicyAclsDataSourceModelRules struct {
Policy jsontypes.String `tfsdk:"policy"`
Protocol jsontypes.String `tfsdk:"protocol"`
SrcPort jsontypes.String `tfsdk:"src_port" json:"srcPort"`
DstPort jsontypes.String `tfsdk:"dst_port" json:"dstPort"`
}
func (d *OrganizationsAdaptivePolicyAclsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_organizations_adaptive_policy_acls"
}
func (d *OrganizationsAdaptivePolicyAclsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "List adaptive policy ACLs in a organization",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
CustomType: jsontypes.StringType,
},
"organization_id": schema.StringAttribute{
MarkdownDescription: "Organization ID",
Optional: true,
CustomType: jsontypes.StringType,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 31),
},
},
"list": schema.ListNestedAttribute{
Optional: true,
Computed: true,
Description: "",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"acl_id": schema.StringAttribute{
MarkdownDescription: "ACL ID",
Optional: true,
CustomType: jsontypes.StringType,
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the adaptive policy ACL",
Optional: true,
CustomType: jsontypes.StringType,
},
"description": schema.StringAttribute{
MarkdownDescription: "Description of the adaptive policy ACL",
Optional: true,
CustomType: jsontypes.StringType,
},
"ip_version": schema.StringAttribute{
MarkdownDescription: "IP version of adaptive policy ACL. One of: 'any', 'ipv4' or 'ipv6",
Optional: true,
CustomType: jsontypes.StringType,
},
"rules": schema.ListNestedAttribute{
Description: "An ordered array of the adaptive policy ACL rules. An empty array will clear the rules.",
Optional: true,
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"policy": schema.StringAttribute{
MarkdownDescription: "",
Optional: true,
CustomType: jsontypes.StringType,
},
"protocol": schema.StringAttribute{
MarkdownDescription: "",
Optional: true,
CustomType: jsontypes.StringType,
},
"src_port": schema.StringAttribute{
MarkdownDescription: "",
Optional: true,
CustomType: jsontypes.StringType,
},
"dst_port": schema.StringAttribute{
MarkdownDescription: "",
Optional: true,
CustomType: jsontypes.StringType,
},
},
},
},
"created_at": schema.StringAttribute{
MarkdownDescription: "",
Optional: true,
CustomType: jsontypes.StringType,
},
"updated_at": schema.StringAttribute{
MarkdownDescription: "",
Optional: true,
CustomType: jsontypes.StringType,
},
},
},
},
},
}
}
func (d *OrganizationsAdaptivePolicyAclsDataSource) 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 *OrganizationsAdaptivePolicyAclsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data OrganizationsAdaptivePolicyAclsDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
_, httpResp, err := d.client.OrganizationsApi.GetOrganizationAdaptivePolicyAcls(context.Background(), data.OrgId.ValueString()).Execute()
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
data.Id = jsontypes.StringValue("example-id")
if err = json.NewDecoder(httpResp.Body).Decode(&data.List); err != nil {
resp.Diagnostics.AddError(
"JSON decoding error",
fmt.Sprintf("%v\n", err.Error()),
)
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
// Write logs using the tflog package
tflog.Trace(ctx, "read data source")
}