Skip to content
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

Resource Group Preparer Logging #17573

Merged
3 commits merged into from
Mar 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions tools/azure-sdk-tools/devtools_testutils/resource_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import logging
from functools import partial

from azure.core.exceptions import HttpResponseError

from azure_devtools.scenario_tests import AzureTestError, ReservedResourceNameError

from azure.mgmt.resource import ResourceManagementClient
Expand Down Expand Up @@ -40,7 +42,7 @@ def __init__(
playback_fake_resource=None,
client_kwargs=None,
random_name_enabled=False,
delete_after_tag_timedelta=datetime.timedelta(days=1),
delete_after_tag_timedelta=datetime.timedelta(hours=8),
):
super(ResourceGroupPreparer, self).__init__(
name_prefix,
Expand All @@ -67,22 +69,20 @@ def create_resource(self, name, **kwargs):
if self.is_live and self._need_creation:
self.client = self.create_mgmt_client(ResourceManagementClient)
parameters = {"location": self.location}
if self.delete_after_tag_timedelta:
expiry = datetime.datetime.utcnow() + self.delete_after_tag_timedelta
parameters["tags"] = {"DeleteAfter": expiry.replace(microsecond=0).isoformat()}
try:
if "tags" not in parameters.keys():
parameters["tags"] = {}
parameters["tags"]["BuildId"] = os.environ["BUILD_BUILDID"]
parameters["tags"]["BuildJob"] = os.environ["AGENT_JOBNAME"]
parameters["tags"]["BuildNumber"] = os.environ["BUILD_BUILDNUMBER"]
parameters["tags"]["BuildReason"] = os.environ["BUILD_REASON"]
except KeyError:
pass
expiry = datetime.datetime.utcnow() + self.delete_after_tag_timedelta
parameters["tags"] = {"DeleteAfter": expiry.replace(microsecond=0).isoformat()}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add these to the try block below with the other tags?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one doesn't need to be, the others do because when running locally those environment variables wont be set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so the there is no try get env variable in python to allow for these to be empty if they aren't found?

parameters["tags"]["BuildId"] = os.environ.get("BUILD_BUILDID", None)
parameters["tags"]["BuildJob"] = os.environ.get("AGENT_JOBNAME", None)
parameters["tags"]["BuildNumber"] = os.environ.get("BUILD_BUILDNUMBER", None)
parameters["tags"]["BuildReason"] = os.environ.get("BUILD_REASON", None)
try:
logging.info(
"Attempting to create a Resource Group with name {} and parameters {}".format(name, parameters)
)
# Prefixing all RGs created here with 'rgpy-' for tracing purposes
name = u"rgpy-" + name
if len(name) > 90:
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
name = name[:90]
self.resource = self.client.resource_groups.create_or_update(name, parameters)
except Exception as ex:
if "ReservedResourceName" in str(ex):
Expand All @@ -104,14 +104,16 @@ def remove_resource(self, name, **kwargs):
if self.is_live and self._need_creation:
try:
if "wait_timeout" in kwargs:
azure_poller = self.client.resource_groups.delete(name)
azure_poller = self.client.resource_groups.begin_delete(name)
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
azure_poller.wait(kwargs.get("wait_timeout"))
if azure_poller.done():
return
raise AzureTestError("Timed out waiting for resource group to be deleted.")
else:
self.client.resource_groups.delete(name, polling=False)
except Exception:
self.client.resource_groups.begin_delete(name, polling=False)
except HttpResponseError as err:
logging.info("Failed to delete resource group with name {}".format(name))
logging.info(err.with_traceback)
pass


Expand Down