/
model_iosxe_community_list_standard.go
187 lines (172 loc) · 6.28 KB
/
model_iosxe_community_list_standard.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
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Mozilla Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://mozilla.org/MPL/2.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: MPL-2.0
// Code generated by "gen/generator.go"; DO NOT EDIT.
package provider
import (
"context"
"fmt"
"net/url"
"regexp"
"github.com/CiscoDevNet/terraform-provider-iosxe/internal/provider/helpers"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
type CommunityListStandard struct {
Device types.String `tfsdk:"device"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
DenyEntries types.List `tfsdk:"deny_entries"`
PermitEntries types.List `tfsdk:"permit_entries"`
}
type CommunityListStandardData struct {
Device types.String `tfsdk:"device"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
DenyEntries types.List `tfsdk:"deny_entries"`
PermitEntries types.List `tfsdk:"permit_entries"`
}
func (data CommunityListStandard) getPath() string {
return fmt.Sprintf("Cisco-IOS-XE-native:native/ip/Cisco-IOS-XE-bgp:community-list/standard=%v", url.QueryEscape(fmt.Sprintf("%v", data.Name.ValueString())))
}
func (data CommunityListStandardData) getPath() string {
return fmt.Sprintf("Cisco-IOS-XE-native:native/ip/Cisco-IOS-XE-bgp:community-list/standard=%v", url.QueryEscape(fmt.Sprintf("%v", data.Name.ValueString())))
}
// if last path element has a key -> remove it
func (data CommunityListStandard) getPathShort() string {
path := data.getPath()
re := regexp.MustCompile(`(.*)=[^\/]*$`)
matches := re.FindStringSubmatch(path)
if len(matches) <= 1 {
return path
}
return matches[1]
}
func (data CommunityListStandard) toBody(ctx context.Context) string {
body := `{"` + helpers.LastElement(data.getPath()) + `":{}}`
if !data.Name.IsNull() && !data.Name.IsUnknown() {
body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"name", data.Name.ValueString())
}
if !data.DenyEntries.IsNull() && !data.DenyEntries.IsUnknown() {
var values []string
data.DenyEntries.ElementsAs(ctx, &values, false)
body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"deny.deny-list", values)
}
if !data.PermitEntries.IsNull() && !data.PermitEntries.IsUnknown() {
var values []string
data.PermitEntries.ElementsAs(ctx, &values, false)
body, _ = sjson.Set(body, helpers.LastElement(data.getPath())+"."+"permit.permit-list", values)
}
return body
}
func (data *CommunityListStandard) updateFromBody(ctx context.Context, res gjson.Result) {
prefix := helpers.LastElement(data.getPath()) + "."
if res.Get(helpers.LastElement(data.getPath())).IsArray() {
prefix += "0."
}
if value := res.Get(prefix + "name"); value.Exists() && !data.Name.IsNull() {
data.Name = types.StringValue(value.String())
} else {
data.Name = types.StringNull()
}
if value := res.Get(prefix + "deny.deny-list"); value.Exists() && !data.DenyEntries.IsNull() {
data.DenyEntries = helpers.GetStringList(value.Array())
} else {
data.DenyEntries = types.ListNull(types.StringType)
}
if value := res.Get(prefix + "permit.permit-list"); value.Exists() && !data.PermitEntries.IsNull() {
data.PermitEntries = helpers.GetStringList(value.Array())
} else {
data.PermitEntries = types.ListNull(types.StringType)
}
}
func (data *CommunityListStandardData) fromBody(ctx context.Context, res gjson.Result) {
prefix := helpers.LastElement(data.getPath()) + "."
if res.Get(helpers.LastElement(data.getPath())).IsArray() {
prefix += "0."
}
if value := res.Get(prefix + "deny.deny-list"); value.Exists() {
data.DenyEntries = helpers.GetStringList(value.Array())
} else {
data.DenyEntries = types.ListNull(types.StringType)
}
if value := res.Get(prefix + "permit.permit-list"); value.Exists() {
data.PermitEntries = helpers.GetStringList(value.Array())
} else {
data.PermitEntries = types.ListNull(types.StringType)
}
}
func (data *CommunityListStandard) getDeletedItems(ctx context.Context, state CommunityListStandard) []string {
deletedItems := make([]string, 0)
if !state.DenyEntries.IsNull() {
if data.DenyEntries.IsNull() {
deletedItems = append(deletedItems, fmt.Sprintf("%v/deny/deny-list", state.getPath()))
} else {
var dataValues, stateValues []string
data.DenyEntries.ElementsAs(ctx, &dataValues, false)
state.DenyEntries.ElementsAs(ctx, &stateValues, false)
for _, v := range stateValues {
found := false
for _, vv := range dataValues {
if v == vv {
found = true
break
}
}
if !found {
deletedItems = append(deletedItems, fmt.Sprintf("%v/deny/deny-list=%v", state.getPath(), v))
}
}
}
}
if !state.PermitEntries.IsNull() {
if data.PermitEntries.IsNull() {
deletedItems = append(deletedItems, fmt.Sprintf("%v/permit/permit-list", state.getPath()))
} else {
var dataValues, stateValues []string
data.PermitEntries.ElementsAs(ctx, &dataValues, false)
state.PermitEntries.ElementsAs(ctx, &stateValues, false)
for _, v := range stateValues {
found := false
for _, vv := range dataValues {
if v == vv {
found = true
break
}
}
if !found {
deletedItems = append(deletedItems, fmt.Sprintf("%v/permit/permit-list=%v", state.getPath(), v))
}
}
}
}
return deletedItems
}
func (data *CommunityListStandard) getEmptyLeafsDelete(ctx context.Context) []string {
emptyLeafsDelete := make([]string, 0)
return emptyLeafsDelete
}
func (data *CommunityListStandard) getDeletePaths(ctx context.Context) []string {
var deletePaths []string
if !data.DenyEntries.IsNull() {
deletePaths = append(deletePaths, fmt.Sprintf("%v/deny/deny-list", data.getPath()))
}
if !data.PermitEntries.IsNull() {
deletePaths = append(deletePaths, fmt.Sprintf("%v/permit/permit-list", data.getPath()))
}
return deletePaths
}