diff --git a/client/client.go b/client/client.go index 9876187..dc88a5f 100644 --- a/client/client.go +++ b/client/client.go @@ -31,6 +31,9 @@ type PortainerClient interface { // Get endpoints GetEndpoints() ([]EndpointSubset, error) + // Get endpoint groups + GetEndpointGroups() ([]EndpointGroup, error) + // Get stacks, optionally filtered by swarmId and endpointId GetStacks(swarmId string, endpointId uint32) ([]Stack, error) @@ -217,6 +220,11 @@ func (n *portainerClientImp) GetEndpoints() (endpoints []EndpointSubset, err err return } +func (n *portainerClientImp) GetEndpointGroups() (endpointGroups []EndpointGroup, err error) { + err = n.doJSON("endpoint_groups", http.MethodGet, nil, &endpointGroups) + return +} + func (n *portainerClientImp) GetStacks(swarmId string, endpointId uint32) (stacks []Stack, err error) { filter := StackListFilter{ SwarmId: swarmId, diff --git a/client/portainerTypes.go b/client/portainerTypes.go index 0def3cf..2f35487 100644 --- a/client/portainerTypes.go +++ b/client/portainerTypes.go @@ -37,6 +37,12 @@ type EndpointSubset struct { GroupID uint32 } +type EndpointGroup struct { + Id uint32 + Name string + Description string +} + type StackCreateRequest struct { Name string SwarmID string diff --git a/cmd/endpointGroup.go b/cmd/endpointGroup.go new file mode 100644 index 0000000..72b8036 --- /dev/null +++ b/cmd/endpointGroup.go @@ -0,0 +1,15 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +// endpointGroupCmd represents the endpoint group command +var endpointGroupCmd = &cobra.Command{ + Use: "group", + Short: "Manage endpoint groups", +} + +func init() { + endpointCmd.AddCommand(endpointGroupCmd) +} diff --git a/cmd/endpointGroupList.go b/cmd/endpointGroupList.go new file mode 100644 index 0000000..874821a --- /dev/null +++ b/cmd/endpointGroupList.go @@ -0,0 +1,65 @@ +package cmd + +import ( + "fmt" + "os" + "text/template" + + "github.com/greenled/portainer-stack-utils/common" + "github.com/sirupsen/logrus" + "github.com/spf13/viper" + + "github.com/spf13/cobra" +) + +// endpointGroupListCmd represents the endpoint group list command +var endpointGroupListCmd = &cobra.Command{ + Use: "list", + Short: "List endpoint groups", + Aliases: []string{"ls"}, + Run: func(cmd *cobra.Command, args []string) { + client, err := common.GetClient() + common.CheckError(err) + + logrus.Debug("Getting endpoint groups") + endpointGroups, err := client.GetEndpointGroups() + common.CheckError(err) + + if viper.GetString("endpoint.group.list.format") != "" { + // Print endpoint group fields formatted + template, templateParsingErr := template.New("endpointGroupTpl").Parse(viper.GetString("endpoint.group.list.format")) + common.CheckError(templateParsingErr) + for _, g := range endpointGroups { + templateExecutionErr := template.Execute(os.Stdout, g) + common.CheckError(templateExecutionErr) + fmt.Println() + } + } else { + // Print all endpoint group fields as a table + writer, err := common.NewTabWriter([]string{ + "ENDPOINT GROUP ID", + "NAME", + "DESCRIPTION", + }) + common.CheckError(err) + for _, g := range endpointGroups { + _, err := fmt.Fprintln(writer, fmt.Sprintf( + "%v\t%s\t%s", + g.Id, + g.Name, + g.Description, + )) + common.CheckError(err) + } + flushErr := writer.Flush() + common.CheckError(flushErr) + } + }, +} + +func init() { + endpointGroupCmd.AddCommand(endpointGroupListCmd) + + endpointGroupListCmd.Flags().String("format", "", "Format output using a Go template.") + viper.BindPFlag("endpoint.group.list.format", endpointGroupListCmd.Flags().Lookup("format")) +}