fix: use workspace loader

This commit is contained in:
Kevin Franklin Kim 2025-03-17 17:53:59 +01:00
parent 3880d503f1
commit f1fe8c2c72
No known key found for this signature in database
9 changed files with 60 additions and 5 deletions

View File

@ -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:

View File

@ -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":

View File

@ -25,6 +25,7 @@ func NewServer(root *cobra.Command) {
"transformations",
"triggers",
"variables",
"workspaces",
"zones",
},
RunE: func(cmd *cobra.Command, args []string) error {

View File

@ -24,6 +24,7 @@ func NewWeb(root *cobra.Command) {
"transformations",
"triggers",
"variables",
"workspaces",
"zones",
},
RunE: func(cmd *cobra.Command, args []string) error {

View File

@ -18,6 +18,7 @@ func init() {
NewList(root)
NewProvision(root)
NewTypeScript(root)
cobra.OnInitialize(pkgcmd.InitConfig)
}

View File

@ -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"`
}

View File

@ -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 {

View File

@ -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,

View File

@ -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: