Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stateful api actions support: pause, resume, recycle, de-allocate #192

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unreleased

ENHANCEMENTS:
* resource/spotinst_elastigroup_aws: added support for stateful instance actions: `pause`, `resume`,`recycle`,`deallocate` ([#192](https://github.com/spotinst/terraform-provider-spotinst/pull/192))

## 1.44.1 (May 28, 2021)

BUG FIXES:
Expand Down
21 changes: 21 additions & 0 deletions docs/resources/elastigroup_aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,27 @@ Usage:
}
```

<a id="stateful_instance_action"></a>
## Stateful Instance Action

* `stateful_instance_action` - (Optional)
* `stateful_instance_id` - (Required) String, Stateful Instance ID on which the action should be performed.
* `type` - (Required) String, Action type. Supported action types: `pause`, `resume`, `recycle`, `deallocate`.

Usage:

```hcl
stateful_instance_action {
type = "pause"
stateful_instance_id = "ssi-foo"
}

stateful_instance_action {
type = "recycle"
stateful_instance_id = "ssi-bar"
}
```

<a id="health-check"></a>
## Health Check

Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/andybalholm/crlf v0.0.0-20171020200849-670099aa064f/go.mod h1:k8feO4+kXDxro6ErPXBRTJ/ro2mf0SsFG8s7doP9kJE=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/apparentlymart/go-cidr v1.0.1/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
Expand All @@ -67,6 +69,7 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkE
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/aws/aws-sdk-go v1.15.78/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3ATZkfNZeM=
github.com/aws/aws-sdk-go v1.25.3/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
Expand Down Expand Up @@ -100,14 +103,17 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
github.com/go-git/go-billy/v5 v5.1.0 h1:4pl5BV4o7ZG/lterP4S6WzJ6xr49Ba5ET9ygheTYahk=
github.com/go-git/go-billy/v5 v5.1.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw=
github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12 h1:PbKy9zOy4aAKrJ5pibIRpVO2BXnK1Tlcg+caKI7Ox5M=
github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw=
github.com/go-git/go-git/v5 v5.1.0/go.mod h1:ZKfuPUoY1ZqIG4QG9BDBh3G4gLM5zvPuSJAozQrZuyM=
github.com/go-git/go-git/v5 v5.3.0 h1:8WKMtJR2j8RntEXR/uvTKagfEt4GYlwQ7mntE4+0GWc=
Expand Down Expand Up @@ -331,6 +337,7 @@ github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -354,8 +361,6 @@ github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spotinst/spotinst-sdk-go v1.87.0 h1:eA5rastAlSH5FIzdYksSuxVHzX1znOHDoXRlSnQElZ0=
github.com/spotinst/spotinst-sdk-go v1.87.0/go.mod h1:RuKksd1/RyQogMCxKFI5B75NeFRcgl3W+26DVX6S4m0=
github.com/spotinst/spotinst-sdk-go v1.88.0 h1:fN6xUPohKza9Rg+pnMw9Q/JCjMqgt+qLwZZqDo7UHm0=
github.com/spotinst/spotinst-sdk-go v1.88.0/go.mod h1:RuKksd1/RyQogMCxKFI5B75NeFRcgl3W+26DVX6S4m0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -532,6 +537,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
12 changes: 7 additions & 5 deletions spotinst/elastigroup_aws_stateful/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package elastigroup_aws_stateful

import "github.com/spotinst/terraform-provider-spotinst/spotinst/commons"

const (
Prefix = "stateful_"
)

const (
PersistRootDevice commons.FieldName = "persist_root_device"
PersistBlockDevices commons.FieldName = "persist_block_devices"
PersistPrivateIp commons.FieldName = "persist_private_ip"
BlockDevicesMode commons.FieldName = "block_devices_mode"
PrivateIps commons.FieldName = "private_ips"

// - DEALLOCATION -------------------------
// - Deallocation -------------------------
StatefulDeallocation commons.FieldName = "stateful_deallocation"
ShouldDeleteImages commons.FieldName = "should_delete_images"
ShouldDeleteNetworkInterfaces commons.FieldName = "should_delete_network_interfaces"
ShouldDeleteVolumes commons.FieldName = "should_delete_volumes"
ShouldDeleteSnapshots commons.FieldName = "should_delete_snapshots"
// ----------------------------------------

// - Instance Action ----------------------
StatefulInstanceAction commons.FieldName = "stateful_instance_action"
StatefulInstanceID commons.FieldName = "stateful_instance_id"
ActionType commons.FieldName = "type"
// ----------------------------------------
)
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
Type: schema.TypeBool,
Optional: true,
},

string(ShouldDeleteSnapshots): {
Type: schema.TypeBool,
Optional: true,
Expand All @@ -257,6 +258,34 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
},
nil, nil, nil, nil,
)

fieldsMap[StatefulInstanceAction] = commons.NewGenericField(
commons.ElastigroupAWSStateful,
StatefulInstanceAction,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(StatefulInstanceID): {
Type: schema.TypeString,
Required: true,
},

string(ActionType): {
Type: schema.TypeString,
Required: true,
},
},
},
},
nil, nil,
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
return nil
},
nil,
)

}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/gcp"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/terraform-provider-spotinst/spotinst/commons"
Expand All @@ -22,8 +23,10 @@ func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(PreemptiblePercentage): {
Type: schema.TypeInt,
Optional: true,
Type: schema.TypeInt,
Optional: true,
Default: -1,
ValidateFunc: validation.IntAtLeast(-1),
},
},
},
Expand Down Expand Up @@ -85,7 +88,7 @@ func expandStrategy(data interface{}) (*gcp.LaunchSpecStrategy, error) {
}
m := list[0].(map[string]interface{})

if v, ok := m[string(PreemptiblePercentage)].(int); ok && v > 0 {
if v, ok := m[string(PreemptiblePercentage)].(int); ok && v > -1 {
strategy.SetPreemptiblePercentage(spotinst.Int(v))
}

Expand Down
113 changes: 113 additions & 0 deletions spotinst/resource_spotinst_elastigroup_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,42 @@ func updateGroup(elastigroup *aws.Group, resourceData *schema.ResourceData, meta
}
}

if instanceActions, exists := resourceData.GetOkExists(string(elastigroup_aws_stateful.StatefulInstanceAction)); exists {
actionList := instanceActions.([]interface{})
if err := checkStatefulActionUniqueness(actionList); err != nil {
log.Printf("[ERROR] Multiple actions are not allowed for the same instance, error: %v", err)
return err
}

ctx := context.TODO()
svc := meta.(*Client).elastigroup.CloudProviderAWS()

for _, action := range actionList {
var (
actionMap = action.(map[string]interface{})
actionType = actionMap[string(elastigroup_aws_stateful.ActionType)].(string)
instanceID = actionMap[string(elastigroup_aws_stateful.StatefulInstanceID)].(string)
err error
)
switch strings.ToLower(actionType) {
case "pause":
err = pauseStatefulInstance(ctx, svc, resourceData.Id(), instanceID)
case "resume":
err = resumeStatefulInstance(ctx, svc, resourceData.Id(), instanceID)
case "recycle":
err = recycleStatefulInstance(ctx, svc, resourceData.Id(), instanceID)
case "deallocate":
err = deallocateStatefulInstance(ctx, svc, resourceData.Id(), instanceID)
default:
err = fmt.Errorf("unsupported action %q on instance %q", actionType, instanceID)
}
if err != nil {
log.Printf("[ERROR] Stateful instance [%v] action failed with error: %v", instanceID, err)
return err
}
}
}

if json, err := commons.ToJson(elastigroup); err != nil {
return err
} else {
Expand Down Expand Up @@ -333,6 +369,83 @@ func updateGroup(elastigroup *aws.Group, resourceData *schema.ResourceData, meta
return nil
}

func checkStatefulActionUniqueness(actionList []interface{}) error {
seenIDs := make(map[string]struct{})
for _, action := range actionList {
actionMap := action.(map[string]interface{})
instanceID := actionMap[string(elastigroup_aws_stateful.StatefulInstanceID)].(string)
if _, seen := seenIDs[instanceID]; seen {
return fmt.Errorf("multiple actions are not allowed for the same instance (%v)", instanceID)
}
seenIDs[instanceID] = struct{}{}
}
return nil
}

func pauseStatefulInstance(ctx context.Context, svc aws.Service, groupID, instanceID string) error {
log.Printf("Pausing instance (%s)", instanceID)

input := &aws.PauseStatefulInstanceInput{
GroupID: spotinst.String(groupID),
StatefulInstanceID: spotinst.String(instanceID),
}
_, err := svc.PauseStatefulInstance(ctx, input)
if err != nil {
return fmt.Errorf("failed to pause instance (%s): %v", instanceID, err)
}

log.Printf("Successfully paused instance (%s)", instanceID)
return nil
}

func resumeStatefulInstance(ctx context.Context, svc aws.Service, groupID, instanceID string) error {
log.Printf("Resuming instance (%s)", instanceID)

input := &aws.ResumeStatefulInstanceInput{
GroupID: spotinst.String(groupID),
StatefulInstanceID: spotinst.String(instanceID),
}
_, err := svc.ResumeStatefulInstance(ctx, input)
if err != nil {
return fmt.Errorf("failed to resume instance (%s): %v", instanceID, err)
}

log.Printf("Successfully resumed instance (%s)", instanceID)
return nil
}

func recycleStatefulInstance(ctx context.Context, svc aws.Service, groupID, instanceID string) error {
log.Printf("Recycling instance (%s)", instanceID)

input := &aws.RecycleStatefulInstanceInput{
GroupID: spotinst.String(groupID),
StatefulInstanceID: spotinst.String(instanceID),
}
_, err := svc.RecycleStatefulInstance(ctx, input)
if err != nil {
return fmt.Errorf("failed to recycle instance (%s): %v", instanceID, err)
}

log.Printf("Successfully recycled instance (%s)", instanceID)
return nil
}

func deallocateStatefulInstance(ctx context.Context, svc aws.Service, groupID, instanceID string) error {
log.Printf("Deallocating instance (%s)", instanceID)

input := &aws.DeallocateStatefulInstanceInput{
GroupID: spotinst.String(groupID),
StatefulInstanceID: spotinst.String(instanceID),
}
_, err := svc.DeallocateStatefulInstance(ctx, input)
if err != nil {
return fmt.Errorf("failed to deallocate instance (%s): %v", instanceID, err)
}

log.Printf("Successfully deallocated instance (%s)", instanceID)
return nil
}

func rollGroup(resourceData *schema.ResourceData, meta interface{}) error {
ctx := context.Background()
groupID := resourceData.Id()
Expand Down