|
| 1 | +// Copyright © 2023 Cisco Systems, Inc. and its affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Mozilla Public License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://mozilla.org/MPL/2.0/ |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +// SPDX-License-Identifier: MPL-2.0 |
| 17 | + |
| 18 | +package provider |
| 19 | + |
| 20 | +// Section below is generated&owned by "gen/generator.go". //template:begin imports |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "strconv" |
| 24 | + |
| 25 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 26 | + "github.com/tidwall/gjson" |
| 27 | + "github.com/tidwall/sjson" |
| 28 | +) |
| 29 | + |
| 30 | +// End of section. //template:end imports |
| 31 | + |
| 32 | +// Section below is generated&owned by "gen/generator.go". //template:begin types |
| 33 | +type PnPImportDevices struct { |
| 34 | + Id types.String `tfsdk:"id"` |
| 35 | + Devices []PnPImportDevicesDevices `tfsdk:"devices"` |
| 36 | +} |
| 37 | + |
| 38 | +type PnPImportDevicesDevices struct { |
| 39 | + SerialNumber types.String `tfsdk:"serial_number"` |
| 40 | + Stack types.Bool `tfsdk:"stack"` |
| 41 | + Pid types.String `tfsdk:"pid"` |
| 42 | + Hostname types.String `tfsdk:"hostname"` |
| 43 | +} |
| 44 | + |
| 45 | +// End of section. //template:end types |
| 46 | + |
| 47 | +// Section below is generated&owned by "gen/generator.go". //template:begin getPath |
| 48 | +func (data PnPImportDevices) getPath() string { |
| 49 | + return "/dna/intent/api/v1/onboarding/pnp-device/import" |
| 50 | +} |
| 51 | + |
| 52 | +// End of section. //template:end getPath |
| 53 | + |
| 54 | +// Section below is generated&owned by "gen/generator.go". //template:begin getPathDelete |
| 55 | + |
| 56 | +// End of section. //template:end getPathDelete |
| 57 | + |
| 58 | +// Section below is generated&owned by "gen/generator.go". //template:begin toBody |
| 59 | +func (data PnPImportDevices) toBody(ctx context.Context, state PnPImportDevices) string { |
| 60 | + body := "[]" |
| 61 | + put := false |
| 62 | + if state.Id.ValueString() != "" { |
| 63 | + put = true |
| 64 | + } |
| 65 | + _ = put |
| 66 | + if len(data.Devices) > 0 { |
| 67 | + body, _ = sjson.Set(body, "", []interface{}{}) |
| 68 | + for _, item := range data.Devices { |
| 69 | + itemBody := "" |
| 70 | + if !item.SerialNumber.IsNull() { |
| 71 | + itemBody, _ = sjson.Set(itemBody, "deviceInfo.serialNumber", item.SerialNumber.ValueString()) |
| 72 | + } |
| 73 | + if !item.Stack.IsNull() { |
| 74 | + itemBody, _ = sjson.Set(itemBody, "deviceInfo.stack", item.Stack.ValueBool()) |
| 75 | + } |
| 76 | + if !item.Pid.IsNull() { |
| 77 | + itemBody, _ = sjson.Set(itemBody, "deviceInfo.pid", item.Pid.ValueString()) |
| 78 | + } |
| 79 | + if !item.Hostname.IsNull() { |
| 80 | + itemBody, _ = sjson.Set(itemBody, "deviceInfo.hostname", item.Hostname.ValueString()) |
| 81 | + } |
| 82 | + body, _ = sjson.SetRaw(body, "-1", itemBody) |
| 83 | + } |
| 84 | + } |
| 85 | + return body |
| 86 | +} |
| 87 | + |
| 88 | +// End of section. //template:end toBody |
| 89 | + |
| 90 | +// Section below is generated&owned by "gen/generator.go". //template:begin fromBody |
| 91 | +func (data *PnPImportDevices) fromBody(ctx context.Context, res gjson.Result) { |
| 92 | + if value := res; value.Exists() && len(value.Array()) > 0 { |
| 93 | + data.Devices = make([]PnPImportDevicesDevices, 0) |
| 94 | + value.ForEach(func(k, v gjson.Result) bool { |
| 95 | + item := PnPImportDevicesDevices{} |
| 96 | + if cValue := v.Get("deviceInfo.serialNumber"); cValue.Exists() { |
| 97 | + item.SerialNumber = types.StringValue(cValue.String()) |
| 98 | + } else { |
| 99 | + item.SerialNumber = types.StringNull() |
| 100 | + } |
| 101 | + if cValue := v.Get("deviceInfo.stack"); cValue.Exists() { |
| 102 | + item.Stack = types.BoolValue(cValue.Bool()) |
| 103 | + } else { |
| 104 | + item.Stack = types.BoolNull() |
| 105 | + } |
| 106 | + if cValue := v.Get("deviceInfo.pid"); cValue.Exists() { |
| 107 | + item.Pid = types.StringValue(cValue.String()) |
| 108 | + } else { |
| 109 | + item.Pid = types.StringNull() |
| 110 | + } |
| 111 | + if cValue := v.Get("deviceInfo.hostname"); cValue.Exists() { |
| 112 | + item.Hostname = types.StringValue(cValue.String()) |
| 113 | + } else { |
| 114 | + item.Hostname = types.StringNull() |
| 115 | + } |
| 116 | + data.Devices = append(data.Devices, item) |
| 117 | + return true |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// End of section. //template:end fromBody |
| 123 | + |
| 124 | +// Section below is generated&owned by "gen/generator.go". //template:begin updateFromBody |
| 125 | +func (data *PnPImportDevices) updateFromBody(ctx context.Context, res gjson.Result) { |
| 126 | + for i := range data.Devices { |
| 127 | + keys := [...]string{"deviceInfo.serialNumber", "deviceInfo.stack", "deviceInfo.pid", "deviceInfo.hostname"} |
| 128 | + keyValues := [...]string{data.Devices[i].SerialNumber.ValueString(), strconv.FormatBool(data.Devices[i].Stack.ValueBool()), data.Devices[i].Pid.ValueString(), data.Devices[i].Hostname.ValueString()} |
| 129 | + |
| 130 | + var r gjson.Result |
| 131 | + res.ForEach( |
| 132 | + func(_, v gjson.Result) bool { |
| 133 | + found := false |
| 134 | + for ik := range keys { |
| 135 | + if v.Get(keys[ik]).String() == keyValues[ik] { |
| 136 | + found = true |
| 137 | + continue |
| 138 | + } |
| 139 | + found = false |
| 140 | + break |
| 141 | + } |
| 142 | + if found { |
| 143 | + r = v |
| 144 | + return false |
| 145 | + } |
| 146 | + return true |
| 147 | + }, |
| 148 | + ) |
| 149 | + if value := r.Get("deviceInfo.serialNumber"); value.Exists() && !data.Devices[i].SerialNumber.IsNull() { |
| 150 | + data.Devices[i].SerialNumber = types.StringValue(value.String()) |
| 151 | + } else { |
| 152 | + data.Devices[i].SerialNumber = types.StringNull() |
| 153 | + } |
| 154 | + if value := r.Get("deviceInfo.stack"); value.Exists() && !data.Devices[i].Stack.IsNull() { |
| 155 | + data.Devices[i].Stack = types.BoolValue(value.Bool()) |
| 156 | + } else { |
| 157 | + data.Devices[i].Stack = types.BoolNull() |
| 158 | + } |
| 159 | + if value := r.Get("deviceInfo.pid"); value.Exists() && !data.Devices[i].Pid.IsNull() { |
| 160 | + data.Devices[i].Pid = types.StringValue(value.String()) |
| 161 | + } else { |
| 162 | + data.Devices[i].Pid = types.StringNull() |
| 163 | + } |
| 164 | + if value := r.Get("deviceInfo.hostname"); value.Exists() && !data.Devices[i].Hostname.IsNull() { |
| 165 | + data.Devices[i].Hostname = types.StringValue(value.String()) |
| 166 | + } else { |
| 167 | + data.Devices[i].Hostname = types.StringNull() |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// End of section. //template:end updateFromBody |
| 173 | + |
| 174 | +// Section below is generated&owned by "gen/generator.go". //template:begin isNull |
| 175 | +func (data *PnPImportDevices) isNull(ctx context.Context, res gjson.Result) bool { |
| 176 | + if len(data.Devices) > 0 { |
| 177 | + return false |
| 178 | + } |
| 179 | + return true |
| 180 | +} |
| 181 | + |
| 182 | +// End of section. //template:end isNull |
0 commit comments