From bb8acfe8a1eaf161f64e7d7b9d250c7638f4f164 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 19 Oct 2023 17:13:20 +1100 Subject: [PATCH] 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) + } +}