This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
/
model_sdwan_qos_map_policy_definition.go
188 lines (177 loc) · 5.98 KB
/
model_sdwan_qos_map_policy_definition.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
// Code generated by "gen/generator.go"; DO NOT EDIT.
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
type QoSMap struct {
Id types.String `tfsdk:"id"`
Version types.Int64 `tfsdk:"version"`
Type types.String `tfsdk:"type"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
QosSchedulers []QoSMapQosSchedulers `tfsdk:"qos_schedulers"`
}
type QoSMapQosSchedulers struct {
Queue types.Int64 `tfsdk:"queue"`
ClassMapId types.String `tfsdk:"class_map_id"`
ClassMapVersion types.Int64 `tfsdk:"class_map_version"`
BandwidthPercent types.Int64 `tfsdk:"bandwidth_percent"`
BufferPercent types.Int64 `tfsdk:"buffer_percent"`
Burst types.Int64 `tfsdk:"burst"`
DropType types.String `tfsdk:"drop_type"`
SchedulingType types.String `tfsdk:"scheduling_type"`
}
func (data QoSMap) getType() string {
return "qosMap"
}
func (data QoSMap) toBody(ctx context.Context) string {
body, _ := sjson.Set("", "name", data.Name.ValueString())
body, _ = sjson.Set(body, "description", data.Description.ValueString())
body, _ = sjson.Set(body, "type", "qosMap")
path := "definition."
if len(data.QosSchedulers) > 0 {
body, _ = sjson.Set(body, path+"qosSchedulers", []interface{}{})
for _, item := range data.QosSchedulers {
itemBody := ""
if !item.Queue.IsNull() {
itemBody, _ = sjson.Set(itemBody, "queue", fmt.Sprint(item.Queue.ValueInt64()))
}
if !item.ClassMapId.IsNull() {
itemBody, _ = sjson.Set(itemBody, "classMapRef", item.ClassMapId.ValueString())
}
if !item.BandwidthPercent.IsNull() {
itemBody, _ = sjson.Set(itemBody, "bandwidthPercent", fmt.Sprint(item.BandwidthPercent.ValueInt64()))
}
if !item.BufferPercent.IsNull() {
itemBody, _ = sjson.Set(itemBody, "bufferPercent", fmt.Sprint(item.BufferPercent.ValueInt64()))
}
if !item.Burst.IsNull() {
itemBody, _ = sjson.Set(itemBody, "burst", fmt.Sprint(item.Burst.ValueInt64()))
}
if !item.DropType.IsNull() {
itemBody, _ = sjson.Set(itemBody, "drops", item.DropType.ValueString())
}
if !item.SchedulingType.IsNull() {
itemBody, _ = sjson.Set(itemBody, "scheduling", item.SchedulingType.ValueString())
}
body, _ = sjson.SetRaw(body, path+"qosSchedulers.-1", itemBody)
}
}
return body
}
func (data *QoSMap) fromBody(ctx context.Context, res gjson.Result) {
if value := res.Get("name"); value.Exists() {
data.Name = types.StringValue(value.String())
} else {
data.Name = types.StringNull()
}
if value := res.Get("description"); value.Exists() {
data.Description = types.StringValue(value.String())
} else {
data.Description = types.StringNull()
}
if value := res.Get("type"); value.Exists() {
data.Type = types.StringValue(value.String())
} else {
data.Type = types.StringNull()
}
path := "definition."
if value := res.Get(path + "qosSchedulers"); value.Exists() {
data.QosSchedulers = make([]QoSMapQosSchedulers, 0)
value.ForEach(func(k, v gjson.Result) bool {
item := QoSMapQosSchedulers{}
if cValue := v.Get("queue"); cValue.Exists() {
item.Queue = types.Int64Value(cValue.Int())
} else {
item.Queue = types.Int64Null()
}
if cValue := v.Get("classMapRef"); cValue.Exists() {
item.ClassMapId = types.StringValue(cValue.String())
} else {
item.ClassMapId = types.StringNull()
}
if cValue := v.Get("bandwidthPercent"); cValue.Exists() {
item.BandwidthPercent = types.Int64Value(cValue.Int())
} else {
item.BandwidthPercent = types.Int64Null()
}
if cValue := v.Get("bufferPercent"); cValue.Exists() {
item.BufferPercent = types.Int64Value(cValue.Int())
} else {
item.BufferPercent = types.Int64Null()
}
if cValue := v.Get("burst"); cValue.Exists() {
item.Burst = types.Int64Value(cValue.Int())
} else {
item.Burst = types.Int64Null()
}
if cValue := v.Get("drops"); cValue.Exists() {
item.DropType = types.StringValue(cValue.String())
} else {
item.DropType = types.StringNull()
}
if cValue := v.Get("scheduling"); cValue.Exists() {
item.SchedulingType = types.StringValue(cValue.String())
} else {
item.SchedulingType = types.StringNull()
}
data.QosSchedulers = append(data.QosSchedulers, item)
return true
})
}
}
func (data *QoSMap) hasChanges(ctx context.Context, state *QoSMap) bool {
hasChanges := false
if !data.Name.Equal(state.Name) {
hasChanges = true
}
if !data.Description.Equal(state.Description) {
hasChanges = true
}
if len(data.QosSchedulers) != len(state.QosSchedulers) {
hasChanges = true
} else {
for i := range data.QosSchedulers {
if !data.QosSchedulers[i].Queue.Equal(state.QosSchedulers[i].Queue) {
hasChanges = true
}
if !data.QosSchedulers[i].ClassMapId.Equal(state.QosSchedulers[i].ClassMapId) {
hasChanges = true
}
if !data.QosSchedulers[i].BandwidthPercent.Equal(state.QosSchedulers[i].BandwidthPercent) {
hasChanges = true
}
if !data.QosSchedulers[i].BufferPercent.Equal(state.QosSchedulers[i].BufferPercent) {
hasChanges = true
}
if !data.QosSchedulers[i].Burst.Equal(state.QosSchedulers[i].Burst) {
hasChanges = true
}
if !data.QosSchedulers[i].DropType.Equal(state.QosSchedulers[i].DropType) {
hasChanges = true
}
if !data.QosSchedulers[i].SchedulingType.Equal(state.QosSchedulers[i].SchedulingType) {
hasChanges = true
}
}
}
return hasChanges
}
func (data *QoSMap) getClassMapVersion(ctx context.Context, queue int64) types.Int64 {
for _, item := range data.QosSchedulers {
if item.Queue.ValueInt64() == queue {
return item.ClassMapVersion
}
}
return types.Int64Null()
}
func (data *QoSMap) updateVersions(ctx context.Context, state QoSMap) {
for qs := range data.QosSchedulers {
queue := data.QosSchedulers[qs].Queue.ValueInt64()
data.QosSchedulers[qs].ClassMapVersion = state.getClassMapVersion(ctx, queue)
}
}