From 6b16ca361c2aea997f8e5e907dd9092bfff19d57 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 26 Sep 2023 18:41:08 +1000 Subject: [PATCH 01/21] Initial commit --- cmd/get.go | 64 +++++++++++++++++++++++++++++++++++++++++++-- cmd/organization.go | 3 +++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 cmd/organization.go diff --git a/cmd/get.go b/cmd/get.go index 193aef2c..d2ead230 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -1,14 +1,17 @@ package cmd import ( + "context" "encoding/json" "fmt" - "os" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/uselagoon/lagoon-cli/pkg/api" "github.com/uselagoon/lagoon-cli/pkg/output" + l "github.com/uselagoon/machinery/api/lagoon" + lclient "github.com/uselagoon/machinery/api/lagoon/client" + "os" + "strconv" ) // GetFlags . @@ -166,10 +169,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 + } + organizationID, err := cmd.Flags().GetInt("org") + if err != nil { + return err + } + if organizationID == 0 { + return fmt.Errorf("missing arguments: Organization ID is not defined") + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + organization, err := l.GetOrganizationByID(context.TODO(), organizationID, lc) + handleError(err) + + if organization == nil { + output.RenderInfo(fmt.Sprintf("No organization found for ID '%d'", organizationID), outputOptions) + os.Exit(0) + } + + data := []output.Data{} + data = append(data, []string{ + strconv.Itoa(organizationID), + 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", "quotaProject", "quotaGroup", "quotaNotification"}, + 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) @@ -179,4 +238,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().Int("org", 0, "ID of the organization") } diff --git a/cmd/organization.go b/cmd/organization.go new file mode 100644 index 00000000..8f08a06c --- /dev/null +++ b/cmd/organization.go @@ -0,0 +1,3 @@ +package cmd + +//TODO From ccb628f17814c9c4bd2a98be5436981a58326cdc Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Wed, 27 Sep 2023 12:13:42 +1000 Subject: [PATCH 02/21] Add Org cmd --- cmd/add.go | 3 ++- cmd/organization.go | 63 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index eeb2951e..3515ee14 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -28,7 +28,8 @@ func init() { addCmd.AddCommand(addProjectCmd) addCmd.AddCommand(addProjectToGroupCmd) addCmd.AddCommand(addNotificationCmd) - addCmd.AddCommand(addUserCmd) + addCmd.AddCommand(addNotificationCmd) + addCmd.AddCommand(addOrganizationCmd) addCmd.AddCommand(addUserToGroupCmd) addCmd.AddCommand(addUserSSHKeyCmd) addCmd.AddCommand(addVariableCmd) diff --git a/cmd/organization.go b/cmd/organization.go index 8f08a06c..b0137e25 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -1,3 +1,64 @@ package cmd -//TODO +import ( + "context" + "fmt" + "github.com/spf13/cobra" + "github.com/uselagoon/lagoon-cli/pkg/output" + lclient "github.com/uselagoon/machinery/api/lagoon/client" + s "github.com/uselagoon/machinery/api/schema" + "os" +) + +// TODO +var addOrganizationCmd = &cobra.Command{ + Use: "organization", + Aliases: []string{"o"}, + Short: "Add a new organization to Lagoon", + PreRunE: func(_ *cobra.Command, _ []string) error { + return validateTokenE(lagoonCLIConfig.Current) + }, + 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 organizationName == "" { + fmt.Println("Missing arguments: Organization name is not defined") + cmd.Help() + os.Exit(1) + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + + organizationInput := s.AddOrganizationInput{ + Name: organizationName, + } + org := s.Organization{} + err = lc.AddOrganization(context.TODO(), &organizationInput, &org) + handleError(err) + + resultData := output.Result{ + Result: "success", + ResultData: map[string]interface{}{ + "Organization Name": organizationName, + }, + } + output.RenderResult(resultData, outputOptions) + return nil + }, +} + +func init() { + addOrganizationCmd.Flags().String("name", "", "Name of the organization") +} From a77b506ccba57b646d49ccead20a6b95a580cae6 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 28 Sep 2023 11:06:53 +1000 Subject: [PATCH 03/21] Delete organization functionality --- cmd/delete.go | 1 + cmd/organization.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/cmd/delete.go b/cmd/delete.go index aa6288af..ef2ece89 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -34,4 +34,5 @@ func init() { deleteCmd.AddCommand(deleteUserFromGroupCmd) deleteCmd.AddCommand(deleteVariableCmd) deleteCmd.AddCommand(deleteDeployTargetConfigCmd) + deleteCmd.AddCommand(deleteOrganizationCmd) } diff --git a/cmd/organization.go b/cmd/organization.go index b0137e25..7c31d0f2 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/spf13/cobra" "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" "os" @@ -59,6 +60,48 @@ var addOrganizationCmd = &cobra.Command{ }, } +var deleteOrganizationCmd = &cobra.Command{ + Use: "organization", + Aliases: []string{"o"}, + Short: "Delete 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") + organizationId, err := cmd.Flags().GetInt("id") + if err != nil { + return err + } + if organizationId == 0 { + fmt.Println("Missing arguments: Organization ID is not defined") + cmd.Help() + os.Exit(1) + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + + organization, err := l.GetOrganizationByID(context.TODO(), organizationId, lc) + handleError(err) + if yesNo(fmt.Sprintf("You are attempting to delete organization '%s', are you sure?", organization.Name)) { + _, err := l.DeleteOrganization(context.TODO(), organizationId, lc) + handleError(err) + resultData := output.Result{ + Result: organization.Name, + } + output.RenderResult(resultData, outputOptions) + } + return nil + }, +} + func init() { addOrganizationCmd.Flags().String("name", "", "Name of the organization") + deleteOrganizationCmd.Flags().Int("id", 0, "ID of the organization") } From ac2723b0f9883c26f22da633ebd3fda3d597ff1f Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 28 Sep 2023 11:09:44 +1000 Subject: [PATCH 04/21] Fixed duplicate cmd --- cmd/add.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/add.go b/cmd/add.go index 3515ee14..702fb053 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -28,7 +28,7 @@ func init() { addCmd.AddCommand(addProjectCmd) addCmd.AddCommand(addProjectToGroupCmd) addCmd.AddCommand(addNotificationCmd) - addCmd.AddCommand(addNotificationCmd) + addCmd.AddCommand(addUserCmd) addCmd.AddCommand(addOrganizationCmd) addCmd.AddCommand(addUserToGroupCmd) addCmd.AddCommand(addUserSSHKeyCmd) From fe9078e96c12db31447504a19fc1449d9a4c2425 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 28 Sep 2023 18:59:21 +1000 Subject: [PATCH 05/21] Included update Org functionality --- cmd/get.go | 9 ++++--- cmd/organization.go | 59 +++++++++++++++++++++++++++++++++++++++++++-- cmd/update.go | 1 + go.mod | 6 ++--- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index d2ead230..bd7b97c4 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -169,6 +169,7 @@ var getToken = &cobra.Command{ }, } +// TODO - update once the API is updated var getOrganizationCmd = &cobra.Command{ Use: "organization", Aliases: []string{"o"}, @@ -181,7 +182,7 @@ var getOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationID, err := cmd.Flags().GetInt("org") + organizationID, err := cmd.Flags().GetUint("id") if err != nil { return err } @@ -199,14 +200,14 @@ var getOrganizationCmd = &cobra.Command{ organization, err := l.GetOrganizationByID(context.TODO(), organizationID, lc) handleError(err) - if organization == nil { + if organization.Name == "" { output.RenderInfo(fmt.Sprintf("No organization found for ID '%d'", organizationID), outputOptions) os.Exit(0) } data := []output.Data{} data = append(data, []string{ - strconv.Itoa(organizationID), + strconv.Itoa(int(organizationID)), organization.Name, organization.Description, strconv.Itoa(int(organization.QuotaProject)), @@ -238,5 +239,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().Int("org", 0, "ID of the organization") + getOrganizationCmd.Flags().Uint("id", 0, "ID of the organization") } diff --git a/cmd/organization.go b/cmd/organization.go index 7c31d0f2..378ab1cf 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -69,7 +69,7 @@ var deleteOrganizationCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - organizationId, err := cmd.Flags().GetInt("id") + organizationId, err := cmd.Flags().GetUint("id") if err != nil { return err } @@ -101,7 +101,62 @@ var deleteOrganizationCmd = &cobra.Command{ }, } +// TODO - update once the API is updated +var updateOrganizationCmd = &cobra.Command{ + Use: "organization", + Aliases: []string{"o"}, + Short: "Update 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") + if err != nil { + return err + } + organizationId, err := cmd.Flags().GetUint("id") + if err != nil { + return err + } + description, err := cmd.Flags().GetString("description") + if err != nil { + return err + } + + if organizationId == 0 { + fmt.Println("Missing arguments: Organization ID is not defined") + cmd.Help() + os.Exit(1) + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + + organizationFlags := s.UpdateOrganizationPatchInput{ + Description: description, + } + result, err := l.UpdateOrganization(context.TODO(), organizationId, organizationFlags, lc) + handleError(err) + + resultData := output.Result{ + Result: "success", + ResultData: map[string]interface{}{ + "Organization Name": result.Name, + }, + } + output.RenderResult(resultData, outputOptions) + return nil + }, +} + func init() { + updateOrganizationCmd.Flags().Uint("id", 0, "ID of the organization to update") + updateOrganizationCmd.Flags().StringP("description", "d", "", "Description of the organization") addOrganizationCmd.Flags().String("name", "", "Name of the organization") - deleteOrganizationCmd.Flags().Int("id", 0, "ID of the organization") + deleteOrganizationCmd.Flags().Uint("id", 0, "ID of the organization") } diff --git a/cmd/update.go b/cmd/update.go index 68a23ff7..12106614 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -29,4 +29,5 @@ func init() { updateCmd.AddCommand(updateUserCmd) updateCmd.AddCommand(updateDeployTargetConfigCmd) updateCmd.AddCommand(updateDeployTargetCmd) + updateCmd.AddCommand(updateOrganizationCmd) } diff --git a/go.mod b/go.mod index 4ec1069e..eaf36fc2 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.11 + github.com/uselagoon/machinery v0.0.12-0.20230928085239-eaa5a60c02b6 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect @@ -39,6 +39,6 @@ require ( // use this version for fixes to formatting of end header replace github.com/olekukonko/tablewriter => github.com/shreddedbacon/tablewriter v0.0.2-0.20200114082015-d810c4a558bf -// replace github.com/machinebox/graphql => ../../shreddedbacon/graphql +//replace github.com/uselagoon/machinery => ../machinery -// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter \ No newline at end of file +// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter From 4fac406574fa777271a2845720d887cde375f5f4 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 3 Oct 2023 16:34:46 +1100 Subject: [PATCH 06/21] Extends add & update cmd --- cmd/environment.go | 16 +------- cmd/organization.go | 95 +++++++++++++++++++++++++++++++++++++++++---- cmd/shared.go | 14 +++++++ go.mod | 2 +- go.sum | 6 +++ 5 files changed, 110 insertions(+), 23 deletions(-) diff --git a/cmd/environment.go b/cmd/environment.go index f7dddd4b..75bc481a 100644 --- a/cmd/environment.go +++ b/cmd/environment.go @@ -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 @@ -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 diff --git a/cmd/organization.go b/cmd/organization.go index 378ab1cf..ec432570 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -28,6 +28,35 @@ var addOrganizationCmd = &cobra.Command{ if err != nil { return err } + organizationFriendlyName, err := cmd.Flags().GetString("friendlyName") + if err != nil { + return err + } + organizationDescription, err := cmd.Flags().GetString("description") + if err != nil { + return err + } + organizationQuotaProject, err := cmd.Flags().GetUint("quotaProject") + if err != nil { + return err + } + organizationQuotaGroup, err := cmd.Flags().GetUint("quotaGroup") + if err != nil { + return err + } + organizationQuotaNotification, err := cmd.Flags().GetUint("quotaNotification") + if err != nil { + return err + } + organizationQuotaEnvironment, err := cmd.Flags().GetUint("quotaEnvironment") + if err != nil { + return err + } + organizationQuotaRoute, err := cmd.Flags().GetUint("quotaRoute") + if err != nil { + return err + } + if organizationName == "" { fmt.Println("Missing arguments: Organization name is not defined") cmd.Help() @@ -43,7 +72,14 @@ var addOrganizationCmd = &cobra.Command{ debug) organizationInput := s.AddOrganizationInput{ - Name: organizationName, + Name: organizationName, + FriendlyName: organizationFriendlyName, + Description: organizationDescription, + QuotaProject: organizationQuotaProject, + QuotaGroup: organizationQuotaGroup, + QuotaNotification: organizationQuotaNotification, + QuotaEnvironment: organizationQuotaEnvironment, + QuotaRoute: organizationQuotaRoute, } org := s.Organization{} err = lc.AddOrganization(context.TODO(), &organizationInput, &org) @@ -118,7 +154,31 @@ var updateOrganizationCmd = &cobra.Command{ if err != nil { return err } - description, err := cmd.Flags().GetString("description") + organizationFriendlyName, err := cmd.Flags().GetString("friendlyName") + if err != nil { + return err + } + organizationDescription, err := cmd.Flags().GetString("description") + if err != nil { + return err + } + organizationQuotaProject, err := cmd.Flags().GetUint("quotaProject") + if err != nil { + return err + } + organizationQuotaGroup, err := cmd.Flags().GetUint("quotaGroup") + if err != nil { + return err + } + organizationQuotaNotification, err := cmd.Flags().GetUint("quotaNotification") + if err != nil { + return err + } + organizationQuotaEnvironment, err := cmd.Flags().GetUint("quotaEnvironment") + if err != nil { + return err + } + organizationQuotaRoute, err := cmd.Flags().GetUint("quotaRoute") if err != nil { return err } @@ -137,10 +197,16 @@ var updateOrganizationCmd = &cobra.Command{ &token, debug) - organizationFlags := s.UpdateOrganizationPatchInput{ - Description: description, + organizationInput := s.UpdateOrganizationPatchInput{ + Description: nullStrCheck(organizationDescription), + FriendlyName: nullStrCheck(organizationFriendlyName), + QuotaProject: nullUintCheck(organizationQuotaProject), + QuotaGroup: nullUintCheck(organizationQuotaGroup), + QuotaNotification: nullUintCheck(organizationQuotaNotification), + QuotaEnvironment: nullUintCheck(organizationQuotaEnvironment), + QuotaRoute: nullUintCheck(organizationQuotaRoute), } - result, err := l.UpdateOrganization(context.TODO(), organizationId, organizationFlags, lc) + result, err := l.UpdateOrganization(context.TODO(), organizationId, organizationInput, lc) handleError(err) resultData := output.Result{ @@ -155,8 +221,23 @@ var updateOrganizationCmd = &cobra.Command{ } func init() { - updateOrganizationCmd.Flags().Uint("id", 0, "ID of the organization to update") - updateOrganizationCmd.Flags().StringP("description", "d", "", "Description of the organization") addOrganizationCmd.Flags().String("name", "", "Name of the organization") + addOrganizationCmd.Flags().String("friendlyName", "", "Friendly name of the organization") + addOrganizationCmd.Flags().String("description", "", "Description of the organization") + addOrganizationCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization") + addOrganizationCmd.Flags().Uint("quotaGroup", 0, "Group quota for the organization") + addOrganizationCmd.Flags().Uint("quotaNotification", 0, "Notification quota for the organization") + addOrganizationCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") + addOrganizationCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") + + updateOrganizationCmd.Flags().Uint("id", 0, "ID of the organization to update") + updateOrganizationCmd.Flags().String("friendlyName", "", "Friendly name of the organization") + updateOrganizationCmd.Flags().String("description", "", "Description of the organization") + updateOrganizationCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization") + updateOrganizationCmd.Flags().Uint("quotaGroup", 0, "Group quota for the organization") + updateOrganizationCmd.Flags().Uint("quotaNotification", 0, "Notification quota for the organization") + updateOrganizationCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") + updateOrganizationCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") + deleteOrganizationCmd.Flags().Uint("id", 0, "ID of the organization") } diff --git a/cmd/shared.go b/cmd/shared.go index d878be0e..2b8b30cf 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -91,3 +91,17 @@ func fileExists(filename string) bool { func stripNewLines(stripString string) string { return strings.TrimSuffix(stripString, "\n") } + +func nullStrCheck(s string) *string { + if s == "" { + return nil + } + return &s +} + +func nullUintCheck(i uint) *uint { + if i == 0 { + return nil + } + return &i +} diff --git a/go.mod b/go.mod index eaf36fc2..5a60621f 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20230928085239-eaa5a60c02b6 + github.com/uselagoon/machinery v0.0.12-0.20231003053009-de29ad78dc9a golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index fe18d60a..35efc03b 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE= github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 h1:I4BOK3PBMjhWfQM2zPJKK7lOBGsrsvOB7kBELP33hiE= github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= @@ -107,6 +109,10 @@ github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9/go.mod h1:q+QjxYv github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/uselagoon/machinery v0.0.11 h1:s6EhyU/pj1+C4FdS0EqmR6C0dLsoeCd9n+5xHL1YDag= github.com/uselagoon/machinery v0.0.11/go.mod h1:IXLxlkahEAEgpCmu9Xa/Wmjo6ja4Aoq7tf8G7VrileE= +github.com/uselagoon/machinery v0.0.12-0.20230928085239-eaa5a60c02b6 h1:Kitan9fWB7Q78ZFYMgiXBRdkwramMSToursudE2Uta8= +github.com/uselagoon/machinery v0.0.12-0.20230928085239-eaa5a60c02b6/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231003053009-de29ad78dc9a h1:B3pt6iYnNFCvQlIqsUvX5MdwGfN5iXeoyWTf6cerNj4= +github.com/uselagoon/machinery v0.0.12-0.20231003053009-de29ad78dc9a/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From 8b836cb4889b4189e938592a5cd74ddd67c659bd Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Wed, 4 Oct 2023 18:57:59 +1100 Subject: [PATCH 07/21] List cmd for groups & projects by org id --- cmd/list.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/cmd/list.go b/cmd/list.go index efbcbbad..487ca7da 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -436,6 +436,95 @@ var listNotificationCmd = &cobra.Command{ }, } +var listOrganizationProjectsCmd = &cobra.Command{ + Use: "organization-projects", + Aliases: []string{"op"}, + Short: "Print a list of projects in 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 + } + organizationID, err := cmd.Flags().GetUint("id") + if organizationID == 0 { + return fmt.Errorf("missing arguments: Organization ID is not defined") + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + orgProjects, err := l.GetProjectsByOrganizationID(context.TODO(), organizationID, lc) + handleError(err) + + data := []output.Data{} + for _, project := range *orgProjects { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%d", project.ID)), + returnNonEmptyString(fmt.Sprintf("%s", project.Name)), + returnNonEmptyString(fmt.Sprintf("%d", project.GroupCount)), + }) + } + dataMain := output.Table{ + Header: []string{"ID", "Name", "GroupCount"}, + Data: data, + } + output.RenderOutput(dataMain, outputOptions) + return nil + }, +} + +var listOrganizationGroupsCmd = &cobra.Command{ + Use: "organization-groups", + Aliases: []string{"og"}, + Short: "Print a list of groups in 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 + } + organizationID, err := cmd.Flags().GetUint("id") + if organizationID == 0 { + return fmt.Errorf("missing arguments: Organization ID is not defined") + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + orgGroups, err := l.GetGroupsByOrganizationID(context.TODO(), organizationID, lc) + handleError(err) + + data := []output.Data{} + for _, group := range *orgGroups { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%s", group.ID.String())), + returnNonEmptyString(fmt.Sprintf("%s", group.Name)), + returnNonEmptyString(fmt.Sprintf("%s", group.Type)), + returnNonEmptyString(fmt.Sprintf("%d", group.MemberCount)), + }) + } + dataMain := output.Table{ + Header: []string{"ID", "Name", "Type", "MemberCount"}, + Data: data, + } + output.RenderOutput(dataMain, outputOptions) + return nil + }, +} + func init() { listCmd.AddCommand(listDeployTargetsCmd) listCmd.AddCommand(listDeploymentsCmd) @@ -450,8 +539,12 @@ func init() { listCmd.AddCommand(listInvokableTasks) listCmd.AddCommand(listBackupsCmd) listCmd.AddCommand(listDeployTargetConfigsCmd) + listCmd.AddCommand(listOrganizationProjectsCmd) + listCmd.AddCommand(listOrganizationGroupsCmd) listCmd.Flags().BoolVarP(&listAllProjects, "all-projects", "", false, "All projects (if supported)") listUsersCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)") listGroupProjectsCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list projects in") listVariablesCmd.Flags().BoolP("reveal", "", false, "Reveal the variable values") + listOrganizationProjectsCmd.Flags().Uint("id", 0, "ID of the organization to list associated projects for") + listOrganizationGroupsCmd.Flags().Uint("id", 0, "ID of the organization to list associated groups for") } diff --git a/go.mod b/go.mod index 5a60621f..cfa1aefd 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231003053009-de29ad78dc9a + github.com/uselagoon/machinery v0.0.12-0.20231004075448-275f86c08ac5 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index 35efc03b..9328d937 100644 --- a/go.sum +++ b/go.sum @@ -113,6 +113,8 @@ github.com/uselagoon/machinery v0.0.12-0.20230928085239-eaa5a60c02b6 h1:Kitan9fW github.com/uselagoon/machinery v0.0.12-0.20230928085239-eaa5a60c02b6/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231003053009-de29ad78dc9a h1:B3pt6iYnNFCvQlIqsUvX5MdwGfN5iXeoyWTf6cerNj4= github.com/uselagoon/machinery v0.0.12-0.20231003053009-de29ad78dc9a/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231004075448-275f86c08ac5 h1:INh2tCt84k4L+2UDEOTORVxR1HAysgXoFSlBs0Fq6Xk= +github.com/uselagoon/machinery v0.0.12-0.20231004075448-275f86c08ac5/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From da89300ec263cef375d831404fe54bcc37bafb57 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Fri, 6 Oct 2023 18:49:55 +1100 Subject: [PATCH 08/21] Add project to org --- cmd/add.go | 1 + cmd/groups.go | 41 +++++++++++++++++++++++++++++++ cmd/project.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/cmd/add.go b/cmd/add.go index 702fb053..1529460c 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -29,6 +29,7 @@ func init() { addCmd.AddCommand(addProjectToGroupCmd) addCmd.AddCommand(addNotificationCmd) addCmd.AddCommand(addUserCmd) + addCmd.AddCommand(addProjectToOrganizationCmd) addCmd.AddCommand(addOrganizationCmd) addCmd.AddCommand(addUserToGroupCmd) addCmd.AddCommand(addUserSSHKeyCmd) diff --git a/cmd/groups.go b/cmd/groups.go index f3351e6b..04a1e37d 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -228,6 +228,45 @@ var deleteGroupCmd = &cobra.Command{ }, } +// TODO +//var addGroupToOrganizationCmd = &cobra.Command{ +// Use: "organization-group", +// Aliases: []string{"og", "orggroup"}, +// Short: "Add a new project to Lagoon", +// 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("organization") +// if organizationName == "" { +// fmt.Println("Missing arguments: Organization name is not defined") +// cmd.Help() +// os.Exit(1) +// } +// groupName, err := cmd.Flags().GetString("group") +// if groupName == "" { +// fmt.Println("Missing arguments: Group name is not defined") +// cmd.Help() +// os.Exit(1) +// } +// +// 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) +// +// output.RenderResult(resultData, outputOptions) +// return nil +// }, +//} + func init() { addGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") addUserToGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") @@ -238,4 +277,6 @@ func init() { deleteUserFromGroupCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user") deleteProjectFromGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") deleteGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") + //addGroupToOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") + //addGroupToOrganizationCmd.Flags().StringP("group", "g", "", "Name of the group") } diff --git a/cmd/project.go b/cmd/project.go index 272e56bf..0966354c 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -4,6 +4,9 @@ import ( "context" "encoding/json" "fmt" + l "github.com/uselagoon/machinery/api/lagoon" + lclient "github.com/uselagoon/machinery/api/lagoon/client" + s "github.com/uselagoon/machinery/api/schema" "os" "github.com/spf13/cobra" @@ -364,6 +367,67 @@ var deleteProjectMetadataByKey = &cobra.Command{ }, } +var addProjectToOrganizationCmd = &cobra.Command{ + Use: "organization-project", + Aliases: []string{"op", "orgproject"}, + Short: "Add a new project to Lagoon", + PreRunE: func(_ *cobra.Command, _ []string) error { + return validateTokenE(lagoonCLIConfig.Current) + }, + RunE: func(cmd *cobra.Command, args []string) error { + debug, err := cmd.Flags().GetBool("debug") + organizationName, err := cmd.Flags().GetString("organization") + if organizationName == "" { + fmt.Println("Missing arguments: Organization name is not defined") + cmd.Help() + os.Exit(1) + } + + if cmdProjectName == "" { + fmt.Println("Missing arguments: Project name is not defined") + cmd.Help() + os.Exit(1) + } + + 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) + project, err := l.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc) + handleError(err) + + if project.Organization == organization.ID { + fmt.Printf("Project %s is already assigned to organization %s\n\n", project.Name, organization.Name) + cmd.Help() + os.Exit(1) + } + + projectInput := s.AddProjectToOrganizationInput{ + Project: project.ID, + Organization: organization.ID, + } + prj := s.Project{} + err = lc.AddProjectToOrganization(context.TODO(), &projectInput, &prj) + handleError(err) + + resultData := output.Result{ + Result: "success", + ResultData: map[string]interface{}{ + "Project Name": prj.Name, + "Organization Name": organizationName, + }, + } + output.RenderResult(resultData, outputOptions) + return nil + }, +} + func init() { updateProjectCmd.Flags().StringVarP(&jsonPatch, "json", "j", "", "JSON string to patch") @@ -405,6 +469,8 @@ func init() { addProjectCmd.Flags().IntVarP(&projectDevelopmentEnvironmentsLimit, "developmentEnvironmentsLimit", "L", 0, "How many environments can be deployed at one time") addProjectCmd.Flags().IntVarP(&projectOpenshift, "openshift", "S", 0, "Reference to OpenShift Object this Project should be deployed to") + addProjectToOrganizationCmd.Flags().StringP("organization", "O", "", "The Organization to add the project to") + listCmd.AddCommand(listProjectByMetadata) listProjectByMetadata.Flags().StringP("key", "K", "", "The key name of the metadata value you are querying on") listProjectByMetadata.Flags().StringP("value", "V", "", "The value for the key you are querying on") From d8464e34ab5a7d53162c618e240d638fe35a0476 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Mon, 9 Oct 2023 17:02:50 +1100 Subject: [PATCH 09/21] Pointing to correct machinery version --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index cfa1aefd..d449097a 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231004075448-275f86c08ac5 + github.com/uselagoon/machinery v0.0.12-0.20231006074832-c51d073ce806 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index 9328d937..2ccab54f 100644 --- a/go.sum +++ b/go.sum @@ -115,6 +115,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231003053009-de29ad78dc9a h1:B3pt6iYn github.com/uselagoon/machinery v0.0.12-0.20231003053009-de29ad78dc9a/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231004075448-275f86c08ac5 h1:INh2tCt84k4L+2UDEOTORVxR1HAysgXoFSlBs0Fq6Xk= github.com/uselagoon/machinery v0.0.12-0.20231004075448-275f86c08ac5/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231006074832-c51d073ce806 h1:bdRDU5ZHXjiPW25NyXxXOeOR6NGzl4du8jhFMKgOipY= +github.com/uselagoon/machinery v0.0.12-0.20231006074832-c51d073ce806/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From 994ac3af792df5780a20e141811958861e9d5736 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Wed, 11 Oct 2023 18:17:03 +1100 Subject: [PATCH 10/21] Updated group organization cmd --- cmd/add.go | 2 +- cmd/groups.go | 101 ++++++++++++++++++++++++++++++------------------- cmd/project.go | 66 -------------------------------- go.mod | 2 +- go.sum | 2 + 5 files changed, 66 insertions(+), 107 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 1529460c..e614e961 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -29,7 +29,7 @@ func init() { addCmd.AddCommand(addProjectToGroupCmd) addCmd.AddCommand(addNotificationCmd) addCmd.AddCommand(addUserCmd) - addCmd.AddCommand(addProjectToOrganizationCmd) + addCmd.AddCommand(addGroupToOrganizationCmd) addCmd.AddCommand(addOrganizationCmd) addCmd.AddCommand(addUserToGroupCmd) addCmd.AddCommand(addUserSSHKeyCmd) diff --git a/cmd/groups.go b/cmd/groups.go index 04a1e37d..67510a74 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -1,8 +1,12 @@ package cmd import ( + "context" "encoding/json" "fmt" + l "github.com/uselagoon/machinery/api/lagoon" + lclient "github.com/uselagoon/machinery/api/lagoon/client" + s "github.com/uselagoon/machinery/api/schema" "os" "strings" @@ -229,43 +233,61 @@ var deleteGroupCmd = &cobra.Command{ } // TODO -//var addGroupToOrganizationCmd = &cobra.Command{ -// Use: "organization-group", -// Aliases: []string{"og", "orggroup"}, -// Short: "Add a new project to Lagoon", -// 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("organization") -// if organizationName == "" { -// fmt.Println("Missing arguments: Organization name is not defined") -// cmd.Help() -// os.Exit(1) -// } -// groupName, err := cmd.Flags().GetString("group") -// if groupName == "" { -// fmt.Println("Missing arguments: Group name is not defined") -// cmd.Help() -// os.Exit(1) -// } -// -// 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) -// -// output.RenderResult(resultData, outputOptions) -// return nil -// }, -//} +var addGroupToOrganizationCmd = &cobra.Command{ + Use: "organization-group", + Aliases: []string{"og", "orggroup"}, + Short: "Add a group 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) + orgOwner, err := cmd.Flags().GetBool("orgOwner") + organizationName, err := cmd.Flags().GetString("organization") + if organizationName == "" { + fmt.Println("Missing arguments: Organization name is not defined") + cmd.Help() + os.Exit(1) + } + groupName, err := cmd.Flags().GetString("group") + if groupName == "" { + fmt.Println("Missing arguments: Group name is not defined") + cmd.Help() + os.Exit(1) + } + + 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) + + groupInput := s.AddGroupToOrganizationInput{ + Name: groupName, + Organization: organization.ID, + AddOrgOwner: orgOwner, + } + group := s.OrgGroup{} + err = lc.AddGroupToOrganization(context.TODO(), &groupInput, &group) + handleError(err) + + resultData := output.Result{ + Result: "success", + ResultData: map[string]interface{}{ + "Group Name": group.Name, + "Organization Name": organizationName, + }, + } + output.RenderResult(resultData, outputOptions) + return nil + }, +} func init() { addGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") @@ -277,6 +299,7 @@ func init() { deleteUserFromGroupCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user") deleteProjectFromGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") deleteGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") - //addGroupToOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") - //addGroupToOrganizationCmd.Flags().StringP("group", "g", "", "Name of the group") + addGroupToOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") + addGroupToOrganizationCmd.Flags().StringP("group", "g", "", "Name of the group") + addGroupToOrganizationCmd.Flags().Bool("orgOwner", false, "Flag to add the user to the group as an owner") } diff --git a/cmd/project.go b/cmd/project.go index 0966354c..272e56bf 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -4,9 +4,6 @@ import ( "context" "encoding/json" "fmt" - l "github.com/uselagoon/machinery/api/lagoon" - lclient "github.com/uselagoon/machinery/api/lagoon/client" - s "github.com/uselagoon/machinery/api/schema" "os" "github.com/spf13/cobra" @@ -367,67 +364,6 @@ var deleteProjectMetadataByKey = &cobra.Command{ }, } -var addProjectToOrganizationCmd = &cobra.Command{ - Use: "organization-project", - Aliases: []string{"op", "orgproject"}, - Short: "Add a new project to Lagoon", - PreRunE: func(_ *cobra.Command, _ []string) error { - return validateTokenE(lagoonCLIConfig.Current) - }, - RunE: func(cmd *cobra.Command, args []string) error { - debug, err := cmd.Flags().GetBool("debug") - organizationName, err := cmd.Flags().GetString("organization") - if organizationName == "" { - fmt.Println("Missing arguments: Organization name is not defined") - cmd.Help() - os.Exit(1) - } - - if cmdProjectName == "" { - fmt.Println("Missing arguments: Project name is not defined") - cmd.Help() - os.Exit(1) - } - - 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) - project, err := l.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc) - handleError(err) - - if project.Organization == organization.ID { - fmt.Printf("Project %s is already assigned to organization %s\n\n", project.Name, organization.Name) - cmd.Help() - os.Exit(1) - } - - projectInput := s.AddProjectToOrganizationInput{ - Project: project.ID, - Organization: organization.ID, - } - prj := s.Project{} - err = lc.AddProjectToOrganization(context.TODO(), &projectInput, &prj) - handleError(err) - - resultData := output.Result{ - Result: "success", - ResultData: map[string]interface{}{ - "Project Name": prj.Name, - "Organization Name": organizationName, - }, - } - output.RenderResult(resultData, outputOptions) - return nil - }, -} - func init() { updateProjectCmd.Flags().StringVarP(&jsonPatch, "json", "j", "", "JSON string to patch") @@ -469,8 +405,6 @@ func init() { addProjectCmd.Flags().IntVarP(&projectDevelopmentEnvironmentsLimit, "developmentEnvironmentsLimit", "L", 0, "How many environments can be deployed at one time") addProjectCmd.Flags().IntVarP(&projectOpenshift, "openshift", "S", 0, "Reference to OpenShift Object this Project should be deployed to") - addProjectToOrganizationCmd.Flags().StringP("organization", "O", "", "The Organization to add the project to") - listCmd.AddCommand(listProjectByMetadata) listProjectByMetadata.Flags().StringP("key", "K", "", "The key name of the metadata value you are querying on") listProjectByMetadata.Flags().StringP("value", "V", "", "The value for the key you are querying on") diff --git a/go.mod b/go.mod index d449097a..a6e2e12e 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231006074832-c51d073ce806 + github.com/uselagoon/machinery v0.0.12-0.20231011071234-6d83d527417a golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index 2ccab54f..b11123ce 100644 --- a/go.sum +++ b/go.sum @@ -117,6 +117,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231004075448-275f86c08ac5 h1:INh2tCt8 github.com/uselagoon/machinery v0.0.12-0.20231004075448-275f86c08ac5/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231006074832-c51d073ce806 h1:bdRDU5ZHXjiPW25NyXxXOeOR6NGzl4du8jhFMKgOipY= github.com/uselagoon/machinery v0.0.12-0.20231006074832-c51d073ce806/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231011071234-6d83d527417a h1:dFLzyv5fRefPphiynTkGXBSmvBSH+Ulu/hlIlm/ye5Y= +github.com/uselagoon/machinery v0.0.12-0.20231011071234-6d83d527417a/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From bbb3db5c2ad80b9947534015b9d30f2bc81606ba Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Fri, 13 Oct 2023 18:01:53 +1100 Subject: [PATCH 11/21] Included deploy targets & users cmds --- cmd/list.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++- go.mod | 2 +- go.sum | 2 + 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 487ca7da..8b8dda48 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -460,7 +460,7 @@ var listOrganizationProjectsCmd = &cobra.Command{ lagoonCLIVersion, &token, debug) - orgProjects, err := l.GetProjectsByOrganizationID(context.TODO(), organizationID, lc) + orgProjects, err := l.ListProjectsByOrganizationID(context.TODO(), organizationID, lc) handleError(err) data := []output.Data{} @@ -504,7 +504,7 @@ var listOrganizationGroupsCmd = &cobra.Command{ lagoonCLIVersion, &token, debug) - orgGroups, err := l.GetGroupsByOrganizationID(context.TODO(), organizationID, lc) + orgGroups, err := l.ListGroupsByOrganizationID(context.TODO(), organizationID, lc) handleError(err) data := []output.Data{} @@ -525,6 +525,104 @@ var listOrganizationGroupsCmd = &cobra.Command{ }, } +var listDeployTargetsByOrganizationCmd = &cobra.Command{ + Use: "organization-deploytargets", + Aliases: []string{"odt"}, + Short: "Print a list of deploytargets in 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("organization") + if organizationName == "" { + return fmt.Errorf("missing arguments: Organization is not defined") + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + deployTargets, err := l.ListDeployTargetsByOrganizationName(context.TODO(), organizationName, lc) + handleError(err) + + data := []output.Data{} + for _, dt := range *deployTargets { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%d", dt.ID)), + returnNonEmptyString(fmt.Sprintf("%s", dt.Name)), + returnNonEmptyString(fmt.Sprintf("%s", dt.RouterPattern)), + returnNonEmptyString(fmt.Sprintf("%s", dt.ConsoleURL)), + returnNonEmptyString(fmt.Sprintf("%s", dt.CloudRegion)), + returnNonEmptyString(fmt.Sprintf("%s", dt.CloudProvider)), + returnNonEmptyString(fmt.Sprintf("%s", dt.SSHHost)), + returnNonEmptyString(fmt.Sprintf("%s", dt.SSHPort)), + }) + } + dataMain := output.Table{ + Header: []string{"ID", "Name", "Router Pattern", "ConsoleURL", "Cloud Region", "Cloud Provider", "SSH Host", "SSH Port"}, + Data: data, + } + output.RenderOutput(dataMain, outputOptions) + return nil + }, +} + +var ListOrganizationUsersCmd = &cobra.Command{ + Use: "organization-users", + Aliases: []string{"ou"}, + Short: "Print a list of users in 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("organization") + if organizationName == "" { + return fmt.Errorf("missing arguments: Organization is not defined") + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + organization, err := l.GetOrganizationByName(context.Background(), organizationName, lc) + handleError(err) + users, err := l.UsersByOrganization(context.TODO(), organization.ID, lc) + handleError(err) + + data := []output.Data{} + for _, user := range *users { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%s", user.ID)), + returnNonEmptyString(fmt.Sprintf("%s", user.Email)), + returnNonEmptyString(fmt.Sprintf("%s", user.FirstName)), + returnNonEmptyString(fmt.Sprintf("%s", user.LastName)), + returnNonEmptyString(fmt.Sprintf("%s", user.Comment)), + returnNonEmptyString(fmt.Sprintf("%v", user.Owner)), + }) + } + dataMain := output.Table{ + Header: []string{"ID", "Email", "First Name", "LastName", "Comment", "Owner"}, + Data: data, + } + output.RenderOutput(dataMain, outputOptions) + return nil + }, +} + func init() { listCmd.AddCommand(listDeployTargetsCmd) listCmd.AddCommand(listDeploymentsCmd) @@ -541,10 +639,14 @@ func init() { listCmd.AddCommand(listDeployTargetConfigsCmd) listCmd.AddCommand(listOrganizationProjectsCmd) listCmd.AddCommand(listOrganizationGroupsCmd) + listCmd.AddCommand(listDeployTargetsByOrganizationCmd) + listCmd.AddCommand(ListOrganizationUsersCmd) listCmd.Flags().BoolVarP(&listAllProjects, "all-projects", "", false, "All projects (if supported)") listUsersCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)") listGroupProjectsCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list projects in") listVariablesCmd.Flags().BoolP("reveal", "", false, "Reveal the variable values") listOrganizationProjectsCmd.Flags().Uint("id", 0, "ID of the organization to list associated projects for") + ListOrganizationUsersCmd.Flags().String("organization", "", "Name of the organization to list associated users for") listOrganizationGroupsCmd.Flags().Uint("id", 0, "ID of the organization to list associated groups for") + listDeployTargetsByOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated deploy targets for") } diff --git a/go.mod b/go.mod index a6e2e12e..5eee60ec 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231011071234-6d83d527417a + github.com/uselagoon/machinery v0.0.12-0.20231013065539-b9d9df834b25 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index b11123ce..04c08928 100644 --- a/go.sum +++ b/go.sum @@ -119,6 +119,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231006074832-c51d073ce806 h1:bdRDU5ZH github.com/uselagoon/machinery v0.0.12-0.20231006074832-c51d073ce806/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231011071234-6d83d527417a h1:dFLzyv5fRefPphiynTkGXBSmvBSH+Ulu/hlIlm/ye5Y= github.com/uselagoon/machinery v0.0.12-0.20231011071234-6d83d527417a/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231013065539-b9d9df834b25 h1:rwIaM/VlShiPdmIjly2uFOGqfZHFOsd/zieUoIsrcQA= +github.com/uselagoon/machinery v0.0.12-0.20231013065539-b9d9df834b25/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From d31a6fbc3094034f0296db554ac5b296becbe289 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 17 Oct 2023 09:57:07 +1100 Subject: [PATCH 12/21] cmd & flag cleanup --- cmd/get.go | 15 +++++++-------- cmd/groups.go | 3 +-- cmd/list.go | 36 ++++++++++++++++++++---------------- cmd/organization.go | 25 ++++++++++++------------- go.mod | 2 +- go.sum | 2 ++ 6 files changed, 43 insertions(+), 40 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index bd7b97c4..7885d1a3 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -169,7 +169,6 @@ var getToken = &cobra.Command{ }, } -// TODO - update once the API is updated var getOrganizationCmd = &cobra.Command{ Use: "organization", Aliases: []string{"o"}, @@ -182,12 +181,12 @@ var getOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationID, err := cmd.Flags().GetUint("id") + organizationName, err := cmd.Flags().GetString("name") if err != nil { return err } - if organizationID == 0 { - return fmt.Errorf("missing arguments: Organization ID is not defined") + if organizationName == "" { + return fmt.Errorf("missing arguments: Organization is not defined") } current := lagoonCLIConfig.Current @@ -197,17 +196,17 @@ var getOrganizationCmd = &cobra.Command{ lagoonCLIVersion, &token, debug) - organization, err := l.GetOrganizationByID(context.TODO(), organizationID, lc) + organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) handleError(err) if organization.Name == "" { - output.RenderInfo(fmt.Sprintf("No organization found for ID '%d'", organizationID), outputOptions) + output.RenderInfo(fmt.Sprintf("No organization found for '%s'", organizationName), outputOptions) os.Exit(0) } data := []output.Data{} data = append(data, []string{ - strconv.Itoa(int(organizationID)), + strconv.Itoa(int(organization.ID)), organization.Name, organization.Description, strconv.Itoa(int(organization.QuotaProject)), @@ -239,5 +238,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().Uint("id", 0, "ID of the organization") + getOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") } diff --git a/cmd/groups.go b/cmd/groups.go index 67510a74..4834e65e 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -232,7 +232,6 @@ var deleteGroupCmd = &cobra.Command{ }, } -// TODO var addGroupToOrganizationCmd = &cobra.Command{ Use: "organization-group", Aliases: []string{"og", "orggroup"}, @@ -300,6 +299,6 @@ func init() { deleteProjectFromGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") deleteGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") addGroupToOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") - addGroupToOrganizationCmd.Flags().StringP("group", "g", "", "Name of the group") + addGroupToOrganizationCmd.Flags().StringP("group", "G", "", "Name of the group") addGroupToOrganizationCmd.Flags().Bool("orgOwner", false, "Flag to add the user to the group as an owner") } diff --git a/cmd/list.go b/cmd/list.go index 8b8dda48..78fa18f6 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -448,9 +448,9 @@ var listOrganizationProjectsCmd = &cobra.Command{ if err != nil { return err } - organizationID, err := cmd.Flags().GetUint("id") - if organizationID == 0 { - return fmt.Errorf("missing arguments: Organization ID is not defined") + organizationName, err := cmd.Flags().GetString("organization") + if organizationName == "" { + return fmt.Errorf("missing arguments: Organization is not defined") } current := lagoonCLIConfig.Current @@ -460,7 +460,9 @@ var listOrganizationProjectsCmd = &cobra.Command{ lagoonCLIVersion, &token, debug) - orgProjects, err := l.ListProjectsByOrganizationID(context.TODO(), organizationID, lc) + + org, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) + orgProjects, err := l.ListProjectsByOrganizationID(context.TODO(), org.ID, lc) handleError(err) data := []output.Data{} @@ -492,9 +494,9 @@ var listOrganizationGroupsCmd = &cobra.Command{ if err != nil { return err } - organizationID, err := cmd.Flags().GetUint("id") - if organizationID == 0 { - return fmt.Errorf("missing arguments: Organization ID is not defined") + organizationName, err := cmd.Flags().GetString("organization") + if organizationName == "" { + return fmt.Errorf("missing arguments: Organization is not defined") } current := lagoonCLIConfig.Current @@ -504,7 +506,9 @@ var listOrganizationGroupsCmd = &cobra.Command{ lagoonCLIVersion, &token, debug) - orgGroups, err := l.ListGroupsByOrganizationID(context.TODO(), organizationID, lc) + + org, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) + orgGroups, err := l.ListGroupsByOrganizationID(context.TODO(), org.ID, lc) handleError(err) data := []output.Data{} @@ -525,10 +529,10 @@ var listOrganizationGroupsCmd = &cobra.Command{ }, } -var listDeployTargetsByOrganizationCmd = &cobra.Command{ - Use: "organization-deploytargets", +var listOrganizationDeployTargetsCmd = &cobra.Command{ + Use: "organization-deploy-targets", Aliases: []string{"odt"}, - Short: "Print a list of deploytargets in an organization", + Short: "Print a list of deploy targets in an organization", PreRunE: func(_ *cobra.Command, _ []string) error { return validateTokenE(cmdLagoon) }, @@ -639,14 +643,14 @@ func init() { listCmd.AddCommand(listDeployTargetConfigsCmd) listCmd.AddCommand(listOrganizationProjectsCmd) listCmd.AddCommand(listOrganizationGroupsCmd) - listCmd.AddCommand(listDeployTargetsByOrganizationCmd) + listCmd.AddCommand(listOrganizationDeployTargetsCmd) listCmd.AddCommand(ListOrganizationUsersCmd) listCmd.Flags().BoolVarP(&listAllProjects, "all-projects", "", false, "All projects (if supported)") listUsersCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)") listGroupProjectsCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list projects in") listVariablesCmd.Flags().BoolP("reveal", "", false, "Reveal the variable values") - listOrganizationProjectsCmd.Flags().Uint("id", 0, "ID of the organization to list associated projects for") - ListOrganizationUsersCmd.Flags().String("organization", "", "Name of the organization to list associated users for") - listOrganizationGroupsCmd.Flags().Uint("id", 0, "ID of the organization to list associated groups for") - listDeployTargetsByOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated deploy targets for") + listOrganizationProjectsCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated projects for") + ListOrganizationUsersCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated users for") + listOrganizationGroupsCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated groups for") + listOrganizationDeployTargetsCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated deploy targets for") } diff --git a/cmd/organization.go b/cmd/organization.go index ec432570..ed0783fe 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -11,7 +11,6 @@ import ( "os" ) -// TODO var addOrganizationCmd = &cobra.Command{ Use: "organization", Aliases: []string{"o"}, @@ -105,12 +104,12 @@ var deleteOrganizationCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - organizationId, err := cmd.Flags().GetUint("id") + organizationName, err := cmd.Flags().GetString("organization") if err != nil { return err } - if organizationId == 0 { - fmt.Println("Missing arguments: Organization ID is not defined") + if organizationName == "" { + fmt.Println("Missing arguments: Organization is not defined") cmd.Help() os.Exit(1) } @@ -123,10 +122,10 @@ var deleteOrganizationCmd = &cobra.Command{ &token, debug) - organization, err := l.GetOrganizationByID(context.TODO(), organizationId, lc) + organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) handleError(err) if yesNo(fmt.Sprintf("You are attempting to delete organization '%s', are you sure?", organization.Name)) { - _, err := l.DeleteOrganization(context.TODO(), organizationId, lc) + _, err := l.DeleteOrganization(context.TODO(), organization.ID, lc) handleError(err) resultData := output.Result{ Result: organization.Name, @@ -137,7 +136,6 @@ var deleteOrganizationCmd = &cobra.Command{ }, } -// TODO - update once the API is updated var updateOrganizationCmd = &cobra.Command{ Use: "organization", Aliases: []string{"o"}, @@ -150,7 +148,7 @@ var updateOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationId, err := cmd.Flags().GetUint("id") + organizationName, err := cmd.Flags().GetString("organization") if err != nil { return err } @@ -183,8 +181,8 @@ var updateOrganizationCmd = &cobra.Command{ return err } - if organizationId == 0 { - fmt.Println("Missing arguments: Organization ID is not defined") + if organizationName == "" { + fmt.Println("Missing arguments: Organization is not defined") cmd.Help() os.Exit(1) } @@ -197,6 +195,7 @@ var updateOrganizationCmd = &cobra.Command{ &token, debug) + organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) organizationInput := s.UpdateOrganizationPatchInput{ Description: nullStrCheck(organizationDescription), FriendlyName: nullStrCheck(organizationFriendlyName), @@ -206,7 +205,7 @@ var updateOrganizationCmd = &cobra.Command{ QuotaEnvironment: nullUintCheck(organizationQuotaEnvironment), QuotaRoute: nullUintCheck(organizationQuotaRoute), } - result, err := l.UpdateOrganization(context.TODO(), organizationId, organizationInput, lc) + result, err := l.UpdateOrganization(context.TODO(), organization.ID, organizationInput, lc) handleError(err) resultData := output.Result{ @@ -230,7 +229,7 @@ func init() { addOrganizationCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") addOrganizationCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") - updateOrganizationCmd.Flags().Uint("id", 0, "ID of the organization to update") + updateOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization to update") updateOrganizationCmd.Flags().String("friendlyName", "", "Friendly name of the organization") updateOrganizationCmd.Flags().String("description", "", "Description of the organization") updateOrganizationCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization") @@ -239,5 +238,5 @@ func init() { updateOrganizationCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") updateOrganizationCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") - deleteOrganizationCmd.Flags().Uint("id", 0, "ID of the organization") + deleteOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization to delete") } diff --git a/go.mod b/go.mod index 5eee60ec..79f62f31 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231013065539-b9d9df834b25 + github.com/uselagoon/machinery v0.0.12-0.20231015225416-9e8151292e0c golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index 04c08928..7bcce8cc 100644 --- a/go.sum +++ b/go.sum @@ -121,6 +121,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231011071234-6d83d527417a h1:dFLzyv5f github.com/uselagoon/machinery v0.0.12-0.20231011071234-6d83d527417a/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231013065539-b9d9df834b25 h1:rwIaM/VlShiPdmIjly2uFOGqfZHFOsd/zieUoIsrcQA= github.com/uselagoon/machinery v0.0.12-0.20231013065539-b9d9df834b25/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231015225416-9e8151292e0c h1:fguK6vwcaDQQkJyzAPGuy7VjzG1xZSNqYgekfIgepjc= +github.com/uselagoon/machinery v0.0.12-0.20231015225416-9e8151292e0c/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From 2a6d686ca139fc34b1e12e0bb633ac69b7c931d9 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 19 Oct 2023 10:28:57 +1100 Subject: [PATCH 13/21] Moved org list commands to sub commands --- cmd/list.go | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 78fa18f6..540661e9 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -436,10 +436,19 @@ var listNotificationCmd = &cobra.Command{ }, } +var listOrganizationCmd = &cobra.Command{ + Use: "organization", + Aliases: []string{"o"}, + Short: "List all organizations projects, groups, deploy targets or users", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + validateToken(lagoonCLIConfig.Current) + }, +} + var listOrganizationProjectsCmd = &cobra.Command{ - Use: "organization-projects", - Aliases: []string{"op"}, - Short: "Print a list of projects in an organization", + Use: "projects", + Aliases: []string{"p"}, + Short: "List projects in an organization", PreRunE: func(_ *cobra.Command, _ []string) error { return validateTokenE(cmdLagoon) }, @@ -483,9 +492,9 @@ var listOrganizationProjectsCmd = &cobra.Command{ } var listOrganizationGroupsCmd = &cobra.Command{ - Use: "organization-groups", - Aliases: []string{"og"}, - Short: "Print a list of groups in an organization", + Use: "groups", + Aliases: []string{"g"}, + Short: "List groups in an organization", PreRunE: func(_ *cobra.Command, _ []string) error { return validateTokenE(cmdLagoon) }, @@ -530,9 +539,9 @@ var listOrganizationGroupsCmd = &cobra.Command{ } var listOrganizationDeployTargetsCmd = &cobra.Command{ - Use: "organization-deploy-targets", - Aliases: []string{"odt"}, - Short: "Print a list of deploy targets in an organization", + Use: "deploytargets", + Aliases: []string{"d"}, + Short: "List deploy targets in an organization", PreRunE: func(_ *cobra.Command, _ []string) error { return validateTokenE(cmdLagoon) }, @@ -579,9 +588,9 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ } var ListOrganizationUsersCmd = &cobra.Command{ - Use: "organization-users", - Aliases: []string{"ou"}, - Short: "Print a list of users in an organization", + Use: "users", + Aliases: []string{"u"}, + Short: "List users in an organization", PreRunE: func(_ *cobra.Command, _ []string) error { return validateTokenE(cmdLagoon) }, @@ -641,10 +650,11 @@ func init() { listCmd.AddCommand(listInvokableTasks) listCmd.AddCommand(listBackupsCmd) listCmd.AddCommand(listDeployTargetConfigsCmd) - listCmd.AddCommand(listOrganizationProjectsCmd) - listCmd.AddCommand(listOrganizationGroupsCmd) - listCmd.AddCommand(listOrganizationDeployTargetsCmd) - listCmd.AddCommand(ListOrganizationUsersCmd) + listCmd.AddCommand(listOrganizationCmd) + listOrganizationCmd.AddCommand(listOrganizationProjectsCmd) + listOrganizationCmd.AddCommand(ListOrganizationUsersCmd) + listOrganizationCmd.AddCommand(listOrganizationGroupsCmd) + listOrganizationCmd.AddCommand(listOrganizationDeployTargetsCmd) listCmd.Flags().BoolVarP(&listAllProjects, "all-projects", "", false, "All projects (if supported)") listUsersCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)") listGroupProjectsCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list projects in") From bb8acfe8a1eaf161f64e7d7b9d250c7638f4f164 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 19 Oct 2023 17:13:20 +1100 Subject: [PATCH 14/21] Introduced addProjectToOrganization & updated add organizations to use sub commands --- cmd/add.go | 10 ++- cmd/groups.go | 4 +- cmd/organization.go | 24 ++++--- cmd/project.go | 165 ++++++++++++++++++++++++++++++++++++++++++++ cmd/shared.go | 11 ++- 5 files changed, 199 insertions(+), 15 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index e614e961..6ea38fef 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -22,6 +22,15 @@ 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) @@ -29,7 +38,6 @@ func init() { addCmd.AddCommand(addProjectToGroupCmd) addCmd.AddCommand(addNotificationCmd) addCmd.AddCommand(addUserCmd) - addCmd.AddCommand(addGroupToOrganizationCmd) addCmd.AddCommand(addOrganizationCmd) addCmd.AddCommand(addUserToGroupCmd) addCmd.AddCommand(addUserSSHKeyCmd) diff --git a/cmd/groups.go b/cmd/groups.go index 4834e65e..fbbe8392 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -233,8 +233,8 @@ var deleteGroupCmd = &cobra.Command{ } var addGroupToOrganizationCmd = &cobra.Command{ - Use: "organization-group", - Aliases: []string{"og", "orggroup"}, + Use: "group", + Aliases: []string{"g"}, Short: "Add a group to an Organization", PreRunE: func(_ *cobra.Command, _ []string) error { return validateTokenE(lagoonCLIConfig.Current) diff --git a/cmd/organization.go b/cmd/organization.go index ed0783fe..f896d6a3 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -11,7 +11,7 @@ import ( "os" ) -var addOrganizationCmd = &cobra.Command{ +var addOrgCmd = &cobra.Command{ Use: "organization", Aliases: []string{"o"}, Short: "Add a new organization to Lagoon", @@ -23,7 +23,7 @@ var addOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("name") + organizationName, err := cmd.Flags().GetString("organization") if err != nil { return err } @@ -220,14 +220,18 @@ var updateOrganizationCmd = &cobra.Command{ } func init() { - addOrganizationCmd.Flags().String("name", "", "Name of the organization") - addOrganizationCmd.Flags().String("friendlyName", "", "Friendly name of the organization") - addOrganizationCmd.Flags().String("description", "", "Description of the organization") - addOrganizationCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization") - addOrganizationCmd.Flags().Uint("quotaGroup", 0, "Group quota for the organization") - addOrganizationCmd.Flags().Uint("quotaNotification", 0, "Notification quota for the organization") - addOrganizationCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") - addOrganizationCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") + addOrganizationCmd.AddCommand(addOrgCmd) + addOrganizationCmd.AddCommand(addGroupToOrganizationCmd) + addOrganizationCmd.AddCommand(addProjectToOrganizationCmd) + + addOrgCmd.Flags().StringP("organization", "O", "", "Name of the organization") + addOrgCmd.Flags().String("friendlyName", "", "Friendly name of the organization") + addOrgCmd.Flags().String("description", "", "Description of the organization") + addOrgCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization") + addOrgCmd.Flags().Uint("quotaGroup", 0, "Group quota for the organization") + addOrgCmd.Flags().Uint("quotaNotification", 0, "Notification quota for the organization") + addOrgCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") + addOrgCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") updateOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization to update") updateOrganizationCmd.Flags().String("friendlyName", "", "Friendly name of the organization") diff --git a/cmd/project.go b/cmd/project.go index 272e56bf..5a04c059 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -4,7 +4,11 @@ import ( "context" "encoding/json" "fmt" + l "github.com/uselagoon/machinery/api/lagoon" + lclient "github.com/uselagoon/machinery/api/lagoon/client" + s "github.com/uselagoon/machinery/api/schema" "os" + "strconv" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -364,6 +368,148 @@ var deleteProjectMetadataByKey = &cobra.Command{ }, } +// TODO - refactor once machinery is updated +var addProjectToOrganizationCmd = &cobra.Command{ + Use: "project", + Aliases: []string{"p"}, + Short: "Add a project 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) + + requiredInputCheck("Project name", cmdProjectName) + organizationName, err := cmd.Flags().GetString("organization") + requiredInputCheck("Organization name", organizationName) + gitUrl, err := cmd.Flags().GetString("gitUrl") + requiredInputCheck("gitUrl", gitUrl) + if err != nil { + return err + } + productionEnvironment, err := cmd.Flags().GetString("productionEnvironment") + requiredInputCheck("Production Environment", productionEnvironment) + if err != nil { + return err + } + openshift, err := cmd.Flags().GetUint("openshift") + requiredInputCheck("openshift", strconv.Itoa(int(openshift))) + if err != nil { + return err + } + standbyProductionEnvironment, err := cmd.Flags().GetString("standbyProductionEnvironment") + if err != nil { + return err + } + branches, err := cmd.Flags().GetString("branches") + if err != nil { + return err + } + pullrequests, err := cmd.Flags().GetString("pullrequests") + if err != nil { + return err + } + openshiftProjectPattern, err := cmd.Flags().GetString("openshiftProjectPattern") + if err != nil { + return err + } + developmentEnvironmentsLimit, err := cmd.Flags().GetUint("developmentEnvironmentsLimit") + if err != nil { + return err + } + storageCalc, err := cmd.Flags().GetUint("storageCalc") + if err != nil { + return err + } + autoIdle, err := cmd.Flags().GetUint("autoIdle") + if err != nil { + return err + } + subfolder, err := cmd.Flags().GetString("subfolder") + if err != nil { + return err + } + privateKey, err := cmd.Flags().GetString("privateKey") + if err != nil { + return err + } + orgOwner, err := cmd.Flags().GetBool("orgOwner") + if err != nil { + return err + } + buildImage, err := cmd.Flags().GetString("buildImage") + if err != nil { + return err + } + availability, err := cmd.Flags().GetString("availability") + if err != nil { + return err + } + + //factsUi, err := cmd.Flags().GetUint("factsUi") + //if err != nil { + // return err + //} + //problemsUi, err := cmd.Flags().GetUint("problemsUi") + //if err != nil { + // return err + //} + //routerPattern, err := cmd.Flags().GetString("routerPattern") + //if err != nil { + // return err + //} + //deploymentsDisabled, err := cmd.Flags().GetUint("deploymentsDisabled") + //if 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) + + projectInput := s.AddProjectInput{ + Name: cmdProjectName, + Organization: organization.ID, + AddOrgOwner: orgOwner, + BuildImage: buildImage, + Availability: s.ProjectAvailability(availability), + GitURL: gitUrl, + ProductionEnvironment: productionEnvironment, + StandbyProductionEnvironment: standbyProductionEnvironment, + Branches: branches, + PullRequests: pullrequests, + OpenshiftProjectPattern: openshiftProjectPattern, + Openshift: openshift, + DevelopmentEnvironmentsLimit: developmentEnvironmentsLimit, + StorageCalc: storageCalc, + AutoIdle: autoIdle, + Subfolder: subfolder, + PrivateKey: privateKey, + } + project := s.Project{} + err = lc.AddProject(context.TODO(), &projectInput, &project) + handleError(err) + + resultData := output.Result{ + Result: "success", + ResultData: map[string]interface{}{ + "Project Name": project.Name, + "Organization Name": organizationName, + }, + } + output.RenderResult(resultData, outputOptions) + return nil + }, +} + func init() { updateProjectCmd.Flags().StringVarP(&jsonPatch, "json", "j", "", "JSON string to patch") @@ -418,4 +564,23 @@ func init() { deleteProjectMetadataByKey.Flags().StringP("key", "K", "", "The key name of the metadata value you are querying on") getCmd.AddCommand(getProjectMetadata) + + addProjectToOrganizationCmd.Flags().String("buildImage", "", "Build Image for the project") + addProjectToOrganizationCmd.Flags().String("availability", "", "Availability of the project") + addProjectToOrganizationCmd.Flags().String("gitUrl", "", "GitURL of the project") + addProjectToOrganizationCmd.Flags().String("productionEnvironment", "", "Production Environment for the project") + addProjectToOrganizationCmd.Flags().String("standbyProductionEnvironment", "", "Standby Production Environment for the project") + addProjectToOrganizationCmd.Flags().String("subfolder", "", "Set if the .lagoon.yml should be found in a subfolder useful if you have multiple Lagoon projects per Git Repository") + addProjectToOrganizationCmd.Flags().String("privateKey", "", "Private key to use for the project") + addProjectToOrganizationCmd.Flags().String("branches", "", "branches") + addProjectToOrganizationCmd.Flags().String("pullrequests", "", "Which Pull Requests should be deployed") + addProjectToOrganizationCmd.Flags().StringP("organization", "O", "", "Organization to add the project to") + addProjectToOrganizationCmd.Flags().String("openshiftProjectPattern", "", "Pattern of OpenShift Project/Namespace that should be generated") + + addProjectToOrganizationCmd.Flags().Uint("openshift", 0, "Reference to OpenShift Object this Project should be deployed to") + addProjectToOrganizationCmd.Flags().Uint("autoIdle", 0, "Auto idle setting of the project") + addProjectToOrganizationCmd.Flags().Uint("storageCalc", 0, "Should storage for this environment be calculated") + addProjectToOrganizationCmd.Flags().Uint("developmentEnvironmentsLimit", 0, "How many environments can be deployed at one time") + + addProjectToOrganizationCmd.Flags().Bool("orgOwner", false, "Add the user as an owner of the project") } diff --git a/cmd/shared.go b/cmd/shared.go index 2b8b30cf..6dea07a3 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -1,10 +1,10 @@ package cmd import ( + "fmt" + "github.com/uselagoon/lagoon-cli/pkg/output" "os" "strings" - - "github.com/uselagoon/lagoon-cli/pkg/output" ) // config vars @@ -105,3 +105,10 @@ func nullUintCheck(i uint) *uint { } return &i } + +func requiredInputCheck(field string, value string) { + if value == "" || value == "0" { + fmt.Println(fmt.Sprintf("Missing argument: %s is not defined", field)) + os.Exit(1) + } +} From 3a55ad3bab544dee98a2b41e04f7fa8ec315b6f0 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Fri, 20 Oct 2023 14:24:47 +1100 Subject: [PATCH 15/21] add & remove deploytarget --- cmd/delete.go | 9 ++++ cmd/deploytarget.go | 112 +++++++++++++++++++++++++++++++++++++++++++- cmd/organization.go | 10 +++- cmd/project.go | 54 +++++++++++++++++++++ cmd/users.go | 67 ++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 + 7 files changed, 252 insertions(+), 4 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index ef2ece89..d2e07788 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -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) diff --git a/cmd/deploytarget.go b/cmd/deploytarget.go index 59fe8785..9bcdef22 100644 --- a/cmd/deploytarget.go +++ b/cmd/deploytarget.go @@ -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{ @@ -319,6 +322,107 @@ 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("organization") + requiredInputCheck("Organization name", organizationName) + deployTarget, err := cmd.Flags().GetUint("deployTarget") + requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))) + if 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("organization") + requiredInputCheck("Organization name", organizationName) + deployTarget, err := cmd.Flags().GetUint("deployTarget") + requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))) + if 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") @@ -332,9 +436,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("organization", "O", "", "Name of Organization") + addDeployTargetToOrganizationCmd.Flags().UintP("deployTarget", "D", 0, "ID of DeployTarget") + deleteDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget") deleteDeployTargetCmd.Flags().StringP("name", "", "", "Name of DeployTarget") + RemoveDeployTargetFromOrganizationCmd.Flags().StringP("organization", "O", "", "Name of Organization") + RemoveDeployTargetFromOrganizationCmd.Flags().UintP("deployTarget", "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") diff --git a/cmd/organization.go b/cmd/organization.go index f896d6a3..201ad665 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -95,7 +95,7 @@ var addOrgCmd = &cobra.Command{ }, } -var deleteOrganizationCmd = &cobra.Command{ +var deleteOrgCmd = &cobra.Command{ Use: "organization", Aliases: []string{"o"}, Short: "Delete an organization", @@ -223,6 +223,12 @@ func init() { addOrganizationCmd.AddCommand(addOrgCmd) addOrganizationCmd.AddCommand(addGroupToOrganizationCmd) addOrganizationCmd.AddCommand(addProjectToOrganizationCmd) + addOrganizationCmd.AddCommand(addDeployTargetToOrganizationCmd) + addOrganizationCmd.AddCommand(addUserToOrganizationCmd) + + deleteOrganizationCmd.AddCommand(deleteOrgCmd) + deleteOrganizationCmd.AddCommand(RemoveDeployTargetFromOrganizationCmd) + deleteOrganizationCmd.AddCommand(RemoveProjectFromOrganizationCmd) addOrgCmd.Flags().StringP("organization", "O", "", "Name of the organization") addOrgCmd.Flags().String("friendlyName", "", "Friendly name of the organization") @@ -242,5 +248,5 @@ func init() { updateOrganizationCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") updateOrganizationCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") - deleteOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization to delete") + deleteOrgCmd.Flags().StringP("organization", "O", "", "Name of the organization to delete") } diff --git a/cmd/project.go b/cmd/project.go index 5a04c059..6339b08c 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -510,6 +510,58 @@ var addProjectToOrganizationCmd = &cobra.Command{ }, } +var RemoveProjectFromOrganizationCmd = &cobra.Command{ + Use: "project", + Aliases: []string{"p"}, + Short: "Remove a project 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("organization") + requiredInputCheck("Organization name", organizationName) + requiredInputCheck("Project", cmdProjectName) + if err != nil { + return err + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + + project, err := l.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc) + handleError(err) + organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) + handleError(err) + + projectInput := s.RemoveProjectFromOrganizationInput{ + Project: project.ID, + Organization: organization.ID, + } + + if yesNo(fmt.Sprintf("You are attempting to remove project '%s' from organization '%s'. This will return the project to a state where it has no groups or notifications associated, are you sure?", cmdProjectName, organization.Name)) { + _, err := l.RemoveProjectFromOrganization(context.TODO(), &projectInput, lc) + handleError(err) + resultData := output.Result{ + Result: "success", + ResultData: map[string]interface{}{ + "Project Name": cmdProjectName, + "Organization Name": organizationName, + }, + } + output.RenderResult(resultData, outputOptions) + } + return nil + }, +} + func init() { updateProjectCmd.Flags().StringVarP(&jsonPatch, "json", "j", "", "JSON string to patch") @@ -583,4 +635,6 @@ func init() { addProjectToOrganizationCmd.Flags().Uint("developmentEnvironmentsLimit", 0, "How many environments can be deployed at one time") addProjectToOrganizationCmd.Flags().Bool("orgOwner", false, "Add the user as an owner of the project") + + RemoveProjectFromOrganizationCmd.Flags().StringP("organization", "O", "", "Organization to remove the project from") } diff --git a/cmd/users.go b/cmd/users.go index 1178213b..653775cb 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -1,9 +1,13 @@ package cmd import ( + "context" "encoding/json" "errors" "fmt" + l "github.com/uselagoon/machinery/api/lagoon" + lclient "github.com/uselagoon/machinery/api/lagoon/client" + s "github.com/uselagoon/machinery/api/schema" "io/ioutil" "os" "strings" @@ -267,6 +271,65 @@ var getAllUserKeysCmd = &cobra.Command{ }, } +var addUserToOrganizationCmd = &cobra.Command{ + Use: "user", + Aliases: []string{"u"}, + Short: "Add a user 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("organization") + requiredInputCheck("Organization name", organizationName) + if err != nil { + return err + } + userEmail, err := cmd.Flags().GetString("email") + requiredInputCheck("User email", userEmail) + if err != nil { + return err + } + owner, err := cmd.Flags().GetBool("owner") + if 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) + + userInput := s.AddUserToOrganizationInput{ + User: s.UserInput{Email: userEmail}, + Organization: organization.ID, + Owner: owner, + } + + orgUser := s.Organization{} + err = lc.AddUserToOrganization(context.TODO(), &userInput, &orgUser) + handleError(err) + + resultData := output.Result{ + Result: "success", + ResultData: map[string]interface{}{ + "User": userEmail, + "Organization Name": organizationName, + }, + } + output.RenderResult(resultData, outputOptions) + return nil + }, +} + var ( currentUserEmail string pubKeyValue string @@ -289,4 +352,8 @@ func init() { getUserKeysCmd.Flags().StringVarP(&userEmail, "email", "E", "", "New email address of the user") getUserKeysCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to check users in (if not specified, will default to all groups)") getAllUserKeysCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)") + + addUserToOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") + addUserToOrganizationCmd.Flags().StringP("email", "E", "", "Email address of the user") + addUserToOrganizationCmd.Flags().Bool("owner", false, "Set the user as an owner of the organization") } diff --git a/go.mod b/go.mod index 79f62f31..5de2eea7 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231015225416-9e8151292e0c + github.com/uselagoon/machinery v0.0.12-0.20231020030557-4ea384607fc3 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index 7bcce8cc..1f9165b9 100644 --- a/go.sum +++ b/go.sum @@ -123,6 +123,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231013065539-b9d9df834b25 h1:rwIaM/Vl github.com/uselagoon/machinery v0.0.12-0.20231013065539-b9d9df834b25/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231015225416-9e8151292e0c h1:fguK6vwcaDQQkJyzAPGuy7VjzG1xZSNqYgekfIgepjc= github.com/uselagoon/machinery v0.0.12-0.20231015225416-9e8151292e0c/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231020030557-4ea384607fc3 h1:2a/j1S+wdbqbCSIuijboMTeTc8X2KpLz8X14Nqk0tG4= +github.com/uselagoon/machinery v0.0.12-0.20231020030557-4ea384607fc3/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From 55fdf00f089e59d87cb6ab572158ca048f426c8a Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Fri, 20 Oct 2023 15:50:37 +1100 Subject: [PATCH 16/21] RemoveUserCmd & implement shared function --- cmd/get.go | 6 ++--- cmd/groups.go | 14 +++++----- cmd/list.go | 16 +++-------- cmd/organization.go | 22 +++------------ cmd/users.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ 7 files changed, 84 insertions(+), 43 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index 7885d1a3..082259e0 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -181,13 +181,11 @@ var getOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("name") + organizationName, err := cmd.Flags().GetString("organization") + requiredInputCheck("Organization name", organizationName) if err != nil { return err } - if organizationName == "" { - return fmt.Errorf("missing arguments: Organization is not defined") - } current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token diff --git a/cmd/groups.go b/cmd/groups.go index fbbe8392..be86fad7 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -244,16 +244,14 @@ var addGroupToOrganizationCmd = &cobra.Command{ handleError(err) orgOwner, err := cmd.Flags().GetBool("orgOwner") organizationName, err := cmd.Flags().GetString("organization") - if organizationName == "" { - fmt.Println("Missing arguments: Organization name is not defined") - cmd.Help() - os.Exit(1) + requiredInputCheck("Organization name", organizationName) + if err != nil { + return err } groupName, err := cmd.Flags().GetString("group") - if groupName == "" { - fmt.Println("Missing arguments: Group name is not defined") - cmd.Help() - os.Exit(1) + requiredInputCheck("Group name", groupName) + if err != nil { + return err } current := lagoonCLIConfig.Current diff --git a/cmd/list.go b/cmd/list.go index 540661e9..2d5e9e34 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -458,9 +458,7 @@ var listOrganizationProjectsCmd = &cobra.Command{ return err } organizationName, err := cmd.Flags().GetString("organization") - if organizationName == "" { - return fmt.Errorf("missing arguments: Organization is not defined") - } + requiredInputCheck("Organization name", organizationName) current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token @@ -504,9 +502,7 @@ var listOrganizationGroupsCmd = &cobra.Command{ return err } organizationName, err := cmd.Flags().GetString("organization") - if organizationName == "" { - return fmt.Errorf("missing arguments: Organization is not defined") - } + requiredInputCheck("Organization name", organizationName) current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token @@ -551,9 +547,7 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ return err } organizationName, err := cmd.Flags().GetString("organization") - if organizationName == "" { - return fmt.Errorf("missing arguments: Organization is not defined") - } + requiredInputCheck("Organization name", organizationName) current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token @@ -600,9 +594,7 @@ var ListOrganizationUsersCmd = &cobra.Command{ return err } organizationName, err := cmd.Flags().GetString("organization") - if organizationName == "" { - return fmt.Errorf("missing arguments: Organization is not defined") - } + requiredInputCheck("Organization name", organizationName) current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token diff --git a/cmd/organization.go b/cmd/organization.go index 201ad665..d781c081 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -8,7 +8,6 @@ import ( l "github.com/uselagoon/machinery/api/lagoon" lclient "github.com/uselagoon/machinery/api/lagoon/client" s "github.com/uselagoon/machinery/api/schema" - "os" ) var addOrgCmd = &cobra.Command{ @@ -24,6 +23,7 @@ var addOrgCmd = &cobra.Command{ return err } organizationName, err := cmd.Flags().GetString("organization") + requiredInputCheck("Organization name", organizationName) if err != nil { return err } @@ -56,12 +56,6 @@ var addOrgCmd = &cobra.Command{ return err } - if organizationName == "" { - fmt.Println("Missing arguments: Organization name is not defined") - cmd.Help() - os.Exit(1) - } - current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token lc := lclient.New( @@ -105,14 +99,10 @@ var deleteOrgCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") organizationName, err := cmd.Flags().GetString("organization") + requiredInputCheck("Organization name", organizationName) if err != nil { return err } - if organizationName == "" { - fmt.Println("Missing arguments: Organization is not defined") - cmd.Help() - os.Exit(1) - } current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token @@ -149,6 +139,7 @@ var updateOrganizationCmd = &cobra.Command{ return err } organizationName, err := cmd.Flags().GetString("organization") + requiredInputCheck("Organization name", organizationName) if err != nil { return err } @@ -181,12 +172,6 @@ var updateOrganizationCmd = &cobra.Command{ return err } - if organizationName == "" { - fmt.Println("Missing arguments: Organization is not defined") - cmd.Help() - os.Exit(1) - } - current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token lc := lclient.New( @@ -229,6 +214,7 @@ func init() { deleteOrganizationCmd.AddCommand(deleteOrgCmd) deleteOrganizationCmd.AddCommand(RemoveDeployTargetFromOrganizationCmd) deleteOrganizationCmd.AddCommand(RemoveProjectFromOrganizationCmd) + deleteOrganizationCmd.AddCommand(RemoveUserFromOrganization) addOrgCmd.Flags().StringP("organization", "O", "", "Name of the organization") addOrgCmd.Flags().String("friendlyName", "", "Friendly name of the organization") diff --git a/cmd/users.go b/cmd/users.go index 653775cb..a648b2be 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -330,6 +330,67 @@ var addUserToOrganizationCmd = &cobra.Command{ }, } +var RemoveUserFromOrganization = &cobra.Command{ + Use: "user", + Aliases: []string{"u"}, + Short: "Remove a user 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("organization") + requiredInputCheck("Organization name", organizationName) + if err != nil { + return err + } + userEmail, err := cmd.Flags().GetString("email") + requiredInputCheck("User email", userEmail) + if err != nil { + return err + } + owner, err := cmd.Flags().GetBool("owner") + if 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) + + userInput := s.AddUserToOrganizationInput{ + User: s.UserInput{Email: userEmail}, + Organization: organization.ID, + Owner: owner, + } + + orgUser := s.Organization{} + + if yesNo(fmt.Sprintf("You are attempting to remove user '%s' from organization '%s'. This removes the users ability to view or manage the organizations groups, projects, & notifications, are you sure?", userEmail, organization.Name)) { + err = lc.RemoveUserFromOrganization(context.TODO(), &userInput, &orgUser) + handleError(err) + resultData := output.Result{ + Result: "success", + ResultData: map[string]interface{}{ + "User": userEmail, + "Organization Name": organizationName, + }, + } + output.RenderResult(resultData, outputOptions) + } + return nil + }, +} + var ( currentUserEmail string pubKeyValue string @@ -356,4 +417,8 @@ func init() { addUserToOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") addUserToOrganizationCmd.Flags().StringP("email", "E", "", "Email address of the user") addUserToOrganizationCmd.Flags().Bool("owner", false, "Set the user as an owner of the organization") + + RemoveUserFromOrganization.Flags().StringP("organization", "O", "", "Name of the organization") + RemoveUserFromOrganization.Flags().StringP("email", "E", "", "Email address of the user") + RemoveUserFromOrganization.Flags().Bool("owner", false, "Set the user as an owner of the organization") } diff --git a/go.mod b/go.mod index 5de2eea7..4f335ab1 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231020030557-4ea384607fc3 + github.com/uselagoon/machinery v0.0.12-0.20231020044620-78ab64a18d9d golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index 1f9165b9..339c9821 100644 --- a/go.sum +++ b/go.sum @@ -125,6 +125,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231015225416-9e8151292e0c h1:fguK6vwc github.com/uselagoon/machinery v0.0.12-0.20231015225416-9e8151292e0c/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231020030557-4ea384607fc3 h1:2a/j1S+wdbqbCSIuijboMTeTc8X2KpLz8X14Nqk0tG4= github.com/uselagoon/machinery v0.0.12-0.20231020030557-4ea384607fc3/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231020044620-78ab64a18d9d h1:fEQ80pWiPLZWCGiecuMhkRGq0PXfWkQP7ACCfDiUx4E= +github.com/uselagoon/machinery v0.0.12-0.20231020044620-78ab64a18d9d/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From 86e5fb44c472ef9dae5efc981c8fa5a4e558f5f7 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Mon, 23 Oct 2023 14:47:37 +1100 Subject: [PATCH 17/21] Updated add project cmd + various fixes --- cmd/deploytarget.go | 38 ++++++++---- cmd/get.go | 12 ++-- cmd/groups.go | 19 ++++-- cmd/list.go | 50 +++++++++++----- cmd/organization.go | 80 +++++++++++++++----------- cmd/project.go | 137 +++++++++++++++++++++++++++----------------- cmd/shared.go | 6 +- cmd/users.go | 24 +++++--- go.mod | 2 +- go.sum | 2 + 10 files changed, 235 insertions(+), 135 deletions(-) diff --git a/cmd/deploytarget.go b/cmd/deploytarget.go index 9bcdef22..e1025b33 100644 --- a/cmd/deploytarget.go +++ b/cmd/deploytarget.go @@ -333,13 +333,20 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{ debug, err := cmd.Flags().GetBool("debug") handleError(err) - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) - deployTarget, err := cmd.Flags().GetUint("deployTarget") - requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))) + 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 @@ -383,13 +390,20 @@ var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{ debug, err := cmd.Flags().GetBool("debug") handleError(err) - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) - deployTarget, err := cmd.Flags().GetUint("deployTarget") - requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))) + 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 @@ -436,14 +450,14 @@ 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("organization", "O", "", "Name of Organization") - addDeployTargetToOrganizationCmd.Flags().UintP("deployTarget", "D", 0, "ID of DeployTarget") + 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("organization", "O", "", "Name of Organization") - RemoveDeployTargetFromOrganizationCmd.Flags().UintP("deployTarget", "D", 0, "ID 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") diff --git a/cmd/get.go b/cmd/get.go index 082259e0..089ab1e3 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -181,11 +181,13 @@ var getOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + 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 @@ -199,7 +201,7 @@ var getOrganizationCmd = &cobra.Command{ if organization.Name == "" { output.RenderInfo(fmt.Sprintf("No organization found for '%s'", organizationName), outputOptions) - os.Exit(0) + return nil } data := []output.Data{} @@ -213,7 +215,7 @@ var getOrganizationCmd = &cobra.Command{ }) dataMain := output.Table{ - Header: []string{"ID", "Name", "Description", "quotaProject", "quotaGroup", "quotaNotification"}, + Header: []string{"ID", "Name", "Description", "Project Quota", "Group Quota", "Notification Quota"}, Data: data, } @@ -236,5 +238,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("organization", "O", "", "Name of the organization") + getOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization") } diff --git a/cmd/groups.go b/cmd/groups.go index be86fad7..1dedda77 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -242,17 +242,24 @@ var addGroupToOrganizationCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") handleError(err) - orgOwner, err := cmd.Flags().GetBool("orgOwner") - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + orgOwner, err := cmd.Flags().GetBool("org-owner") 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 + } groupName, err := cmd.Flags().GetString("group") - requiredInputCheck("Group name", groupName) if err != nil { return err } + if err := requiredInputCheck("Group name", groupName); err != nil { + return err + } current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token @@ -296,7 +303,7 @@ func init() { deleteUserFromGroupCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user") deleteProjectFromGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") deleteGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group") - addGroupToOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") + addGroupToOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization") addGroupToOrganizationCmd.Flags().StringP("group", "G", "", "Name of the group") - addGroupToOrganizationCmd.Flags().Bool("orgOwner", false, "Flag to add the user to the group as an owner") + addGroupToOrganizationCmd.Flags().Bool("org-owner", false, "Flag to add the user to the group as an owner") } diff --git a/cmd/list.go b/cmd/list.go index 2d5e9e34..0061fec7 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -457,8 +457,13 @@ var listOrganizationProjectsCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + 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 @@ -481,7 +486,7 @@ var listOrganizationProjectsCmd = &cobra.Command{ }) } dataMain := output.Table{ - Header: []string{"ID", "Name", "GroupCount"}, + Header: []string{"ID", "Name", "Group Count"}, Data: data, } output.RenderOutput(dataMain, outputOptions) @@ -501,8 +506,13 @@ var listOrganizationGroupsCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + 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 @@ -526,7 +536,7 @@ var listOrganizationGroupsCmd = &cobra.Command{ }) } dataMain := output.Table{ - Header: []string{"ID", "Name", "Type", "MemberCount"}, + Header: []string{"ID", "Name", "Type", "Member Count"}, Data: data, } output.RenderOutput(dataMain, outputOptions) @@ -546,8 +556,13 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + 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 @@ -573,7 +588,7 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ }) } dataMain := output.Table{ - Header: []string{"ID", "Name", "Router Pattern", "ConsoleURL", "Cloud Region", "Cloud Provider", "SSH Host", "SSH Port"}, + Header: []string{"ID", "Name", "Router Pattern", "Console URL", "Cloud Region", "Cloud Provider", "SSH Host", "SSH Port"}, Data: data, } output.RenderOutput(dataMain, outputOptions) @@ -593,8 +608,13 @@ var ListOrganizationUsersCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + 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 @@ -651,8 +671,8 @@ func init() { listUsersCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)") listGroupProjectsCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list projects in") listVariablesCmd.Flags().BoolP("reveal", "", false, "Reveal the variable values") - listOrganizationProjectsCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated projects for") - ListOrganizationUsersCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated users for") - listOrganizationGroupsCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated groups for") - listOrganizationDeployTargetsCmd.Flags().StringP("organization", "O", "", "Name of the organization to list associated deploy targets for") + listOrganizationProjectsCmd.Flags().StringP("name", "O", "", "Name of the organization to list associated projects for") + ListOrganizationUsersCmd.Flags().StringP("name", "O", "", "Name of the organization to list associated users for") + listOrganizationGroupsCmd.Flags().StringP("name", "O", "", "Name of the organization to list associated groups for") + listOrganizationDeployTargetsCmd.Flags().StringP("name", "O", "", "Name of the organization to list associated deploy targets for") } diff --git a/cmd/organization.go b/cmd/organization.go index d781c081..b5b77b51 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -22,12 +22,14 @@ var addOrgCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + organizationName, err := cmd.Flags().GetString("name") if err != nil { return err } - organizationFriendlyName, err := cmd.Flags().GetString("friendlyName") + if err := requiredInputCheck("Organization name", organizationName); err != nil { + return err + } + organizationFriendlyName, err := cmd.Flags().GetString("friendly-name") if err != nil { return err } @@ -35,23 +37,23 @@ var addOrgCmd = &cobra.Command{ if err != nil { return err } - organizationQuotaProject, err := cmd.Flags().GetUint("quotaProject") + organizationQuotaProject, err := cmd.Flags().GetUint("project-quota") if err != nil { return err } - organizationQuotaGroup, err := cmd.Flags().GetUint("quotaGroup") + organizationQuotaGroup, err := cmd.Flags().GetUint("group-quota") if err != nil { return err } - organizationQuotaNotification, err := cmd.Flags().GetUint("quotaNotification") + organizationQuotaNotification, err := cmd.Flags().GetUint("notification-quota") if err != nil { return err } - organizationQuotaEnvironment, err := cmd.Flags().GetUint("quotaEnvironment") + organizationQuotaEnvironment, err := cmd.Flags().GetUint("environment-quota") if err != nil { return err } - organizationQuotaRoute, err := cmd.Flags().GetUint("quotaRoute") + organizationQuotaRoute, err := cmd.Flags().GetUint("route-quota") if err != nil { return err } @@ -98,11 +100,16 @@ var deleteOrgCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) 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 @@ -138,12 +145,17 @@ var updateOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + organizationName, err := cmd.Flags().GetString("name") + if err != nil { + return err + } + if err := requiredInputCheck("Organization name", organizationName); err != nil { + return err + } if err != nil { return err } - organizationFriendlyName, err := cmd.Flags().GetString("friendlyName") + organizationFriendlyName, err := cmd.Flags().GetString("friendly-name") if err != nil { return err } @@ -151,23 +163,23 @@ var updateOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationQuotaProject, err := cmd.Flags().GetUint("quotaProject") + organizationQuotaProject, err := cmd.Flags().GetUint("project-quota") if err != nil { return err } - organizationQuotaGroup, err := cmd.Flags().GetUint("quotaGroup") + organizationQuotaGroup, err := cmd.Flags().GetUint("group-quota") if err != nil { return err } - organizationQuotaNotification, err := cmd.Flags().GetUint("quotaNotification") + organizationQuotaNotification, err := cmd.Flags().GetUint("notification-quota") if err != nil { return err } - organizationQuotaEnvironment, err := cmd.Flags().GetUint("quotaEnvironment") + organizationQuotaEnvironment, err := cmd.Flags().GetUint("environment-quota") if err != nil { return err } - organizationQuotaRoute, err := cmd.Flags().GetUint("quotaRoute") + organizationQuotaRoute, err := cmd.Flags().GetUint("route-quota") if err != nil { return err } @@ -216,23 +228,23 @@ func init() { deleteOrganizationCmd.AddCommand(RemoveProjectFromOrganizationCmd) deleteOrganizationCmd.AddCommand(RemoveUserFromOrganization) - addOrgCmd.Flags().StringP("organization", "O", "", "Name of the organization") - addOrgCmd.Flags().String("friendlyName", "", "Friendly name of the organization") + addOrgCmd.Flags().StringP("name", "O", "", "Name of the organization") + addOrgCmd.Flags().String("friendly-name", "", "Friendly name of the organization") addOrgCmd.Flags().String("description", "", "Description of the organization") - addOrgCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization") - addOrgCmd.Flags().Uint("quotaGroup", 0, "Group quota for the organization") - addOrgCmd.Flags().Uint("quotaNotification", 0, "Notification quota for the organization") - addOrgCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") - addOrgCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") - - updateOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization to update") - updateOrganizationCmd.Flags().String("friendlyName", "", "Friendly name of the organization") + addOrgCmd.Flags().Uint("project-quota", 0, "Project quota for the organization") + addOrgCmd.Flags().Uint("group-quota", 0, "Group quota for the organization") + addOrgCmd.Flags().Uint("notification-quota", 0, "Notification quota for the organization") + addOrgCmd.Flags().Uint("environment-quota", 0, "Environment quota for the organization") + addOrgCmd.Flags().Uint("route-quota", 0, "Route quota for the organization") + + updateOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization to update") + updateOrganizationCmd.Flags().String("friendly-name", "", "Friendly name of the organization") updateOrganizationCmd.Flags().String("description", "", "Description of the organization") - updateOrganizationCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization") - updateOrganizationCmd.Flags().Uint("quotaGroup", 0, "Group quota for the organization") - updateOrganizationCmd.Flags().Uint("quotaNotification", 0, "Notification quota for the organization") - updateOrganizationCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization") - updateOrganizationCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization") + updateOrganizationCmd.Flags().Uint("project-quota", 0, "Project quota for the organization") + updateOrganizationCmd.Flags().Uint("group-quota", 0, "Group quota for the organization") + updateOrganizationCmd.Flags().Uint("notification-quota", 0, "Notification quota for the organization") + updateOrganizationCmd.Flags().Uint("environment-quota", 0, "Environment quota for the organization") + updateOrganizationCmd.Flags().Uint("route-quota", 0, "Route quota for the organization") - deleteOrgCmd.Flags().StringP("organization", "O", "", "Name of the organization to delete") + deleteOrgCmd.Flags().StringP("name", "O", "", "Name of the organization to delete") } diff --git a/cmd/project.go b/cmd/project.go index 6339b08c..d458364c 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -368,7 +368,6 @@ var deleteProjectMetadataByKey = &cobra.Command{ }, } -// TODO - refactor once machinery is updated var addProjectToOrganizationCmd = &cobra.Command{ Use: "project", Aliases: []string{"p"}, @@ -380,25 +379,38 @@ var addProjectToOrganizationCmd = &cobra.Command{ debug, err := cmd.Flags().GetBool("debug") handleError(err) - requiredInputCheck("Project name", cmdProjectName) - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) - gitUrl, err := cmd.Flags().GetString("gitUrl") - requiredInputCheck("gitUrl", gitUrl) + if err := requiredInputCheck("Project name", cmdProjectName); 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 + } + gitUrl, err := cmd.Flags().GetString("git-url") if err != nil { return err } - productionEnvironment, err := cmd.Flags().GetString("productionEnvironment") - requiredInputCheck("Production Environment", productionEnvironment) + if err := requiredInputCheck("gitUrl", gitUrl); err != nil { + return err + } + productionEnvironment, err := cmd.Flags().GetString("production-environment") if err != nil { return err } + if err := requiredInputCheck("Production Environment", productionEnvironment); err != nil { + return err + } openshift, err := cmd.Flags().GetUint("openshift") - requiredInputCheck("openshift", strconv.Itoa(int(openshift))) if err != nil { return err } - standbyProductionEnvironment, err := cmd.Flags().GetString("standbyProductionEnvironment") + if err := requiredInputCheck("openshift", strconv.Itoa(int(openshift))); err != nil { + return err + } + standbyProductionEnvironment, err := cmd.Flags().GetString("standby-production-environment") if err != nil { return err } @@ -410,19 +422,19 @@ var addProjectToOrganizationCmd = &cobra.Command{ if err != nil { return err } - openshiftProjectPattern, err := cmd.Flags().GetString("openshiftProjectPattern") + openshiftProjectPattern, err := cmd.Flags().GetString("openshift-project-pattern") if err != nil { return err } - developmentEnvironmentsLimit, err := cmd.Flags().GetUint("developmentEnvironmentsLimit") + developmentEnvironmentsLimit, err := cmd.Flags().GetUint("development-environments-limit") if err != nil { return err } - storageCalc, err := cmd.Flags().GetUint("storageCalc") + storageCalc, err := cmd.Flags().GetUint("storage-calc") if err != nil { return err } - autoIdle, err := cmd.Flags().GetUint("autoIdle") + autoIdle, err := cmd.Flags().GetUint("auto-idle") if err != nil { return err } @@ -430,15 +442,15 @@ var addProjectToOrganizationCmd = &cobra.Command{ if err != nil { return err } - privateKey, err := cmd.Flags().GetString("privateKey") + privateKey, err := cmd.Flags().GetString("private-key") if err != nil { return err } - orgOwner, err := cmd.Flags().GetBool("orgOwner") + orgOwner, err := cmd.Flags().GetBool("org-owner") if err != nil { return err } - buildImage, err := cmd.Flags().GetString("buildImage") + buildImage, err := cmd.Flags().GetString("build-image") if err != nil { return err } @@ -446,23 +458,30 @@ var addProjectToOrganizationCmd = &cobra.Command{ if err != nil { return err } - - //factsUi, err := cmd.Flags().GetUint("factsUi") - //if err != nil { - // return err - //} - //problemsUi, err := cmd.Flags().GetUint("problemsUi") - //if err != nil { - // return err - //} - //routerPattern, err := cmd.Flags().GetString("routerPattern") - //if err != nil { - // return err - //} - //deploymentsDisabled, err := cmd.Flags().GetUint("deploymentsDisabled") - //if err != nil { - // return err - //} + factsUi, err := cmd.Flags().GetUint("facts-ui") + if err != nil { + return err + } + problemsUi, err := cmd.Flags().GetUint("problems-ui") + if err != nil { + return err + } + routerPattern, err := cmd.Flags().GetString("router-pattern") + if err != nil { + return err + } + deploymentsDisabled, err := cmd.Flags().GetUint("deployments-disabled") + if err != nil { + return err + } + ProductionBuildPriority, err := cmd.Flags().GetUint("production-build-priority") + if err != nil { + return err + } + DevelopmentBuildPriority, err := cmd.Flags().GetUint("development-build-priority") + if err != nil { + return err + } current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token @@ -493,6 +512,12 @@ var addProjectToOrganizationCmd = &cobra.Command{ AutoIdle: autoIdle, Subfolder: subfolder, PrivateKey: privateKey, + RouterPattern: routerPattern, + ProblemsUI: problemsUi, + FactsUI: factsUi, + ProductionBuildPriority: ProductionBuildPriority, + DevelopmentBuildPriority: DevelopmentBuildPriority, + DeploymentsDisabled: deploymentsDisabled, } project := s.Project{} err = lc.AddProject(context.TODO(), &projectInput, &project) @@ -521,12 +546,16 @@ var RemoveProjectFromOrganizationCmd = &cobra.Command{ debug, err := cmd.Flags().GetBool("debug") handleError(err) - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) - requiredInputCheck("Project", cmdProjectName) + if err := requiredInputCheck("Project name", cmdProjectName); 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 @@ -617,24 +646,30 @@ func init() { getCmd.AddCommand(getProjectMetadata) - addProjectToOrganizationCmd.Flags().String("buildImage", "", "Build Image for the project") + addProjectToOrganizationCmd.Flags().String("build-image", "", "Build Image for the project") addProjectToOrganizationCmd.Flags().String("availability", "", "Availability of the project") - addProjectToOrganizationCmd.Flags().String("gitUrl", "", "GitURL of the project") - addProjectToOrganizationCmd.Flags().String("productionEnvironment", "", "Production Environment for the project") - addProjectToOrganizationCmd.Flags().String("standbyProductionEnvironment", "", "Standby Production Environment for the project") + addProjectToOrganizationCmd.Flags().String("git-url", "", "GitURL of the project") + addProjectToOrganizationCmd.Flags().String("production-environment", "", "Production Environment for the project") + addProjectToOrganizationCmd.Flags().String("standby-production-environment", "", "Standby Production Environment for the project") addProjectToOrganizationCmd.Flags().String("subfolder", "", "Set if the .lagoon.yml should be found in a subfolder useful if you have multiple Lagoon projects per Git Repository") - addProjectToOrganizationCmd.Flags().String("privateKey", "", "Private key to use for the project") + addProjectToOrganizationCmd.Flags().String("private-key", "", "Private key to use for the project") addProjectToOrganizationCmd.Flags().String("branches", "", "branches") addProjectToOrganizationCmd.Flags().String("pullrequests", "", "Which Pull Requests should be deployed") - addProjectToOrganizationCmd.Flags().StringP("organization", "O", "", "Organization to add the project to") - addProjectToOrganizationCmd.Flags().String("openshiftProjectPattern", "", "Pattern of OpenShift Project/Namespace that should be generated") + addProjectToOrganizationCmd.Flags().StringP("name", "O", "", "Name of the Organization to add the project to") + addProjectToOrganizationCmd.Flags().String("openshift-project-pattern", "", "Pattern of OpenShift Project/Namespace that should be generated") + addProjectToOrganizationCmd.Flags().String("router-pattern", "", "Router pattern of the project, e.g. '${service}-${environment}-${project}.lagoon.example.com'") addProjectToOrganizationCmd.Flags().Uint("openshift", 0, "Reference to OpenShift Object this Project should be deployed to") - addProjectToOrganizationCmd.Flags().Uint("autoIdle", 0, "Auto idle setting of the project") - addProjectToOrganizationCmd.Flags().Uint("storageCalc", 0, "Should storage for this environment be calculated") - addProjectToOrganizationCmd.Flags().Uint("developmentEnvironmentsLimit", 0, "How many environments can be deployed at one time") - - addProjectToOrganizationCmd.Flags().Bool("orgOwner", false, "Add the user as an owner of the project") - - RemoveProjectFromOrganizationCmd.Flags().StringP("organization", "O", "", "Organization to remove the project from") + addProjectToOrganizationCmd.Flags().Uint("auto-idle", 0, "Auto idle setting of the project") + addProjectToOrganizationCmd.Flags().Uint("storage-calc", 0, "Should storage for this environment be calculated") + addProjectToOrganizationCmd.Flags().Uint("development-environments-limit", 0, "How many environments can be deployed at one time") + addProjectToOrganizationCmd.Flags().Uint("facts-ui", 0, "Enables the Lagoon insights Facts tab in the UI. Set to 1 to enable, 0 to disable") + addProjectToOrganizationCmd.Flags().Uint("problems-ui", 0, "Enables the Lagoon insights Problems tab in the UI. Set to 1 to enable, 0 to disable") + addProjectToOrganizationCmd.Flags().Uint("deployments-disabled", 0, "Admin only flag for disabling deployments on a project, 1 to disable deployments, 0 to enable") + addProjectToOrganizationCmd.Flags().Uint("production-build-priority", 0, "Set the priority of the production build") + addProjectToOrganizationCmd.Flags().Uint("development-build-priority", 0, "Set the priority of the development build") + + addProjectToOrganizationCmd.Flags().Bool("org-owner", false, "Add the user as an owner of the project") + + RemoveProjectFromOrganizationCmd.Flags().StringP("name", "O", "", "Name of the Organization to remove the project from") } diff --git a/cmd/shared.go b/cmd/shared.go index 6dea07a3..7f1adfed 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -106,9 +106,9 @@ func nullUintCheck(i uint) *uint { return &i } -func requiredInputCheck(field string, value string) { +func requiredInputCheck(field string, value string) error { if value == "" || value == "0" { - fmt.Println(fmt.Sprintf("Missing argument: %s is not defined", field)) - os.Exit(1) + return fmt.Errorf(fmt.Sprintf("Missing argument: %s is not defined", field)) } + return nil } diff --git a/cmd/users.go b/cmd/users.go index a648b2be..9339cd1c 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -282,16 +282,20 @@ var addUserToOrganizationCmd = &cobra.Command{ debug, err := cmd.Flags().GetBool("debug") handleError(err) - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + organizationName, err := cmd.Flags().GetString("name") if err != nil { return err } + if err := requiredInputCheck("Organization name", organizationName); err != nil { + return err + } userEmail, err := cmd.Flags().GetString("email") - requiredInputCheck("User email", userEmail) if err != nil { return err } + if err := requiredInputCheck("User email", userEmail); err != nil { + return err + } owner, err := cmd.Flags().GetBool("owner") if err != nil { return err @@ -341,16 +345,20 @@ var RemoveUserFromOrganization = &cobra.Command{ debug, err := cmd.Flags().GetBool("debug") handleError(err) - organizationName, err := cmd.Flags().GetString("organization") - requiredInputCheck("Organization name", organizationName) + organizationName, err := cmd.Flags().GetString("name") if err != nil { return err } + if err := requiredInputCheck("Organization name", organizationName); err != nil { + return err + } userEmail, err := cmd.Flags().GetString("email") - requiredInputCheck("User email", userEmail) if err != nil { return err } + if err := requiredInputCheck("User email", userEmail); err != nil { + return err + } owner, err := cmd.Flags().GetBool("owner") if err != nil { return err @@ -414,11 +422,11 @@ func init() { getUserKeysCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to check users in (if not specified, will default to all groups)") getAllUserKeysCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)") - addUserToOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization") + addUserToOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization") addUserToOrganizationCmd.Flags().StringP("email", "E", "", "Email address of the user") addUserToOrganizationCmd.Flags().Bool("owner", false, "Set the user as an owner of the organization") - RemoveUserFromOrganization.Flags().StringP("organization", "O", "", "Name of the organization") + RemoveUserFromOrganization.Flags().StringP("name", "O", "", "Name of the organization") RemoveUserFromOrganization.Flags().StringP("email", "E", "", "Email address of the user") RemoveUserFromOrganization.Flags().Bool("owner", false, "Set the user as an owner of the organization") } diff --git a/go.mod b/go.mod index 4f335ab1..e38f3792 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231020044620-78ab64a18d9d + github.com/uselagoon/machinery v0.0.12-0.20231023034503-73831e83e104 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index 339c9821..d06a7b30 100644 --- a/go.sum +++ b/go.sum @@ -127,6 +127,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231020030557-4ea384607fc3 h1:2a/j1S+w github.com/uselagoon/machinery v0.0.12-0.20231020030557-4ea384607fc3/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231020044620-78ab64a18d9d h1:fEQ80pWiPLZWCGiecuMhkRGq0PXfWkQP7ACCfDiUx4E= github.com/uselagoon/machinery v0.0.12-0.20231020044620-78ab64a18d9d/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231023034503-73831e83e104 h1:4U+8Re8V0AY8jBuWAiGu7d07+LiKky9tDcfZBpgd/NU= +github.com/uselagoon/machinery v0.0.12-0.20231023034503-73831e83e104/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From bbb125b8698f5ab235e92c58a8d089d670d0553a Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Mon, 23 Oct 2023 16:23:16 +1100 Subject: [PATCH 18/21] Updated to account for negative quota --- cmd/organization.go | 50 ++++++++++++++++++++++----------------------- cmd/shared.go | 7 +++++++ go.mod | 2 +- go.sum | 2 ++ 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/cmd/organization.go b/cmd/organization.go index b5b77b51..4ca36b13 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -37,23 +37,23 @@ var addOrgCmd = &cobra.Command{ if err != nil { return err } - organizationQuotaProject, err := cmd.Flags().GetUint("project-quota") + organizationQuotaProject, err := cmd.Flags().GetInt("project-quota") if err != nil { return err } - organizationQuotaGroup, err := cmd.Flags().GetUint("group-quota") + organizationQuotaGroup, err := cmd.Flags().GetInt("group-quota") if err != nil { return err } - organizationQuotaNotification, err := cmd.Flags().GetUint("notification-quota") + organizationQuotaNotification, err := cmd.Flags().GetInt("notification-quota") if err != nil { return err } - organizationQuotaEnvironment, err := cmd.Flags().GetUint("environment-quota") + organizationQuotaEnvironment, err := cmd.Flags().GetInt("environment-quota") if err != nil { return err } - organizationQuotaRoute, err := cmd.Flags().GetUint("route-quota") + organizationQuotaRoute, err := cmd.Flags().GetInt("route-quota") if err != nil { return err } @@ -163,23 +163,23 @@ var updateOrganizationCmd = &cobra.Command{ if err != nil { return err } - organizationQuotaProject, err := cmd.Flags().GetUint("project-quota") + organizationQuotaProject, err := cmd.Flags().GetInt("project-quota") if err != nil { return err } - organizationQuotaGroup, err := cmd.Flags().GetUint("group-quota") + organizationQuotaGroup, err := cmd.Flags().GetInt("group-quota") if err != nil { return err } - organizationQuotaNotification, err := cmd.Flags().GetUint("notification-quota") + organizationQuotaNotification, err := cmd.Flags().GetInt("notification-quota") if err != nil { return err } - organizationQuotaEnvironment, err := cmd.Flags().GetUint("environment-quota") + organizationQuotaEnvironment, err := cmd.Flags().GetInt("environment-quota") if err != nil { return err } - organizationQuotaRoute, err := cmd.Flags().GetUint("route-quota") + organizationQuotaRoute, err := cmd.Flags().GetInt("route-quota") if err != nil { return err } @@ -196,11 +196,11 @@ var updateOrganizationCmd = &cobra.Command{ organizationInput := s.UpdateOrganizationPatchInput{ Description: nullStrCheck(organizationDescription), FriendlyName: nullStrCheck(organizationFriendlyName), - QuotaProject: nullUintCheck(organizationQuotaProject), - QuotaGroup: nullUintCheck(organizationQuotaGroup), - QuotaNotification: nullUintCheck(organizationQuotaNotification), - QuotaEnvironment: nullUintCheck(organizationQuotaEnvironment), - QuotaRoute: nullUintCheck(organizationQuotaRoute), + QuotaProject: nullIntCheck(organizationQuotaProject), + QuotaGroup: nullIntCheck(organizationQuotaGroup), + QuotaNotification: nullIntCheck(organizationQuotaNotification), + QuotaEnvironment: nullIntCheck(organizationQuotaEnvironment), + QuotaRoute: nullIntCheck(organizationQuotaRoute), } result, err := l.UpdateOrganization(context.TODO(), organization.ID, organizationInput, lc) handleError(err) @@ -231,20 +231,20 @@ func init() { addOrgCmd.Flags().StringP("name", "O", "", "Name of the organization") addOrgCmd.Flags().String("friendly-name", "", "Friendly name of the organization") addOrgCmd.Flags().String("description", "", "Description of the organization") - addOrgCmd.Flags().Uint("project-quota", 0, "Project quota for the organization") - addOrgCmd.Flags().Uint("group-quota", 0, "Group quota for the organization") - addOrgCmd.Flags().Uint("notification-quota", 0, "Notification quota for the organization") - addOrgCmd.Flags().Uint("environment-quota", 0, "Environment quota for the organization") - addOrgCmd.Flags().Uint("route-quota", 0, "Route quota for the organization") + addOrgCmd.Flags().Int("project-quota", 0, "Project quota for the organization") + addOrgCmd.Flags().Int("group-quota", 0, "Group quota for the organization") + addOrgCmd.Flags().Int("notification-quota", 0, "Notification quota for the organization") + addOrgCmd.Flags().Int("environment-quota", 0, "Environment quota for the organization") + addOrgCmd.Flags().Int("route-quota", 0, "Route quota for the organization") updateOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization to update") updateOrganizationCmd.Flags().String("friendly-name", "", "Friendly name of the organization") updateOrganizationCmd.Flags().String("description", "", "Description of the organization") - updateOrganizationCmd.Flags().Uint("project-quota", 0, "Project quota for the organization") - updateOrganizationCmd.Flags().Uint("group-quota", 0, "Group quota for the organization") - updateOrganizationCmd.Flags().Uint("notification-quota", 0, "Notification quota for the organization") - updateOrganizationCmd.Flags().Uint("environment-quota", 0, "Environment quota for the organization") - updateOrganizationCmd.Flags().Uint("route-quota", 0, "Route quota for the organization") + updateOrganizationCmd.Flags().Int("project-quota", 0, "Project quota for the organization") + updateOrganizationCmd.Flags().Int("group-quota", 0, "Group quota for the organization") + updateOrganizationCmd.Flags().Int("notification-quota", 0, "Notification quota for the organization") + updateOrganizationCmd.Flags().Int("environment-quota", 0, "Environment quota for the organization") + updateOrganizationCmd.Flags().Int("route-quota", 0, "Route quota for the organization") deleteOrgCmd.Flags().StringP("name", "O", "", "Name of the organization to delete") } diff --git a/cmd/shared.go b/cmd/shared.go index 7f1adfed..7af150db 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -106,6 +106,13 @@ func nullUintCheck(i uint) *uint { return &i } +func nullIntCheck(i int) *int { + if i == 0 { + return nil + } + return &i +} + func requiredInputCheck(field string, value string) error { if value == "" || value == "0" { return fmt.Errorf(fmt.Sprintf("Missing argument: %s is not defined", field)) diff --git a/go.mod b/go.mod index e38f3792..359c4419 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231023034503-73831e83e104 + github.com/uselagoon/machinery v0.0.12-0.20231023051946-037b2d611755 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index d06a7b30..d9d0f33a 100644 --- a/go.sum +++ b/go.sum @@ -129,6 +129,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231020044620-78ab64a18d9d h1:fEQ80pWi github.com/uselagoon/machinery v0.0.12-0.20231020044620-78ab64a18d9d/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231023034503-73831e83e104 h1:4U+8Re8V0AY8jBuWAiGu7d07+LiKky9tDcfZBpgd/NU= github.com/uselagoon/machinery v0.0.12-0.20231023034503-73831e83e104/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.12-0.20231023051946-037b2d611755 h1:wRzZdJmoItWrkwcdWxFI0yaHgt6cbm42xP66XrHVb8c= +github.com/uselagoon/machinery v0.0.12-0.20231023051946-037b2d611755/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From 32d38841f2c2311f0305bb3f843fd1f562911ba8 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 28 Nov 2023 12:01:52 +1100 Subject: [PATCH 19/21] Updated listOrganizationDeployTargetsCmd & included listOrganizationsCmd --- cmd/list.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 0061fec7..4e737c8f 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "strconv" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -560,9 +561,15 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ if err != nil { return err } - if err := requiredInputCheck("Organization name", organizationName); err != nil { + organizationID, err := cmd.Flags().GetUint("id") + if err != nil { return err } + if err := requiredInputCheck("Organization name", organizationName); err != nil { + if err := requiredInputCheck("Organization ID", strconv.Itoa(int(organizationID))); err != nil { + return err + } + } current := lagoonCLIConfig.Current token := lagoonCLIConfig.Lagoons[current].Token @@ -571,7 +578,7 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ lagoonCLIVersion, &token, debug) - deployTargets, err := l.ListDeployTargetsByOrganizationName(context.TODO(), organizationName, lc) + deployTargets, err := l.ListDeployTargetsByOrganizationNameOrID(context.TODO(), nullStrCheck(organizationName), nullUintCheck(organizationID), lc) handleError(err) data := []output.Data{} @@ -580,7 +587,6 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%d", dt.ID)), returnNonEmptyString(fmt.Sprintf("%s", dt.Name)), returnNonEmptyString(fmt.Sprintf("%s", dt.RouterPattern)), - returnNonEmptyString(fmt.Sprintf("%s", dt.ConsoleURL)), returnNonEmptyString(fmt.Sprintf("%s", dt.CloudRegion)), returnNonEmptyString(fmt.Sprintf("%s", dt.CloudProvider)), returnNonEmptyString(fmt.Sprintf("%s", dt.SSHHost)), @@ -588,7 +594,7 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ }) } dataMain := output.Table{ - Header: []string{"ID", "Name", "Router Pattern", "Console URL", "Cloud Region", "Cloud Provider", "SSH Host", "SSH Port"}, + Header: []string{"ID", "Name", "Router Pattern", "Cloud Region", "Cloud Provider", "SSH Host", "SSH Port"}, Data: data, } output.RenderOutput(dataMain, outputOptions) @@ -648,6 +654,51 @@ var ListOrganizationUsersCmd = &cobra.Command{ }, } +var listOrganizationsCmd = &cobra.Command{ + Use: "organizations", + Aliases: []string{"o"}, + Short: "List all organizations", + 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 + } + + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + &token, + debug) + + organizations, err := l.AllOrganizations(context.TODO(), lc) + + data := []output.Data{} + for _, organization := range *organizations { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%d", organization.ID)), + returnNonEmptyString(fmt.Sprintf("%s", organization.Name)), + returnNonEmptyString(fmt.Sprintf("%d", organization.Description)), + returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaProject)), + returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaGroup)), + returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaNotification)), + returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaEnvironment)), + returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaRoute)), + }) + } + dataMain := output.Table{ + Header: []string{"ID", "Name", "Description", "Project Quota", "Group Quota", "Notification Quota", "Environment Quota", "Route Quota"}, + Data: data, + } + output.RenderOutput(dataMain, outputOptions) + return nil + }, +} + func init() { listCmd.AddCommand(listDeployTargetsCmd) listCmd.AddCommand(listDeploymentsCmd) @@ -667,6 +718,7 @@ func init() { listOrganizationCmd.AddCommand(ListOrganizationUsersCmd) listOrganizationCmd.AddCommand(listOrganizationGroupsCmd) listOrganizationCmd.AddCommand(listOrganizationDeployTargetsCmd) + listOrganizationCmd.AddCommand(listOrganizationsCmd) listCmd.Flags().BoolVarP(&listAllProjects, "all-projects", "", false, "All projects (if supported)") listUsersCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)") listGroupProjectsCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list projects in") @@ -675,4 +727,5 @@ func init() { ListOrganizationUsersCmd.Flags().StringP("name", "O", "", "Name of the organization to list associated users for") listOrganizationGroupsCmd.Flags().StringP("name", "O", "", "Name of the organization to list associated groups for") listOrganizationDeployTargetsCmd.Flags().StringP("name", "O", "", "Name of the organization to list associated deploy targets for") + listOrganizationDeployTargetsCmd.Flags().Uint("id", 0, "ID of the organization to list associated deploy targets for") } From 3d8bd61285937603daf609aabd8dd303bf00f08e Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 28 Nov 2023 12:09:54 +1100 Subject: [PATCH 20/21] Updated to machinery v0.0.13 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 359c4419..3bcebbdb 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20231023051946-037b2d611755 + github.com/uselagoon/machinery v0.0.13 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index d9d0f33a..de85fba8 100644 --- a/go.sum +++ b/go.sum @@ -131,6 +131,8 @@ github.com/uselagoon/machinery v0.0.12-0.20231023034503-73831e83e104 h1:4U+8Re8V github.com/uselagoon/machinery v0.0.12-0.20231023034503-73831e83e104/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/uselagoon/machinery v0.0.12-0.20231023051946-037b2d611755 h1:wRzZdJmoItWrkwcdWxFI0yaHgt6cbm42xP66XrHVb8c= github.com/uselagoon/machinery v0.0.12-0.20231023051946-037b2d611755/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.13 h1:ZCLBNWJmDr3wikaHs3pWhQ1j8MprhIqRuChgSqmLyZc= +github.com/uselagoon/machinery v0.0.13/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From e0318dbfe2588c933fb76bcd9929578564526cde Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 28 Nov 2023 12:13:25 +1100 Subject: [PATCH 21/21] Resolved test err --- cmd/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/list.go b/cmd/list.go index 4e737c8f..4fe1c1ee 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -682,7 +682,7 @@ var listOrganizationsCmd = &cobra.Command{ data = append(data, []string{ returnNonEmptyString(fmt.Sprintf("%d", organization.ID)), returnNonEmptyString(fmt.Sprintf("%s", organization.Name)), - returnNonEmptyString(fmt.Sprintf("%d", organization.Description)), + returnNonEmptyString(fmt.Sprintf("%s", organization.Description)), returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaProject)), returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaGroup)), returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaNotification)),