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

Add set_delete_after_tag as a parameter on ResourceGroupPreparer #11749

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
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)