Skip to content

Commit

Permalink
Merge pull request #102 from Appliscale/update-stack-mode
Browse files Browse the repository at this point in the history
Update Stack Mode #90
  • Loading branch information
Piotr Figwer authored Jun 18, 2018
2 parents f88b9b7 + 849651d commit d63fd03
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cliparser/cliparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var OfflineValidateMode = "validate_offline"
var ConfigureMode = "configure"
var CreateStackMode = "create-stack"
var DestroyStackMode = "delete-stack"
var UpdateStackMode = "update-stack"
var MfaMode = "mfa"

type CliArguments struct {
Expand Down Expand Up @@ -88,6 +89,13 @@ func ParseCliArguments(args []string) (cliArguments CliArguments, err error) {
deleteStack = app.Command(DestroyStackMode, "Deletes a stack on aws")
deleteStackName = deleteStack.Arg("stack", "An AWS stack name.").Required().String()

updateStack = app.Command(UpdateStackMode, "Updates a stack on aws")
updateStackName = updateStack.Arg("stack", "An AWS stack name").String()
updateStackTemplate = updateStack.Arg("template", "A path to the template file.").String()
updateStackImpName = updateStack.Flag("stack", "Sn AWS stack name.").String()
updateStackImpTemplate = updateStack.Flag("template", "A path to the template file.").String()
updateStackCapabilities = updateStack.Flag("capabilities", "Capabilities: CAPABILITY_IAM | CAPABILITY_NAMED_IAM").Enums("CAPABILITY_IAM", "CAPABILITY_NAMED_IAM")

mfaCommand = app.Command(MfaMode, "Create temporary secure credentials with MFA.")
)

Expand Down Expand Up @@ -133,6 +141,23 @@ func ParseCliArguments(args []string) (cliArguments CliArguments, err error) {
case mfaCommand.FullCommand():
cliArguments.Mode = &MfaMode

// update Stack
case updateStack.FullCommand():
cliArguments.Mode = &UpdateStackMode
cliArguments.Capabilities = updateStackCapabilities
if len(*updateStackImpTemplate) > 0 && len(*updateStackImpName) > 0 {
cliArguments.Stack = updateStackImpName
cliArguments.TemplatePath = updateStackImpTemplate
} else if len(*updateStackName) > 0 && len(*updateStackTemplate) > 0 {
cliArguments.Stack = updateStackName
cliArguments.TemplatePath = updateStackTemplate
} else if len(*updateStackName) > 0 && len(*updateStackImpTemplate) > 0 {
cliArguments.Stack = updateStackName
cliArguments.TemplatePath = updateStackImpTemplate
} else {
err = errors.New("You have to specify stack name and template file, try --help")
return
}
}

// OTHER FLAGS
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,10 @@ func main() {
os.Exit(1)
}
}

if *context.CliArguments.Mode == cliparser.UpdateStackMode {
stack.UpdateStack(&context)
os.Exit(0)
}

}
32 changes: 32 additions & 0 deletions stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ func createStackInput(context *context.Context, template *string, stackName *str
return templateStruct
}

// This function gets template and name of stack. It creates "CreateStackInput" structure.
func updateStackInput(context *context.Context, template *string, stackName *string) cloudformation.UpdateStackInput {
rawCapabilities := *context.CliArguments.Capabilities
capabilities := make([]*string, len(rawCapabilities))
for i, capability := range rawCapabilities {
capabilities[i] = &capability
}
templateStruct := cloudformation.UpdateStackInput{
TemplateBody: template,
StackName: stackName,
Capabilities: capabilities,
}
return templateStruct
}

// This function reads "StackName" from Stack in CliArguments and file from TemplatePath in CliArguments. It converts these to type string.
func getTemplateFromFile(context *context.Context) (string, string) {

Expand Down Expand Up @@ -70,6 +85,23 @@ func DestroyStack(context *context.Context) {
}
}

func UpdateStack(context *context.Context) {
template, stackName := getTemplateFromFile(context)
templateStruct := updateStackInput(context, &template, &stackName)
session := mysession.InitializeSession(context)
err := updateStack(templateStruct, session)
if err != nil {
context.Logger.Error(err.Error())
os.Exit(1)
}
}

func updateStack(updateStackInput cloudformation.UpdateStackInput, session *session.Session) error {
api := cloudformation.New(session)
_, err := api.UpdateStack(&updateStackInput)
return err
}

// This function gets "StackName" from Stack in CliArguments and creates "DeleteStackInput" structure.
func deleteStackInput(context *context.Context) cloudformation.DeleteStackInput {
name := *context.CliArguments.Stack
Expand Down

0 comments on commit d63fd03

Please sign in to comment.