Skip to content

Commit

Permalink
DNS: allow to revert creation/deletion of records; Returns the Change…
Browse files Browse the repository at this point in the history
…Info id when creating/deleting a record. #80
  • Loading branch information
fxaguessy committed Mar 17, 2017
1 parent e796656 commit fe0b5f9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Better error messaging on parsing template errors
- Infra: basic support of RDS: listing, creation and deletion of databases: `awless list databases`; `awless create/delete database`
- Access: create an AWS access key for a user
- DNS: allow to revert creation/deletion of records
- [#80](https://github.com/wallix/awless/issues/80) DNS: return the ChangeInfo id when creating/deleting a record

### Bugfixes

Expand Down
8 changes: 4 additions & 4 deletions aws/driver/driver_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,13 @@ func (d *Route53Driver) Create_Record(params map[string]interface{}) (interface{
start := time.Now()
var output *route53.ChangeResourceRecordSetsOutput
output, err = d.ChangeResourceRecordSets(input)
output = output

if err != nil {
return nil, fmt.Errorf("create record: %s", err)
}
d.logger.ExtraVerbosef("route53.ChangeResourceRecordSets call took %s", time.Since(start))
d.logger.Verbose("create record done")
return output, nil
return aws.StringValue(output.ChangeInfo.Id), nil
}

func (d *Route53Driver) Delete_Record_DryRun(params map[string]interface{}) (interface{}, error) {
Expand Down Expand Up @@ -680,13 +680,13 @@ func (d *Route53Driver) Delete_Record(params map[string]interface{}) (interface{
start := time.Now()
var output *route53.ChangeResourceRecordSetsOutput
output, err = d.ChangeResourceRecordSets(input)
output = output

if err != nil {
return nil, fmt.Errorf("delete record: %s", err)
}
d.logger.ExtraVerbosef("route53.ChangeResourceRecordSets call took %s", time.Since(start))
d.logger.Verbose("delete record done")
return output, nil
return aws.StringValue(output.ChangeInfo.Id), nil
}

func buildIpPermissionsFromParams(params map[string]interface{}) ([]*ec2.IpPermission, error) {
Expand Down
6 changes: 3 additions & 3 deletions aws/driver/gen_driver_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2037,8 +2037,8 @@ func (d *RdsDriver) Create_Database_DryRun(params map[string]interface{}) (inter
return nil, errors.New("create database: missing required params 'username'")
}

if _, ok := params["storage"]; !ok {
return nil, errors.New("create database: missing required params 'storage'")
if _, ok := params["size"]; !ok {
return nil, errors.New("create database: missing required params 'size'")
}

d.logger.Verbose("params dry run: create database ok")
Expand Down Expand Up @@ -2071,7 +2071,7 @@ func (d *RdsDriver) Create_Database(params map[string]interface{}) (interface{},
if err != nil {
return nil, err
}
err = setFieldWithType(params["storage"], input, "AllocatedStorage", awsint64)
err = setFieldWithType(params["size"], input, "AllocatedStorage", awsint64)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion aws/driver/gen_template_defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ var AWSTemplatesDefinitions = map[string]template.TemplateDefinition{
Action: "create",
Entity: "database",
Api: "rds",
RequiredParams: []string{"engine", "id", "password", "storage", "type", "username"},
RequiredParams: []string{"engine", "id", "password", "size", "type", "username"},
ExtraParams: []string{"autoupgrade", "backupretention", "backupwindow", "cluster", "dbname", "dbsecgroup", "domain", "encrypted", "iamrole", "iops", "license", "maintenancewindow", "multiaz", "optiongroup", "parametergroup", "port", "public", "storagetype", "subnetgroup", "timezone", "version", "vpcsecgroup", "zone"},
},
"deletedatabase": {
Expand Down
2 changes: 1 addition & 1 deletion gen/aws/drivers_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ var DriversDefs = []driversDef{
{AwsField: "Engine", TemplateName: "engine", AwsType: "awsstr"},
{AwsField: "MasterUserPassword", TemplateName: "password", AwsType: "awsstr"},
{AwsField: "MasterUsername", TemplateName: "username", AwsType: "awsstr"},
{AwsField: "AllocatedStorage", TemplateName: "storage", AwsType: "awsint64"},
{AwsField: "AllocatedStorage", TemplateName: "size", AwsType: "awsint64"},
},
ExtraParams: []param{
{AwsField: "AutoMinorVersionUpgrade", TemplateName: "autoupgrade", AwsType: "awsbool"},
Expand Down
23 changes: 21 additions & 2 deletions template/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,31 @@ func (te *Template) Revert() (*Template, error) {
revertAction = "attach"
case "attach":
revertAction = "detach"
case "delete":
revertAction = "create"
}

switch cmd.Action {
case "start", "stop", "attach", "detach":
for k, v := range cmd.Params {
params = append(params, fmt.Sprintf("%s=%s", k, v))
params = append(params, fmt.Sprintf("%s=%v", k, v))
}
case "create":
params = append(params, fmt.Sprintf("id=%s", cmd.CmdResult))
switch cmd.Entity {
case "record":
for k, v := range cmd.Params {
params = append(params, fmt.Sprintf("%s=%v", k, v))
}
default:
params = append(params, fmt.Sprintf("id=%s", cmd.CmdResult))
}
case "delete":
switch cmd.Entity {
case "record":
for k, v := range cmd.Params {
params = append(params, fmt.Sprintf("%s=%v", k, v))
}
}
}

lines = append(lines, fmt.Sprintf("%s %s %s", revertAction, cmd.Entity, strings.Join(params, " ")))
Expand Down Expand Up @@ -76,6 +92,9 @@ func isRevertible(cmd *ast.CommandNode) bool {
if cmd.Action == "check" {
return false
}
if cmd.Entity == "record" && (cmd.Action == "create" || cmd.Action == "delete") {
return true
}

if v, ok := cmd.CmdResult.(string); ok && v != "" {
if cmd.Action == "create" || cmd.Action == "start" || cmd.Action == "stop" {
Expand Down
2 changes: 2 additions & 0 deletions template/revert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func TestCmdNodeIsRevertible(t *testing.T) {
{line: "stop instance", result: "any", revertible: true},
{line: "attach policy", revertible: true},
{line: "detach policy", revertible: true},
{line: "create record", revertible: true},
{line: "delete record", revertible: true},
}

for _, tc := range tcases {
Expand Down

0 comments on commit fe0b5f9

Please sign in to comment.