-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathannotation.go
173 lines (145 loc) · 4.53 KB
/
annotation.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
// Copyright (C) 2023 Percona LLC
//
// 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.
package commands
import (
"strings"
"github.com/pkg/errors"
"github.com/percona/pmm/admin/agentlocal"
"github.com/percona/pmm/admin/helpers"
"github.com/percona/pmm/api/inventory/v1/json/client"
nodes "github.com/percona/pmm/api/inventory/v1/json/client/nodes_service"
services "github.com/percona/pmm/api/inventory/v1/json/client/services_service"
managementClient "github.com/percona/pmm/api/management/v1/json/client"
annotation "github.com/percona/pmm/api/management/v1/json/client/annotation_service"
)
var annotationResultT = ParseTemplate(`
Annotation added.
`)
// annotationResult is a result of annotation command.
type annotationResult struct{}
// Result is a command run result.
func (res *annotationResult) Result() {}
// String stringifies command result.
func (res *annotationResult) String() string {
return RenderTemplate(annotationResultT, res)
}
// AnnotationCommand is used by Kong for CLI flags and commands.
type AnnotationCommand struct {
Text string `arg:"" help:"Text of annotation"`
Tags []string `help:"Tags to filter annotations. Multiple tags are separated by a comma"`
Node bool `help:"Annotate current node"`
NodeName string `help:"Name of node to annotate"`
Service bool `help:"Annotate services of current node"`
ServiceName string `help:"Name of service to annotate"`
}
func (cmd *AnnotationCommand) nodeName() (string, error) {
if cmd.NodeName != "" {
return cmd.NodeName, nil
}
if !cmd.Node {
return "", nil
}
node, err := cmd.getCurrentNode()
if err != nil {
return "", err
}
return helpers.GetNodeName(node)
}
func (cmd *AnnotationCommand) getCurrentNode() (*nodes.GetNodeOKBody, error) {
status, err := agentlocal.GetStatus(agentlocal.DoNotRequestNetworkInfo)
if err != nil {
return nil, err
}
params := &nodes.GetNodeParams{
Body: nodes.GetNodeBody{
NodeID: status.NodeID,
},
Context: Ctx,
}
result, err := client.Default.NodesService.GetNode(params)
if err != nil {
return nil, errors.Wrap(err, "default get node")
}
return result.GetPayload(), nil
}
func (cmd *AnnotationCommand) serviceNames() ([]string, error) {
switch {
case cmd.ServiceName != "":
return []string{cmd.ServiceName}, nil
case cmd.Service:
return cmd.getCurrentNodeAllServices()
default:
return []string{}, nil
}
}
func (cmd *AnnotationCommand) getCurrentNodeAllServices() ([]string, error) {
status, err := agentlocal.GetStatus(agentlocal.DoNotRequestNetworkInfo)
if err != nil {
return nil, err
}
params := &services.ListServicesParams{
Body: services.ListServicesBody{
NodeID: status.NodeID,
},
Context: Ctx,
}
result, err := client.Default.ServicesService.ListServices(params)
if err != nil {
return nil, err
}
servicesNameList := []string{}
for _, s := range result.Payload.Mysql {
servicesNameList = append(servicesNameList, s.ServiceName)
}
for _, s := range result.Payload.Mongodb {
servicesNameList = append(servicesNameList, s.ServiceName)
}
for _, s := range result.Payload.Postgresql {
servicesNameList = append(servicesNameList, s.ServiceName)
}
for _, s := range result.Payload.Proxysql {
servicesNameList = append(servicesNameList, s.ServiceName)
}
for _, s := range result.Payload.External {
servicesNameList = append(servicesNameList, s.ServiceName)
}
return servicesNameList, nil
}
// RunCmd runs annotation command.
func (cmd *AnnotationCommand) RunCmd() (Result, error) {
for i := range cmd.Tags {
cmd.Tags[i] = strings.TrimSpace(cmd.Tags[i])
}
nodeName, err := cmd.nodeName()
if err != nil {
return nil, err
}
serviceNames, err := cmd.serviceNames()
if err != nil {
return nil, err
}
_, err = managementClient.Default.AnnotationService.AddAnnotation(&annotation.AddAnnotationParams{
Body: annotation.AddAnnotationBody{
Text: cmd.Text,
Tags: cmd.Tags,
NodeName: nodeName,
ServiceNames: serviceNames,
},
Context: Ctx,
})
if err != nil {
return nil, err
}
return &annotationResult{}, nil
}