Skip to content

Commit

Permalink
move UID from workspace to account level, closes #26
Browse files Browse the repository at this point in the history
this effectively allows multiple regions within workspace in the same AWS account
  • Loading branch information
Ivan Vlasic committed Oct 18, 2021
1 parent 22b8234 commit 0de67ce
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 48 deletions.
22 changes: 11 additions & 11 deletions api/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
)

type Handler struct {
store *store
aws *aws.AWS
projectName string
stageName string
workspaceKey string
store *store
aws *aws.AWS
projectName string
stageName string
accountKey string
}

func NewHandler() (*Handler, error) {
Expand All @@ -33,11 +33,11 @@ func NewHandler() (*Handler, error) {
return nil, err
}
return &Handler{
store: store,
aws: aws,
projectName: os.Getenv(workspace.EnvProjectName),
stageName: os.Getenv(workspace.EnvStageName),
workspaceKey: os.Getenv(workspace.EnvWorkspaceKey),
store: store,
aws: aws,
projectName: os.Getenv(workspace.EnvProjectName),
stageName: os.Getenv(workspace.EnvStageName),
accountKey: os.Getenv(workspace.EnvAccountKey),
}, nil
}

Expand Down Expand Up @@ -129,7 +129,7 @@ func (h *Handler) clientRequest(client *client, m *proto.Message) error {
function := uriParts[0]
var functionName string
if h.projectName != "" {
functionName = workspace.ProjectResource(h.projectName, h.stageName, function, h.workspaceKey)
functionName = workspace.ProjectResource(h.projectName, h.stageName, function, h.accountKey)
} else {
functionName = workspace.RuntimeResource(function)
}
Expand Down
10 changes: 5 additions & 5 deletions cli/cmd/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ func New(a *Args) (*Cmd, error) {

func (c *Cmd) Create() error {
ws := c.store.Workspace()
c.stackName = ws.SetupStackName()
c.lambdaName = ws.SetupLambdaName()
c.resourceTags = ws.ResourceTags()
v := build.Version()
ac, err := ws.NewAccount(c.accountName, c.aws.AccountID(), c.aws.Region(),
v.FunctionsBucket(c.aws.Region()),
Expand All @@ -66,6 +63,9 @@ func (c *Cmd) Create() error {
}
return log.Wrap(err)
}
c.stackName = ac.SetupStackName()
c.lambdaName = ac.SetupLambdaName()
c.resourceTags = ac.ResourceTags()

if err := c.create(ac); err != nil {
return log.Wrap(err)
Expand Down Expand Up @@ -136,12 +136,12 @@ func (c *Cmd) createSetupStack(acf workspace.AccountFunctions) error {

func (c *Cmd) Destroy() error {
ws := c.store.Workspace()
c.stackName = ws.SetupStackName()
c.lambdaName = ws.SetupLambdaName()
ac := ws.Account(c.accountName)
if ac == nil {
return log.WithUserMessage(nil, fmt.Sprintf("Account %s don't exists", c.accountName))
}
c.stackName = ac.SetupStackName()
c.lambdaName = ac.SetupLambdaName()

if err := c.destroy(ac); err != nil {
return log.Wrap(err)
Expand Down
8 changes: 4 additions & 4 deletions workspace/file_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func TestStageResourceNaming(t *testing.T) {
require.Equal(t, "misteriozo-mister1-ping-fpdtuji", stage.Functions[0].LambdaName())
}

func TestWorkspaceResourceNaming(t *testing.T) {
func TestAccountResourceNaming(t *testing.T) {
fs := testStore(t)
ws := fs.Workspace()
ac := fs.Workspace().Account("dev")

require.Equal(t, "mantil-setup-fpdtuji", ws.SetupStackName())
require.Equal(t, "mantil-setup-fpdtuji", ws.SetupLambdaName())
require.Equal(t, "mantil-setup-fpdtuji", ac.SetupStackName())
require.Equal(t, "mantil-setup-fpdtuji", ac.SetupLambdaName())
}
6 changes: 3 additions & 3 deletions workspace/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func (s *Stage) ApplyEnv() bool {

func (s *Stage) defaultEnv() map[string]string {
env := map[string]string{
EnvProjectName: s.project.Name,
EnvStageName: s.Name,
EnvWorkspaceKey: s.Account().ResourceSuffix(),
EnvProjectName: s.project.Name,
EnvStageName: s.Name,
EnvAccountKey: s.Account().ResourceSuffix(),
}
return env
}
Expand Down
2 changes: 1 addition & 1 deletion workspace/testdata/workspace.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: my-workspace
uid: fpdtuji
accounts:
- name: dev
id: "052548195718"
uid: fpdtuji
region: eu-central-1
bucket: mantil-eu-central-1-052548195718
keys:
Expand Down
45 changes: 21 additions & 24 deletions workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
)

const (
EnvWorkspaceKey = "MANTIL_WORKSPACE_KEY"
EnvAccountKey = "MANTIL_ACCOUNT_KEY"
)

var (
Expand All @@ -35,13 +35,13 @@ var (

type Workspace struct {
Name string `yaml:"name"`
UID string `yaml:"uid"`
Accounts []*Account `yaml:"accounts"`
}

type Account struct {
Name string `yaml:"name"`
ID string `yaml:"id"`
UID string `yaml:"uid"`
Region string `yaml:"region"`
Bucket string `yaml:"bucket"`
Keys AccountKeys `yaml:"keys"`
Expand Down Expand Up @@ -69,7 +69,6 @@ type AccountFunctions struct {
func newWorkspace(name string) *Workspace {
return &Workspace{
Name: name,
UID: uid(),
}
}

Expand Down Expand Up @@ -99,10 +98,12 @@ func (w *Workspace) NewAccount(name, awsAccountID, awsRegion, functionsBucket, f
if err != nil {
return nil, log.Wrap(err, "could not create public/private key pair")
}
bucket := fmt.Sprintf("mantil-%s-%s", awsRegion, w.UID)
uid := uid()
bucket := fmt.Sprintf("mantil-%s-%s", awsRegion, uid)
a := &Account{
Name: name,
ID: awsAccountID,
UID: uid,
Region: awsRegion,
Bucket: bucket,
Keys: AccountKeys{
Expand All @@ -128,22 +129,10 @@ func (w *Workspace) accountExists(name string) bool {
return false
}

const (
workspaceResourcePrefix = "mantil-setup"
)

func (w *Workspace) SetupStackName() string {
return w.SetupLambdaName()
}

func (w *Workspace) SetupLambdaName() string {
return fmt.Sprintf("%s-%s", workspaceResourcePrefix, w.UID)
}

func (w *Workspace) ResourceTags() map[string]string {
func (a *Account) ResourceTags() map[string]string {
return map[string]string{
TagWorkspace: w.Name,
TagKey: w.UID,
TagWorkspace: a.workspace.Name,
TagKey: a.UID,
}
}

Expand All @@ -155,20 +144,28 @@ func (w *Workspace) afterRestore() {
for _, a := range w.Accounts {
a.workspace = w
}
// when restored from previous version
if w.UID == "" && len(w.Accounts) == 0 {
w.UID = uid()
}
}

const (
accountResourcePrefix = "mantil-setup"
)

func (a *Account) ResourceSuffix() string {
return a.workspace.UID
return a.UID
}

func (a *Account) WorkspaceName() string {
return a.workspace.Name
}

func (a *Account) SetupStackName() string {
return a.SetupLambdaName()
}

func (a *Account) SetupLambdaName() string {
return fmt.Sprintf("%s-%s", accountResourcePrefix, a.UID)
}

// idea stolen from: https://github.com/nats-io/nats-server/blob/fd9e9480dad9498ed8109e659fc8ed5c9b2a1b41/server/nkey.go#L41
func uid() string {
var rndData [4]byte
Expand Down
1 change: 1 addition & 0 deletions workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestNewWorkspaceAccount(t *testing.T) {
require.Equal(t, a.Functions.Path, "path")
require.NotEmpty(t, a.Keys.Public)
require.NotEmpty(t, a.Keys.Private)
require.NotEmpty(t, a.UID)
require.Len(t, w.Accounts, 1)
})

Expand Down

0 comments on commit 0de67ce

Please sign in to comment.