|
| 1 | +package services |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + gqlclient "github.com/pluralsh/console/go/client" |
| 12 | + "github.com/samber/lo" |
| 13 | + |
| 14 | + "github.com/pluralsh/plural-cli/pkg/bridge" |
| 15 | + "github.com/pluralsh/plural-cli/pkg/utils" |
| 16 | +) |
| 17 | + |
| 18 | +// CreateInput is the credential-free create payload. |
| 19 | +type CreateInput struct { |
| 20 | + ClusterID string |
| 21 | + Name string |
| 22 | + Namespace string |
| 23 | + RepoID string |
| 24 | + GitRef string |
| 25 | + GitFolder string |
| 26 | + Kustomize string |
| 27 | + Version string |
| 28 | + DryRun bool |
| 29 | +} |
| 30 | + |
| 31 | +// UpdateInput is the credential-free update payload. |
| 32 | +type UpdateInput struct { |
| 33 | + ID string |
| 34 | + GitRef string |
| 35 | + GitFolder string |
| 36 | + Kustomize string |
| 37 | + Version string |
| 38 | + DryRun *bool |
| 39 | +} |
| 40 | + |
| 41 | +// CloneInput is the credential-free clone payload. |
| 42 | +type CloneInput struct { |
| 43 | + SourceID string |
| 44 | + DestClusterID string |
| 45 | + Name string |
| 46 | + Namespace string |
| 47 | +} |
| 48 | + |
| 49 | +// Loader is the narrow contract consumed by the Services screen. |
| 50 | +type Loader interface { |
| 51 | + ListClusters(ctx context.Context, query string) ([]Cluster, error) |
| 52 | + List(ctx context.Context, clusterID string, after *string, query string) (Page, error) |
| 53 | + Get(ctx context.Context, id string) (Detail, error) |
| 54 | + Kick(ctx context.Context, id string) (Detail, error) |
| 55 | + Delete(ctx context.Context, id string) error |
| 56 | + Create(ctx context.Context, input CreateInput) (Detail, error) |
| 57 | + Update(ctx context.Context, input UpdateInput) (Detail, error) |
| 58 | + Clone(ctx context.Context, input CloneInput) (Detail, error) |
| 59 | + DownloadTarball(ctx context.Context, id, dir string) (string, error) |
| 60 | +} |
| 61 | + |
| 62 | +// API is the Console surface required by this package. |
| 63 | +type API interface { |
| 64 | + ListClusters() (*gqlclient.ListClusters, error) |
| 65 | + ListClusterServices(clusterId, handle *string) ([]*gqlclient.ServiceDeploymentEdgeFragment, error) |
| 66 | + GetClusterService(serviceId, serviceName, clusterName *string) (*gqlclient.ServiceDeploymentExtended, error) |
| 67 | + KickClusterService(serviceId, serviceName, clusterName *string) (*gqlclient.ServiceDeploymentExtended, error) |
| 68 | + DeleteClusterService(serviceId string) (*gqlclient.DeleteServiceDeployment, error) |
| 69 | + CreateClusterService(clusterId, clusterName *string, attr gqlclient.ServiceDeploymentAttributes) (*gqlclient.ServiceDeploymentExtended, error) |
| 70 | + UpdateClusterService(serviceId, serviceName, clusterName *string, attributes gqlclient.ServiceUpdateAttributes) (*gqlclient.ServiceDeploymentExtended, error) |
| 71 | + CloneService(clusterId string, serviceId, serviceName, clusterName *string, attributes gqlclient.ServiceCloneAttributes) (*gqlclient.ServiceDeploymentFragment, error) |
| 72 | + GetDeployToken(clusterId, clusterName *string) (string, error) |
| 73 | +} |
| 74 | + |
| 75 | +func (s *Service) Kick(ctx context.Context, id string) (Detail, error) { |
| 76 | + if err := ctx.Err(); err != nil { |
| 77 | + return Detail{}, err |
| 78 | + } |
| 79 | + id = strings.TrimSpace(id) |
| 80 | + if id == "" { |
| 81 | + return Detail{}, &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingID} |
| 82 | + } |
| 83 | + client, err := s.client(ctx) |
| 84 | + if err != nil { |
| 85 | + return Detail{}, err |
| 86 | + } |
| 87 | + service, err := client.KickClusterService(&id, nil, nil) |
| 88 | + if err != nil { |
| 89 | + return Detail{}, err |
| 90 | + } |
| 91 | + if service == nil { |
| 92 | + return Detail{}, &bridge.Error{Code: bridge.ErrorUnavailable, Err: errMissingService} |
| 93 | + } |
| 94 | + return detailFromExtended(service), nil |
| 95 | +} |
| 96 | + |
| 97 | +func (s *Service) Delete(ctx context.Context, id string) error { |
| 98 | + if err := ctx.Err(); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + id = strings.TrimSpace(id) |
| 102 | + if id == "" { |
| 103 | + return &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingID} |
| 104 | + } |
| 105 | + client, err := s.client(ctx) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + _, err = client.DeleteClusterService(id) |
| 110 | + return err |
| 111 | +} |
| 112 | + |
| 113 | +func (s *Service) Create(ctx context.Context, input CreateInput) (Detail, error) { |
| 114 | + if err := ctx.Err(); err != nil { |
| 115 | + return Detail{}, err |
| 116 | + } |
| 117 | + input.ClusterID = strings.TrimSpace(input.ClusterID) |
| 118 | + input.Name = strings.TrimSpace(input.Name) |
| 119 | + input.RepoID = strings.TrimSpace(input.RepoID) |
| 120 | + input.GitRef = strings.TrimSpace(input.GitRef) |
| 121 | + input.GitFolder = strings.TrimSpace(input.GitFolder) |
| 122 | + if input.ClusterID == "" { |
| 123 | + return Detail{}, &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingCluster} |
| 124 | + } |
| 125 | + if input.Name == "" || input.RepoID == "" || input.GitRef == "" || input.GitFolder == "" { |
| 126 | + return Detail{}, &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingCreateFields} |
| 127 | + } |
| 128 | + if input.Namespace == "" { |
| 129 | + input.Namespace = "default" |
| 130 | + } |
| 131 | + if input.Version == "" { |
| 132 | + input.Version = "0.0.1" |
| 133 | + } |
| 134 | + client, err := s.client(ctx) |
| 135 | + if err != nil { |
| 136 | + return Detail{}, err |
| 137 | + } |
| 138 | + attrs := gqlclient.ServiceDeploymentAttributes{ |
| 139 | + Name: input.Name, |
| 140 | + Namespace: input.Namespace, |
| 141 | + Version: lo.ToPtr(input.Version), |
| 142 | + RepositoryID: lo.ToPtr(input.RepoID), |
| 143 | + Git: &gqlclient.GitRefAttributes{Ref: input.GitRef, Folder: input.GitFolder}, |
| 144 | + DryRun: lo.ToPtr(input.DryRun), |
| 145 | + } |
| 146 | + if input.Kustomize != "" { |
| 147 | + attrs.Kustomize = &gqlclient.KustomizeAttributes{Path: input.Kustomize} |
| 148 | + } |
| 149 | + service, err := client.CreateClusterService(&input.ClusterID, nil, attrs) |
| 150 | + if err != nil { |
| 151 | + return Detail{}, err |
| 152 | + } |
| 153 | + if service == nil { |
| 154 | + return Detail{}, &bridge.Error{Code: bridge.ErrorUnavailable, Err: errMissingService} |
| 155 | + } |
| 156 | + return detailFromExtended(service), nil |
| 157 | +} |
| 158 | + |
| 159 | +func (s *Service) Update(ctx context.Context, input UpdateInput) (Detail, error) { |
| 160 | + if err := ctx.Err(); err != nil { |
| 161 | + return Detail{}, err |
| 162 | + } |
| 163 | + input.ID = strings.TrimSpace(input.ID) |
| 164 | + if input.ID == "" { |
| 165 | + return Detail{}, &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingID} |
| 166 | + } |
| 167 | + client, err := s.client(ctx) |
| 168 | + if err != nil { |
| 169 | + return Detail{}, err |
| 170 | + } |
| 171 | + attrs := gqlclient.ServiceUpdateAttributes{} |
| 172 | + if input.GitRef != "" || input.GitFolder != "" { |
| 173 | + attrs.Git = &gqlclient.GitRefAttributes{Ref: input.GitRef, Folder: input.GitFolder} |
| 174 | + } |
| 175 | + if input.Version != "" { |
| 176 | + attrs.Version = lo.ToPtr(input.Version) |
| 177 | + } |
| 178 | + if input.DryRun != nil { |
| 179 | + attrs.DryRun = input.DryRun |
| 180 | + } |
| 181 | + if input.Kustomize != "" { |
| 182 | + attrs.Kustomize = &gqlclient.KustomizeAttributes{Path: input.Kustomize} |
| 183 | + } |
| 184 | + service, err := client.UpdateClusterService(&input.ID, nil, nil, attrs) |
| 185 | + if err != nil { |
| 186 | + return Detail{}, err |
| 187 | + } |
| 188 | + if service == nil { |
| 189 | + return Detail{}, &bridge.Error{Code: bridge.ErrorUnavailable, Err: errMissingService} |
| 190 | + } |
| 191 | + return detailFromExtended(service), nil |
| 192 | +} |
| 193 | + |
| 194 | +func (s *Service) Clone(ctx context.Context, input CloneInput) (Detail, error) { |
| 195 | + if err := ctx.Err(); err != nil { |
| 196 | + return Detail{}, err |
| 197 | + } |
| 198 | + input.SourceID = strings.TrimSpace(input.SourceID) |
| 199 | + input.DestClusterID = strings.TrimSpace(input.DestClusterID) |
| 200 | + input.Name = strings.TrimSpace(input.Name) |
| 201 | + if input.SourceID == "" { |
| 202 | + return Detail{}, &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingID} |
| 203 | + } |
| 204 | + if input.DestClusterID == "" { |
| 205 | + return Detail{}, &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingCluster} |
| 206 | + } |
| 207 | + if input.Name == "" { |
| 208 | + return Detail{}, &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingCreateFields} |
| 209 | + } |
| 210 | + if input.Namespace == "" { |
| 211 | + input.Namespace = "default" |
| 212 | + } |
| 213 | + client, err := s.client(ctx) |
| 214 | + if err != nil { |
| 215 | + return Detail{}, err |
| 216 | + } |
| 217 | + attrs := gqlclient.ServiceCloneAttributes{Name: input.Name, Namespace: lo.ToPtr(input.Namespace)} |
| 218 | + frag, err := client.CloneService(input.DestClusterID, &input.SourceID, nil, nil, attrs) |
| 219 | + if err != nil { |
| 220 | + return Detail{}, err |
| 221 | + } |
| 222 | + if frag == nil { |
| 223 | + return Detail{}, &bridge.Error{Code: bridge.ErrorUnavailable, Err: errMissingService} |
| 224 | + } |
| 225 | + return s.Get(ctx, frag.ID) |
| 226 | +} |
| 227 | + |
| 228 | +func (s *Service) DownloadTarball(ctx context.Context, id, dir string) (string, error) { |
| 229 | + if err := ctx.Err(); err != nil { |
| 230 | + return "", err |
| 231 | + } |
| 232 | + id = strings.TrimSpace(id) |
| 233 | + if id == "" { |
| 234 | + return "", &bridge.Error{Code: bridge.ErrorInvalid, Err: errMissingID} |
| 235 | + } |
| 236 | + client, err := s.client(ctx) |
| 237 | + if err != nil { |
| 238 | + return "", err |
| 239 | + } |
| 240 | + service, err := client.GetClusterService(&id, nil, nil) |
| 241 | + if err != nil { |
| 242 | + return "", err |
| 243 | + } |
| 244 | + if service == nil { |
| 245 | + return "", &bridge.Error{Code: bridge.ErrorUnavailable, Err: errMissingService} |
| 246 | + } |
| 247 | + if service.Tarball == nil || strings.TrimSpace(*service.Tarball) == "" { |
| 248 | + return "", &bridge.Error{Code: bridge.ErrorUnavailable, Err: errMissingTarball} |
| 249 | + } |
| 250 | + dir = strings.TrimSpace(dir) |
| 251 | + if dir == "" { |
| 252 | + dir = filepath.Join(".", service.Name+"-tarball") |
| 253 | + } |
| 254 | + if err := utils.EnsureEmptyDir(dir); err != nil { |
| 255 | + return "", err |
| 256 | + } |
| 257 | + if service.Cluster == nil { |
| 258 | + return "", &bridge.Error{Code: bridge.ErrorUnavailable, Err: errMissingCluster} |
| 259 | + } |
| 260 | + token, err := client.GetDeployToken(&service.Cluster.ID, nil) |
| 261 | + if err != nil { |
| 262 | + return "", err |
| 263 | + } |
| 264 | + resp, err := utils.ReadRemoteFileWithRetries(*service.Tarball, token, 3) |
| 265 | + if err != nil { |
| 266 | + return "", err |
| 267 | + } |
| 268 | + defer func(c io.Closer) { _ = c.Close() }(resp) |
| 269 | + if err := utils.Untar(dir, resp); err != nil { |
| 270 | + return "", err |
| 271 | + } |
| 272 | + abs, err := filepath.Abs(dir) |
| 273 | + if err != nil { |
| 274 | + return dir, nil |
| 275 | + } |
| 276 | + if _, err := os.Stat(abs); err != nil { |
| 277 | + return "", fmt.Errorf("tarball directory missing after unpack: %w", err) |
| 278 | + } |
| 279 | + return abs, nil |
| 280 | +} |
0 commit comments