Skip to content

Commit

Permalink
pg migration (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
yec-akamai authored Nov 27, 2024
1 parent 0987f21 commit 3e7524c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
23 changes: 22 additions & 1 deletion linode_api4/objects/placement.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import List, Union
from typing import List, Optional, Union

from linode_api4.objects.base import Base, Property
from linode_api4.objects.linode import Instance
Expand Down Expand Up @@ -34,6 +34,26 @@ class PlacementGroupMember(JSONObject):
is_compliant: bool = False


@dataclass
class MigratedInstance(JSONObject):
"""
The ID for a compute instance being migrated into or out of the placement group.
"""

linode_id: int = 0


@dataclass
class PlacementGroupMigrations(JSONObject):
"""
Any compute instances that are being migrated to or from the placement group.
Returns an empty object if no migrations are taking place.
"""

inbound: Optional[List[MigratedInstance]] = None
outbound: Optional[List[MigratedInstance]] = None


class PlacementGroup(Base):
"""
NOTE: Placement Groups may not currently be available to all users.
Expand All @@ -54,6 +74,7 @@ class PlacementGroup(Base):
"placement_group_policy": Property(),
"is_compliant": Property(),
"members": Property(json_object=PlacementGroupMember),
"migrations": Property(json_object=PlacementGroupMigrations),
}

def assign(
Expand Down
14 changes: 13 additions & 1 deletion test/fixtures/placement_groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@
"linode_id": 123,
"is_compliant": true
}
]
],
"migrations": {
"inbound": [
{
"linode_id": 123
}
],
"outbound": [
{
"linode_id": 456
}
]
}
}
],
"page": 1,
Expand Down
14 changes: 13 additions & 1 deletion test/fixtures/placement_groups_123.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@
"linode_id": 123,
"is_compliant": true
}
]
],
"migrations": {
"inbound": [
{
"linode_id": 123
}
],
"outbound": [
{
"linode_id": 456
}
]
}
}
63 changes: 62 additions & 1 deletion test/integration/models/placement/test_placement.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
from test.integration.conftest import get_region
from test.integration.helpers import (
get_test_label,
send_request_when_resource_available,
)

import pytest

from linode_api4 import PlacementGroup
from linode_api4 import (
MigratedInstance,
MigrationType,
PlacementGroup,
PlacementGroupPolicy,
PlacementGroupType,
)


@pytest.mark.smoke
Expand Down Expand Up @@ -48,3 +60,52 @@ def test_pg_assignment(test_linode_client, create_placement_group_with_linode):

assert pg.members[0].linode_id == inst.id
assert inst.placement_group.id == pg.id


def test_pg_migration(
test_linode_client, e2e_test_firewall, create_placement_group
):
"""
Tests that an instance can be migrated into and our of PGs successfully.
"""
client = test_linode_client

label = get_test_label(10)

pg_outbound = client.placement.group_create(
label,
get_region(test_linode_client, {"Placement Group"}),
PlacementGroupType.anti_affinity_local,
PlacementGroupPolicy.flexible,
)

linode = client.linode.instance_create(
"g6-nanode-1",
pg_outbound.region,
label=create_placement_group.label,
placement_group=pg_outbound,
)

pg_inbound = create_placement_group

# Says it could take up to ~6 hrs for migration to fully complete
send_request_when_resource_available(
300,
linode.initiate_migration,
placement_group=pg_inbound.id,
migration_type=MigrationType.COLD,
region=pg_inbound.region,
)

pg_inbound = test_linode_client.load(PlacementGroup, pg_inbound.id)
pg_outbound = test_linode_client.load(PlacementGroup, pg_outbound.id)

assert pg_inbound.migrations.inbound[0] == MigratedInstance(
linode_id=linode.id
)
assert pg_outbound.migrations.outbound[0] == MigratedInstance(
linode_id=linode.id
)

linode.delete()
pg_outbound.delete()
3 changes: 3 additions & 0 deletions test/unit/objects/placement_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from linode_api4 import PlacementGroupPolicy
from linode_api4.objects import (
MigratedInstance,
PlacementGroup,
PlacementGroupMember,
PlacementGroupType,
Expand Down Expand Up @@ -116,3 +117,5 @@ def validate_pg_123(self, pg: PlacementGroup):
assert pg.members[0] == PlacementGroupMember(
linode_id=123, is_compliant=True
)
assert pg.migrations.inbound[0] == MigratedInstance(linode_id=123)
assert pg.migrations.outbound[0] == MigratedInstance(linode_id=456)

0 comments on commit 3e7524c

Please sign in to comment.