-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgroup_tree.go
165 lines (133 loc) · 5.04 KB
/
group_tree.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
// Copyright 2024 qbee.io
//
// Licensed under the Apache 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://www.apache.org/licenses/LICENSE-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.
//
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"net/http"
)
// GroupTreeAction represents an action on the group tree.
type GroupTreeAction string
// GroupTreeAction types enumeration.
const (
TreeActionCreate GroupTreeAction = "create"
TreeActionRename GroupTreeAction = "rename"
TreeActionUpdate GroupTreeAction = "update"
TreeActionMove GroupTreeAction = "move"
TreeActionDelete GroupTreeAction = "delete"
)
// System node IDs
const (
// RootNodeID is the ID of the root node.
// Root node cannot be moved or deleted.
RootNodeID = "root"
// UnassignedGroupNodeID is the ID of the unassigned group node.
// Unassigned group node cannot be moved or deleted.
UnassignedGroupNodeID = "unassigned_group"
)
// GroupTreeChangeData represents the data for a group tree change.
type GroupTreeChangeData struct {
ParentID string `json:"parent_id"`
NodeID string `json:"node_id"`
// Type is the type of the group (device or group).
// This parameter is used only for moving of existing groups.
Type NodeType `json:"type,omitempty"`
// OldParentID is the ID of the parent group before the move.
// This parameter is used only for moving groups.
OldParentID string `json:"old_parent_id,omitempty"`
// Title is the title of the new group.
// This parameter is used only for creating new groups.
Title string `json:"title,omitempty"`
// Tags is the list of tags to set on the node.
// This parameter is used only for updates.
Tags []string `json:"tags,omitempty"`
}
// GroupTree represents the device group tree.
type GroupTree struct {
TotalDevices int `json:"total_devices"`
Tree GroupTreeNode `json:"tree"`
}
// GroupTreeNode represents a node in the device group tree.
type GroupTreeNode struct {
ID string `json:"id"`
NodeID string `json:"node_id"`
Title string `json:"title"`
Type NodeType `json:"type"`
Tags []string `json:"tags,omitempty"`
Nodes []GroupTreeNode `json:"nodes"`
// device only attributes
PublicKeyDigest string `json:"pub_key_digest,omitempty"`
DeviceCommitSHA string `json:"device_commit_sha,omitempty"`
Status string `json:"status,omitempty"`
ConfigPropagated bool `json:"config_propagated,omitempty"`
AgentInterval int `json:"agentinterval,omitempty"`
LastReported int64 `json:"last_reported,omitempty"`
Attributes DeviceAttributes `json:"attributes"`
// internal attributes
ParentID string `json:"-"`
Hostname string `json:"-"`
}
// GroupTreeChange represents a change in the device tree.
// Data is specific to the action:
// - create: parent_id, node_id, title, type
// - rename: node_id, title, type=group (only groups can be renamed)
// - move: parent_id, node_id, old_parent_id, type=device (only devices can be moved)
// - delete: node_id, type=group (only empty groups can be deleted)
type GroupTreeChange struct {
Action GroupTreeAction `json:"action"`
Data GroupTreeChangeData `json:"data"`
}
const grouptreePath = "/api/v2/grouptree"
// GroupTreeRequest is the request to update the group tree.
type GroupTreeRequest struct {
Changes []GroupTreeChange `json:"changes"`
}
// GroupTreeUpdate updates the group tree.
func (cli *Client) GroupTreeUpdate(ctx context.Context, req GroupTreeRequest) error {
return cli.Call(ctx, http.MethodPut, grouptreePath, req, nil)
}
// GroupTreeGet returns the device group tree.
func (cli *Client) GroupTreeGet(ctx context.Context, skipUnassigned bool) (*GroupTree, error) {
groupTree := new(GroupTree)
path := grouptreePath
if skipUnassigned {
path += "?skip_unassigned=true"
}
if err := cli.Call(ctx, http.MethodGet, path, nil, groupTree); err != nil {
return nil, err
}
return groupTree, nil
}
// GroupTreeGetNode returns the node from device group tree.
func (cli *Client) GroupTreeGetNode(ctx context.Context, nodeID string) (*NodeInfo, error) {
nodeInfo := new(NodeInfo)
path := grouptreePath + "/" + nodeID
if err := cli.Call(ctx, http.MethodGet, path, nil, nodeInfo); err != nil {
return nil, err
}
return nodeInfo, nil
}
// GroupTreeSetTagsRequest is the request to set tags for a node.
type GroupTreeSetTagsRequest struct {
Tags []string `json:"tags"`
}
// GroupTreeSetTags sets tags for a node.
func (cli *Client) GroupTreeSetTags(ctx context.Context, nodeID string, tags []string) error {
path := grouptreePath + "/" + nodeID
req := GroupTreeSetTagsRequest{
Tags: tags,
}
return cli.Call(ctx, http.MethodPatch, path, req, nil)
}