Skip to content

Commit

Permalink
Add fields for unified migration (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
zliang-akamai authored Nov 28, 2023
1 parent e4ec4c1 commit 3a4cc70
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
26 changes: 23 additions & 3 deletions instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ const (
InstanceResizing InstanceStatus = "resizing"
)

type InstanceMigrationType string

const (
WarmMigration InstanceMigrationType = "warm"
ColdMigration InstanceMigrationType = "cold"
)

// Instance represents a linode object
type Instance struct {
ID int `json:"id"`
Expand Down Expand Up @@ -192,12 +199,19 @@ type InstanceCloneOptions struct {

// InstanceResizeOptions is an options struct used when resizing an instance
type InstanceResizeOptions struct {
Type string `json:"type"`
Type string `json:"type"`
MigrationType InstanceMigrationType `json:"migration_type,omitempty"`

// When enabled, an instance resize will also resize a data disk if the instance has no more than one data disk and one swap disk
AllowAutoDiskResize *bool `json:"allow_auto_disk_resize,omitempty"`
}

// InstanceResizeOptions is an options struct used when resizing an instance
type InstanceMigrateOptions struct {
Type InstanceMigrationType `json:"type,omitempty"`
Region string `json:"region,omitempty"`
}

// InstancesPagedResponse represents a linode API response for listing
type InstancesPagedResponse struct {
*PageOptions
Expand Down Expand Up @@ -417,8 +431,14 @@ func (c *Client) MutateInstance(ctx context.Context, id int) error {
}

// MigrateInstance - Migrate an instance
func (c *Client) MigrateInstance(ctx context.Context, id int) error {
return c.simpleInstanceAction(ctx, "migrate", id)
func (c *Client) MigrateInstance(ctx context.Context, linodeID int, opts InstanceMigrateOptions) error {
body, err := json.Marshal(opts)
if err != nil {
return err
}
e := fmt.Sprintf("linode/instances/%d/migrate", linodeID)
_, err = coupleAPIErrors(c.R(ctx).SetBody(string(body)).Post(e))
return err
}

// simpleInstanceAction is a helper for Instance actions that take no parameters
Expand Down
39 changes: 39 additions & 0 deletions test/integration/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,45 @@ func TestInstance_Get_smoke(t *testing.T) {
assertDateSet(t, instance.Updated)
}

func TestInstance_Resize(t *testing.T) {
client, instance, teardown, err := setupInstance(
t,
"fixtures/TestInstance_Resize",
func(client *linodego.Client, options *linodego.InstanceCreateOptions) {
boot := true
options.Type = "g6-nanode-1"
options.Booted = &boot
},
)

defer teardown()
if err != nil {
t.Error(err)
}

instance, err = client.WaitForInstanceStatus(
context.Background(),
instance.ID,
linodego.InstanceRunning,
180,
)
if err != nil {
t.Errorf("Error waiting for instance readiness for resize: %s", err.Error())
}

err = client.ResizeInstance(
context.Background(),
instance.ID,
linodego.InstanceResizeOptions{
Type: "g6-standard-1",
MigrationType: "warm",
},
)
if err != nil {
t.Errorf("failed to resize instance %d: %v", instance.ID, err.Error())
}
}

func TestInstance_Disks_List(t *testing.T) {
client, instance, teardown, err := setupInstance(t, "fixtures/TestInstance_Disks_List")
defer teardown()
Expand Down

0 comments on commit 3a4cc70

Please sign in to comment.