From a386f6719c0837f3d94a3ac682e2d01d6d527eaf Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Fri, 14 Jun 2024 11:15:42 +1000 Subject: [PATCH] refactor: refactor and fix organizationbyname to error if organization not found (#353) --- cmd/deploytarget.go | 6 ++++++ cmd/get.go | 4 +--- cmd/groups.go | 3 +++ cmd/list.go | 17 +++++++++++++---- cmd/organization.go | 9 ++++++--- cmd/project.go | 21 +++++++++++++++++++-- cmd/users.go | 6 ++++++ docs/commands/lagoon_add_project.md | 1 + 8 files changed, 55 insertions(+), 12 deletions(-) diff --git a/cmd/deploytarget.go b/cmd/deploytarget.go index b2b7892e..20867cdb 100644 --- a/cmd/deploytarget.go +++ b/cmd/deploytarget.go @@ -361,6 +361,9 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } deployTargetInput := s.AddDeployTargetToOrganizationInput{ DeployTarget: deploytarget, @@ -421,6 +424,9 @@ var removeDeployTargetFromOrganizationCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } deployTargetInput := s.RemoveDeployTargetFromOrganizationInput{ DeployTarget: deploytarget, diff --git a/cmd/get.go b/cmd/get.go index d4f38264..daaab7bf 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -288,10 +288,8 @@ var getOrganizationCmd = &cobra.Command{ if err != nil { return err } - if organization.Name == "" { - output.RenderInfo(fmt.Sprintf("No organization found for '%s'", organizationName), outputOptions) - return nil + return fmt.Errorf("error querying organization by name") } data := []output.Data{} diff --git a/cmd/groups.go b/cmd/groups.go index 6a1c0622..a5150917 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -73,6 +73,9 @@ var addGroupCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } groupInput := s.AddGroupToOrganizationInput{ Name: groupName, Organization: organization.ID, diff --git a/cmd/list.go b/cmd/list.go index 46695b51..1e36e5ef 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -657,11 +657,14 @@ var listOrganizationProjectsCmd = &cobra.Command{ &token, debug) - org, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) + organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) if err != nil { return err } - orgProjects, err := l.ListProjectsByOrganizationID(context.TODO(), org.ID, lc) + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } + orgProjects, err := l.ListProjectsByOrganizationID(context.TODO(), organization.ID, lc) if err != nil { return err } @@ -716,11 +719,14 @@ var listOrganizationGroupsCmd = &cobra.Command{ &token, debug) - org, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) + organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) if err != nil { return err } - orgGroups, err := l.ListGroupsByOrganizationID(context.TODO(), org.ID, lc) + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } + orgGroups, err := l.ListGroupsByOrganizationID(context.TODO(), organization.ID, lc) if err != nil { return err } @@ -839,6 +845,9 @@ var ListOrganizationUsersCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } users, err := l.UsersByOrganization(context.TODO(), organization.ID, lc) if err != nil { return err diff --git a/cmd/organization.go b/cmd/organization.go index 7df40439..b18b60ca 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -128,6 +128,9 @@ var deleteOrganizationCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } if yesNo(fmt.Sprintf("You are attempting to delete organization '%s', are you sure?", organization.Name)) { _, err := l.DeleteOrganization(context.TODO(), organization.ID, lc) if err != nil { @@ -161,9 +164,6 @@ var updateOrganizationCmd = &cobra.Command{ if err := requiredInputCheck("Organization name", organizationName); err != nil { return err } - if err != nil { - return err - } organizationFriendlyName, err := cmd.Flags().GetString("friendly-name") if err != nil { return err @@ -206,6 +206,9 @@ var updateOrganizationCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } organizationInput := s.UpdateOrganizationPatchInput{ Description: nullStrCheck(organizationDescription), FriendlyName: nullStrCheck(organizationFriendlyName), diff --git a/cmd/project.go b/cmd/project.go index 45702d1b..19684d74 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -81,6 +81,10 @@ var addProjectCmd = &cobra.Command{ if err != nil { return err } + organizationID, err := cmd.Flags().GetUint("organization-id") + if err != nil { + return err + } gitUrl, err := cmd.Flags().GetString("gitUrl") if err != nil { return err @@ -168,12 +172,21 @@ var addProjectCmd = &cobra.Command{ PrivateKey: privateKey, RouterPattern: routerPattern, } - - if organizationName != "" { + // if organizationid is provided, use it over the name + if organizationID != 0 { + projectInput.Organization = organizationID + } + // otherwise if name is provided use it + if organizationName != "" && organizationID == 0 { organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) if err != nil { return err } + // since getorganizationbyname returns null response if an organization doesn't exist + // check if the result has a name + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } projectInput.Organization = organization.ID } @@ -510,6 +523,9 @@ var removeProjectFromOrganizationCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } projectInput := s.RemoveProjectFromOrganizationInput{ Project: project.ID, @@ -576,6 +592,7 @@ func init() { addProjectCmd.Flags().UintP("openshift", "S", 0, "Reference to OpenShift Object this Project should be deployed to") addProjectCmd.Flags().Bool("owner", false, "Add the user as an owner of the project") addProjectCmd.Flags().StringP("organization-name", "O", "", "Name of the Organization to add the project to") + addProjectCmd.Flags().UintP("organization-id", "", 0, "ID of 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") diff --git a/cmd/users.go b/cmd/users.go index 29294a52..28c90ba8 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -409,6 +409,9 @@ var addAdministratorToOrganizationCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } userInput := s.AddUserToOrganizationInput{ User: s.UserInput{Email: userEmail}, @@ -479,6 +482,9 @@ var removeAdministratorFromOrganizationCmd = &cobra.Command{ if err != nil { return err } + if organization.Name == "" { + return fmt.Errorf("error querying organization by name") + } userInput := s.AddUserToOrganizationInput{ User: s.UserInput{Email: userEmail}, diff --git a/docs/commands/lagoon_add_project.md b/docs/commands/lagoon_add_project.md index 31699314..5011bf4a 100644 --- a/docs/commands/lagoon_add_project.md +++ b/docs/commands/lagoon_add_project.md @@ -22,6 +22,7 @@ lagoon add project [flags] -j, --json string JSON string to patch -S, --openshift uint Reference to OpenShift Object this Project should be deployed to -o, --openshiftProjectPattern string Pattern of OpenShift Project/Namespace that should be generated + --organization-id uint ID of the Organization to add the project to -O, --organization-name string Name of the Organization to add the project to --owner Add the user as an owner of the project -I, --privateKey string Private key to use for the project