|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | + |
| 15 | + "github.com/noderings/cli/internal/api" |
| 16 | + generated "github.com/noderings/cli/internal/api/generated" |
| 17 | +) |
| 18 | + |
| 19 | +type providerOrg struct { |
| 20 | + id string |
| 21 | + name string |
| 22 | +} |
| 23 | + |
| 24 | +func organizationIDFromCmd(cmd *cobra.Command) string { |
| 25 | + if cmd != nil { |
| 26 | + if f := cmd.Flag("organization-id"); f != nil { |
| 27 | + if v := strings.TrimSpace(f.Value.String()); v != "" { |
| 28 | + return v |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + for _, key := range []string{"NR_ORGANIZATION_ID", "NODERINGS_ORGANIZATION_ID"} { |
| 33 | + if v := strings.TrimSpace(os.Getenv(key)); v != "" { |
| 34 | + return v |
| 35 | + } |
| 36 | + } |
| 37 | + return "" |
| 38 | +} |
| 39 | + |
| 40 | +// ensureProviderOrganization sets X-Organization-ID so provider APIs (ListAgents, |
| 41 | +// CreateAgent) run against the provider tenant instead of the OAuth home org. |
| 42 | +func ensureProviderOrganization(ctx context.Context, client *api.Client, yes bool) error { |
| 43 | + if client == nil { |
| 44 | + return nil |
| 45 | + } |
| 46 | + if id := strings.TrimSpace(client.GetOrganizationID()); id != "" { |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + orgs, err := listProviderOrganizations(ctx, client) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("list organizations: %w\nPass --organization-id from the UI install command", err) |
| 53 | + } |
| 54 | + id, err := chooseProviderOrganization(orgs, yes) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + client.SetOrganizationID(id) |
| 59 | + fmt.Fprintf(os.Stderr, "Using provider organization %s (%s)\n", orgNameByID(orgs, id), id) |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func listProviderOrganizations(ctx context.Context, client *api.Client) ([]providerOrg, error) { |
| 64 | + pageSize := int32(-1) |
| 65 | + resp, err := client.DoWithAutoRefresh(ctx, 3, func() (*http.Response, error) { |
| 66 | + return client.GetGeneratedClient().OrganizationServiceListOrganizations(ctx, |
| 67 | + &generated.OrganizationServiceListOrganizationsParams{PageSize: &pageSize}) |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + defer func() { _ = resp.Body.Close() }() |
| 73 | + if resp.StatusCode >= 400 { |
| 74 | + return nil, api.ParseError(resp) |
| 75 | + } |
| 76 | + body, err := io.ReadAll(resp.Body) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + return providerOrgsFromListJSON(body), nil |
| 81 | +} |
| 82 | + |
| 83 | +func providerOrgsFromListJSON(body []byte) []providerOrg { |
| 84 | + var list generated.V1ListOrganizationsResponse |
| 85 | + if err := json.Unmarshal(body, &list); err != nil || list.Organizations == nil { |
| 86 | + return nil |
| 87 | + } |
| 88 | + out := make([]providerOrg, 0, len(*list.Organizations)) |
| 89 | + for _, org := range *list.Organizations { |
| 90 | + if org.Type == nil || *org.Type != generated.ORGANIZATIONTYPEPROVIDER { |
| 91 | + continue |
| 92 | + } |
| 93 | + id := "" |
| 94 | + if org.Id != nil { |
| 95 | + id = strings.TrimSpace(*org.Id) |
| 96 | + } |
| 97 | + if id == "" { |
| 98 | + continue |
| 99 | + } |
| 100 | + name := id |
| 101 | + if org.Name != nil && strings.TrimSpace(*org.Name) != "" { |
| 102 | + name = strings.TrimSpace(*org.Name) |
| 103 | + } |
| 104 | + out = append(out, providerOrg{id: id, name: name}) |
| 105 | + } |
| 106 | + return out |
| 107 | +} |
| 108 | + |
| 109 | +func chooseProviderOrganization(orgs []providerOrg, yes bool) (string, error) { |
| 110 | + if len(orgs) == 0 { |
| 111 | + return "", fmt.Errorf("no provider organization found for this account. Pass --organization-id from the UI install command") |
| 112 | + } |
| 113 | + if len(orgs) == 1 && (yes || !isStdinTerminal()) { |
| 114 | + return orgs[0].id, nil |
| 115 | + } |
| 116 | + if !isStdinTerminal() { |
| 117 | + return "", fmt.Errorf("pass --organization-id <uuid> (provider organization). The UI install command includes this flag") |
| 118 | + } |
| 119 | + |
| 120 | + fmt.Fprintln(os.Stderr, "Provider organizations:") |
| 121 | + for i, org := range orgs { |
| 122 | + fmt.Fprintf(os.Stderr, " %d) %s (%s)\n", i+1, org.name, org.id) |
| 123 | + } |
| 124 | + raw, err := promptString("Select a provider organization", "1") |
| 125 | + if err != nil { |
| 126 | + return "", err |
| 127 | + } |
| 128 | + n, err := strconv.Atoi(strings.TrimSpace(raw)) |
| 129 | + if err != nil || n < 1 || n > len(orgs) { |
| 130 | + return "", fmt.Errorf("invalid selection %q (enter 1-%d)", raw, len(orgs)) |
| 131 | + } |
| 132 | + return orgs[n-1].id, nil |
| 133 | +} |
| 134 | + |
| 135 | +func orgNameByID(orgs []providerOrg, id string) string { |
| 136 | + for _, org := range orgs { |
| 137 | + if org.id == id { |
| 138 | + return org.name |
| 139 | + } |
| 140 | + } |
| 141 | + return id |
| 142 | +} |
0 commit comments