chore(coderd): move provisionerd tags to provisionersdk (#11100)

This commit is contained in:
Cian Johnston
2023-12-08 12:10:25 +00:00
committed by GitHub
parent 4ca4736411
commit 2b19a2369f
10 changed files with 45 additions and 40 deletions
+1 -1
View File
@@ -1160,7 +1160,7 @@ func (api *API) CreateInMemoryProvisionerDaemon(ctx context.Context, name string
}()
tags := provisionerdserver.Tags{
provisionerdserver.TagScope: provisionerdserver.ScopeOrganization,
provisionersdk.TagScope: provisionersdk.ScopeOrganization,
}
mux := drpcmux.New()
@@ -1,33 +0,0 @@
package provisionerdserver
import "github.com/google/uuid"
const (
TagScope = "scope"
TagOwner = "owner"
ScopeUser = "user"
ScopeOrganization = "organization"
)
// MutateTags adjusts the "owner" tag dependent on the "scope".
// If the scope is "user", the "owner" is changed to the user ID.
// This is for user-scoped provisioner daemons, where users should
// own their own operations.
func MutateTags(userID uuid.UUID, tags map[string]string) map[string]string {
if tags == nil {
tags = map[string]string{}
}
_, ok := tags[TagScope]
if !ok {
tags[TagScope] = ScopeOrganization
}
switch tags[TagScope] {
case ScopeUser:
tags[TagOwner] = userID.String()
case ScopeOrganization:
default:
tags[TagScope] = ScopeOrganization
}
return tags
}
@@ -1,80 +0,0 @@
package provisionerdserver_test
import (
"encoding/json"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/provisionerdserver"
)
func TestMutateTags(t *testing.T) {
t.Parallel()
testUserID := uuid.New()
for _, tt := range []struct {
name string
userID uuid.UUID
tags map[string]string
want map[string]string
}{
{
name: "nil tags",
userID: uuid.Nil,
tags: nil,
want: map[string]string{
provisionerdserver.TagScope: provisionerdserver.ScopeOrganization,
},
},
{
name: "empty tags",
userID: uuid.Nil,
tags: map[string]string{},
want: map[string]string{
provisionerdserver.TagScope: provisionerdserver.ScopeOrganization,
},
},
{
name: "user scope",
tags: map[string]string{provisionerdserver.TagScope: provisionerdserver.ScopeUser},
userID: testUserID,
want: map[string]string{
provisionerdserver.TagScope: provisionerdserver.ScopeUser,
provisionerdserver.TagOwner: testUserID.String(),
},
},
{
name: "organization scope",
tags: map[string]string{provisionerdserver.TagScope: provisionerdserver.ScopeOrganization},
userID: testUserID,
want: map[string]string{
provisionerdserver.TagScope: provisionerdserver.ScopeOrganization,
},
},
{
name: "invalid scope",
tags: map[string]string{provisionerdserver.TagScope: "360noscope"},
userID: testUserID,
want: map[string]string{
provisionerdserver.TagScope: provisionerdserver.ScopeOrganization,
},
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// make a copy of the map because the function under test
// mutates the map
bytes, err := json.Marshal(tt.tags)
require.NoError(t, err)
var tags map[string]string
err = json.Unmarshal(bytes, &tags)
require.NoError(t, err)
got := provisionerdserver.MutateTags(tt.userID, tags)
require.Equal(t, tt.want, got)
})
}
}
+2 -1
View File
@@ -31,6 +31,7 @@ import (
"github.com/coder/coder/v2/coderd/tracing"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/examples"
"github.com/coder/coder/v2/provisionersdk"
sdkproto "github.com/coder/coder/v2/provisionersdk/proto"
)
@@ -1331,7 +1332,7 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
}
// Ensures the "owner" is properly applied.
tags := provisionerdserver.MutateTags(apiKey.UserID, req.ProvisionerTags)
tags := provisionersdk.MutateTags(apiKey.UserID, req.ProvisionerTags)
if req.ExampleID != "" && req.FileID != uuid.Nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
+2 -2
View File
@@ -17,11 +17,11 @@ import (
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/externalauth"
"github.com/coder/coder/v2/coderd/provisionerdserver"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/examples"
"github.com/coder/coder/v2/provisioner/echo"
"github.com/coder/coder/v2/provisionersdk"
"github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/coder/v2/testutil"
)
@@ -154,7 +154,7 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
})
require.NoError(t, err)
require.Equal(t, "bananas", version.Name)
require.Equal(t, provisionerdserver.ScopeOrganization, version.Job.Tags[provisionerdserver.TagScope])
require.Equal(t, provisionersdk.ScopeOrganization, version.Job.Tags[provisionersdk.TagScope])
require.Len(t, auditor.AuditLogs(), 2)
assert.Equal(t, database.AuditActionCreate, auditor.AuditLogs()[1].Action)
+3 -1
View File
@@ -10,6 +10,8 @@ import (
"net/http"
"time"
"github.com/coder/coder/v2/provisionersdk"
"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
"golang.org/x/xerrors"
@@ -294,7 +296,7 @@ func (b *Builder) buildTx(authFunc func(action rbac.Action, object rbac.Objecter
if err != nil {
return nil, nil, BuildError{http.StatusInternalServerError, "marshal metadata", err}
}
tags := provisionerdserver.MutateTags(b.workspace.OwnerID, templateVersionJob.Tags)
tags := provisionersdk.MutateTags(b.workspace.OwnerID, templateVersionJob.Tags)
now := dbtime.Now()
provisionerJob, err := b.store.InsertProvisionerJob(b.ctx, database.InsertProvisionerJobParams{
+6 -4
View File
@@ -8,6 +8,8 @@ import (
"testing"
"time"
"github.com/coder/coder/v2/provisionersdk"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
@@ -690,8 +692,8 @@ func withActiveVersion(params []database.TemplateVersionParameter) func(mTx *dbm
Type: database.ProvisionerJobTypeTemplateVersionImport,
Input: nil,
Tags: database.StringMap{
"version": "active",
provisionerdserver.TagScope: provisionerdserver.ScopeUser,
"version": "active",
provisionersdk.TagScope: provisionersdk.ScopeUser,
},
FileID: activeFileID,
StartedAt: sql.NullTime{Time: dbtime.Now(), Valid: true},
@@ -730,8 +732,8 @@ func withInactiveVersion(params []database.TemplateVersionParameter) func(mTx *d
Type: database.ProvisionerJobTypeTemplateVersionImport,
Input: nil,
Tags: database.StringMap{
"version": "inactive",
provisionerdserver.TagScope: provisionerdserver.ScopeUser,
"version": "inactive",
provisionersdk.TagScope: provisionersdk.ScopeUser,
},
FileID: inactiveFileID,
StartedAt: sql.NullTime{Time: dbtime.Now(), Valid: true},