From da89300ec263cef375d831404fe54bcc37bafb57 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Fri, 6 Oct 2023 18:49:55 +1100 Subject: [PATCH] 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")