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 3 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
11 changes: 9 additions & 2 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 All @@ -28,6 +29,7 @@
class ResourceGroupPreparer(AzureMgmtPreparer):
def __init__(self, name_prefix='',
use_cache=False,
delete_after_tag_timedelta=datetime.timedelta(days=1),
random_name_length=75,
parameter_name=RESOURCE_GROUP_PARAM,
parameter_name_for_location='location', location='westus',
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)