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

Fix server power state conflict when deleting server #275

Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gridscale/gsclient-go/v3 v3.14.1 h1:AJkrQuO4TZGxVkZouS1iZ8fZHRqlM97aGFWuGZnkHNE=
github.com/gridscale/gsclient-go/v3 v3.14.1/go.mod h1:0NjkxHexfHQBbmcN03WBF1G4JhZo3Msai50Zcygv+/8=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
Expand Down
17 changes: 16 additions & 1 deletion gridscale/error-handler/skipHTTPErrCode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package errorhandler

import "github.com/gridscale/gsclient-go/v3"
import (
"strings"

"github.com/gridscale/gsclient-go/v3"
)

// SuppressHTTPErrorCodes suppresses the error, if the error
// is in the list errorCodes.
Expand All @@ -13,6 +17,17 @@ func SuppressHTTPErrorCodes(err error, errorCodes ...int) error {
return err
}

// SuppressHTTPErrorCodesWithSubErrString suppresses the error, if the error
// is in the list errorCodes and contains the subString.
func SuppressHTTPErrorCodesWithSubErrString(err error, subString string, errorCodes ...int) error {
if requestError, ok := err.(gsclient.RequestError); ok {
if containsInt(errorCodes, requestError.StatusCode) && strings.Contains(requestError.Error(), subString) {
nvthongswansea marked this conversation as resolved.
Show resolved Hide resolved
err = nil
}
}
return err
}

// containsInt check if an int array contains a specific int.
func containsInt(arr []int, target int) bool {
for _, a := range arr {
Expand Down
14 changes: 12 additions & 2 deletions gridscale/server_concurrency_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
errHandler "github.com/terraform-providers/terraform-provider-gridscale/gridscale/error-handler"
)

const serverAlreadyInRequestedPowerStateErrSubStr = "already in the requested power state"

type serverStatus struct {
//is the server is deleted
deleted bool
Expand Down Expand Up @@ -74,7 +76,11 @@ func (l *serverStatusList) removeServerSynchronously(ctx context.Context, c *gsc
//set the shutdown timeout specifically
shutdownCtx, cancel := context.WithTimeout(context.Background(), serverShutdownTimeoutSecs*time.Second)
defer cancel()
err := c.ShutdownServer(shutdownCtx, id)
err := errHandler.SuppressHTTPErrorCodesWithSubErrString(
c.ShutdownServer(shutdownCtx, id),
serverAlreadyInRequestedPowerStateErrSubStr,
http.StatusBadRequest,
nvthongswansea marked this conversation as resolved.
Show resolved Hide resolved
)
//if error is returned and it is not caused by an expired context, returns error
if err != nil && err != shutdownCtx.Err() {
return err
Expand All @@ -89,7 +95,11 @@ func (l *serverStatusList) removeServerSynchronously(ctx context.Context, c *gsc
default:
}
//force the sever to stop
err = c.StopServer(ctx, id)
err = errHandler.SuppressHTTPErrorCodesWithSubErrString(
c.StopServer(ctx, id),
serverAlreadyInRequestedPowerStateErrSubStr,
http.StatusBadRequest,
)
if err != nil {
return err
}
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/google/uuid/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/google/uuid/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions vendor/github.com/google/uuid/time.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions vendor/github.com/google/uuid/uuid.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions vendor/github.com/google/uuid/version6.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions vendor/github.com/google/uuid/version7.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ github.com/google/go-cmp/cmp/internal/diff
github.com/google/go-cmp/cmp/internal/flags
github.com/google/go-cmp/cmp/internal/function
github.com/google/go-cmp/cmp/internal/value
# github.com/google/uuid v1.4.0
# github.com/google/uuid v1.6.0
## explicit
github.com/google/uuid
# github.com/gridscale/gsclient-go/v3 v3.14.1
Expand Down
Loading