Skip to content

Commit

Permalink
Add set_delete_after_tag as a parameter on ResourceGroupPreparer (#11749
Browse files Browse the repository at this point in the history
)

* Add set_delete_after_tag as a parameter on ResourceGroupPreparer so that test-prepared RGs can be automatically cleaned up by engsys.
* Populate this into the two "default helper RG preparers" (randomized/cached) to implicitly opt-in the tests consuming those into the best practice. (Currently this is a very limited set of SDKs)
* Make delete_after_tag take a timespan and be enabled by default.
  • Loading branch information
KieranBrantnerMagee authored Jun 23, 2020
1 parent 18f13aa commit f3522cd
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions tools/azure-sdk-tools/devtools_testutils/resource_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from collections import namedtuple
import functools
import os
import datetime
from functools import partial

from azure_devtools.scenario_tests import AzureTestError, ReservedResourceNameError
Expand Down Expand Up @@ -33,7 +34,8 @@ def __init__(self, name_prefix='',
parameter_name_for_location='location', location='westus',
disable_recording=True, playback_fake_resource=None,
client_kwargs=None,
random_name_enabled=False):
random_name_enabled=False,
delete_after_tag_timedelta=datetime.timedelta(days=1)):
super(ResourceGroupPreparer, self).__init__(name_prefix, random_name_length,
disable_recording=disable_recording,
playback_fake_resource=playback_fake_resource,
Expand All @@ -50,13 +52,18 @@ def __init__(self, name_prefix='',
if self.random_name_enabled:
self.resource_moniker = self.name_prefix + "rgname"
self.set_cache(use_cache, parameter_name)
self.delete_after_tag_timedelta = delete_after_tag_timedelta

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.isoformat()}
try:
self.resource = self.client.resource_groups.create_or_update(
name, {'location': self.location}
name, parameters
)
except Exception as ex:
if "ReservedResourceName" in str(ex):
Expand Down Expand Up @@ -92,4 +99,4 @@ def remove_resource(self, name, **kwargs):
pass

RandomNameResourceGroupPreparer = partial(ResourceGroupPreparer, random_name_enabled=True)
CachedResourceGroupPreparer = functools.partial(ResourceGroupPreparer, use_cache=True, random_name_enabled=True)
CachedResourceGroupPreparer = partial(ResourceGroupPreparer, use_cache=True, random_name_enabled=True)

0 comments on commit f3522cd

Please sign in to comment.