Skip to content

Commit

Permalink
Replaced match-case to if-else for supporting python3.8. Rename field…
Browse files Browse the repository at this point in the history
…s in rrset and zone.
  • Loading branch information
dchudik authored Jan 24, 2024
1 parent efdac0a commit 607f122
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 129 deletions.
65 changes: 31 additions & 34 deletions octodns_selectel/v2/dns_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def __init__(self, library_version: str, openstack_token: str):
)

@classmethod
def _rrset_path(cls, zone_uuid):
return cls.__rrsets_path.format(zone_uuid)
def _rrset_path(cls, zone_id):
return cls.__rrsets_path.format(zone_id)

@classmethod
def _rrset_path_specific(cls, zone_uuid, rrset_uuid):
return cls.__rrsets_path_specific.format(zone_uuid, rrset_uuid)
def _rrset_path_specific(cls, zone_id, rrset_id):
return cls.__rrsets_path_specific.format(zone_id, rrset_id)

def _request(self, method, path, params=None, data=None):
url = f'{self.API_URL}{path}'
Expand All @@ -38,28 +38,25 @@ def _request(self, method, path, params=None, data=None):
resp_json = resp.json()
except ValueError:
resp_json = {}
match resp.status_code:
case 200 | 201 | 204:
return resp_json
case 400 | 422:
raise ApiException(
f'Bad request. Description: {resp_json.get("description", "Invalid payload")}.'
)
case 401:
raise ApiException(
'Authorization failed. Invalid or empty token.'
)
case 404:
raise ApiException(
'Resource not found: '
f'{resp_json.get("error", "invalid path")}.'
)
case 409:
raise ApiException(
f'Conflict: {resp_json.get("error", "resource maybe already created")}.'
)
case _:
raise ApiException('Internal server error.')
if resp.status_code in {200, 201, 204}:
return resp_json
elif resp.status_code in {400, 422}:
raise ApiException(
f'Bad request. Description: {resp_json.get("description", "Invalid payload")}.'
)
elif resp.status_code == 401:
raise ApiException('Authorization failed. Invalid or empty token.')
elif resp.status_code == 404:
raise ApiException(
'Resource not found: '
f'{resp_json.get("error", "invalid path")}.'
)
elif resp.status_code == 409:
raise ApiException(
f'Conflict: {resp_json.get("error", "resource maybe already created")}.'
)
else:
raise ApiException('Internal server error.')

def _request_all_entities(self, path, offset=0) -> list[int]:
items = []
Expand All @@ -77,18 +74,18 @@ def list_zones(self):
def create_zone(self, name):
return self._request('POST', self._zone_path, data=dict(name=name))

def list_rrsets(self, zone_uuid):
path = self._rrset_path(zone_uuid)
def list_rrsets(self, zone_id):
path = self._rrset_path(zone_id)
return self._request_all_entities(path)

def create_rrset(self, zone_uuid, data):
path = self._rrset_path(zone_uuid)
def create_rrset(self, zone_id, data):
path = self._rrset_path(zone_id)
return self._request('POST', path, data=data)

def update_rrset(self, zone_uuid, rrset_uuid, data):
path = self._rrset_path_specific(zone_uuid, rrset_uuid)
def update_rrset(self, zone_id, rrset_id, data):
path = self._rrset_path_specific(zone_id, rrset_id)
return self._request('PATCH', path, data=data)

def delete_rrset(self, zone_uuid, rrset_uuid):
path = self._rrset_path_specific(zone_uuid, rrset_uuid)
def delete_rrset(self, zone_id, rrset_id):
path = self._rrset_path_specific(zone_id, rrset_id)
return self._request('DELETE', path)
4 changes: 2 additions & 2 deletions octodns_selectel/v2/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _get_rrset_id(self, zone_name, rrset_type, rrset_name):
and rrset["name"] == rrset_name,
self._zone_rrsets[zone_name],
)
)["uuid"]
)["id"]

def _apply_create(self, zone_id, change):
new_record = change.new
Expand Down Expand Up @@ -124,7 +124,7 @@ def populate(self, zone, target=False, lenient=False):
self.log.info('populate: found %s records', len(zone.records) - before)

def _get_zone_id_by_name(self, zone_name):
return self._zones.get(zone_name, False)["uuid"]
return self._zones.get(zone_name, False)["id"]

def create_zone(self, name):
self.log.debug('Create zone: %s', name)
Expand Down
Loading

0 comments on commit 607f122

Please sign in to comment.