Skip to content

Commit a386f67

Browse files
refactor: refactor and fix organizationbyname to error if organization not found (#353)
1 parent 146b272 commit a386f67

File tree

8 files changed

+55
-12
lines changed

8 files changed

+55
-12
lines changed

cmd/deploytarget.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{
361361
if err != nil {
362362
return err
363363
}
364+
if organization.Name == "" {
365+
return fmt.Errorf("error querying organization by name")
366+
}
364367

365368
deployTargetInput := s.AddDeployTargetToOrganizationInput{
366369
DeployTarget: deploytarget,
@@ -421,6 +424,9 @@ var removeDeployTargetFromOrganizationCmd = &cobra.Command{
421424
if err != nil {
422425
return err
423426
}
427+
if organization.Name == "" {
428+
return fmt.Errorf("error querying organization by name")
429+
}
424430

425431
deployTargetInput := s.RemoveDeployTargetFromOrganizationInput{
426432
DeployTarget: deploytarget,

cmd/get.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,8 @@ var getOrganizationCmd = &cobra.Command{
288288
if err != nil {
289289
return err
290290
}
291-
292291
if organization.Name == "" {
293-
output.RenderInfo(fmt.Sprintf("No organization found for '%s'", organizationName), outputOptions)
294-
return nil
292+
return fmt.Errorf("error querying organization by name")
295293
}
296294

297295
data := []output.Data{}

cmd/groups.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ var addGroupCmd = &cobra.Command{
7373
if err != nil {
7474
return err
7575
}
76+
if organization.Name == "" {
77+
return fmt.Errorf("error querying organization by name")
78+
}
7679
groupInput := s.AddGroupToOrganizationInput{
7780
Name: groupName,
7881
Organization: organization.ID,

cmd/list.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,14 @@ var listOrganizationProjectsCmd = &cobra.Command{
657657
&token,
658658
debug)
659659

660-
org, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
660+
organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
661661
if err != nil {
662662
return err
663663
}
664-
orgProjects, err := l.ListProjectsByOrganizationID(context.TODO(), org.ID, lc)
664+
if organization.Name == "" {
665+
return fmt.Errorf("error querying organization by name")
666+
}
667+
orgProjects, err := l.ListProjectsByOrganizationID(context.TODO(), organization.ID, lc)
665668
if err != nil {
666669
return err
667670
}
@@ -716,11 +719,14 @@ var listOrganizationGroupsCmd = &cobra.Command{
716719
&token,
717720
debug)
718721

719-
org, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
722+
organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
720723
if err != nil {
721724
return err
722725
}
723-
orgGroups, err := l.ListGroupsByOrganizationID(context.TODO(), org.ID, lc)
726+
if organization.Name == "" {
727+
return fmt.Errorf("error querying organization by name")
728+
}
729+
orgGroups, err := l.ListGroupsByOrganizationID(context.TODO(), organization.ID, lc)
724730
if err != nil {
725731
return err
726732
}
@@ -839,6 +845,9 @@ var ListOrganizationUsersCmd = &cobra.Command{
839845
if err != nil {
840846
return err
841847
}
848+
if organization.Name == "" {
849+
return fmt.Errorf("error querying organization by name")
850+
}
842851
users, err := l.UsersByOrganization(context.TODO(), organization.ID, lc)
843852
if err != nil {
844853
return err

cmd/organization.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ var deleteOrganizationCmd = &cobra.Command{
128128
if err != nil {
129129
return err
130130
}
131+
if organization.Name == "" {
132+
return fmt.Errorf("error querying organization by name")
133+
}
131134
if yesNo(fmt.Sprintf("You are attempting to delete organization '%s', are you sure?", organization.Name)) {
132135
_, err := l.DeleteOrganization(context.TODO(), organization.ID, lc)
133136
if err != nil {
@@ -161,9 +164,6 @@ var updateOrganizationCmd = &cobra.Command{
161164
if err := requiredInputCheck("Organization name", organizationName); err != nil {
162165
return err
163166
}
164-
if err != nil {
165-
return err
166-
}
167167
organizationFriendlyName, err := cmd.Flags().GetString("friendly-name")
168168
if err != nil {
169169
return err
@@ -206,6 +206,9 @@ var updateOrganizationCmd = &cobra.Command{
206206
if err != nil {
207207
return err
208208
}
209+
if organization.Name == "" {
210+
return fmt.Errorf("error querying organization by name")
211+
}
209212
organizationInput := s.UpdateOrganizationPatchInput{
210213
Description: nullStrCheck(organizationDescription),
211214
FriendlyName: nullStrCheck(organizationFriendlyName),

cmd/project.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ var addProjectCmd = &cobra.Command{
8181
if err != nil {
8282
return err
8383
}
84+
organizationID, err := cmd.Flags().GetUint("organization-id")
85+
if err != nil {
86+
return err
87+
}
8488
gitUrl, err := cmd.Flags().GetString("gitUrl")
8589
if err != nil {
8690
return err
@@ -168,12 +172,21 @@ var addProjectCmd = &cobra.Command{
168172
PrivateKey: privateKey,
169173
RouterPattern: routerPattern,
170174
}
171-
172-
if organizationName != "" {
175+
// if organizationid is provided, use it over the name
176+
if organizationID != 0 {
177+
projectInput.Organization = organizationID
178+
}
179+
// otherwise if name is provided use it
180+
if organizationName != "" && organizationID == 0 {
173181
organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
174182
if err != nil {
175183
return err
176184
}
185+
// since getorganizationbyname returns null response if an organization doesn't exist
186+
// check if the result has a name
187+
if organization.Name == "" {
188+
return fmt.Errorf("error querying organization by name")
189+
}
177190
projectInput.Organization = organization.ID
178191
}
179192

@@ -510,6 +523,9 @@ var removeProjectFromOrganizationCmd = &cobra.Command{
510523
if err != nil {
511524
return err
512525
}
526+
if organization.Name == "" {
527+
return fmt.Errorf("error querying organization by name")
528+
}
513529

514530
projectInput := s.RemoveProjectFromOrganizationInput{
515531
Project: project.ID,
@@ -576,6 +592,7 @@ func init() {
576592
addProjectCmd.Flags().UintP("openshift", "S", 0, "Reference to OpenShift Object this Project should be deployed to")
577593
addProjectCmd.Flags().Bool("owner", false, "Add the user as an owner of the project")
578594
addProjectCmd.Flags().StringP("organization-name", "O", "", "Name of the Organization to add the project to")
595+
addProjectCmd.Flags().UintP("organization-id", "", 0, "ID of the Organization to add the project to")
579596

580597
listCmd.AddCommand(listProjectByMetadata)
581598
listProjectByMetadata.Flags().StringP("key", "K", "", "The key name of the metadata value you are querying on")

cmd/users.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ var addAdministratorToOrganizationCmd = &cobra.Command{
409409
if err != nil {
410410
return err
411411
}
412+
if organization.Name == "" {
413+
return fmt.Errorf("error querying organization by name")
414+
}
412415

413416
userInput := s.AddUserToOrganizationInput{
414417
User: s.UserInput{Email: userEmail},
@@ -479,6 +482,9 @@ var removeAdministratorFromOrganizationCmd = &cobra.Command{
479482
if err != nil {
480483
return err
481484
}
485+
if organization.Name == "" {
486+
return fmt.Errorf("error querying organization by name")
487+
}
482488

483489
userInput := s.AddUserToOrganizationInput{
484490
User: s.UserInput{Email: userEmail},

docs/commands/lagoon_add_project.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ lagoon add project [flags]
2222
-j, --json string JSON string to patch
2323
-S, --openshift uint Reference to OpenShift Object this Project should be deployed to
2424
-o, --openshiftProjectPattern string Pattern of OpenShift Project/Namespace that should be generated
25+
--organization-id uint ID of the Organization to add the project to
2526
-O, --organization-name string Name of the Organization to add the project to
2627
--owner Add the user as an owner of the project
2728
-I, --privateKey string Private key to use for the project

0 commit comments

Comments
 (0)