Skip to content

Commit

Permalink
feat: add ability to disable tenants (#445)
Browse files Browse the repository at this point in the history
* feat: add ability to disable tenants

* fix: disable wording
  • Loading branch information
grace-rehn authored Nov 20, 2024
1 parent 801cb05 commit 44063aa
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/OctopusDeploy/go-octodiff v1.0.0
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.54.0
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.58.0
github.com/bmatcuk/doublestar/v4 v4.4.0
github.com/briandowns/spinner v1.19.0
github.com/google/uuid v1.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,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-octodiff v1.0.0 h1:U+ORg6azniwwYo+O44giOw6TiD5USk8S4VDhOQ0Ven0=
github.com/OctopusDeploy/go-octodiff v1.0.0/go.mod h1:Mze0+EkOWTgTmi8++fyUc6r0aLZT7qD9gX+31t8MmIU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.54.0 h1:cmtRQZxy4W+871TnhebdaKCg/Woxu5JOgasl/hHWUjA=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.54.0/go.mod h1:ggvOXzMnq+w0pLg6C9zdjz6YBaHfO3B3tqmmB7JQdaw=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.58.0 h1:vWw3dS4bEghzXnnGzoDD3Wm434MqNIwD2g73XeJebW0=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.58.0/go.mod h1:ggvOXzMnq+w0pLg6C9zdjz6YBaHfO3B3tqmmB7JQdaw=
github.com/bmatcuk/doublestar/v4 v4.4.0 h1:LmAwNwhjEbYtyVLzjcP/XeVw4nhuScHGkF/XWXnvIic=
github.com/bmatcuk/doublestar/v4 v4.4.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
Expand Down
84 changes: 84 additions & 0 deletions pkg/cmd/tenant/disable/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package disable

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/question/selectors"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants"
"github.com/spf13/cobra"
)

type DisableOptions struct {
*cmd.Dependencies
IdOrName string
}

func NewDisableOptions(args []string, dependencies *cmd.Dependencies) *DisableOptions {
return &DisableOptions{
Dependencies: dependencies,
IdOrName: args[0],
}
}

func NewCmdDisable(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "disable",
Short: "Disable a tenant",
Long: "Disable a tenant in Octopus Deploy",
Example: heredoc.Docf("$ %[1]s tenant enable", constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
if len(args) == 0 {
args = append(args, "")
}

opts := NewDisableOptions(args, cmd.NewDependencies(f, c))
return disableRun(opts)
},
}

return cmd
}

func disableRun(opts *DisableOptions) error {
if !opts.NoPrompt {
if err := PromptMissing(opts); err != nil {
return err
}
}

if opts.IdOrName == "" {
return fmt.Errorf("tenant identifier is required but was not provided")
}

tenantToUpdate, err := opts.Client.Tenants.GetByIdentifier(opts.IdOrName)
if err != nil {
return err
}

tenantToUpdate.IsDisabled = true
_, err = tenants.Update(opts.Client, tenantToUpdate)
if err != nil {
return err
}

return nil
}

func PromptMissing(opts *DisableOptions) error {
if opts.IdOrName == "" {
existingTenants, err := opts.Client.Tenants.GetAll()
if err != nil {
return err
}
selectedTenant, err := selectors.ByName(opts.Ask, existingTenants, "Select the tenant you wish to disable:")
if err != nil {
return err
}
opts.IdOrName = selectedTenant.GetID()
}

return nil
}
84 changes: 84 additions & 0 deletions pkg/cmd/tenant/enable/enable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package enable

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/question/selectors"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants"
"github.com/spf13/cobra"
)

type EnableOptions struct {
*cmd.Dependencies
IdOrName string
}

func NewEnableOptions(args []string, dependencies *cmd.Dependencies) *EnableOptions {
return &EnableOptions{
Dependencies: dependencies,
IdOrName: args[0],
}
}

func NewCmdEnable(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "enable",
Short: "Enable a tenant",
Long: "Enable a tenant in Octopus Deploy",
Example: heredoc.Docf("$ %[1]s tenant enable", constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
if len(args) == 0 {
args = append(args, "")
}

opts := NewEnableOptions(args, cmd.NewDependencies(f, c))
return enableRun(opts)
},
}

return cmd
}

func enableRun(opts *EnableOptions) error {
if !opts.NoPrompt {
if err := PromptMissing(opts); err != nil {
return err
}
}

if opts.IdOrName == "" {
return fmt.Errorf("tenant identifier is required but was not provided")
}

tenantToUpdate, err := opts.Client.Tenants.GetByIdentifier(opts.IdOrName)
if err != nil {
return err
}

tenantToUpdate.IsDisabled = false
_, err = tenants.Update(opts.Client, tenantToUpdate)
if err != nil {
return err
}

return nil
}

func PromptMissing(opts *EnableOptions) error {
if opts.IdOrName == "" {
existingTenants, err := opts.Client.Tenants.GetAll()
if err != nil {
return err
}
selectedTenant, err := selectors.ByName(opts.Ask, existingTenants, "Select the tenant you wish to enable:")
if err != nil {
return err
}
opts.IdOrName = selectedTenant.GetID()
}

return nil
}
5 changes: 3 additions & 2 deletions pkg/cmd/tenant/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants"
"github.com/spf13/cobra"
"strconv"
)

func NewCmdList(f factory.Factory) *cobra.Command {
Expand Down Expand Up @@ -71,9 +72,9 @@ func listRun(cmd *cobra.Command, f factory.Factory) error {
}
},
Table: output.TableDefinition[*tenants.Tenant]{
Header: []string{"NAME", "DESCRIPTION", "ID", "TAGS"},
Header: []string{"NAME", "DESCRIPTION", "ID", "IS DISABLED", "TAGS"},
Row: func(t *tenants.Tenant) []string {
return []string{output.Bold(t.Name), t.Description, output.Dim(t.GetID()), output.FormatAsList(t.TenantTags)}
return []string{output.Bold(t.Name), t.Description, output.Dim(t.GetID()), strconv.FormatBool(t.IsDisabled), output.FormatAsList(t.TenantTags)}
},
},
Basic: func(t *tenants.Tenant) string {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/tenant/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
cmdConnect "github.com/OctopusDeploy/cli/pkg/cmd/tenant/connect"
cmdCreate "github.com/OctopusDeploy/cli/pkg/cmd/tenant/create"
cmdDelete "github.com/OctopusDeploy/cli/pkg/cmd/tenant/delete"
cmdDisable "github.com/OctopusDeploy/cli/pkg/cmd/tenant/disable"
cmdDisconnect "github.com/OctopusDeploy/cli/pkg/cmd/tenant/disconnect"
cmdEnable "github.com/OctopusDeploy/cli/pkg/cmd/tenant/enable"
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/tenant/list"
cmdTag "github.com/OctopusDeploy/cli/pkg/cmd/tenant/tag"
cmdVariable "github.com/OctopusDeploy/cli/pkg/cmd/tenant/variables"
Expand Down Expand Up @@ -40,6 +42,8 @@ func NewCmdTenant(f factory.Factory) *cobra.Command {
cmd.AddCommand(cmdDelete.NewCmdDelete(f))
cmd.AddCommand(cmdView.NewCmdView(f))
cmd.AddCommand(cmdVariable.NewCmdVariables(f))
cmd.AddCommand(cmdEnable.NewCmdEnable(f))
cmd.AddCommand(cmdDisable.NewCmdDisable(f))

return cmd
}
11 changes: 9 additions & 2 deletions pkg/cmd/tenant/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package view
import (
"fmt"
"io"
"strconv"
"strings"

"github.com/OctopusDeploy/cli/pkg/apiclient"
Expand Down Expand Up @@ -118,9 +119,9 @@ func viewRun(opts *ViewOptions, cmd *cobra.Command) error {
}
},
Table: output.TableDefinition[*tenants.Tenant]{
Header: []string{"NAME", "DESCRIPTION", "ID", "TAGS"},
Header: []string{"NAME", "DESCRIPTION", "ID", "IS DISABLED", "TAGS"},
Row: func(t *tenants.Tenant) []string {
return []string{output.Bold(t.Name), t.Description, output.Dim(t.GetID()), output.FormatAsList(t.TenantTags)}
return []string{output.Bold(t.Name), t.Description, output.Dim(t.GetID()), strconv.FormatBool(t.IsDisabled), output.FormatAsList(t.TenantTags)}
},
},
Basic: func(item *tenants.Tenant) string {
Expand All @@ -138,6 +139,12 @@ func viewRun(opts *ViewOptions, cmd *cobra.Command) error {
s.WriteString(fmt.Sprintln(output.Dim(tenant.Description)))
}

if tenant.IsDisabled {
s.WriteString(fmt.Sprintln("Tenant is disabled"))
} else {
s.WriteString(fmt.Sprintln("Tenant is enabled"))
}

link := fmt.Sprintf("%s/app#/%s/tenants/%s/overview", opts.Host, tenant.SpaceID, tenant.ID)
// footer
s.WriteString(fmt.Sprintf("View this tenant in Octopus Deploy: %s\n", output.Blue(link)))
Expand Down

0 comments on commit 44063aa

Please sign in to comment.