Skip to content

Commit e6872bb

Browse files
authored
troubleshoot/collector for vendor-cli (#56)
troubleshoot/collector cli feature
1 parent 57adee8 commit e6872bb

36 files changed

+1280
-192
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dist/
22
.glide
33
gen/docs
44
logs/
5+
.DS_Store

cli/cmd/collector_create.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func (r *runners) InitCollectorCreate(parent *cobra.Command) {
11+
cmd := &cobra.Command{
12+
Use: "create",
13+
Short: "Create a new collector",
14+
Long: "Create a new collector by providing a name and YAML configuration",
15+
}
16+
17+
parent.AddCommand(cmd)
18+
19+
cmd.Flags().StringVar(&r.args.createCollectorYaml, "yaml", "", "The YAML config for this collector. Use '-' to read from stdin. Cannot be used with the `yaml-file` falg.")
20+
cmd.Flags().StringVar(&r.args.createCollectorYamlFile, "yaml-file", "", "The file name with YAML config for this collector. Cannot be used with the `yaml` flag.")
21+
cmd.Flags().StringVar(&r.args.createCollectorName, "name", "", "The name for this collector")
22+
23+
cmd.RunE = r.collectorCreate
24+
}
25+
26+
func (r *runners) collectorCreate(cmd *cobra.Command, args []string) error {
27+
28+
if r.args.createCollectorName == "" {
29+
return fmt.Errorf("collector name is required")
30+
}
31+
32+
if r.args.createCollectorYaml == "" && r.args.createCollectorYamlFile == "" {
33+
return fmt.Errorf("yaml is required")
34+
}
35+
36+
if r.args.createCollectorYaml != "" && r.args.createCollectorYamlFile != "" {
37+
return fmt.Errorf("only one of yaml or yaml-file may be specified")
38+
}
39+
40+
if r.args.createCollectorYaml == "-" {
41+
bytes, err := ioutil.ReadAll(r.stdin)
42+
if err != nil {
43+
return err
44+
}
45+
r.args.createCollectorYaml = string(bytes)
46+
}
47+
48+
if r.args.createCollectorYamlFile != "" {
49+
bytes, err := ioutil.ReadFile(r.args.createCollectorYamlFile)
50+
if err != nil {
51+
return err
52+
}
53+
r.args.createCollectorYaml = string(bytes)
54+
}
55+
56+
_, err := r.api.CreateCollector(r.appID, r.args.createCollectorName, r.args.createCollectorYaml)
57+
if err != nil {
58+
return err
59+
}
60+
61+
fmt.Fprintf(r.w, "Collector %s created\n", r.args.createCollectorName)
62+
r.w.Flush()
63+
64+
return nil
65+
66+
}

cli/cmd/collector_inspect.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/replicatedhq/replicated/cli/print"
8+
"github.com/replicatedhq/replicated/pkg/platformclient"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func (r *runners) InitCollectorInspect(parent *cobra.Command) {
13+
cmd := &cobra.Command{
14+
Use: "inspect SPEC_ID",
15+
Short: "Print the YAML config for a collector",
16+
Long: "Print the YAML config for a collector",
17+
}
18+
19+
parent.AddCommand(cmd)
20+
cmd.RunE = r.collectorInspect
21+
}
22+
23+
func (r *runners) collectorInspect(cmd *cobra.Command, args []string) error {
24+
if len(args) != 1 {
25+
return errors.New("collector ID is required")
26+
}
27+
id := args[0]
28+
29+
collector, err := r.api.GetCollector(r.appID, id)
30+
if err != nil {
31+
if err == platformclient.ErrNotFound {
32+
return fmt.Errorf("No such collector %d", id)
33+
}
34+
return err
35+
}
36+
37+
return print.Collector(r.w, collector)
38+
}

cli/cmd/collector_ls.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"github.com/replicatedhq/replicated/cli/print"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func (r *runners) InitCollectorList(parent *cobra.Command) {
9+
cmd := &cobra.Command{
10+
Use: "ls",
11+
Short: "List all of an app's collectors",
12+
Long: "List all of an app's collectors",
13+
}
14+
15+
parent.AddCommand(cmd)
16+
cmd.RunE = r.collectorList
17+
}
18+
19+
func (r *runners) collectorList(cmd *cobra.Command, args []string) error {
20+
collectors, err := r.api.ListCollectors(r.appID)
21+
if err != nil {
22+
return err
23+
}
24+
25+
return print.Collectors(r.w, collectors)
26+
}

cli/cmd/collector_promote.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func (r *runners) InitCollectorPromote(parent *cobra.Command) {
11+
cmd := &cobra.Command{
12+
Use: "promote SPEC_ID CHANNEL_ID",
13+
Short: "Set the collector for a channel",
14+
Long: `Set the collector for a channel
15+
16+
Example: replicated collectors promote skd204095829040 fe4901690971757689f022f7a460f9b2`,
17+
}
18+
19+
parent.AddCommand(cmd)
20+
21+
cmd.RunE = r.collectorPromote
22+
}
23+
24+
func (r *runners) collectorPromote(cmd *cobra.Command, args []string) error {
25+
// parse spec ID and channel ID positional arguments
26+
if len(args) != 2 {
27+
return errors.New("collector spec ID and channel ID are required")
28+
}
29+
specID := args[0]
30+
chanID := args[1]
31+
32+
err := r.api.PromoteCollector(r.appID, specID, chanID)
33+
34+
if err != nil {
35+
return err
36+
}
37+
38+
// ignore error since operation was successful
39+
fmt.Fprintf(r.w, "Collector %s successfully promoted to channel %s\n", specID, chanID)
40+
41+
r.w.Flush()
42+
43+
return nil
44+
45+
}

cli/cmd/collector_update.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io/ioutil"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func (r *runners) InitCollectorUpdate(parent *cobra.Command) {
12+
cmd := &cobra.Command{
13+
Use: "update SPEC_ID",
14+
Short: "Update a collector's name or yaml config",
15+
Long: "Update a collector's name or yaml config",
16+
}
17+
18+
parent.AddCommand(cmd)
19+
20+
cmd.Flags().StringVar(&r.args.updateCollectorYaml, "yaml", "", "The new YAML config for this collector. Use '-' to read from stdin. Cannot be used with the `yaml-file` flag.")
21+
cmd.Flags().StringVar(&r.args.updateCollectorYamlFile, "yaml-file", "", "The file name with YAML config for this collector. Cannot be used with the `yaml` flag.")
22+
cmd.Flags().StringVar(&r.args.updateCollectorName, "name", "", "The name for this collector")
23+
24+
cmd.RunE = r.collectorUpdate
25+
}
26+
27+
func (r *runners) collectorUpdate(cmd *cobra.Command, args []string) error {
28+
29+
if len(args) < 1 {
30+
return errors.New("collector spec ID is required")
31+
}
32+
specID := args[0]
33+
34+
if r.args.updateCollectorName == "" && r.args.updateCollectorYaml == "" && r.args.updateCollectorYamlFile == "" {
35+
return fmt.Errorf("name or yaml is required")
36+
}
37+
38+
if r.args.updateCollectorYaml != "" && r.args.updateCollectorYamlFile != "" {
39+
return fmt.Errorf("only yaml or yaml-file has to be specified")
40+
}
41+
42+
if r.args.updateCollectorYaml == "-" {
43+
bytes, err := ioutil.ReadAll(r.stdin)
44+
if err != nil {
45+
return err
46+
}
47+
r.args.updateCollectorYaml = string(bytes)
48+
}
49+
50+
if r.args.updateCollectorYamlFile != "" {
51+
bytes, err := ioutil.ReadFile(r.args.updateCollectorYamlFile)
52+
if err != nil {
53+
return err
54+
}
55+
r.args.updateCollectorYaml = string(bytes)
56+
}
57+
58+
if r.args.updateCollectorYaml != "" {
59+
_, err := r.api.UpdateCollector(r.appID, specID, r.args.updateCollectorYaml)
60+
if err != nil {
61+
return fmt.Errorf("Failure setting updates for collector: %v", err)
62+
}
63+
}
64+
65+
if r.args.updateCollectorName != "" {
66+
_, err := r.api.UpdateCollectorName(r.appID, specID, r.args.updateCollectorName)
67+
if err != nil {
68+
return fmt.Errorf("Failure setting new yaml config for collector: %v", err)
69+
}
70+
}
71+
72+
// ignore the error since operation was successful
73+
fmt.Fprintf(r.w, "Collector %s updated\n", specID)
74+
r.w.Flush()
75+
76+
return nil
77+
}

cli/cmd/collectors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
func (r *runners) InitCollectorsCommand(parent *cobra.Command) *cobra.Command {
8+
collectorsCmd := &cobra.Command{
9+
Use: "collector",
10+
Short: "Manage customer collectors",
11+
Long: `The collector command allows vendors to create, display, modify entitlement values for end customer licensing.`,
12+
}
13+
parent.AddCommand(collectorsCmd)
14+
15+
var tmp bool
16+
collectorsCmd.Flags().BoolVar(&tmp, "active", false, "Only show active collectors")
17+
18+
return collectorsCmd
19+
}

cli/cmd/entitlements.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"github.com/spf13/cobra"
55
)
66

7-
8-
97
func (r *runners) InitEntitlementsCommand(parent *cobra.Command) *cobra.Command {
108
entitlementsCmd := &cobra.Command{
119
Use: "entitlements",
@@ -19,4 +17,3 @@ func (r *runners) InitEntitlementsCommand(parent *cobra.Command) *cobra.Command
1917

2018
return entitlementsCmd
2119
}
22-

cli/cmd/root.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
var appSlugOrID string
2525
var apiToken string
2626
var platformOrigin = "https://api.replicated.com/vendor"
27-
var shipOrigin = "https://g.replicated.com/graphql"
27+
var graphqlOrigin = "https://g.replicated.com/graphql"
2828

2929
func init() {
3030
originFromEnv := os.Getenv("REPLICATED_API_ORIGIN")
@@ -34,16 +34,16 @@ func init() {
3434

3535
shipOriginFromEnv := os.Getenv("REPLICATED_SHIP_ORIGIN")
3636
if shipOriginFromEnv != "" {
37-
shipOrigin = shipOriginFromEnv
37+
graphqlOrigin = shipOriginFromEnv
3838
}
3939
}
4040

4141
// RootCmd represents the base command when called without any subcommands
4242
func GetRootCmd() *cobra.Command {
4343
rootCmd := &cobra.Command{
4444
Use: "replicated",
45-
Short: "Manage channels and releases",
46-
Long: `The replicated CLI allows vendors to manage their apps' channels and releases.`,
45+
Short: "Manage channels, releases and collectors",
46+
Long: `The replicated CLI allows vendors to manage their apps' channels, releases and collectors.`,
4747
}
4848
rootCmd.PersistentFlags().StringVar(&appSlugOrID, "app", "", "The app slug or app id to use in all calls")
4949
rootCmd.PersistentFlags().StringVar(&apiToken, "token", "", "The API token to use to access your app in the Vendor API")
@@ -124,6 +124,13 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
124124
runCmds.InitReleasePromote(releaseCmd)
125125
runCmds.InitReleaseLint(releaseCmd)
126126

127+
collectorsCmd := runCmds.InitCollectorsCommand(runCmds.rootCmd)
128+
runCmds.InitCollectorList(collectorsCmd)
129+
runCmds.InitCollectorUpdate(collectorsCmd)
130+
runCmds.InitCollectorPromote(collectorsCmd)
131+
runCmds.InitCollectorCreate(collectorsCmd)
132+
runCmds.InitCollectorInspect(collectorsCmd)
133+
127134
entitlementsCmd := runCmds.InitEntitlementsCommand(runCmds.rootCmd)
128135
runCmds.InitEntitlementsDefineFields(entitlementsCmd)
129136
runCmds.InitEntitlementsSetValueCommand(entitlementsCmd)
@@ -141,10 +148,10 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
141148
platformAPI := platformclient.NewHTTPClient(platformOrigin, apiToken)
142149
runCmds.platformAPI = platformAPI
143150

144-
shipAPI := shipclient.NewGraphQLClient(shipOrigin, apiToken)
151+
shipAPI := shipclient.NewGraphQLClient(graphqlOrigin, apiToken)
145152
runCmds.shipAPI = shipAPI
146153

147-
commonAPI := client.NewClient(platformOrigin, shipOrigin, apiToken)
154+
commonAPI := client.NewClient(platformOrigin, graphqlOrigin, apiToken)
148155
runCmds.api = commonAPI
149156

150157
if appSlugOrID == "" {

cli/cmd/runner.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ type runnerArgs struct {
3030
channelCreateName string
3131
channelCreateDescription string
3232

33+
createCollectorName string
34+
createCollectorYaml string
35+
createCollectorYamlFile string
36+
updateCollectorYaml string
37+
updateCollectorYamlFile string
38+
updateCollectorName string
39+
3340
createReleaseYaml string
3441
createReleaseYamlFile string
3542
createReleasePromote string
@@ -44,16 +51,16 @@ type runnerArgs struct {
4451
updateReleaseYaml string
4552
updateReleaseYamlFile string
4653

47-
entitlementsAPIServer string
48-
entitlementsVerbose bool
49-
entitlementsDefineFieldsFile string
50-
entitlementsDefineFieldsName string
51-
entitlementsGetReleaseCustomerID string
54+
entitlementsAPIServer string
55+
entitlementsVerbose bool
56+
entitlementsDefineFieldsFile string
57+
entitlementsDefineFieldsName string
58+
entitlementsGetReleaseCustomerID string
5259
entitlementsGetReleaseInstallationID string
53-
entitlementsGetReleaseAPIServer string
54-
entitlementsSetValueCustomerID string
55-
entitlementsSetValueDefinitionsID string
56-
entitlementsSetValueKey string
57-
entitlementsSetValueValue string
58-
entitlementsSetValueType string
60+
entitlementsGetReleaseAPIServer string
61+
entitlementsSetValueCustomerID string
62+
entitlementsSetValueDefinitionsID string
63+
entitlementsSetValueKey string
64+
entitlementsSetValueValue string
65+
entitlementsSetValueType string
5966
}

0 commit comments

Comments
 (0)