-
Notifications
You must be signed in to change notification settings - Fork 1
feat: waiting functionality for DNS service #40
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
Closed
Closed
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f2af4a9
adds waiting for dns
MelvinKl e673231
linting
MelvinKl 850bd36
improve tests
MelvinKl 2b24e82
made tests better
MelvinKl 6c32aff
zone tests completed
MelvinKl 19567b4
wip
MelvinKl bbcadee
Merge remote-tracking branch 'origin/main' into feature/waiter
MelvinKl fa81d18
adds full test for dns wait
MelvinKl 1ab81cd
adds example for waiting
MelvinKl 560bb86
adjust core version
MelvinKl 2de317e
adjusted for new wait config in core
MelvinKl 997f1dd
adjust example
MelvinKl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
|
||
from stackit.core.configuration import Configuration | ||
from stackit.dns.api.default_api import DefaultApi | ||
from stackit.dns.models.create_record_set_payload import CreateRecordSetPayload | ||
from stackit.dns.models.create_zone_payload import CreateZonePayload | ||
from stackit.dns.models.record_payload import RecordPayload | ||
from stackit.dns.wait import ( | ||
wait_for_create_recordset, | ||
wait_for_create_zone, | ||
wait_for_delete_recordset, | ||
wait_for_delete_zone, | ||
) | ||
|
||
project_id = os.getenv("PROJECT_ID") | ||
|
||
# Create a new API client, that uses default authentication and configuration | ||
config = Configuration() | ||
client = DefaultApi(config) | ||
|
||
|
||
# Create a new DNS zone | ||
create_zone_response = client.create_zone( | ||
create_zone_payload=CreateZonePayload(name="myZone", dnsName="testZone.com"), project_id=project_id | ||
) | ||
zone_id = create_zone_response.zone.id | ||
|
||
# Wait for the zone to be fully created | ||
wait_for_zone_response = wait_for_create_zone(client, project_id, zone_id) | ||
|
||
# Create a DNS record in the newly created DNS zone | ||
create_record_response = client.create_record_set( | ||
project_id, | ||
zone_id, | ||
CreateRecordSetPayload( | ||
name="a", | ||
records=[ | ||
RecordPayload(content="192.168.0.1"), # Will go nowhere | ||
], | ||
type="A", | ||
), | ||
) | ||
rr_set_id = create_record_response.rrset.id | ||
|
||
# Wait for the DNS record to be fully created | ||
wait_for_rr_set_record_response = wait_for_create_recordset(client, project_id, zone_id, rr_set_id) | ||
|
||
# delete newly created recordset | ||
delete_rr_set_message = client.delete_record_set(project_id, zone_id, rr_set_id) | ||
|
||
# Wait for rr_set deletion | ||
wait_for_rr_set_deletion_response = wait_for_delete_recordset(client, project_id, zone_id, rr_set_id) | ||
|
||
# Delete newly created DNS zone | ||
delete_zone_message = client.delete_zone(project_id, zone_id) | ||
|
||
# Wait for deletion of DNS zone | ||
wait_for_zone_deletion_response = wait_for_delete_zone(client, project_id, zone_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
from enum import Enum | ||
from typing import Any, Tuple, Union | ||
|
||
from stackit.core.wait import Wait | ||
|
||
from stackit.dns.api.default_api import DefaultApi | ||
from stackit.dns.exceptions import ApiException | ||
from stackit.dns.models.record_set_response import RecordSetResponse | ||
from stackit.dns.models.zone_response import ZoneResponse | ||
|
||
|
||
class _States(str, Enum): | ||
CreateSuccess = "CREATE_SUCCEEDED" | ||
CreateFail = "CREATE_FAILED" | ||
UpdateSuccess = "UPDATE_SUCCEEDED" | ||
UpdateFail = "UPDATE_FAILED" | ||
DeleteSuccess = "DELETE_SUCCEEDED" | ||
DeleteFail = "DELETE_FAILED" | ||
|
||
|
||
def wait_for_create_zone( | ||
api_client: DefaultApi, | ||
project_id: str, | ||
zone_id: str, | ||
sleep_before_wait: int = 0, | ||
throttle: int = 5, | ||
timeout: int = 30, | ||
temp_error_retry_limit: int = 5, | ||
) -> ZoneResponse: | ||
|
||
def get_zone_execute_state() -> Tuple[bool, Union[Exception, None], Union[int, None], Any]: | ||
|
||
nonlocal api_client, project_id, zone_id | ||
|
||
try: | ||
response = api_client.get_zone(project_id, zone_id) | ||
if response.zone.id != zone_id: | ||
return False, ValueError("ID of zone in return not equal to ID of requested zone."), None, None | ||
elif response.zone.state == _States.CreateSuccess: | ||
return True, None, None, response | ||
elif response.zone.state == _States.CreateFail: | ||
return True, Exception("Create failed for zone with ID %s" % zone_id), None, response | ||
else: | ||
return False, None, None, None | ||
except ApiException as e: | ||
return False, e, e.status, None | ||
except Exception as e: | ||
return False, e, None, None | ||
|
||
wait = Wait( | ||
get_zone_execute_state, | ||
sleep_before_wait=sleep_before_wait, | ||
throttle=throttle, | ||
timeout=timeout, | ||
temp_error_retry_limit=temp_error_retry_limit, | ||
) | ||
return wait.wait() | ||
|
||
|
||
def wait_for_partial_update_zone( | ||
api_client: DefaultApi, | ||
project_id: str, | ||
zone_id: str, | ||
sleep_before_wait: int = 0, | ||
throttle: int = 5, | ||
timeout: int = 30, | ||
temp_error_retry_limit: int = 5, | ||
) -> ZoneResponse: | ||
|
||
def get_zone_execute_state() -> Tuple[bool, Union[Exception, None], Union[int, None], Any]: | ||
|
||
nonlocal api_client, project_id, zone_id | ||
|
||
try: | ||
response = api_client.get_zone(project_id, zone_id) | ||
if response.zone.id != zone_id: | ||
return False, ValueError("ID of zone in return not equal to ID of requested zone."), None, None | ||
elif response.zone.state == _States.UpdateSuccess: | ||
return True, None, None, response | ||
elif response.zone.state == _States.UpdateFail: | ||
return True, Exception("Update failed for zone with ID %s" % zone_id), None, response | ||
else: | ||
return False, None, None, None | ||
except ApiException as e: | ||
return False, e, e.status, None | ||
except Exception as e: | ||
return False, e, None, None | ||
|
||
wait = Wait( | ||
get_zone_execute_state, | ||
sleep_before_wait=sleep_before_wait, | ||
throttle=throttle, | ||
timeout=timeout, | ||
temp_error_retry_limit=temp_error_retry_limit, | ||
) | ||
return wait.wait() | ||
|
||
|
||
def wait_for_delete_zone( | ||
api_client: DefaultApi, | ||
project_id: str, | ||
zone_id: str, | ||
sleep_before_wait: int = 0, | ||
throttle: int = 5, | ||
timeout: int = 30, | ||
temp_error_retry_limit: int = 5, | ||
) -> ZoneResponse: | ||
|
||
def get_zone_execute_state() -> Tuple[bool, Union[Exception, None], Union[int, None], Any]: | ||
|
||
nonlocal api_client, project_id, zone_id | ||
|
||
try: | ||
response = api_client.get_zone(project_id, zone_id) | ||
if response.zone.id != zone_id: | ||
return False, ValueError("ID of zone in return not equal to ID of requested zone."), None, None | ||
elif response.zone.state == _States.DeleteSuccess: | ||
return True, None, None, response | ||
elif response.zone.state == _States.DeleteFail: | ||
return True, Exception("Delete failed for zone with ID %s" % zone_id), None, response | ||
else: | ||
return False, None, None, None | ||
except ApiException as e: | ||
return False, e, e.status, None | ||
except Exception as e: | ||
return False, e, None, None | ||
|
||
wait = Wait( | ||
get_zone_execute_state, | ||
sleep_before_wait=sleep_before_wait, | ||
throttle=throttle, | ||
timeout=timeout, | ||
temp_error_retry_limit=temp_error_retry_limit, | ||
) | ||
return wait.wait() | ||
|
||
|
||
def wait_for_create_recordset( | ||
api_client: DefaultApi, | ||
project_id: str, | ||
zone_id: str, | ||
rr_set_id: str, | ||
sleep_before_wait: int = 0, | ||
throttle: int = 5, | ||
timeout: int = 30, | ||
temp_error_retry_limit: int = 5, | ||
) -> RecordSetResponse: | ||
|
||
def get_rr_set_execute_state() -> Tuple[bool, Union[Exception, None], Union[int, None], Any]: | ||
|
||
nonlocal api_client, project_id, zone_id, rr_set_id | ||
|
||
try: | ||
response = api_client.get_record_set(project_id, zone_id, rr_set_id) | ||
if response.rrset.id != rr_set_id: | ||
return False, ValueError("ID of rrset in return not equal to ID of requested rrset."), None, None | ||
elif response.rrset.state == _States.CreateSuccess: | ||
return True, None, None, response | ||
elif response.rrset.state == _States.CreateFail: | ||
return True, Exception("Create failed for rrset with ID %s" % rr_set_id), None, response | ||
else: | ||
return False, None, None, None | ||
except ApiException as e: | ||
return False, e, e.status, None | ||
except Exception as e: | ||
return False, e, None, None | ||
|
||
wait = Wait( | ||
get_rr_set_execute_state, | ||
sleep_before_wait=sleep_before_wait, | ||
throttle=throttle, | ||
timeout=timeout, | ||
temp_error_retry_limit=temp_error_retry_limit, | ||
) | ||
return wait.wait() | ||
|
||
|
||
def wait_for_partial_update_recordset( | ||
api_client: DefaultApi, | ||
project_id: str, | ||
zone_id: str, | ||
rr_set_id: str, | ||
sleep_before_wait: int = 0, | ||
throttle: int = 5, | ||
timeout: int = 30, | ||
temp_error_retry_limit: int = 5, | ||
) -> RecordSetResponse: | ||
|
||
def get_rr_set_execute_state() -> Tuple[bool, Union[Exception, None], Union[int, None], Any]: | ||
|
||
nonlocal api_client, project_id, zone_id, rr_set_id | ||
|
||
try: | ||
response = api_client.get_record_set(project_id, zone_id, rr_set_id) | ||
if response.rrset.id != rr_set_id: | ||
return False, ValueError("ID of rrset in return not equal to ID of requested rrset."), None, None | ||
elif response.rrset.state == _States.UpdateSuccess: | ||
return True, None, None, response | ||
elif response.rrset.state == _States.UpdateFail: | ||
return True, Exception("Update failed for rrset with ID %s" % rr_set_id), None, response | ||
else: | ||
return False, None, None, None | ||
except ApiException as e: | ||
return False, e, e.status, None | ||
except Exception as e: | ||
return False, e, None, None | ||
|
||
wait = Wait( | ||
get_rr_set_execute_state, | ||
sleep_before_wait=sleep_before_wait, | ||
throttle=throttle, | ||
timeout=timeout, | ||
temp_error_retry_limit=temp_error_retry_limit, | ||
) | ||
return wait.wait() | ||
|
||
|
||
def wait_for_delete_recordset( | ||
api_client: DefaultApi, | ||
project_id: str, | ||
zone_id: str, | ||
rr_set_id: str, | ||
sleep_before_wait: int = 0, | ||
throttle: int = 5, | ||
timeout: int = 30, | ||
temp_error_retry_limit: int = 5, | ||
) -> RecordSetResponse: | ||
|
||
def get_rr_set_execute_state() -> Tuple[bool, Union[Exception, None], Union[int, None], Any]: | ||
|
||
nonlocal api_client, project_id, zone_id, rr_set_id | ||
|
||
try: | ||
response = api_client.get_record_set(project_id, zone_id, rr_set_id) | ||
if response.rrset.id != rr_set_id: | ||
return False, ValueError("ID of rrset in return not equal to ID of requested rrset."), None, None | ||
elif response.rrset.state == _States.DeleteSuccess: | ||
return True, None, None, response | ||
elif response.rrset.state == _States.DeleteFail: | ||
return True, Exception("Delete failed for rrset with ID %s" % rr_set_id), None, response | ||
else: | ||
return False, None, None, None | ||
except ApiException as e: | ||
return False, e, e.status, None | ||
except Exception as e: | ||
return False, e, None, None | ||
|
||
wait = Wait( | ||
get_rr_set_execute_state, | ||
sleep_before_wait=sleep_before_wait, | ||
throttle=throttle, | ||
timeout=timeout, | ||
temp_error_retry_limit=temp_error_retry_limit, | ||
) | ||
return wait.wait() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a generic example to demonstrate the
wait
functionality, I think it would be enough to have a single standard wait case (e.g. thewait_for_create_zone(client, project_id, zone_id)
) and then add some examples to configure timeout, sleep before wait, etc