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

feat(exp): add ensure powerOff powerOn shutdown in serverutil #512

Closed
wants to merge 1 commit into from
Closed
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
33 changes: 32 additions & 1 deletion hcloud/exp/actionutil/actions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package actionutil

import "github.com/hetznercloud/hcloud-go/v2/hcloud"
import (
"context"
"slices"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// AppendNext return the action and the next actions in a new slice.
func AppendNext(action *hcloud.Action, nextActions []*hcloud.Action) []*hcloud.Action {
Expand All @@ -9,3 +14,29 @@ func AppendNext(action *hcloud.Action, nextActions []*hcloud.Action) []*hcloud.A
all = append(all, nextActions...)
return all
}

func RunningForResource(
ctx context.Context,
client *hcloud.Client,
kind hcloud.ActionResourceType,
id int64,
) ([]*hcloud.Action, error) {
actions, err := client.Server.Action.All(ctx,
hcloud.ActionListOpts{
Status: []hcloud.ActionStatus{hcloud.ActionStatusRunning},
},
)
if err != nil {
return nil, err
}

actions = slices.Clip(
slices.DeleteFunc(actions, func(a *hcloud.Action) bool {
return !slices.ContainsFunc(a.Resources, func(r *hcloud.ActionResource) bool {
return r.Type == kind && r.ID == id
})
}),
)

return actions, nil
}
75 changes: 75 additions & 0 deletions hcloud/exp/serverutil/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package serverutil

import (
"context"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/actionutil"
)

func EnsureShutdown(ctx context.Context, client *hcloud.Client, server *hcloud.Server) error {
return ensureOff(ctx, client, client.Server.Shutdown, server)
}

func EnsurePowerOff(ctx context.Context, client *hcloud.Client, server *hcloud.Server) error {
return ensureOff(ctx, client, client.Server.Poweroff, server)
}

func ensureOff(
ctx context.Context,
client *hcloud.Client,
clientShutdownFunc func(context.Context, *hcloud.Server) (*hcloud.Action, *hcloud.Response, error),
server *hcloud.Server,
) error {
switch server.Status {
case hcloud.ServerStatusOff:
return nil // Nothing to do

case hcloud.ServerStatusStopping:
actions, err := actionutil.RunningForResource(ctx, client, hcloud.ActionResourceTypeServer, server.ID)
if err != nil {
return err
}
if err := client.Action.WaitFor(ctx, actions...); err != nil {
return err
}

default:
shutdown, _, err := clientShutdownFunc(ctx, server)
if err != nil {
return err
}
if err := client.Action.WaitFor(ctx, shutdown); err != nil {
return err
}
}

return nil
}

func EnsurePowerOn(ctx context.Context, client *hcloud.Client, server *hcloud.Server) error {
switch server.Status {
case hcloud.ServerStatusRunning:
return nil // Nothing to do

case hcloud.ServerStatusStarting:
actions, err := actionutil.RunningForResource(ctx, client, hcloud.ActionResourceTypeServer, server.ID)
if err != nil {
return err
}
if err := client.Action.WaitFor(ctx, actions...); err != nil {
return err
}

default:
powerOn, _, err := client.Server.Poweron(ctx, server)
if err != nil {
return err
}
if err := client.Action.WaitFor(ctx, powerOn); err != nil {
return err
}
}

return nil
}
59 changes: 59 additions & 0 deletions hcloud/exp/serverutil/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package serverutil

import (
"context"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutil"
)

func TestEnsurePowerOff(t *testing.T) {
for _, testCase := range []struct {
Name string
WantRequests []mockutil.Request
Run func(client *hcloud.Client)
}{
{
Name: "succeed",
WantRequests: []mockutil.Request{
{
Method: "POST", Path: "/servers/1/actions/poweroff",
Status: 200,
JSONRaw: `{
"action": { "id": 1509772237, "status": "running", "progress": 0 }
}`,
},
{
Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "success", "progress": 100 }
]
}`,
},
},
Run: func(client *hcloud.Client) {
ctx := context.Background()
err := EnsurePowerOff(ctx, client, &hcloud.Server{ID: 1, Status: hcloud.ServerStatusRunning})
assert.NoError(t, err)
},
},
} {
t.Run(testCase.Name, func(t *testing.T) {
server := httptest.NewServer(mockutil.Handler(t, testCase.WantRequests))
defer server.Close()

client := hcloud.NewClient(
hcloud.WithEndpoint(server.URL),
hcloud.WithPollOpts(hcloud.PollOpts{BackoffFunc: hcloud.ConstantBackoff(0)}),
)

testCase.Run(client)
})
}
}