Skip to content

Commit

Permalink
Deprecate facility in the favor of metro
Browse files Browse the repository at this point in the history
    Signed-off-by: Ayush Rangwala <ayush.rangwala@gmail.com>
  • Loading branch information
aayushrangwala committed Nov 12, 2023
1 parent dfbb595 commit ea215b8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
1 change: 1 addition & 0 deletions libcloud/compute/drivers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
"vcloud",
"vpsnet",
"onapp",
"equinixmetal"
]
43 changes: 20 additions & 23 deletions libcloud/compute/drivers/equinixmetal.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def ex_list_nodes_for_project(self, ex_project_id, include="plan", page=1, per_p
return list(map(self._to_node, data))

def list_locations(self):
data = self.connection.request("/metal/v1/facilities").object["facilities"]
data = self.connection.request("/metal/v1/locations/metros").object["metros"]
return list(map(self._to_location, data))

def list_images(self):
Expand Down Expand Up @@ -285,12 +285,15 @@ def create_node(
if not ex_project_id:
raise Exception("ex_project_id needs to be specified")

facility = location.extra["code"]
location_code = location.extra["code"]
if not self._valid_location:
raise ValueError("Failed to create node: valid parameter metro [code] is required in the input")

params = {
"hostname": name,
"plan": size.id,
"operating_system": image.id,
"facility": facility,
"metro": location_code,
"include": "plan",
"billing_cycle": "hourly",
}
Expand Down Expand Up @@ -453,6 +456,8 @@ def _to_node(self, data):
size = None
if "facility" in data:
extra["facility"] = data["facility"]
if "metro" in data and data["metro"] is not None:
extra["metro"] = data["metro"]

for key in extra_keys:
if key in data:
Expand Down Expand Up @@ -491,8 +496,8 @@ def _to_size(self, data):
except KeyError:
cpus = None
regions = [
region.get("href").replace("/metal/v1/facilities/", "")
for region in data.get("available_in", [])
region.get("href").replace("/metal/v1/locations/metros", "")
for region in data.get("available_in_metros", [])
]
extra = {
"description": data["description"],
Expand Down Expand Up @@ -675,7 +680,7 @@ def ex_request_address_reservation(
"quantity": quantity,
}
if location_id:
params["facility"] = location_id
params["metro"] = location_id
if comments:
params["comments"] = comments
if customdata:
Expand Down Expand Up @@ -746,10 +751,8 @@ def create_volume(
):
"""
Create a new volume.
:param size: Size of volume in gigabytes (required)
:type size: ``int``
:param location: Which data center to create a volume in. If
empty, undefined behavior will be selected.
(optional)
Expand Down Expand Up @@ -778,10 +781,8 @@ def create_volume(
def destroy_volume(self, volume):
"""
Destroys a storage volume.
:param volume: Volume to be destroyed
:type volume: :class:`StorageVolume`
:rtype: ``bool``
"""
path = "/metal/v1/storage/%s" % volume.id
Expand All @@ -791,13 +792,10 @@ def destroy_volume(self, volume):
def attach_volume(self, node, volume):
"""
Attaches volume to node.
:param node: Node to attach volume to.
:type node: :class:`.Node`
:param volume: Volume to attach.
:type volume: :class:`.StorageVolume`
:rytpe: ``bool``
"""
path = "/metal/v1/storage/%s/attachments" % volume.id
Expand All @@ -808,14 +806,11 @@ def attach_volume(self, node, volume):
def detach_volume(self, volume, ex_node=None, ex_attachment_id=""):
"""
Detaches a volume from a node.
:param volume: Volume to be detached
:type volume: :class:`.StorageVolume`
:param ex_attachment_id: Attachment id to be detached, if empty detach
all attachments
:type name: ``str``
:rtype: ``bool``
"""
path = "/metal/v1/storage/%s/attachments" % volume.id
Expand All @@ -841,10 +836,8 @@ def detach_volume(self, volume, ex_node=None, ex_attachment_id=""):
def create_volume_snapshot(self, volume, name=""):
"""
Create a new volume snapshot.
:param volume: Volume to create a snapshot for
:type volume: class:`StorageVolume`
:return: The newly created volume snapshot.
:rtype: :class:`VolumeSnapshot`
"""
Expand All @@ -856,10 +849,8 @@ def create_volume_snapshot(self, volume, name=""):
def destroy_volume_snapshot(self, snapshot):
"""
Delete a volume snapshot
:param snapshot: volume snapshot to delete
:type snapshot: class:`VolumeSnapshot`
:rtype: ``bool``
"""
volume_id = snapshot.extra["volume"]["href"].split("/")[-1]
Expand All @@ -870,10 +861,8 @@ def destroy_volume_snapshot(self, snapshot):
def list_volume_snapshots(self, volume, include=""):
"""
List snapshots for a volume.
:param volume: Volume to list snapshots for
:type volume: class:`StorageVolume`
:return: List of volume snapshots.
:rtype: ``list`` of :class: `VolumeSnapshot`
"""
Expand All @@ -885,7 +874,7 @@ def list_volume_snapshots(self, volume, include=""):
return list(map(self._to_volume_snapshot, data))

def _to_volume_snapshot(self, data):
created = datetime.datetime.strptime(data["created_at"], "%Y-%m-%dT%H:%M:%S")
created = datetime.strptime(data["created_at"], "%Y-%m-%dT%H:%M:%S")
return VolumeSnapshot(
id=data["id"],
name=data["id"],
Expand Down Expand Up @@ -941,6 +930,14 @@ def ex_describe_attachment(self, attachment_id):
data = self.connection.request(path).object
return data

def _valid_location(self, metro_code):
if metro_code == None or metro_code == "":
return False
metros = self.connection.request("/metal/v1/locations/metros").object["metros"]
for metro in metros:
if metro["code"] == metro_code:
return True
return False

class Project:
def __init__(self, project):
Expand Down
12 changes: 0 additions & 12 deletions libcloud/test/compute/fixtures/equinixmetal/facilities.json

This file was deleted.

10 changes: 10 additions & 0 deletions libcloud/test/compute/fixtures/equinixmetal/metros.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"metros": [
{
"id": "d3d6b29f-042d-43b7-b3ce-0bf53d5754ca",
"name": "Dallas",
"code": "da",
"country": "US"
}
]
}
4 changes: 2 additions & 2 deletions libcloud/test/compute/test_equinixmetal.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ def test_destroy_volume(self):
class EquinixMetalMockHttp(MockHttp):
fixtures = ComputeFileFixtures("equinixmetal")

def _metal_v1_facilities(self, method, url, body, headers):
body = self.fixtures.load("facilities.json")
def _metal_v1_metros(self, method, url, body, headers):
body = self.fixtures.load("metros.json")
return (httplib.OK, body, {}, httplib.responses[httplib.OK])

def _metal_v1_plans(self, method, url, body, headers):
Expand Down

0 comments on commit ea215b8

Please sign in to comment.