-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomfields.go
More file actions
160 lines (141 loc) · 6.04 KB
/
Copy pathcustomfields.go
File metadata and controls
160 lines (141 loc) · 6.04 KB
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
package vsax
import (
"context"
"fmt"
"iter"
"net/http"
"strconv"
)
// Custom field types.
const (
CustomFieldTypeText = "Text"
CustomFieldTypeNumber = "Number"
CustomFieldTypeDate = "Date"
CustomFieldTypeBoolean = "Boolean"
)
// Custom field context types.
const (
ContextGlobal = "Global"
ContextOrganization = "Organization"
ContextSite = "Site"
ContextGroup = "Group"
ContextDevice = "Device"
ContextScope = "Scope"
ContextScript = "Script"
)
// CustomFieldDef is a custom-field definition (as distinct from CustomField,
// which is a per-entity assignment).
type CustomFieldDef struct {
ID int64 `json:"Id"`
Name string `json:"Name,omitempty"`
Description string `json:"Description,omitempty"`
Type string `json:"Type,omitempty"`
DefaultValue string `json:"DefaultValue,omitempty"`
GlobalValue string `json:"GlobalValue,omitempty"`
IsReadOnly bool `json:"IsReadOnly"`
Contexts []string `json:"Contexts,omitempty"`
ExpirationDate Time `json:"ExpirationDate,omitzero"`
CreatedAt Time `json:"CreatedAt,omitzero"`
}
// CustomFieldUsageEntry records one place a custom field is used.
type CustomFieldUsageEntry struct {
UsageContextType string `json:"UsageContextType,omitempty"`
ContextItemID string `json:"ContextItemId,omitempty"`
Value string `json:"Value,omitempty"`
}
// AssignCustomFieldRequest is the body for POST /customFields/{id}/assign.
//
// Set UseDefaultValue=true to adopt the field's default; otherwise supply a
// Value. Assigning to a (ContextType, ContextItemId) pair that already has
// the field assigned returns 400; use UpdateAssignment instead.
type AssignCustomFieldRequest struct {
ContextType string `json:"ContextType"`
ContextItemID string `json:"ContextItemId"`
UseDefaultValue bool `json:"UseDefaultValue,omitempty"`
Value string `json:"Value,omitempty"`
}
// UpdateAssignedCustomFieldRequest is the body for PUT /customFields/{id}/updateAssigned.
type UpdateAssignedCustomFieldRequest struct {
ContextType string `json:"ContextType"`
ContextItemID string `json:"ContextItemId"`
UseDefaultValue bool `json:"UseDefaultValue,omitempty"`
Value string `json:"Value,omitempty"`
}
// UnassignCustomFieldRequest is the body for POST /customFields/{id}/unassign.
type UnassignCustomFieldRequest struct {
ContextType string `json:"ContextType"`
ContextItemID string `json:"ContextItemId"`
}
// CustomFieldAssignmentResult is the response to Assign/UpdateAssignment.
type CustomFieldAssignmentResult struct {
ContextType string `json:"ContextType,omitempty"`
ContextItemID string `json:"ContextItemId,omitempty"`
ID int64 `json:"Id"`
Name string `json:"Name,omitempty"`
Value string `json:"Value,omitempty"`
Type string `json:"Type,omitempty"`
}
// CustomFieldService provides access to custom-field endpoints under /customFields.
type CustomFieldService struct {
client *Client
list listService[CustomFieldDef]
}
func newCustomFieldService(c *Client) *CustomFieldService {
return &CustomFieldService{client: c, list: listService[CustomFieldDef]{client: c, path: "/customFields"}}
}
// List retrieves one page of custom-field definitions.
func (s *CustomFieldService) List(ctx context.Context, q *Query) (*PagedResult[CustomFieldDef], error) {
return s.list.List(ctx, q)
}
// All iterates all custom-field definitions across pages.
func (s *CustomFieldService) All(ctx context.Context, q *Query) iter.Seq2[CustomFieldDef, error] {
return s.list.All(ctx, q)
}
// Get retrieves one custom-field definition by ID.
func (s *CustomFieldService) Get(ctx context.Context, id int64) (*CustomFieldDef, error) {
env, err := s.client.do(ctx, http.MethodGet, "/customFields/"+strconv.FormatInt(id, 10), nil)
if err != nil {
return nil, err
}
return decodeOne[CustomFieldDef](env)
}
// Usage lists where a custom field is currently assigned.
func (s *CustomFieldService) Usage(ctx context.Context, id int64, q *Query) (*PagedResult[CustomFieldUsageEntry], error) {
return listSub[CustomFieldUsageEntry](ctx, s.client, "/customFields/"+strconv.FormatInt(id, 10)+"/usage", q)
}
// AllUsage iterates all usage entries for a custom field across pages.
func (s *CustomFieldService) AllUsage(ctx context.Context, id int64, q *Query) iter.Seq2[CustomFieldUsageEntry, error] {
return allFromList(ctx, func() (*PagedResult[CustomFieldUsageEntry], error) {
return s.Usage(ctx, id, q)
})
}
// Assign assigns the custom field to an entity. Returns 400 if already assigned.
func (s *CustomFieldService) Assign(ctx context.Context, id int64, body *AssignCustomFieldRequest) (*CustomFieldAssignmentResult, error) {
if body == nil || body.ContextType == "" || body.ContextItemID == "" {
return nil, fmt.Errorf("vsax: ContextType and ContextItemId are required")
}
env, err := s.client.do(ctx, http.MethodPost, "/customFields/"+strconv.FormatInt(id, 10)+"/assign", body)
if err != nil {
return nil, err
}
return decodeOne[CustomFieldAssignmentResult](env)
}
// UpdateAssignment updates the value of an existing custom-field assignment.
func (s *CustomFieldService) UpdateAssignment(ctx context.Context, id int64, body *UpdateAssignedCustomFieldRequest) (*CustomFieldAssignmentResult, error) {
if body == nil || body.ContextType == "" || body.ContextItemID == "" {
return nil, fmt.Errorf("vsax: ContextType and ContextItemId are required")
}
env, err := s.client.do(ctx, http.MethodPut, "/customFields/"+strconv.FormatInt(id, 10)+"/updateAssigned", body)
if err != nil {
return nil, err
}
return decodeOne[CustomFieldAssignmentResult](env)
}
// Unassign removes a custom field from an entity.
func (s *CustomFieldService) Unassign(ctx context.Context, id int64, body *UnassignCustomFieldRequest) error {
if body == nil || body.ContextType == "" || body.ContextItemID == "" {
return fmt.Errorf("vsax: ContextType and ContextItemId are required")
}
_, err := s.client.do(ctx, http.MethodPost, "/customFields/"+strconv.FormatInt(id, 10)+"/unassign", body)
return err
}