-
Notifications
You must be signed in to change notification settings - Fork 32
/
commands.operator_search_attribute.go
141 lines (121 loc) · 4.1 KB
/
commands.operator_search_attribute.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
package temporalcli
import (
"fmt"
"strings"
"github.com/fatih/color"
"github.com/temporalio/cli/temporalcli/internal/printer"
"go.temporal.io/api/enums/v1"
"go.temporal.io/api/operatorservice/v1"
)
func (c *TemporalOperatorSearchAttributeCreateCommand) run(cctx *CommandContext, args []string) error {
cl, err := c.Parent.Parent.ClientOptions.dialClient(cctx)
if err != nil {
return err
}
defer cl.Close()
// Name and Type are required parameters, and there must be the same number of them.
listReq := &operatorservice.ListSearchAttributesRequest{
Namespace: c.Parent.Parent.Namespace,
}
existingSearchAttributes, err := cl.OperatorService().ListSearchAttributes(cctx, listReq)
if err != nil {
return fmt.Errorf("unable to get existing search attributes: %w", err)
}
searchAttributes := make(map[string]enums.IndexedValueType, len(c.Type))
for i, saType := range c.Type {
saName := c.Name[i]
typeInt, err := searchAttributeTypeStringToEnum(saType)
if err != nil {
return fmt.Errorf("unable to parse search attribute type %s: %w", saType, err)
}
existingSearchAttributeType, searchAttributeExists := existingSearchAttributes.CustomAttributes[saName]
if searchAttributeExists && existingSearchAttributeType != typeInt {
return fmt.Errorf("search attribute %s already exists and has different type %s", saName, existingSearchAttributeType.String())
}
searchAttributes[saName] = typeInt
}
request := &operatorservice.AddSearchAttributesRequest{
SearchAttributes: searchAttributes,
Namespace: c.Parent.Parent.Namespace,
}
_, err = cl.OperatorService().AddSearchAttributes(cctx, request)
if err != nil {
return fmt.Errorf("unable to add search attributes: %w", err)
}
cctx.Printer.Println(color.GreenString("Search attributes have been added"))
return nil
}
func searchAttributeTypeStringToEnum(search string) (enums.IndexedValueType, error) {
for k, v := range enums.IndexedValueType_shorthandValue {
if strings.EqualFold(search, k) {
return enums.IndexedValueType(v), nil
}
}
return enums.INDEXED_VALUE_TYPE_UNSPECIFIED, fmt.Errorf("unsupported search attribute type: %v", search)
}
func (c *TemporalOperatorSearchAttributeRemoveCommand) run(cctx *CommandContext, args []string) error {
cl, err := c.Parent.Parent.ClientOptions.dialClient(cctx)
if err != nil {
return err
}
defer cl.Close()
yes, err := cctx.promptYes(
fmt.Sprintf("You are about to remove search attribute %q? y/N", c.Name), c.Yes)
if err != nil {
return err
} else if !yes {
// We consider this a command failure
return fmt.Errorf("user denied confirmation, operation aborted")
}
names := c.Name
namespace := c.Parent.Parent.Namespace
if err != nil {
return err
}
request := &operatorservice.RemoveSearchAttributesRequest{
SearchAttributes: names,
Namespace: namespace,
}
_, err = cl.OperatorService().RemoveSearchAttributes(cctx, request)
if err != nil {
return fmt.Errorf("unable to remove search attributes: %w", err)
}
// response contains nothing
cctx.Printer.Println(color.GreenString("Search attributes have been removed"))
return nil
}
func (c *TemporalOperatorSearchAttributeListCommand) run(cctx *CommandContext, args []string) error {
cl, err := c.Parent.Parent.ClientOptions.dialClient(cctx)
if err != nil {
return err
}
defer cl.Close()
request := &operatorservice.ListSearchAttributesRequest{
Namespace: c.Parent.Parent.Namespace,
}
resp, err := cl.OperatorService().ListSearchAttributes(cctx, request)
if err != nil {
return fmt.Errorf("unable to list search attributes: %w", err)
}
if cctx.JSONOutput {
return cctx.Printer.PrintStructured(resp, printer.StructuredOptions{})
}
type saNameType struct {
Name string `json:"name"`
Type string `json:"type"`
}
var sas []saNameType
for saName, saType := range resp.SystemAttributes {
sas = append(sas, saNameType{
Name: saName,
Type: saType.String(),
})
}
for saName, saType := range resp.CustomAttributes {
sas = append(sas, saNameType{
Name: saName,
Type: saType.String(),
})
}
return cctx.Printer.PrintStructured(sas, printer.StructuredOptions{Table: &printer.TableOptions{}})
}