-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
444 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/replicated/pkg/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (r *runners) InitInstanceCommand(parent *cobra.Command) *cobra.Command { | ||
instanceCmd := &cobra.Command{ | ||
Use: "instance", | ||
Short: "Manage instances", | ||
Long: `The instance command allows vendors to display and tag customer instances.`, | ||
} | ||
parent.AddCommand(instanceCmd) | ||
|
||
return instanceCmd | ||
} | ||
|
||
func findInstanceByNameOrID(nameOrID string, instances []types.Instance) (types.Instance, error) { | ||
var instance types.Instance | ||
|
||
isFound := false | ||
for _, i := range instances { | ||
if i.InstanceID == nameOrID || i.Name() == nameOrID { | ||
if isFound { | ||
return types.Instance{}, errors.Errorf("multiple instances found with name or id %q", nameOrID) | ||
} | ||
instance = i | ||
isFound = true | ||
} | ||
} | ||
|
||
if !isFound { | ||
return types.Instance{}, errors.Errorf("instance %q not found", nameOrID) | ||
} | ||
|
||
return instance, nil | ||
} | ||
|
||
func findInstancesByTags(tags []types.Tag, instances []types.Instance) []types.Instance { | ||
result := []types.Instance{} | ||
|
||
for _, instance := range instances { | ||
for _, instanceTag := range instance.Tags { | ||
for _, tag := range tags { | ||
if instanceTag.Key == tag.Key && instanceTag.Value == tag.Value { | ||
result = append(result, instance) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/replicatedhq/replicated/cli/print" | ||
) | ||
|
||
func (r *runners) InitInstanceInspectCommand(parent *cobra.Command) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "inspect", | ||
Short: "Show full details for a customer instance", | ||
Long: `Show full details for a customer instance`, | ||
RunE: r.inspectInstance, | ||
SilenceUsage: false, | ||
SilenceErrors: true, // this command uses custom error printing | ||
} | ||
parent.AddCommand(cmd) | ||
cmd.Flags().StringVar(&r.args.instanceInspectCustomer, "customer", "", "Customer Name or ID") | ||
cmd.Flags().StringVar(&r.args.instanceInspectInstance, "instance", "", "Instance Name or ID") | ||
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)") | ||
|
||
return cmd | ||
} | ||
|
||
func (r *runners) inspectInstance(cmd *cobra.Command, _ []string) (err error) { | ||
defer func() { | ||
printIfError(cmd, err) | ||
}() | ||
|
||
if r.args.instanceInspectCustomer == "" { | ||
return errors.Errorf("missing or invalid parameters: customer") | ||
} | ||
|
||
if r.args.instanceInspectInstance == "" { | ||
return errors.Errorf("missing or invalid parameters: instance") | ||
} | ||
|
||
customer, err := r.api.GetCustomerByNameOrId(r.appType, r.appID, r.args.instanceInspectCustomer) | ||
if err != nil { | ||
return errors.Wrapf(err, "get customer %q", r.args.instanceInspectCustomer) | ||
} | ||
|
||
instance, err := findInstanceByNameOrID(r.args.instanceInspectInstance, customer.Instances) | ||
if err != nil { | ||
return errors.Wrap(err, "find instance") | ||
} | ||
|
||
if err = print.Instance(r.outputFormat, r.w, instance); err != nil { | ||
return errors.Wrap(err, "print instance") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/replicated/cli/print" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (r *runners) InitInstanceLSCommand(parent *cobra.Command) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "ls", | ||
Short: "list customer instances", | ||
Long: `list customer instances`, | ||
RunE: r.listInstances, | ||
SilenceUsage: false, | ||
SilenceErrors: true, // this command uses custom error printing | ||
} | ||
|
||
parent.AddCommand(cmd) | ||
cmd.Flags().StringVar(&r.args.instanceListCustomer, "customer", "", "Customer Name or ID") | ||
cmd.Flags().StringArrayVar(&r.args.instanceListTags, "tag", []string{}, "Tags to use to filter instances (key=value format, can be specified multiple times). Only one tag needs to match (an OR operation)") | ||
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)") | ||
|
||
return cmd | ||
} | ||
|
||
func (r *runners) listInstances(cmd *cobra.Command, _ []string) (err error) { | ||
defer func() { | ||
printIfError(cmd, err) | ||
}() | ||
|
||
if r.args.instanceListCustomer == "" { | ||
return errors.Errorf("missing or invalid parameters: customer") | ||
} | ||
|
||
customer, err := r.api.GetCustomerByNameOrId(r.appType, r.appID, r.args.instanceListCustomer) | ||
if err != nil { | ||
return errors.Wrapf(err, "get customer %q", r.args.instanceListCustomer) | ||
} | ||
|
||
instances := customer.Instances | ||
if len(r.args.instanceListTags) > 0 { | ||
tags, err := parseTags(r.args.instanceListTags) | ||
if err != nil { | ||
return errors.Wrap(err, "parse tags") | ||
} | ||
instances = findInstancesByTags(tags, customer.Instances) | ||
} | ||
|
||
if err := print.Instances(r.outputFormat, r.w, instances); err != nil { | ||
return errors.Wrap(err, "print instances") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/replicated/cli/print" | ||
"github.com/replicatedhq/replicated/pkg/kotsclient" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (r *runners) InitInstanceTagCommand(parent *cobra.Command) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "tag", | ||
Short: "tag an instance", | ||
Long: `remove or add instance tags`, | ||
RunE: r.tagInstance, | ||
SilenceUsage: true, | ||
} | ||
parent.AddCommand(cmd) | ||
cmd.Flags().StringVar(&r.args.instanceTagCustomer, "customer", "", "Customer Name or ID") | ||
cmd.Flags().StringVar(&r.args.instanceTagInstacne, "instance", "", "Instance Name or ID") | ||
cmd.Flags().StringArrayVar(&r.args.instanceTagTags, "tag", []string{}, "Tags to use to filter instances (key=value format, can be specified multiple times). Only one tag needs to match (an OR operation)") | ||
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)") | ||
|
||
return cmd | ||
} | ||
|
||
func (r *runners) tagInstance(cmd *cobra.Command, _ []string) (err error) { | ||
defer func() { | ||
printIfError(cmd, err) | ||
}() | ||
|
||
if r.args.instanceTagCustomer == "" { | ||
return errors.Errorf("missing or invalid parameters: customer") | ||
} | ||
|
||
if r.args.instanceTagInstacne == "" { | ||
return errors.Errorf("missing or invalid parameters: instance") | ||
} | ||
|
||
if len(r.args.instanceTagTags) == 0 { | ||
return errors.Errorf("missing or invalid parameters: tag") | ||
} | ||
|
||
tags, err := parseTags(r.args.instanceTagTags) | ||
if err != nil { | ||
return errors.Wrap(err, "parse tags") | ||
} | ||
|
||
customer, err := r.api.GetCustomerByNameOrId(r.appType, r.appID, r.args.instanceTagCustomer) | ||
if err != nil { | ||
return errors.Wrapf(err, "find customer %q", r.args.instanceTagCustomer) | ||
} | ||
|
||
instance, err := findInstanceByNameOrID(r.args.instanceTagInstacne, customer.Instances) | ||
if err != nil { | ||
return errors.Wrap(err, "find instance") | ||
} | ||
|
||
apiTags := []kotsclient.InstanceTag{} | ||
for _, tag := range tags { | ||
apiTags = append(apiTags, kotsclient.InstanceTag{ | ||
Key: tag.Key, | ||
Value: tag.Value, | ||
}) | ||
} | ||
|
||
updatedInstance, err := r.api.SetInstanceTags(r.appID, r.appType, customer.ID, instance.InstanceID, apiTags) | ||
if err != nil { | ||
return errors.Wrap(err, "set instance tags") | ||
} | ||
|
||
if err := print.Instance(r.outputFormat, r.w, *updatedInstance); err != nil { | ||
return errors.Wrap(err, "print instance") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.