-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroups.go
More file actions
147 lines (130 loc) · 5.26 KB
/
Copy pathgroups.go
File metadata and controls
147 lines (130 loc) · 5.26 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
package vsax
import (
"context"
"fmt"
"iter"
"net/http"
"strconv"
)
// Group package types for GET /groups/{id}/package/{type}. The upstream docs
// list only the two Windows installers, but a live tenant additionally offers
// Linux (deb/rpm) and macOS packages across multiple architectures. The full
// set of values observed in the wild is enumerated here; pass any of them to
// GroupService.Package as the pkgType argument.
const (
PackageTypeWindowsAgentX64 = "windows_agent_x64"
PackageTypeWindowsAgentX86 = "windows_agent_x86"
PackageTypeLinuxAgentX64Deb = "linux_agent_x64_deb"
PackageTypeLinuxAgentX86Deb = "linux_agent_x86_deb"
PackageTypeLinuxAgentArm64Deb = "linux_agent_arm64_deb"
PackageTypeLinuxAgentArmDeb = "linux_agent_arm_deb"
PackageTypeLinuxAgentX64Rpm = "linux_agent_x64_rpm"
PackageTypeLinuxAgentX86Rpm = "linux_agent_x86_rpm"
PackageTypeLinuxAgentX64Pkg = "linux_agent_x64_pkg"
)
// Group is a VSA X group (a child of a site; groups may nest).
type Group struct {
ID int64 `json:"Id"`
IsActive bool `json:"IsActive"`
Name string `json:"Name,omitempty"`
ParentSiteID int64 `json:"ParentSiteId,omitempty"`
ParentSiteName string `json:"ParentSiteName,omitempty"`
ParentOrganizationID int64 `json:"ParentOrganizationId,omitempty"`
ParentOrganizationName string `json:"ParentOrganizationName,omitempty"`
Notes string `json:"Notes,omitempty"`
HasCustomFields bool `json:"HasCustomFields,omitempty"`
PsaMappingID *int64 `json:"PsaMappingId,omitempty"`
PsaIntegrationType string `json:"PsaIntegrationType,omitempty"`
Packages []GroupPackageRef `json:"Packages,omitempty"`
}
// GroupPackageRef identifies a downloadable package variant available for a
// group (detail view only). Fetch the download URL via
// GroupService.Package(id, Type).
type GroupPackageRef struct {
Type string `json:"Type"`
Name string `json:"Name,omitempty"`
}
// GroupPackage is the download link returned by GET /groups/{id}/package/{type}.
type GroupPackage struct {
Name string `json:"Name,omitempty"`
URL string `json:"Url,omitempty"`
}
// CreateGroupRequest is the body for POST /groups. ParentID is the parent
// site's ID.
type CreateGroupRequest struct {
Name string `json:"Name"`
ParentID int64 `json:"ParentId"`
Notes string `json:"Notes,omitempty"`
}
// UpdateGroupRequest is the body for PUT /groups/{id}.
type UpdateGroupRequest struct {
Name string `json:"Name"`
Notes string `json:"Notes,omitempty"`
}
// GroupService provides access to group endpoints.
type GroupService struct {
client *Client
list listService[Group]
}
func newGroupService(c *Client) *GroupService {
return &GroupService{client: c, list: listService[Group]{client: c, path: "/groups"}}
}
// List retrieves one page of groups.
func (s *GroupService) List(ctx context.Context, q *Query) (*PagedResult[Group], error) {
return s.list.List(ctx, q)
}
// All iterates all groups across pages.
func (s *GroupService) All(ctx context.Context, q *Query) iter.Seq2[Group, error] {
return s.list.All(ctx, q)
}
// Get retrieves one group by ID.
func (s *GroupService) Get(ctx context.Context, id int64) (*Group, error) {
env, err := s.client.do(ctx, http.MethodGet, "/groups/"+strconv.FormatInt(id, 10), nil)
if err != nil {
return nil, err
}
return decodeOne[Group](env)
}
// Create creates a new group under a site.
func (s *GroupService) Create(ctx context.Context, body *CreateGroupRequest) (*Group, error) {
if body == nil || body.Name == "" || body.ParentID == 0 {
return nil, fmt.Errorf("vsax: Name and ParentId are required")
}
env, err := s.client.do(ctx, http.MethodPost, "/groups", body)
if err != nil {
return nil, err
}
return decodeOne[Group](env)
}
// Update modifies an existing group.
func (s *GroupService) Update(ctx context.Context, id int64, body *UpdateGroupRequest) (*Group, error) {
if body == nil || body.Name == "" {
return nil, fmt.Errorf("vsax: Name is required")
}
env, err := s.client.do(ctx, http.MethodPut, "/groups/"+strconv.FormatInt(id, 10), body)
if err != nil {
return nil, err
}
return decodeOne[Group](env)
}
// Delete removes a group.
func (s *GroupService) Delete(ctx context.Context, id int64) error {
_, err := s.client.do(ctx, http.MethodDelete, "/groups/"+strconv.FormatInt(id, 10), nil)
return err
}
// CustomFields lists the group's custom-field assignments.
func (s *GroupService) CustomFields(ctx context.Context, id int64, q *Query) (*PagedResult[CustomField], error) {
return listSub[CustomField](ctx, s.client, "/groups/"+strconv.FormatInt(id, 10)+"/customfields", q)
}
// Package returns the download information for a specific package variant.
// Use one of the PackageType* constants for pkgType (e.g. PackageTypeWindowsAgentX64).
func (s *GroupService) Package(ctx context.Context, id int64, pkgType string) (*GroupPackage, error) {
if pkgType == "" {
return nil, fmt.Errorf("vsax: packageType is required")
}
env, err := s.client.do(ctx, http.MethodGet, "/groups/"+strconv.FormatInt(id, 10)+"/package/"+pkgType, nil)
if err != nil {
return nil, err
}
return decodeOne[GroupPackage](env)
}