-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfirmware_baseline_helper.go
273 lines (239 loc) · 9.65 KB
/
firmware_baseline_helper.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Copyright (c) 2024 Dell Inc., or its subsidiaries. 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
http://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.
*/
package helper
import (
"context"
"fmt"
"terraform-provider-ome/clients"
"terraform-provider-ome/models"
"terraform-provider-ome/utils"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// CreateTargetModel create the target model based on the input plan
func CreateTargetModel(client *clients.Client, plan models.FirmwareBaselineResource) ([]models.TargetModel, error) {
var targets []models.TargetModel
var filterList []string
if len(plan.DeviceNames.Elements()) != 0 {
filterList = utils.ConvertListValueToStringSlice(plan.DeviceNames)
// Get all devices based on the names in the list
devices, err := client.GetValidDevicesByNames(filterList)
if err != nil {
return targets, err
}
for _, device := range devices {
targetModel := models.TargetModel{}
targetModel.ID = device.ID
targetModeltype := models.TargetTypeModel{}
targetModeltype.ID = device.Type
targetModeltype.Name = "DEVICE"
targetModel.Type = targetModeltype
targets = append(targets, targetModel)
}
} else if len(plan.GroupNames.Elements()) != 0 {
filterList = utils.ConvertListValueToStringSlice(plan.GroupNames)
// Get all groups and subgroups based on the names in the list
groups, err := client.GetValidGroupsByNames(filterList)
if err != nil {
return targets, err
}
for _, group := range groups {
targetModel := models.TargetModel{}
targetModel.ID = group.ID
targetModeltype := models.TargetTypeModel{}
targetModeltype.ID = group.TypeID
targetModeltype.Name = "GROUP"
targetModel.Type = targetModeltype
targets = append(targets, targetModel)
}
} else if len(plan.DeviceServiceTags.Elements()) != 0 {
filterList = utils.ConvertListValueToStringSlice(plan.DeviceServiceTags)
// Get all devices based on the service tags in the list
devices, err := client.GetDevices(filterList, nil, nil)
if err != nil {
return targets, err
}
for _, device := range devices {
targetModel := models.TargetModel{}
targetModel.ID = device.ID
targetModeltype := models.TargetTypeModel{}
targetModeltype.ID = device.Type
targetModeltype.Name = "DEVICE"
targetModel.Type = targetModeltype
targets = append(targets, targetModel)
}
} else {
return targets, fmt.Errorf("either device_names or group_names or device_service_tags is required")
}
return targets, nil
}
// SetStateBaseline set state baseline
func SetStateBaseline(ctx context.Context, baseline models.FirmwareBaselinesModel, plan models.FirmwareBaselineResource) (models.FirmwareBaselineResource, error) {
var state models.FirmwareBaselineResource
// Copy a majority of the state fields
errCopy := utils.CopyFields(ctx, baseline, &state)
if errCopy != nil {
return state, errCopy
}
// Map compliance summary object
mappedComplianceSummary, errMap := MapBaselineComplianceSummary(baseline.ComplianceSummary)
if errMap.HasError() {
return state, fmt.Errorf("failed to map compliance summary")
}
state.ComplianceSummary = mappedComplianceSummary
// Map Targets
mappedTargets, mapTargetDiags := MapBaselineTargets(baseline.Targets)
if mapTargetDiags.HasError() {
return state, fmt.Errorf("failed to map targets")
}
state.Targets = mappedTargets
if baseline.ID != nil {
state.ID = types.Int64Value(int64(*baseline.ID))
}
if baseline.TaskStatusID != nil {
state.TaskStatus = types.StringValue(GetJobStatus(int64(*baseline.TaskStatusID)))
}
if baseline.TaskID != nil {
state.TaskID = types.Int64Value(int64(*baseline.TaskID))
}
//state.LastRun = types.StringPointerValue(baseline.LastRun)
// state.Description = types.StringPointerValue(baseline.Description)
// state.Is64Bit = types.BoolValue(baseline.Is64Bit)
// state.FilterNoRebootRequired = types.BoolPointerValue(baseline.FilterNoRebootRequired)
// Set the user input values to the state
state.Name = plan.Name
state.CatalogName = plan.CatalogName
return state, nil
}
// MapBaselineComplianceSummary maps the compliance summary model to a types.Object.
func MapBaselineComplianceSummary(complianceSummary models.ComplianceSummaryModel) (types.Object, diag.Diagnostics) {
typeKey := map[string]attr.Type{
"compliance_status": types.StringType,
"number_of_critical": types.Int64Type,
"number_of_downgrade": types.Int64Type,
"number_of_normal": types.Int64Type,
"number_of_warning": types.Int64Type,
"number_of_unknown": types.Int64Type,
}
complianceMap := make(map[string]attr.Value)
complianceMap["compliance_status"] = types.StringPointerValue(complianceSummary.ComplianceStatus)
complianceMap["number_of_critical"] = types.Int64PointerValue(complianceSummary.NumberOfCritical)
complianceMap["number_of_downgrade"] = types.Int64PointerValue(complianceSummary.NumberOfDowngrade)
complianceMap["number_of_normal"] = types.Int64PointerValue(complianceSummary.NumberOfNormal)
complianceMap["number_of_warning"] = types.Int64PointerValue(complianceSummary.NumberOfWarning)
complianceMap["number_of_unknown"] = types.Int64PointerValue(complianceSummary.NumberOfUnknown)
dcrObject, diags := types.ObjectValue(typeKey, complianceMap)
return dcrObject, diags
}
// MapBaselineTargets maps a list of TargetModel objects into a List of attr.Value objects.
func MapBaselineTargets(targets []models.TargetModel) (types.List, diag.Diagnostics) {
var targetsMap []attr.Value
targetKey := map[string]attr.Type{
"id": types.Int64Type,
"type": types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.Int64Type,
"name": types.StringType,
},
},
}
for _, target := range targets {
targetMap := make(map[string]attr.Value)
targetMap["id"] = types.Int64Value(int64(target.ID))
typeMap := make(map[string]attr.Value)
typeMap["id"] = types.Int64Value(int64(target.Type.ID))
typeMap["name"] = types.StringValue(target.Type.Name)
typeObject, diags := types.ObjectValue(
map[string]attr.Type{
"id": types.Int64Type,
"name": types.StringType,
}, typeMap)
if diags.HasError() {
return types.List{}, diags
}
targetMap["type"] = typeObject
targetObject, diags := types.ObjectValue(targetKey, targetMap)
if diags.HasError() {
return types.List{}, diags
}
targetsMap = append(targetsMap, targetObject)
}
return types.ListValue(types.ObjectType{AttrTypes: targetKey}, targetsMap)
}
// GetFirmwareBaselineWithName gets a baseline by name
func GetFirmwareBaselineWithName(client clients.Client, name string) (models.FirmwareBaselinesModel, error) {
return client.GetFirmwareBaselineWithName(name)
}
// GetFirmwareBaselineWithID gets a baseline by id
func GetFirmwareBaselineWithID(client clients.Client, id int64) (models.FirmwareBaselinesModel, error) {
return client.GetFirmwareBaselineWithID(id)
}
// CreateFirmwareBaseline - Creates a new Firmware baseline
func CreateFirmwareBaseline(client *clients.Client, payload models.CreateUpdateFirmwareBaseline) (int64, error) {
return client.CreateFirmwareBaseline(payload)
}
// DeleteFirmwareBaseline deletes the given firmware baseline
func DeleteFirmwareBaseline(client clients.Client, id int64) error {
baselineIds := []int64{id}
return client.DeleteFirmwareBaseline(baselineIds)
}
// UpdateFirmwareBaseline updates the given firmware baseline
func UpdateFirmwareBaseline(client clients.Client, state models.FirmwareBaselineResource, plan models.FirmwareBaselineResource) (int64, error) {
payload := models.CreateUpdateFirmwareBaseline{}
payload.ID = state.ID.ValueInt64()
if plan.CatalogName.ValueString() != "" && plan.CatalogName.ValueString() != state.CatalogName.ValueString() {
catalog, err := GetCatalogFirmwareByName(&client, plan.CatalogName.ValueString())
if err != nil {
return -1, err
}
if catalog == nil {
return -1, fmt.Errorf("catalog %s not found", plan.CatalogName.ValueString())
}
payload.CatalogID = catalog.ID
payload.RepositoryID = catalog.Repository.ID
} else {
payload.CatalogID = state.CatalogID.ValueInt64()
payload.RepositoryID = state.RepositoryID.ValueInt64()
}
if plan.Is64Bit.ValueBool() != state.Is64Bit.ValueBool() {
payload.Is64Bit = plan.Is64Bit.ValueBool()
} else {
payload.Is64Bit = state.Is64Bit.ValueBool()
}
if plan.FilterNoRebootRequired.ValueBool() != state.FilterNoRebootRequired.ValueBool() {
payload.FilterNoRebootRequired = plan.FilterNoRebootRequired.ValueBool()
} else {
payload.FilterNoRebootRequired = state.FilterNoRebootRequired.ValueBool()
}
if plan.Description.ValueString() != "" && plan.Description.ValueString() != state.Description.ValueString() {
payload.Description = plan.Description.ValueString()
} else {
payload.Description = state.Description.ValueString()
}
if plan.Name.ValueString() != "" && plan.Name.ValueString() != state.Name.ValueString() {
payload.Name = plan.Name.ValueString()
} else {
payload.Name = state.Name.ValueString()
}
targets, err := CreateTargetModel(&client, plan)
if err != nil {
return -1, fmt.Errorf("unable to create target model for: %s. details: %s", plan.Name.ValueString(), err.Error())
}
payload.Targets = targets
id, err := client.UpdateFirmwareBaseline(payload)
if err != nil {
return -1, err
}
return id, nil
}