/
label.go
204 lines (167 loc) · 5.84 KB
/
label.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
package provider
// Note: This file is automatically generated. Manually made changes
// will be overwritten when the provider is generated.
import (
"context"
"strconv"
"strings"
"github.com/paloaltonetworks/scm-go"
iYmUVvF "github.com/paloaltonetworks/scm-go/netsec/services/labels"
"github.com/hashicorp/terraform-plugin-framework/datasource"
dsschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Data source (listing).
var (
_ datasource.DataSource = &labelListDataSource{}
_ datasource.DataSourceWithConfigure = &labelListDataSource{}
)
func NewLabelListDataSource() datasource.DataSource {
return &labelListDataSource{}
}
type labelListDataSource struct {
client *scm.Client
}
// labelListDsModel is the model.
type labelListDsModel struct {
Tfid types.String `tfsdk:"tfid"`
// Input.
Limit types.Int64 `tfsdk:"limit"`
Name types.String `tfsdk:"name"`
Offset types.Int64 `tfsdk:"offset"`
// Output.
Data []labelListDsModel_fOGwhfV_Config `tfsdk:"data"`
// omit input: limit
// omit input: offset
Total types.Int64 `tfsdk:"total"`
}
type labelListDsModel_fOGwhfV_Config struct {
Description types.String `tfsdk:"description"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
// Metadata returns the data source type name.
func (d *labelListDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_label_list"
}
// Schema defines the schema for this listing data source.
func (d *labelListDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = dsschema.Schema{
Description: "Retrieves a listing of config items.",
Attributes: map[string]dsschema.Attribute{
// inputs:map[string]bool{"limit":true, "name":true, "offset":true} outputs:map[string]bool{"data":true, "limit":true, "offset":true, "tfid":true, "total":true} forceNew:map[string]bool{"limit":true, "name":true, "offset":true}
"data": dsschema.ListNestedAttribute{
Description: "The Data param.",
Computed: true,
NestedObject: dsschema.NestedAttributeObject{
Attributes: map[string]dsschema.Attribute{
// inputs:map[string]bool{} outputs:map[string]bool{"description":true, "id":true, "name":true} forceNew:map[string]bool(nil)
"description": dsschema.StringAttribute{
Description: "The Description param.",
Computed: true,
},
"id": dsschema.StringAttribute{
Description: "The Id param.",
Computed: true,
},
"name": dsschema.StringAttribute{
Description: "The Name param.",
Computed: true,
},
},
},
},
"limit": dsschema.Int64Attribute{
Description: "The Limit param. A limit of -1 will return all configured items. Default: `200`.",
Optional: true,
Computed: true,
},
"name": dsschema.StringAttribute{
Description: "The Name param.",
Optional: true,
},
"offset": dsschema.Int64Attribute{
Description: "The Offset param. Default: `0`.",
Optional: true,
Computed: true,
},
"tfid": dsschema.StringAttribute{
Description: "The Terraform ID.",
Computed: true,
},
"total": dsschema.Int64Attribute{
Description: "The Total param.",
Computed: true,
},
},
}
}
// Configure prepares the struct.
func (d *labelListDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*scm.Client)
}
// Read performs Read for the struct.
func (d *labelListDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state labelListDsModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// Basic logging.
tflog.Info(ctx, "performing data source listing", map[string]any{
"data_source_name": "scm_label_list",
"terraform_provider_function": "Read",
"name": state.Name.ValueString(),
"limit": state.Limit.ValueInt64(),
"offset": state.Offset.ValueInt64(),
})
// Prepare to run the command.
svc := iYmUVvF.NewClient(d.client)
// Prepare input for the API endpoint.
input := iYmUVvF.ListInput{}
input.Name = state.Name.ValueStringPointer()
input.Limit = state.Limit.ValueInt64Pointer()
input.Offset = state.Offset.ValueInt64Pointer()
// Perform the operation.
ans, err := svc.List(ctx, input)
if err != nil {
resp.Diagnostics.AddError("Error getting listing", err.Error())
return
}
// Create the Terraform ID.
var idBuilder strings.Builder
if input.Name != nil {
idBuilder.WriteString(*input.Name)
}
idBuilder.WriteString(IdSeparator)
if input.Limit != nil {
idBuilder.WriteString(strconv.FormatInt(*input.Limit, 10))
}
idBuilder.WriteString(IdSeparator)
if input.Offset != nil {
idBuilder.WriteString(strconv.FormatInt(*input.Offset, 10))
}
// Store the answer to state.
state.Tfid = types.StringValue(idBuilder.String())
if len(ans.Data) == 0 {
state.Data = nil
} else {
state.Data = make([]labelListDsModel_fOGwhfV_Config, 0, len(ans.Data))
for _, var0 := range ans.Data {
var1 := labelListDsModel_fOGwhfV_Config{}
var1.Description = types.StringPointerValue(var0.Description)
var1.Id = types.StringPointerValue(var0.Id)
var1.Name = types.StringValue(var0.Name)
state.Data = append(state.Data, var1)
}
}
state.Limit = types.Int64PointerValue(ans.Limit)
state.Offset = types.Int64PointerValue(ans.Offset)
state.Total = types.Int64PointerValue(ans.Total)
// Done.
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}