/
tag.go
184 lines (159 loc) · 4.42 KB
/
tag.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
174
175
176
177
178
179
180
181
182
183
184
package commands
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/killbill/kbcli/kbcmd/kblib"
"github.com/killbill/kbcli/kbclient/tag_definition"
"github.com/killbill/kbcli/kbcmd/cmdlib"
"github.com/killbill/kbcli/kbcmd/cmdlib/args"
"github.com/killbill/kbcli/kbmodel"
"github.com/urfave/cli"
)
var (
createTagDefinitionProperties args.Properties
)
var tagDefinitionFormatter = cmdlib.Formatter{
Columns: []cmdlib.Column{
{
Name: "NAME",
Path: "$.name",
},
{
Name: "ID",
Path: "$.id",
},
{
Name: "IS_CONTROL",
Path: "$.isControlTag",
},
{
Name: "DESCRIPTION",
Path: "$.description",
},
},
}
func listTagDefinitions(ctx context.Context, o *cmdlib.Options) error {
if len(o.Args) != 0 {
return cmdlib.ErrorInvalidArgs
}
resp, err := o.Client().TagDefinition.GetTagDefinitions(ctx, &tag_definition.GetTagDefinitionsParams{})
if err != nil {
return err
}
o.Print(resp.Payload)
return nil
}
func getTagDefinition(ctx context.Context, o *cmdlib.Options) error {
if len(o.Args) != 1 {
return cmdlib.ErrorInvalidArgs
}
idOrName := o.Args[0]
td, err := kblib.GetTagDefinition(ctx, o.Client(), idOrName)
if err != nil {
return err
}
if td == nil {
return fmt.Errorf("tag definition %s not found", idOrName)
}
o.Print(td)
return nil
}
func createTagDefinition(ctx context.Context, o *cmdlib.Options) error {
if len(o.Args) < 3 {
return cmdlib.ErrorInvalidArgs
}
name := o.Args[0]
description := o.Args[1]
var applicableTypes []kbmodel.TagDefinitionApplicableObjectTypesEnum
if o.Args[2] == "all" {
for _, v := range kbmodel.TagDefinitionApplicableObjectTypesEnumValues {
applicableTypes = append(applicableTypes, kbmodel.TagDefinitionApplicableObjectTypesEnum(v))
}
} else {
list := strings.Split(o.Args[2], ",")
for _, v := range list {
e := kbmodel.TagDefinitionApplicableObjectTypesEnum(v)
if !e.IsValid() {
return fmt.Errorf("Invalid type: %s. Must be one of %s", v, strings.Join(kbmodel.TagObjectTypeEnumValues, ","))
}
}
}
tda := &kbmodel.TagDefinition{}
err := args.LoadProperties(tda, createTagDefinitionProperties, o.Args[3:])
if err != nil {
return err
}
t := &kbmodel.TagDefinition{
Name: &name,
Description: &description,
ApplicableObjectTypes: applicableTypes,
IsControlTag: tda.IsControlTag,
}
resp, err := o.Client().TagDefinition.CreateTagDefinition(ctx, &tag_definition.CreateTagDefinitionParams{
Body: t,
ProcessLocationHeader: true,
})
if err != nil {
return err
}
o.Print(resp.Payload)
return nil
}
func deleteTagDefinitions(ctx context.Context, o *cmdlib.Options) error {
if len(o.Args) != 1 {
return cmdlib.ErrorInvalidArgs
}
idOrName := o.Args[0]
td, err := kblib.GetTagDefinition(ctx, o.Client(), idOrName)
if err != nil {
return err
}
if td == nil {
return fmt.Errorf("tag definition %s not found", idOrName)
}
_, err = o.Client().TagDefinition.DeleteTagDefinition(ctx, &tag_definition.DeleteTagDefinitionParams{
TagDefinitionID: td.ID,
})
return err
}
func registerTagDefinitionCommands(r *cmdlib.App) {
cmdlib.AddFormatter(reflect.TypeOf(&kbmodel.TagDefinition{}), tagDefinitionFormatter)
createTagDefinitionProperties = args.GetProperties(&kbmodel.TagDefinition{})
createTagDefinitionProperties.Get("IsControlTag").Default = "False"
// Register top level command
r.Register("", cli.Command{
Name: "tags",
Usage: "tag related commands",
}, nil)
r.Register("tags", cli.Command{
Name: "list",
Usage: "list tag definitions",
}, listTagDefinitions)
r.Register("tags", cli.Command{
Name: "get",
Usage: "get tag definition by name or id",
ArgsUsage: "[NAME|ID]",
}, getTagDefinition)
r.Register("tags", cli.Command{
Name: "create",
Usage: "create new tag definition",
ArgsUsage: fmt.Sprintf(`NAME DESCRIPTION APPLICABLE_TYPES [IsControlTag=true/false]
APPLICABLE_TYPES: comma separated values of tag targets. (use 'all' for all targets)
Valid targets are:
%s
For e.g.,
kbcmd tags create premium_account "Account has high transaction volume" ACCOUNT,INVOICE`,
strings.Join(kbmodel.TagDefinitionApplicableObjectTypesEnumValues, "\n ")),
}, createTagDefinition)
r.Register("tags", cli.Command{
Name: "delete",
Usage: "delete tag definition",
ArgsUsage: `[ID|NAME]
For e.g.,
kbcmd tags delete mytag
(or)
kbcmd tags delete d3cb5fcc-2004-47ee-ace3-ce7002c909af`,
}, deleteTagDefinitions)
}