Skip to content

Commit

Permalink
refactor: refactor and fix organizationbyname to error if organizatio…
Browse files Browse the repository at this point in the history
…n not found (#353)
  • Loading branch information
shreddedbacon authored Jun 14, 2024
1 parent 146b272 commit a386f67
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 12 deletions.
6 changes: 6 additions & 0 deletions cmd/deploytarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
3 changes: 3 additions & 0 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions cmd/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
21 changes: 19 additions & 2 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a386f67

Please sign in to comment.