@@ -2,13 +2,15 @@ package project
22
33import (
44 "context"
5+ "encoding/json"
56 "errors"
67 "fmt"
78 "github.com/charmbracelet/huh"
89 "github.com/charmbracelet/lipgloss"
910 "github.com/uselagoon/lagoon-cli/internal/util"
1011 "github.com/uselagoon/machinery/api/lagoon"
1112 "github.com/uselagoon/machinery/api/lagoon/client"
13+ "github.com/uselagoon/machinery/api/schema"
1214 "strconv"
1315)
1416
@@ -47,17 +49,48 @@ func RunCreateWizard(lc *client.Client) (*util.CreateConfig, error) {
4749 return nil , formErr
4850 }
4951 if organizationConfirm {
50- organizations , err := lagoon .AllOrganizationsExtended (context .TODO (), lc ) // requires machinery changes current - todo: change to raw
52+ raw := `query allOrgsWithProjects {
53+ allOrganizations {
54+ id
55+ name
56+ description
57+ friendlyName
58+ quotaProject
59+ quotaGroup
60+ quotaNotification
61+ quotaEnvironment
62+ quotaRoute
63+ projects {
64+ id
65+ name
66+ }
67+ }
68+ }`
69+ resp , err := lc .ProcessRaw (context .TODO (), raw , map [string ]interface {}{})
70+ if err != nil {
71+ return nil , err
72+ }
73+
74+ allOrgs := resp .(map [string ]interface {})["allOrganizations" ]
75+ o , err := json .Marshal (allOrgs )
76+ if err != nil {
77+ return nil , err
78+ }
79+
80+ var organizations []schema.Organization
81+ err = json .Unmarshal (o , & organizations )
5182 if err != nil {
5283 return nil , err
5384 }
85+
86+ orgValidationErr := false
5487 form2 := huh .NewForm (
5588 huh .NewGroup (
5689 huh .NewSelect [string ]().
5790 Title ("Select an organization" ).
5891 OptionsFunc (func () []huh.Option [string ] {
59- options := make ([]huh.Option [string ], len (* organizations ))
60- for i , org := range * organizations {
92+ options := make ([]huh.Option [string ], len (organizations ))
93+ for i , org := range organizations {
6194 orgProjectCount := len (org .Projects )
6295 if orgProjectCount >= org .QuotaProject && org .QuotaProject >= 0 {
6396 quotaFullLabel := lipgloss .NewStyle ().Foreground (lipgloss .Color ("240" )).Render (fmt .Sprintf ("%s | Project Limit: %v/%v | ⚠️ Project Quota full, cannot assigned project to this organization." , org .Name , orgProjectCount , org .QuotaProject ))
@@ -71,11 +104,13 @@ func RunCreateWizard(lc *client.Client) (*util.CreateConfig, error) {
71104 }, & config .OrganizationDetails .Name ).
72105 Value (& config .OrganizationDetails .Name ).
73106 Validate (func (selectedOrg string ) error {
74- for _ , org := range * organizations {
107+ for _ , org := range organizations {
75108 if org .Name == selectedOrg {
76109 orgProjectCount := len (org .Projects )
77110 if orgProjectCount >= org .QuotaProject && org .QuotaProject >= 0 {
78- return fmt .Errorf ("Organization %s has reached its project quota (%d/%d)" , org .Name , orgProjectCount , org .QuotaProject )
111+ orgValidationErr = true
112+ return nil
113+ //return fmt.Errorf("Organization %s has reached its project quota (%d/%d)", org.Name, orgProjectCount, org.QuotaProject)
79114 }
80115 }
81116 }
@@ -88,6 +123,33 @@ func RunCreateWizard(lc *client.Client) (*util.CreateConfig, error) {
88123 if err != nil {
89124 return nil , err
90125 }
126+
127+ if orgValidationErr {
128+ var orgRetryResp string
129+ orgRetryRespForm := huh .NewForm (
130+ huh .NewGroup (
131+ huh .NewSelect [string ]().
132+ Title ("Organization quota exceeded. What would you like to do?" ).
133+ Options (
134+ huh .NewOption ("Go back to previous form" , "back" ),
135+ huh .NewOption ("Cancel wizard" , "cancel" ),
136+ ).
137+ Value (& orgRetryResp ),
138+ ),
139+ ).WithTheme (huh .ThemeCharm ())
140+
141+ err := orgRetryRespForm .Run ()
142+ if err != nil {
143+ return nil , err
144+ }
145+
146+ switch orgRetryResp {
147+ case "back" :
148+ return RunCreateWizard (lc )
149+ case "cancel" :
150+ return nil , huh .ErrUserAborted
151+ }
152+ }
91153 }
92154
93155 deploytargets , err := lagoon .ListDeployTargets (context .TODO (), lc )
0 commit comments