Skip to content

Commit

Permalink
feat: target list
Browse files Browse the repository at this point in the history
 feat: deployment-target list
 feat: deployment-target cloud-region list
 feat: deployment-target listening-tentacle list
 feat: deployment-target polling-tentacle list
 feat: deployment-target azure-web-app list
 feat: deployment-target ssh list
  • Loading branch information
benPearce1 authored Nov 16, 2022
1 parent a169d2e commit 4ab66c2
Show file tree
Hide file tree
Showing 17 changed files with 389 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.0
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.1
github.com/briandowns/spinner v1.19.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.14.0 h1:BMFtEXFvzp+m942qRBfautdPE4V+jzWBFLSB2ShTDGc=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.14.0/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.0 h1:8R5q30r9oGJKclKbXDZdWHjyZdosYjqKQsiSaK6E9L8=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.0/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.1 h1:AJ/UhXqHpFZOK2EtdSBvqTseIDb9VxqXS9Rosneco8w=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.1/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/target/azure-web-app/azure-web-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azure_web_app
import (
"fmt"
cmdCreate "github.com/OctopusDeploy/cli/pkg/cmd/target/azure-web-app/create"
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/target/azure-web-app/list"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/spf13/cobra"
Expand All @@ -16,7 +17,7 @@ func NewCmdAzureWebApp(f factory.Factory) *cobra.Command {
Example: fmt.Sprintf("$ %s deployment-target azure-web-app list", constants.ExecutableName),
}

//cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdCreate.NewCmdCreate(f))
return cmd
}
30 changes: 30 additions & 0 deletions pkg/cmd/target/azure-web-app/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package list

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/target/list"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
)

func NewCmdList(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List Azure Web App deployment targets in an instance of Octopus Deploy",
Long: "List Azure Web App deployment targets in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s deployment-target azure-web-app list
`), constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
dependencies := cmd.NewDependencies(f, c)
options := list.NewListOptions(dependencies, c, machines.MachinesQuery{DeploymentTargetTypes: []string{"AzureWebApp"}})
return list.ListRun(options)
},
}

return cmd
}
2 changes: 2 additions & 0 deletions pkg/cmd/target/cloud-region/cloud-region.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud_region
import (
"fmt"
cmdCreate "github.com/OctopusDeploy/cli/pkg/cmd/target/cloud-region/create"
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/target/cloud-region/list"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/spf13/cobra"
Expand All @@ -17,5 +18,6 @@ func NewCmdCloudRegion(f factory.Factory) *cobra.Command {
}

cmd.AddCommand(cmdCreate.NewCmdCreate(f))
cmd.AddCommand(cmdList.NewCmdList(f))
return cmd
}
30 changes: 30 additions & 0 deletions pkg/cmd/target/cloud-region/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package list

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/target/list"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
)

func NewCmdList(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List Cloud Region deployment targets in an instance of Octopus Deploy",
Long: "List Cloud Region deployment targets in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s deployment-target cloud-region list
`), constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
dependencies := cmd.NewDependencies(f, c)
options := list.NewListOptions(dependencies, c, machines.MachinesQuery{DeploymentTargetTypes: []string{"CloudRegion"}})
return list.ListRun(options)
},
}

return cmd
}
10 changes: 5 additions & 5 deletions pkg/cmd/target/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (

type DeleteOptions struct {
*cmd.Dependencies
*shared.GetAllTargetsOptions
*shared.GetTargetsOptions
}

func NewDeleteOptions(dependencies *cmd.Dependencies) *DeleteOptions {
return &DeleteOptions{
Dependencies: dependencies,
GetAllTargetsOptions: shared.NewGetAllTargetsOptions(dependencies),
Dependencies: dependencies,
GetTargetsOptions: shared.NewGetTargetsOptionsForAllTargets(dependencies),
}
}

Expand All @@ -48,7 +48,7 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {

idOrName := args[0]
opts := NewDeleteOptions(deps)
targets, err := opts.GetAllTargetsCallback()
targets, err := opts.GetTargetsCallback()
if err != nil {
return err
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func NewCmdDelete(f factory.Factory) *cobra.Command {
}

func deleteRun(opts *DeleteOptions) error {
itemToDelete, err := selectors.Select(opts.Ask, "Select the deployment target you wish to delete:", opts.GetAllTargetsCallback, func(target *machines.DeploymentTarget) string { return target.Name })
itemToDelete, err := selectors.Select(opts.Ask, "Select the deployment target you wish to delete:", opts.GetTargetsCallback, func(target *machines.DeploymentTarget) string { return target.Name })
if err != nil {
return err
}
Expand Down
148 changes: 148 additions & 0 deletions pkg/cmd/target/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package list

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/target/shared"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
"strings"
)

type ListOptions struct {
*cobra.Command
*cmd.Dependencies
*shared.GetTargetsOptions
}

type Entity struct {
Id string `json:"Id"`
Name string `json:"Name"`
}

func NewListOptions(dependencies *cmd.Dependencies, command *cobra.Command, query machines.MachinesQuery) *ListOptions {
return &ListOptions{
Command: command,
Dependencies: dependencies,
GetTargetsOptions: shared.NewGetTargetsOptions(dependencies, query),
}
}

func NewCmdList(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List deployment targets in an instance of Octopus Deploy",
Long: "List deployment targets in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s deployment-target list
`), constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
return ListRun(NewListOptions(cmd.NewDependencies(f, c), c, machines.MachinesQuery{}))
},
}

return cmd
}

func ListRun(opts *ListOptions) error {
allTargets, err := opts.GetTargetsCallback()
if err != nil {
return err
}

type TargetAsJson struct {
Id string `json:"Id"`
Name string `json:"Name"`
Type string `json:"Type"`
Roles []string `json:"Roles"`
Environments []Entity `json:"Environments"`
Tenants []Entity `json:"Tenants"`
TenantTags []string `json:"TenantTags"`
}

environmentMap, err := GetEnvironmentMap(opts, err)
if err != nil {
return err
}

tenantMap, err := GetTenantMap(opts, err)
if err != nil {
return err
}

return output.PrintArray(allTargets, opts.Command, output.Mappers[*machines.DeploymentTarget]{
Json: func(item *machines.DeploymentTarget) any {
environments := resolveEntities(item.EnvironmentIDs, environmentMap)
tenants := resolveEntities(item.TenantIDs, tenantMap)
return TargetAsJson{
Id: item.GetID(),
Name: item.Name,
Type: shared.CommunicationStyleToDeploymentTargetTypeMap[item.Endpoint.GetCommunicationStyle()],
Roles: item.Roles,
Environments: environments,
Tenants: tenants,
TenantTags: item.TenantTags,
}
},
Table: output.TableDefinition[*machines.DeploymentTarget]{
Header: []string{"NAME", "TYPE", "ROLES", "ENVIRONMENTS", "TENANTS", "TAGS"},
Row: func(item *machines.DeploymentTarget) []string {
environmentNames := resolveValues(item.EnvironmentIDs, environmentMap)
tenantNames := resolveValues(item.TenantIDs, tenantMap)
return []string{output.Bold(item.Name), shared.CommunicationStyleToDescriptionMap[item.Endpoint.GetCommunicationStyle()], formatAsList(item.Roles), formatAsList(environmentNames), formatAsList(tenantNames), formatAsList(item.TenantTags)}
},
},
Basic: func(item *machines.DeploymentTarget) string {
return item.Name
},
})
}

func resolveValues(keys []string, lookup map[string]string) []string {
var values []string
for _, key := range keys {
values = append(values, lookup[key])
}
return values
}

func resolveEntities(keys []string, lookup map[string]string) []Entity {
var entities []Entity
for _, k := range keys {
entities = append(entities, Entity{Id: k, Name: lookup[k]})
}

return entities
}

func GetEnvironmentMap(opts *ListOptions, err error) (map[string]string, error) {
environmentMap := make(map[string]string)
allEnvs, err := opts.Client.Environments.GetAll()
if err != nil {
return nil, err
}
for _, e := range allEnvs {
environmentMap[e.GetID()] = e.GetName()
}
return environmentMap, nil
}

func GetTenantMap(opts *ListOptions, err error) (map[string]string, error) {
tenantMap := make(map[string]string)
allEnvs, err := opts.Client.Tenants.GetAll()
if err != nil {
return nil, err
}
for _, e := range allEnvs {
tenantMap[e.GetID()] = e.Name
}
return tenantMap, nil
}

func formatAsList(items []string) string {
return strings.Join(items, ",")
}
30 changes: 30 additions & 0 deletions pkg/cmd/target/listening-tentacle/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package list

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/target/list"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
)

func NewCmdList(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List Listening Tentacle deployment targets in an instance of Octopus Deploy",
Long: "List Listening Tentacle deployment targets in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s deployment-target listening-tentacle list
`), constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
dependencies := cmd.NewDependencies(f, c)
options := list.NewListOptions(dependencies, c, machines.MachinesQuery{DeploymentTargetTypes: []string{"TentaclePassive"}})
return list.ListRun(options)
},
}

return cmd
}
3 changes: 2 additions & 1 deletion pkg/cmd/target/listening-tentacle/listening-tentacle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

cmdCreate "github.com/OctopusDeploy/cli/pkg/cmd/target/listening-tentacle/create"
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/target/listening-tentacle/list"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/spf13/cobra"
Expand All @@ -17,7 +18,7 @@ func NewCmdListeningTentacle(f factory.Factory) *cobra.Command {
Example: fmt.Sprintf("$ %s deployment-target listening-tenatacle list", constants.ExecutableName),
}

//cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdCreate.NewCmdCreate(f))
return cmd
}
30 changes: 30 additions & 0 deletions pkg/cmd/target/polling-tentacle/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package list

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/target/list"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
)

func NewCmdList(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List Polling Tentacle deployment targets in an instance of Octopus Deploy",
Long: "List Polling Tentacle deployment targets in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s deployment-target polling-tentacle list
`), constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
dependencies := cmd.NewDependencies(f, c)
options := list.NewListOptions(dependencies, c, machines.MachinesQuery{DeploymentTargetTypes: []string{"TentacleActive"}})
return list.ListRun(options)
},
}

return cmd
}
22 changes: 22 additions & 0 deletions pkg/cmd/target/polling-tentacle/polling-tentacle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package polling_tentacle

import (
"fmt"

cmdList "github.com/OctopusDeploy/cli/pkg/cmd/target/polling-tentacle/list"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/spf13/cobra"
)

func NewCmdPollingTentacle(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "polling-tentacle <command>",
Short: "Manage polling tentacle deployment targets",
Long: "Work with Octopus Deploy polling tentacle deployment targets.",
Example: fmt.Sprintf("$ %s deployment-target polling-tenatacle list", constants.ExecutableName),
}

cmd.AddCommand(cmdList.NewCmdList(f))
return cmd
}
Loading

0 comments on commit 4ab66c2

Please sign in to comment.