/
computer_extension_attr_entity.go
108 lines (93 loc) · 4.56 KB
/
computer_extension_attr_entity.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
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2020 Datadog, Inc.
package classic
import (
"encoding/xml"
"fmt"
"strings"
)
// ComputerExtensionAttributes represents all attributes that exist in Jamf
type ComputerExtensionAttributes struct {
List []ComputerExtensionAttribute `json:"computer_extension_attributes"`
}
// ComputerExtensionAttributeDetails holds the details for a single extension attribute
type ComputerExtensionAttributeDetails struct {
Details *ComputerExtensionAttribute `json:"computer_extension_attribute"`
}
// ComputerExtensionAttribute represents an extension attribute in Jamf
type ComputerExtensionAttribute struct {
XMLName xml.Name `json:"-" xml:"computer_extension_attribute,omitempty"`
ID int `json:"id" xml:"id,omitempty"`
Name string `json:"name" xml:"name,omitempty"`
Enabled bool `json:"enabled" xml:"enabled"` // we don't omit since false values are omitted if needed this can be changed to a *bool
Description string `json:"description,omitempty" xml:"description,omitempty"`
DataType string `json:"data_type,omitempty" xml:"data_type,omitempty"`
InputType *ComputerExtensionAttrInputType `json:"input_type,omitempty" xml:"input_type,omitempty"`
InventoryDisplay string `json:"inventory_display,omitempty" xml:"inventory_display,omitempty"`
ReconDisplay string `json:"recon_display,omitempty" xml:"recon_display,omitempty"`
}
// ComputerExtensionAttrInputType represents an input type for a computer extension attribute in Jamf
type ComputerExtensionAttrInputType struct {
Type string `json:"type,omitempty" xml:"type,omitempty"`
Platform string `json:"platform,omitempty" xml:"platform,omitempty"`
Script string `json:"script,omitempty" xml:"script,omitempty"`
}
// ValidateComputerExtensionAttribute orchestrates computer extension content validation
func ValidateComputerExtensionAttribute(ce *ComputerExtensionAttribute) error {
if err := ce.ValidateDataType(); err != nil {
return err
}
if ce.InputType != nil {
if err := ce.InputType.ValidateInputType(); err != nil {
return err
}
}
if err := ce.ValidateReconDisplay(); err != nil {
return err
}
if err := ce.ValidateInventoryDisplay(); err != nil {
return err
}
return nil
}
// ValidateDataType will validate that a computer extension attribute's data type is valid
func (ce *ComputerExtensionAttribute) ValidateDataType() error {
switch strings.ToLower(ce.DataType) {
case "", "string", "integer", "date":
return nil
default:
return fmt.Errorf("%s is not a valid computer extension attribute data type must be of type [ String, Integer, Date ]", ce.DataType)
}
}
// ValidateInventoryDisplay will validate that a computer extension attribute's data type is valid
func (ce *ComputerExtensionAttribute) ValidateInventoryDisplay() error {
switch strings.ToLower(ce.InventoryDisplay) {
case "", "general", "hardware", "operating system", "user and location", "purchasing", "extension attributes":
return nil
default:
return fmt.Errorf("%s is not a valid computer extension inventory display type must be of type [ General, Hardware, Operating System, User and Location, Purchasing, Extension Attributes ]", ce.InventoryDisplay)
}
}
// ValidateReconDisplay will validate that a computer extension attribute's data type is valid
func (ce *ComputerExtensionAttribute) ValidateReconDisplay() error {
switch strings.ToLower(ce.ReconDisplay) {
case "", "computer", "user and location", "purchasing", "extension attributes":
return nil
default:
return fmt.Errorf("%s is not a valid computer extension inventory display type must be of type [ Computer, User and Location, Purchasing, Extension Attributes ]", ce.ReconDisplay)
}
}
// ValidateInputType will validate that a computer extension attribute's input type is valid
func (it *ComputerExtensionAttrInputType) ValidateInputType() error {
switch it.Type {
case "", "Text Field", "LDAP Mapping", "Pop-up Menu":
return nil
case "script":
if it.Script == "" {
return fmt.Errorf("script contents must be provided for input type %s", it.Type)
}
return nil
default:
return fmt.Errorf("%s is not a valid computer extension attribute input type must be of type [ script, Text Field, LDAP Mapping, Pop-up Menu ]", it.Type)
}
}