Skip to content

Commit

Permalink
rename account to node, closes #68
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vlasic committed Nov 4, 2021
1 parent ca8e802 commit a1ac830
Show file tree
Hide file tree
Showing 25 changed files with 251 additions and 252 deletions.
25 changes: 12 additions & 13 deletions cli/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func newAwsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "aws",
Short: "AWS account subcommand",
Short: "AWS node subcommand",
Args: cobra.NoArgs,
}
cmd.AddCommand(newAwsInstallCommand())
Expand All @@ -29,12 +29,12 @@ func newAwsInstallCommand() *cobra.Command {
==> Run mantil new to start a new project
==> Further documentation: https://docs.mantil.io`
argumentsUsage := fmt.Sprintf(`
[account-name] Mantil account name reference.
If not provided default name %s will be used for the first account.`, domain.DefaultAccountName)
[node-name] Mantil node name reference.
If not provided default name %s will be used for the first node.`, domain.DefaultNodeName)

a := &controller.SetupArgs{}
cmd := &cobra.Command{
Use: "install [account-name] [flags]",
Use: "install [node-name] [flags]",
Short: "Install Mantil into AWS account",
Long: `Install Mantil into AWS account
Expand Down Expand Up @@ -64,14 +64,13 @@ and what account will be managed by command.`,
}
setUsageTemplate(cmd, argumentsUsage)
bindAwsInstallFlags(cmd, a)
//cmd.Flags().BoolVar(&a.Override, "override", false, "force override access tokens on already installed account")
return cmd
}

func newAwsUninstallCommand() *cobra.Command {
a := &controller.SetupArgs{}
cmd := &cobra.Command{
Use: "uninstall [account-name] [flags]",
Use: "uninstall [node-name] [flags]",
Short: "Uninstall Mantil from AWS account",
Long: `Uninstall Mantil from AWS account
Expand All @@ -96,8 +95,8 @@ and what account will be managed by command.`,
},
}
cmd.SetUsageTemplate(usageTemplate(fmt.Sprintf(`
[account-name] Mantil account name reference.
If not provided default name %s will be used for the first account.`, domain.DefaultAccountName)))
[node-name] Mantil node name reference.
If not provided default name %s will be used for the first node.`, domain.DefaultNodeName)))
bindAwsInstallFlags(cmd, a)
return cmd
}
Expand Down Expand Up @@ -144,7 +143,7 @@ func showAwsDryRunInfo(a *controller.SetupArgs) {
aws-region: %s`, a.AccessKeyID, a.SecretAccessKey, a.Region)
}
ui.Info("To manage AWS account ID: %s in region %s", a.AccountID, a.Region)
ui.Info("Account name in Mantil is %s", a.AccountName)
ui.Info("Node name in Mantil is %s", a.NodeName)
}

func newEnvCommand() *cobra.Command {
Expand Down Expand Up @@ -322,7 +321,7 @@ func newStageCommand() *cobra.Command {
A stage represents a named deployment of the project. Each stage creates a set of resources
which can be managed and configured separately.
Stages can be deployed to any account in the workspace.`,
Stages can be deployed to any node in the workspace.`,
}
cmd.AddCommand(newStageNewCommand())
cmd.AddCommand(newStageDestroyCommand())
Expand All @@ -338,8 +337,8 @@ func newStageNewCommand() *cobra.Command {
This command will create a new stage with the given name. If the name is left empty it will default to "%s".
If only one account is set up in the workspace, the stage will be deployed to that account by default.
Otherwise, you will be asked to pick an account. The account can also be specified via the --account flag.`, domain.DefaultStageName),
If only one node is set up in the workspace, the stage will be deployed to that node by default.
Otherwise, you will be asked to pick a node. The node can also be specified via the --node flag.`, domain.DefaultStageName),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
Expand All @@ -355,7 +354,7 @@ Otherwise, you will be asked to pick an account. The account can also be specifi
return nil
},
}
cmd.Flags().StringVarP(&a.Account, "account", "a", "", "account in which the stage will be created")
cmd.Flags().StringVarP(&a.Node, "node", "n", "", "node in which the stage will be created")
return cmd
}

Expand Down
12 changes: 6 additions & 6 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ func showError(cmd *cobra.Command, err error) {
return
}

var aee *domain.AccountExistsError
if errors.As(err, &aee) {
ui.Errorf("account '%s' already exists", aee.Name)
if aee.Name == domain.DefaultAccountName {
var nee *domain.NodeExistsError
if errors.As(err, &nee) {
ui.Errorf("node '%s' already exists", nee.Name)
if nee.Name == domain.DefaultNodeName {
fmt.Printf(`
'%s' is default account name and it is already used.
'%s' is default node name and it is already used.
Please specify another name in mantil command.
`, aee.Name)
`, nee.Name)
}
return
}
Expand Down
22 changes: 11 additions & 11 deletions cli/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ func NewArgumentError(format string, v ...interface{}) *ArgumentError {
return &ArgumentError{msg: msg}
}

func awsClient(account *domain.Account, project *domain.Project, stage *domain.Stage) (*aws.AWS, error) {
restEndpoint := account.Endpoints.Rest
func awsClient(node *domain.Node, project *domain.Project, stage *domain.Stage) (*aws.AWS, error) {
restEndpoint := node.Endpoints.Rest

url, err := url.Parse(fmt.Sprintf("%s/security", restEndpoint))
if err != nil {
return nil, log.Wrap(err)
}

req := dto.SecurityRequest{
CliRole: account.CliRole,
Buckets: []string{account.Bucket},
CliRole: node.CliRole,
Buckets: []string{node.Bucket},
}
if stage != nil {
req.Buckets = append(req.Buckets, stage.Public.Bucket)
Expand All @@ -57,30 +57,30 @@ func awsClient(account *domain.Account, project *domain.Project, stage *domain.S
url.RawQuery = q.Encode()

token := func() string {
token, err := authToken(account)
token, err := authToken(node)
if err != nil {
return ""
}
return token
}

awsClient, err := aws.NewWithEndpointCredentials(url.String(), account.Region, token)
awsClient, err := aws.NewWithEndpointCredentials(url.String(), node.Region, token)
if err != nil {
return nil, log.Wrap(err)
}
return awsClient, nil
}

func authToken(account *domain.Account) (string, error) {
return account.AuthToken()
func authToken(node *domain.Node) (string, error) {
return node.AuthToken()
}

func Backend(account *domain.Account) (*backend.Backend, error) {
token, err := authToken(account)
func Backend(node *domain.Node) (*backend.Backend, error) {
token, err := authToken(node)
if err != nil {
return nil, log.Wrap(err)
}
return backend.New(account.Endpoints.Rest, token), nil
return backend.New(node.Endpoints.Rest, token), nil
}

func InvokeCallback(stage *domain.Stage, path, req string, includeLogs bool, cb func(*http.Response) error) func() error {
Expand Down
28 changes: 14 additions & 14 deletions cli/controller/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewDeployWithStage(fs *domain.FileStore, stage *domain.Stage) (*Deploy, err

func (d *Deploy) setAWSclient() error {
stage := d.stage
awsClient, err := awsClient(stage.Account(), stage.Project(), stage)
awsClient, err := awsClient(stage.Node(), stage.Project(), stage)
if err != nil {
return log.Wrap(err)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func (d *Deploy) buildAndFindDiffs() error {
}

func (d *Deploy) callBackend() error {
backend, err := Backend(d.stage.Account())
backend, err := Backend(d.stage.Node())
if err != nil {
return log.Wrap(err)
}
Expand All @@ -164,7 +164,7 @@ func (d *Deploy) callBackend() error {

func (d *Deploy) backendRequest() dto.DeployRequest {
req := dto.DeployRequest{
AccountBucket: d.stage.Account().Bucket,
NodeBucket: d.stage.Node().Bucket,
FunctionsForUpdate: nil,
StageTemplate: nil,
}
Expand All @@ -182,17 +182,17 @@ func (d *Deploy) backendRequest() dto.DeployRequest {
req.FunctionsForUpdate = fnsu
if d.diff.InfrastructureChanged() {
req.StageTemplate = &dto.StageTemplate{
Project: d.stage.Project().Name,
Bucket: d.stage.Account().Bucket,
BucketPrefix: d.stage.StateBucketPrefix(),
Functions: fns,
Region: d.stage.Account().Region,
Stage: d.stage.Name,
AccountFunctionsBucket: d.stage.Account().Functions.Bucket,
AccountFunctionsPath: d.stage.Account().Functions.Path,
ResourceSuffix: d.stage.Account().ResourceSuffix(),
ResourceTags: d.stage.ResourceTags(),
WsEnv: d.stage.WsEnv(),
Project: d.stage.Project().Name,
Bucket: d.stage.Node().Bucket,
BucketPrefix: d.stage.StateBucketPrefix(),
Functions: fns,
Region: d.stage.Node().Region,
Stage: d.stage.Name,
NodeFunctionsBucket: d.stage.Node().Functions.Bucket,
NodeFunctionsPath: d.stage.Node().Functions.Path,
ResourceSuffix: d.stage.Node().ResourceSuffix(),
ResourceTags: d.stage.ResourceTags(),
WsEnv: d.stage.WsEnv(),
}
}
return req
Expand Down
2 changes: 1 addition & 1 deletion cli/controller/deploy_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (d *Deploy) uploadBinaryToS3(key, binaryPath string) error {
return err
}
d.uploadBytes += int64(len(buf))
if err := d.repoPut(d.stage.Account().Bucket, key, buf); err != nil {
if err := d.repoPut(d.stage.Node().Bucket, key, buf); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion cli/controller/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Logs(a LogsArgs) error {
return log.Wrap(err)
}
stage := fs.Stage(a.Stage)
awsClient, err := awsClient(stage.Account(), stage.Project(), stage)
awsClient, err := awsClient(stage.Node(), stage.Project(), stage)
if err != nil {
return log.Wrap(err)
}
Expand Down
54 changes: 27 additions & 27 deletions cli/controller/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (

type Setup struct {
aws *aws.AWS
accountName string
nodeName string
override bool // TODO unused
store *domain.FileStore
resourceTags map[string]string
Expand Down Expand Up @@ -52,10 +52,10 @@ func NewSetup(a *SetupArgs) (*Setup, error) {
return nil, log.Wrap(err)
}
return &Setup{
aws: awsClient,
accountName: a.AccountName,
override: a.Override,
store: fs,
aws: awsClient,
nodeName: a.NodeName,
override: a.Override,
store: fs,
}, nil
}

Expand All @@ -64,7 +64,7 @@ func (c *Setup) Create(getPath func(string) (string, string)) error {
defer ui.ShowCursor()
ws := c.store.Workspace()
bucket, key := getPath(c.aws.Region())
ac, err := ws.NewAccount(c.accountName, c.aws.AccountID(), c.aws.Region(), bucket, key)
ac, err := ws.NewNode(c.nodeName, c.aws.AccountID(), c.aws.Region(), bucket, key)
if err != nil {
return log.Wrap(err)
}
Expand All @@ -81,37 +81,37 @@ func (c *Setup) Create(getPath func(string) (string, string)) error {
return nil
}

func (c *Setup) create(ac *domain.Account) error {
func (c *Setup) create(n *domain.Node) error {
exists, err := c.backendExists()
if err != nil {
return log.Wrap(err)
}
if exists {
return log.Wrapf("Mantil is already installed in this AWS account")
}
if err := c.createSetupStack(ac.Functions); err != nil {
if err := c.createSetupStack(n.Functions); err != nil {
return log.Wrap(err)
}
ui.Title("Setting up AWS infrastructure...\n")
req := &dto.SetupRequest{
BucketConfig: dto.SetupBucketConfig{
Name: ac.Bucket,
Name: n.Bucket,
ExpirePrefix: domain.FunctionsBucketPrefix,
ExpireDays: domain.FunctionsBucketExpireDays,
},
FunctionsBucket: ac.Functions.Bucket,
FunctionsPath: ac.Functions.Path,
AuthEnv: ac.AuthEnv(),
ResourceSuffix: ac.ResourceSuffix(),
FunctionsBucket: n.Functions.Bucket,
FunctionsPath: n.Functions.Path,
AuthEnv: n.AuthEnv(),
ResourceSuffix: n.ResourceSuffix(),
ResourceTags: c.resourceTags,
}
rsp := &dto.SetupResponse{}
if err := backend.Lambda(c.aws.Lambda(), c.lambdaName, nil).Call("create", req, rsp); err != nil {
return log.Wrap(err, "failed to invoke setup function")
}
ac.Endpoints.Rest = rsp.APIGatewayRestURL
ac.CliRole = rsp.CliRole
ui.Title("\nNode %s created with:", c.accountName)
n.Endpoints.Rest = rsp.APIGatewayRestURL
n.CliRole = rsp.CliRole
ui.Title("\nNode %s created with:", c.nodeName)
ui.Info(`
+ Lambda functions
+ API Gateways
Expand All @@ -128,7 +128,7 @@ func (c *Setup) backendExists() (bool, error) {
return c.aws.LambdaExists(c.lambdaName)
}

func (c *Setup) createSetupStack(acf domain.AccountFunctions) error {
func (c *Setup) createSetupStack(acf domain.NodeFunctions) error {
td := stackTemplateData{
Name: c.stackName,
Bucket: acf.Bucket,
Expand All @@ -155,24 +155,24 @@ func (c *Setup) Destroy() error {
ui.HideCursor()
defer ui.ShowCursor()
ws := c.store.Workspace()
ac := ws.Account(c.accountName)
if ac == nil {
return log.Wrapf("Account %s don't exists", c.accountName)
n := ws.Node(c.nodeName)
if n == nil {
return log.Wrapf("Node %s don't exists", c.nodeName)
}
c.stackName = ac.SetupStackName()
c.lambdaName = ac.SetupLambdaName()
c.stackName = n.SetupStackName()
c.lambdaName = n.SetupLambdaName()

if err := c.destroy(ac); err != nil {
if err := c.destroy(n); err != nil {
return log.Wrap(err)
}
ws.RemoveAccount(ac.Name)
ws.RemoveNode(n.Name)
if err := c.store.Store(); err != nil {
return log.Wrap(err)
}
return nil
}

func (c *Setup) destroy(ac *domain.Account) error {
func (c *Setup) destroy(n *domain.Node) error {
exists, err := c.backendExists()
if err != nil {
return log.Wrap(err)
Expand All @@ -182,7 +182,7 @@ func (c *Setup) destroy(ac *domain.Account) error {
}

req := &dto.SetupDestroyRequest{
Bucket: ac.Bucket,
Bucket: n.Bucket,
}

ui.Title("\nDestroying AWS infrastructure...\n")
Expand All @@ -194,7 +194,7 @@ func (c *Setup) destroy(ac *domain.Account) error {
if err := stackWaiter.Wait(); err != nil {
return log.Wrap(err)
}
ui.Notice("\nNode %s destroyed!", c.accountName)
ui.Notice("\nNode %s destroyed!", c.nodeName)
return nil
}

Expand Down
Loading

0 comments on commit a1ac830

Please sign in to comment.