Skip to content

Commit

Permalink
Ignore invalid workspaces (with debug log) + load configs and instanc…
Browse files Browse the repository at this point in the history
…es in parallel + simplify output
  • Loading branch information
jprochazk committed Sep 28, 2022
1 parent b6ebb52 commit 7b7414d
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions pkg/lib/operation/project/remote/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workspace
import (
"context"
"fmt"
"sync"

"go.opentelemetry.io/otel/trace"

Expand All @@ -11,6 +12,7 @@ import (
"github.com/keboola/go-client/pkg/storageapi"
"github.com/keboola/keboola-as-code/internal/pkg/log"
"github.com/keboola/keboola-as-code/internal/pkg/telemetry"
"github.com/keboola/keboola-as-code/internal/pkg/utils"
)

type listDeps interface {
Expand All @@ -24,41 +26,65 @@ func List(ctx context.Context, d listDeps) (err error) {
ctx, span := d.Tracer().Start(ctx, "kac.lib.operation.project.remote.workspace.list")
defer telemetry.EndSpan(span, &err)

w := d.Logger().InfoWriter()
logger := d.Logger()

branch, err := storageapi.GetDefaultBranchRequest().Send(ctx, d.StorageApiClient())
if err != nil {
return fmt.Errorf("cannot find default branch: %w", err)
}

sandboxConfigs, err := sandboxesapi.ListConfigRequest(branch.ID).Send(ctx, d.StorageApiClient())
if err != nil {
return fmt.Errorf("cannot list workspace configs: %w", err)
}
logger.Info("Loading workspaces, please wait.")

sandboxes, err := sandboxesapi.ListRequest().Send(ctx, d.SandboxesApiClient())
if err != nil {
return fmt.Errorf("cannot list workspaces: %w", err)
}
sandboxesMap := make(map[string]*sandboxesapi.Sandbox, 0)
for _, sandbox := range *sandboxes {
sandboxesMap[sandbox.ID.String()] = sandbox
}
// Load configs and instances in parallel
var configs []*storageapi.Config
var instances map[string]*sandboxesapi.Sandbox
errors := utils.NewMultiError()
wg := &sync.WaitGroup{}

wg.Add(1)
go func() {
defer wg.Done()
data, err := sandboxesapi.ListConfigRequest(branch.ID).Send(ctx, d.StorageApiClient())
if err != nil {
errors.Append(fmt.Errorf("cannot list workspace configs: %w", err))
return
}
configs = *data
}()

wg.Add(1)
go func() {
defer wg.Done()
data, err := sandboxesapi.ListRequest().Send(ctx, d.SandboxesApiClient())
if err != nil {
errors.Append(fmt.Errorf("cannot list workspaces: %w", err))
return
}
m := make(map[string]*sandboxesapi.Sandbox, 0)
for _, sandbox := range *data {
m[sandbox.ID.String()] = sandbox
}
instances = m
}()

wg.Wait()

for _, sandboxConfig := range *sandboxConfigs {
sandboxId, err := sandboxesapi.GetSandboxID(sandboxConfig)
logger.Info("Found workspaces:")
for _, config := range configs {
instanceId, err := sandboxesapi.GetSandboxID(config)
if err != nil {
return fmt.Errorf("invalid workspace config: %w", err)
logger.Debugf(" invalid workspace config (%s): %w", config.ID, err)
continue
}
sandboxInstance := sandboxesMap[sandboxId.String()]
instance := instances[instanceId.String()]

w.Writef("ID: %s", sandboxInstance.ID)
w.Writef("Name: %s", sandboxConfig.Name)
w.Writef("Type: %s", sandboxInstance.Type)
if sandboxesapi.SupportsSizes(sandboxInstance.Type) {
w.Writef("Size: %s", sandboxInstance.Size)
var info string
if !sandboxesapi.SupportsSizes(instance.Type) {
info = fmt.Sprintf(" ID: %s, Type: %s, Name: %s", instance.ID, instance.Type, config.Name)
} else {
info = fmt.Sprintf(" ID: %s, Type: %s, Size: %s, Name: %s", instance.ID, instance.Type, instance.Size, config.Name)
}
w.Writef("")
logger.Info(info)
}

return nil
Expand Down

0 comments on commit 7b7414d

Please sign in to comment.