|
| 1 | +// Package clusters exposes read-only Console cluster list/get use cases to |
| 2 | +// presentation layers without importing TUI code. |
| 3 | +package clusters |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "strings" |
| 9 | + |
| 10 | + gqlclient "github.com/pluralsh/console/go/client" |
| 11 | + |
| 12 | + "github.com/pluralsh/plural-cli/pkg/bridge" |
| 13 | + "github.com/pluralsh/plural-cli/pkg/console" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + errNoConsole = errors.New("connect a Console profile before browsing Console resources") |
| 18 | + errMissingID = errors.New("cluster id is required") |
| 19 | + errMissingCluster = errors.New("cluster was not found") |
| 20 | +) |
| 21 | + |
| 22 | +// Summary is a credential-free list row for a Console cluster. |
| 23 | +type Summary struct { |
| 24 | + ID string |
| 25 | + Name string |
| 26 | + Handle string |
| 27 | + Version string |
| 28 | + Distro string |
| 29 | +} |
| 30 | + |
| 31 | +// Tag is a credential-free cluster tag. |
| 32 | +type Tag struct { |
| 33 | + Name string |
| 34 | + Value string |
| 35 | +} |
| 36 | + |
| 37 | +// Detail is the credential-free detail payload for a Console cluster. |
| 38 | +type Detail struct { |
| 39 | + Summary |
| 40 | + Self bool |
| 41 | + PingedAt string |
| 42 | + Protect bool |
| 43 | + DeletedAt string |
| 44 | + Project string |
| 45 | + Provider string |
| 46 | + Tags []Tag |
| 47 | + NodePools int |
| 48 | +} |
| 49 | + |
| 50 | +// Loader is the narrow contract consumed by the Clusters screen. |
| 51 | +type Loader interface { |
| 52 | + List(ctx context.Context, query string) ([]Summary, error) |
| 53 | + Get(ctx context.Context, id string) (Detail, error) |
| 54 | +} |
| 55 | + |
| 56 | +// ConsoleResolver supplies the active Console URL and token. |
| 57 | +type ConsoleResolver interface { |
| 58 | + ActiveConsole(ctx context.Context) (url, token string, err error) |
| 59 | +} |
| 60 | + |
| 61 | +// API is the Console surface required by this package. |
| 62 | +type API interface { |
| 63 | + ListClusters() (*gqlclient.ListClusters, error) |
| 64 | + GetCluster(clusterId, clusterName *string) (*gqlclient.ClusterFragment, error) |
| 65 | +} |
| 66 | + |
| 67 | +// ClientFactory builds a Console API for an authenticated endpoint. |
| 68 | +type ClientFactory func(token, url string) (API, error) |
| 69 | + |
| 70 | +// Service implements Loader against Console GraphQL. |
| 71 | +type Service struct { |
| 72 | + resolve ConsoleResolver |
| 73 | + newClient ClientFactory |
| 74 | +} |
| 75 | + |
| 76 | +// NewService wires production Console credentials and client construction. |
| 77 | +func NewService(resolve ConsoleResolver) *Service { |
| 78 | + return &Service{ |
| 79 | + resolve: resolve, |
| 80 | + newClient: func(token, url string) (API, error) { |
| 81 | + return console.NewConsoleClient(token, url) |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (s *Service) client(ctx context.Context) (API, error) { |
| 87 | + if s.resolve == nil { |
| 88 | + return nil, &bridge.Error{Code: bridge.ErrorUnauthenticated, Err: errNoConsole} |
| 89 | + } |
| 90 | + url, token, err := s.resolve.ActiveConsole(ctx) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + factory := s.newClient |
| 95 | + if factory == nil { |
| 96 | + factory = func(token, url string) (API, error) { |
| 97 | + return console.NewConsoleClient(token, url) |
| 98 | + } |
| 99 | + } |
| 100 | + return factory(token, url) |
| 101 | +} |
| 102 | + |
| 103 | +func (s *Service) List(ctx context.Context, query string) ([]Summary, error) { |
| 104 | + if err := ctx.Err(); err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + client, err := s.client(ctx) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + result, err := client.ListClusters() |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + if result == nil || result.Clusters == nil { |
| 116 | + return nil, nil |
| 117 | + } |
| 118 | + items := make([]Summary, 0, len(result.Clusters.Edges)) |
| 119 | + for _, edge := range result.Clusters.Edges { |
| 120 | + if edge == nil || edge.Node == nil { |
| 121 | + continue |
| 122 | + } |
| 123 | + summary := summaryFromFragment(edge.Node) |
| 124 | + if !matchesQuery(summary, query) { |
| 125 | + continue |
| 126 | + } |
| 127 | + items = append(items, summary) |
| 128 | + } |
| 129 | + return items, nil |
| 130 | +} |
| 131 | + |
| 132 | +func (s *Service) Get(ctx context.Context, id string) (Detail, error) { |
| 133 | + if err := ctx.Err(); err != nil { |
| 134 | + return Detail{}, err |
| 135 | + } |
| 136 | + id = strings.TrimSpace(id) |
| 137 | + if id == "" { |
| 138 | + return Detail{}, &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingID} |
| 139 | + } |
| 140 | + client, err := s.client(ctx) |
| 141 | + if err != nil { |
| 142 | + return Detail{}, err |
| 143 | + } |
| 144 | + cluster, err := client.GetCluster(&id, nil) |
| 145 | + if err != nil { |
| 146 | + return Detail{}, err |
| 147 | + } |
| 148 | + if cluster == nil { |
| 149 | + return Detail{}, &bridge.Error{Code: bridge.ErrorUnavailable, Err: errMissingCluster} |
| 150 | + } |
| 151 | + return detailFromFragment(cluster), nil |
| 152 | +} |
| 153 | + |
| 154 | +func summaryFromFragment(node *gqlclient.ClusterFragment) Summary { |
| 155 | + summary := Summary{ID: node.ID, Name: node.Name} |
| 156 | + if node.Handle != nil { |
| 157 | + summary.Handle = *node.Handle |
| 158 | + } |
| 159 | + if node.CurrentVersion != nil { |
| 160 | + summary.Version = *node.CurrentVersion |
| 161 | + } |
| 162 | + if node.Distro != nil { |
| 163 | + summary.Distro = string(*node.Distro) |
| 164 | + } |
| 165 | + return summary |
| 166 | +} |
| 167 | + |
| 168 | +func detailFromFragment(cluster *gqlclient.ClusterFragment) Detail { |
| 169 | + detail := Detail{Summary: summaryFromFragment(cluster)} |
| 170 | + if cluster.Self != nil { |
| 171 | + detail.Self = *cluster.Self |
| 172 | + } |
| 173 | + if cluster.PingedAt != nil { |
| 174 | + detail.PingedAt = *cluster.PingedAt |
| 175 | + } |
| 176 | + if cluster.Protect != nil { |
| 177 | + detail.Protect = *cluster.Protect |
| 178 | + } |
| 179 | + if cluster.DeletedAt != nil { |
| 180 | + detail.DeletedAt = *cluster.DeletedAt |
| 181 | + } |
| 182 | + if cluster.Project != nil { |
| 183 | + detail.Project = cluster.Project.Name |
| 184 | + } |
| 185 | + if cluster.Provider != nil { |
| 186 | + detail.Provider = cluster.Provider.Name |
| 187 | + if cluster.Provider.Cloud != "" { |
| 188 | + detail.Provider = strings.TrimSpace(detail.Provider + " · " + cluster.Provider.Cloud) |
| 189 | + } |
| 190 | + } |
| 191 | + for _, tag := range cluster.Tags { |
| 192 | + if tag == nil { |
| 193 | + continue |
| 194 | + } |
| 195 | + detail.Tags = append(detail.Tags, Tag{Name: tag.Name, Value: tag.Value}) |
| 196 | + } |
| 197 | + detail.NodePools = len(cluster.NodePools) |
| 198 | + return detail |
| 199 | +} |
| 200 | + |
| 201 | +func matchesQuery(summary Summary, query string) bool { |
| 202 | + query = strings.TrimSpace(strings.ToLower(query)) |
| 203 | + if query == "" { |
| 204 | + return true |
| 205 | + } |
| 206 | + haystack := strings.ToLower(strings.Join([]string{summary.Name, summary.Handle, summary.ID, summary.Version, summary.Distro}, " ")) |
| 207 | + return strings.Contains(haystack, query) |
| 208 | +} |
0 commit comments