Skip to content

Commit

Permalink
Feature: Organizations support (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Dec 5, 2023
1 parent eeb83b5 commit 472d8ce
Show file tree
Hide file tree
Showing 36 changed files with 2,021 additions and 21 deletions.
10 changes: 10 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@ var addNotificationCmd = &cobra.Command{
},
}

var addOrganizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Add an organization, or add a group/project to an organization",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid
},
}

func init() {
addCmd.AddCommand(addDeployTargetCmd)
addCmd.AddCommand(addGroupCmd)
addCmd.AddCommand(addProjectCmd)
addCmd.AddCommand(addProjectToGroupCmd)
addCmd.AddCommand(addNotificationCmd)
addCmd.AddCommand(addUserCmd)
addCmd.AddCommand(addOrganizationCmd)
addCmd.AddCommand(addUserToGroupCmd)
addCmd.AddCommand(addUserSSHKeyCmd)
addCmd.AddCommand(addVariableCmd)
Expand Down
10 changes: 10 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ var deleteNotificationCmd = &cobra.Command{
},
}

var deleteOrganizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Add an organization, or add a group/project to an organization",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid
},
}

func init() {
deleteCmd.AddCommand(deleteEnvCmd)
deleteCmd.AddCommand(deleteGroupCmd)
Expand All @@ -34,4 +43,5 @@ func init() {
deleteCmd.AddCommand(deleteUserFromGroupCmd)
deleteCmd.AddCommand(deleteVariableCmd)
deleteCmd.AddCommand(deleteDeployTargetConfigCmd)
deleteCmd.AddCommand(deleteOrganizationCmd)
}
126 changes: 125 additions & 1 deletion cmd/deploytarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package cmd
import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"github.com/uselagoon/lagoon-cli/internal/schema"
"github.com/uselagoon/lagoon-cli/pkg/output"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"
"strconv"
)

var addDeployTargetCmd = &cobra.Command{
Expand Down Expand Up @@ -319,6 +322,121 @@ var deleteDeployTargetCmd = &cobra.Command{
},
}

var addDeployTargetToOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"dt"},
Short: "Add a deploy target to an Organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
handleError(err)

organizationName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Organization name", organizationName); err != nil {
return err
}
deployTarget, err := cmd.Flags().GetUint("deploy-target")
if err != nil {
return err
}
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))); err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)

organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)

deployTargetInput := s.AddDeployTargetToOrganizationInput{
DeployTarget: deployTarget,
Organization: organization.ID,
}

deployTargetResponse, err := l.AddDeployTargetToOrganization(context.TODO(), &deployTargetInput, lc)
handleError(err)

resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Deploy Target": deployTargetResponse.Name,
"Organization Name": organizationName,
},
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"dt"},
Short: "Remove a deploy target from an Organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
handleError(err)

organizationName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Organization name", organizationName); err != nil {
return err
}
deployTarget, err := cmd.Flags().GetUint("deploy-target")
if err != nil {
return err
}
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))); err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)

organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)

deployTargetInput := s.RemoveDeployTargetFromOrganizationInput{
DeployTarget: deployTarget,
Organization: organization.ID,
}

if yesNo(fmt.Sprintf("You are attempting to remove deploy target '%d' from organization '%s', are you sure?", deployTarget, organization.Name)) {
_, err := l.RemoveDeployTargetFromOrganization(context.TODO(), &deployTargetInput, lc)
handleError(err)
resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Deploy Target": deployTarget,
"Organization Name": organizationName,
},
}
output.RenderResult(resultData, outputOptions)
}
return nil
},
}

func init() {
addDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
addDeployTargetCmd.Flags().StringP("name", "", "", "Name of DeployTarget")
Expand All @@ -332,9 +450,15 @@ func init() {
addDeployTargetCmd.Flags().StringP("ssh-port", "", "", "DeployTarget ssh port")
addDeployTargetCmd.Flags().StringP("build-image", "", "", "DeployTarget build image to use (if different to the default)")

addDeployTargetToOrganizationCmd.Flags().StringP("name", "O", "", "Name of Organization")
addDeployTargetToOrganizationCmd.Flags().UintP("deploy-target", "D", 0, "ID of DeployTarget")

deleteDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
deleteDeployTargetCmd.Flags().StringP("name", "", "", "Name of DeployTarget")

RemoveDeployTargetFromOrganizationCmd.Flags().StringP("name", "O", "", "Name of Organization")
RemoveDeployTargetFromOrganizationCmd.Flags().UintP("deploy-target", "D", 0, "ID of DeployTarget")

updateDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
updateDeployTargetCmd.Flags().StringP("console-url", "", "", "DeployTarget console URL")
updateDeployTargetCmd.Flags().StringP("token", "", "", "DeployTarget token")
Expand Down
16 changes: 1 addition & 15 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var updateEnvironmentCmd = &cobra.Command{
Route: nullStrCheck(route),
Routes: nullStrCheck(routes),
DeployTitle: nullStrCheck(deployTitle),
Openshift: nullIntCheck(openShift),
Openshift: nullUintCheck(openShift),
}
if environmentAutoIdleProvided {
environmentFlags.AutoIdle = &environmentAutoIdle
Expand Down Expand Up @@ -158,20 +158,6 @@ var updateEnvironmentCmd = &cobra.Command{
},
}

func nullStrCheck(s string) *string {
if s == "" {
return nil
}
return &s
}

func nullIntCheck(i uint) *uint {
if i == 0 {
return nil
}
return &i
}

func checkFlags(f *pflag.Flag) {
if f.Name == "auto-idle" {
environmentAutoIdleProvided = true
Expand Down
63 changes: 60 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/uselagoon/lagoon-cli/pkg/api"
"github.com/uselagoon/lagoon-cli/pkg/output"
"os"
"strconv"

l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
)
Expand Down Expand Up @@ -218,10 +218,66 @@ var getToken = &cobra.Command{
},
}

var getOrganizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Get details about an organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(cmdLagoon)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
organizationName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Organization name", organizationName); err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)

if organization.Name == "" {
output.RenderInfo(fmt.Sprintf("No organization found for '%s'", organizationName), outputOptions)
return nil
}

data := []output.Data{}
data = append(data, []string{
strconv.Itoa(int(organization.ID)),
organization.Name,
organization.Description,
strconv.Itoa(int(organization.QuotaProject)),
strconv.Itoa(int(organization.QuotaGroup)),
strconv.Itoa(int(organization.QuotaNotification)),
})

dataMain := output.Table{
Header: []string{"ID", "Name", "Description", "Project Quota", "Group Quota", "Notification Quota"},
Data: data,
}

output.RenderOutput(dataMain, outputOptions)
return nil
},
}

func init() {
getCmd.AddCommand(getAllUserKeysCmd)
getCmd.AddCommand(getDeploymentCmd)
getCmd.AddCommand(getEnvironmentCmd)
getCmd.AddCommand(getOrganizationCmd)
getCmd.AddCommand(getProjectCmd)
getCmd.AddCommand(getProjectKeyCmd)
getCmd.AddCommand(getUserKeysCmd)
Expand All @@ -231,4 +287,5 @@ func init() {
getTaskByID.Flags().BoolP("logs", "L", false, "Show the task logs if available")
getProjectKeyCmd.Flags().BoolVarP(&revealValue, "reveal", "", false, "Reveal the variable values")
getDeploymentCmd.Flags().StringVarP(&remoteID, "remoteid", "R", "", "The remote ID of the deployment")
getOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization")
}

0 comments on commit 472d8ce

Please sign in to comment.