diff --git a/README.md b/README.md index 8880886..df831b7 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ googleTagManager: containerId: '175355532' # (Optional) The workspace id that should be used by the api workspaceId: '23' + # (Optional) The workspace name that should be used by the api + workspace: 'Default Workspace' # Server container settings serverContainer: # The container tag id @@ -90,6 +92,8 @@ googleTagManager: containerId: '175348980' # (Optional) The workspace id that should be used by the api workspaceId: '10' + # (Optional) The workspace name that should be used by the api + workspace: 'Default Workspace' # Web container variables webContainerVariables: dataLayer: diff --git a/cmd/list/list.go b/cmd/list/list.go index 0e2a0b9..1571f5a 100644 --- a/cmd/list/list.go +++ b/cmd/list/list.go @@ -37,6 +37,8 @@ func list(ctx context.Context, l *slog.Logger, tm *tagmanager.TagManager, resour switch resource { case "environments": return dump(tm.Service().Accounts.Containers.Environments.List(tm.ContainerPath()).Context(ctx).Do()) + case "workspaces": + return dump(tm.Service().Accounts.Containers.Workspaces.List(tm.ContainerPath()).Context(ctx).Do()) case "status": return dump(tm.Service().Accounts.Containers.Workspaces.GetStatus(tm.WorkspacePath()).Context(ctx).Do()) case "clients": diff --git a/cmd/list/server.go b/cmd/list/server.go index 4bfe67f..8247be8 100644 --- a/cmd/list/server.go +++ b/cmd/list/server.go @@ -25,6 +25,7 @@ func NewServer(root *cobra.Command) { "transformations", "triggers", "variables", + "workspaces", "zones", }, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/list/web.go b/cmd/list/web.go index a2bc596..f288840 100644 --- a/cmd/list/web.go +++ b/cmd/list/web.go @@ -24,6 +24,7 @@ func NewWeb(root *cobra.Command) { "transformations", "triggers", "variables", + "workspaces", "zones", }, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/root.go b/cmd/root.go index 01352aa..1f817f4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,6 +18,7 @@ func init() { NewList(root) NewProvision(root) NewTypeScript(root) + cobra.OnInitialize(pkgcmd.InitConfig) } diff --git a/pkg/config/googletagmanagercontainer.go b/pkg/config/googletagmanagercontainer.go index e0e7bee..d1b58ea 100644 --- a/pkg/config/googletagmanagercontainer.go +++ b/pkg/config/googletagmanagercontainer.go @@ -7,4 +7,6 @@ type GoogleTagManagerContainer struct { ContainerID string `json:"containerId" yaml:"containerId"` // (Optional) The workspace id that should be used by the api WorkspaceID string `json:"workspaceId" yaml:"workspaceId"` + // (Optional) The workspace name that should be used by the api + Workspace string `json:"workspace" yaml:"workspace"` } diff --git a/pkg/tagmanager/tagmanager.go b/pkg/tagmanager/tagmanager.go index 6a95518..7df8573 100644 --- a/pkg/tagmanager/tagmanager.go +++ b/pkg/tagmanager/tagmanager.go @@ -29,6 +29,7 @@ type ( folders *AccessedMap[*tagmanager.Folder] variables *AccessedMap[*tagmanager.Variable] environments *AccessedMap[*tagmanager.Environment] + workspaces *AccessedMap[*tagmanager.Workspace] builtInVariables *AccessedMap[*tagmanager.BuiltInVariable] triggers *AccessedMap[*tagmanager.Trigger] tags *AccessedMap[*tagmanager.Tag] @@ -236,11 +237,15 @@ func (t *TagManager) Notes(v any) string { func (t *TagManager) EnsureWorkspaceID(ctx context.Context) error { if t.WorkspaceID() == "" { - environment, err := t.GetEnvironment(ctx, "workspace") - if err != nil { - return err + name := t.container.Workspace + if name == "" { + name = "Default Workspace" } - t.container.WorkspaceID = environment.WorkspaceId + workspace, err := t.GetWorkspace(ctx, name) + if err != nil { + return errors.Wrap(err, "failed to get default workspace") + } + t.container.WorkspaceID = workspace.WorkspaceId } return nil } @@ -350,6 +355,19 @@ func (t *TagManager) GetEnvironment(ctx context.Context, typeName string) (*tagm return elems.Get(typeName), nil } +func (t *TagManager) GetWorkspace(ctx context.Context, name string) (*tagmanager.Workspace, error) { + elems, err := t.LoadWorkspaces(ctx) + if err != nil { + return nil, err + } + + if !elems.Has(name) { + return nil, ErrNotFound + } + + return elems.Get(name), nil +} + func (t *TagManager) GetBuiltInVariable(ctx context.Context, typeName string) (*tagmanager.BuiltInVariable, error) { elems, err := t.LoadBuiltInVariables(ctx) if err != nil { @@ -363,8 +381,26 @@ func (t *TagManager) GetBuiltInVariable(ctx context.Context, typeName string) (* return elems.Get(typeName), nil } +func (t *TagManager) LoadWorkspaces(ctx context.Context) (*AccessedMap[*tagmanager.Workspace], error) { + if t.workspaces == nil { + t.l.Info("└ ⬇︎ Loading list", "type", "Workspaces") + r, err := t.Service().Accounts.Containers.Workspaces.List(t.ContainerPath()).Context(ctx).Do() + if err != nil { + return nil, err + } + + res := map[string]*tagmanager.Workspace{} + for _, value := range r.Workspace { + res[value.Name] = value + } + t.workspaces = NewAccessedMap(res) + } + + return t.workspaces, nil +} + func (t *TagManager) LoadEnvironments(ctx context.Context) (*AccessedMap[*tagmanager.Environment], error) { - if t.builtInVariables == nil { + if t.environments == nil { t.l.Info("└ ⬇︎ Loading list", "type", "Environments") r, err := t.Service().Accounts.Containers.Environments.List(t.ContainerPath()).Context(ctx).Do() if err != nil { diff --git a/sesamy.schema.json b/sesamy.schema.json index efa7250..0cbd89c 100644 --- a/sesamy.schema.json +++ b/sesamy.schema.json @@ -534,6 +534,10 @@ "workspaceId": { "type": "string", "description": "(Optional) The workspace id that should be used by the api" + }, + "workspace": { + "type": "string", + "description": "(Optional) The workspace name that should be used by the api" } }, "additionalProperties": false, diff --git a/sesamy.yaml b/sesamy.yaml index 1f961e7..8cf7d30 100644 --- a/sesamy.yaml +++ b/sesamy.yaml @@ -28,6 +28,8 @@ googleTagManager: containerId: '175355532' # (Optional) The workspace id that should be used by the api workspaceId: '23' + # (Optional) The workspace name that should be used by the api + workspace: 'Default Workspace' # Server container settings serverContainer: # The container tag id @@ -36,6 +38,8 @@ googleTagManager: containerId: '175348980' # (Optional) The workspace id that should be used by the api workspaceId: '10' + # (Optional) The workspace name that should be used by the api + workspace: 'Default Workspace' # Web container variables webContainerVariables: dataLayer: