Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Refactor code for unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
yufangzhang committed Jul 20, 2016
1 parent 20340e7 commit 38d2c75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
24 changes: 4 additions & 20 deletions bootstrap_cfn/fab_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,25 +397,26 @@ def set_stack_name():
"""
# create a stack id
r53_conn = get_connection(R53)
zone_name = get_zone_name()
zone_id = get_zone_id()
stack_suffix = uuid.uuid4().__str__()[-8:]
if hasattr(env, 'tag'):
if env.tag == 'active':
raise ActiveTagExistConflictError(stack_suffix)
elif hastag(env.tag):
elif r53_conn.hastag(zone_name, zone_id, get_tag_record_name(env.tag)):
raise TagRecordExistConflictError(env.tag)
else:
stack_tag = env.tag
else:
stack_tag = stack_suffix
env.tag = stack_tag
zone_id = get_zone_id()
record = "{}.{}".format(get_tag_record_name(stack_tag), get_zone_name())
logger.info("fab_tasks::set_stack_name: "
"Creating stack suffix {} "
"for record '{}' "
"in zone id '{}'...".format(stack_suffix, record, zone_id))
# Let DNS update DNSServerError propogate
r53_conn = get_connection(R53)
try:
r53_conn.update_dns_record(zone_id, record, 'TXT', '"{0}"'.format(stack_suffix))
env.stack_name = "{0}-{1}".format(get_legacy_name(), stack_suffix)
Expand All @@ -424,22 +425,6 @@ def set_stack_name():
return env.stack_name


def hastag(stack_tag):
"""
Check if stack_tag is in use
Args:
stack_tag: the tag of stack
Returns:
String if stack exists
None if not.
"""
r53_conn = get_connection(R53)
zone_id = get_zone_id()
record_name = get_tag_record_name(stack_tag)
hasrecord = r53_conn.get_record(get_zone_name(), zone_id, record_name, 'TXT')
return hasrecord


def get_zone_name():
zone_name = get_basic_config().get('master_zone', None)
if not zone_name:
Expand Down Expand Up @@ -552,7 +537,6 @@ def cfn_delete(force=False, pre_delete_callbacks=None):
stack_id = stack_name.split('-')[-1]
zone_name = get_zone_name()
zone_id = r53_conn.get_hosted_zone_id(zone_name)
elb = get_first_public_elb()
if hasattr(env, "tag") and env.tag != 'active':
# delete inactive stack
stack_tag = env.tag
Expand Down
14 changes: 14 additions & 0 deletions bootstrap_cfn/r53.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,17 @@ def get_full_record(self, zone_name, zone_id, record_name, record_type):
if rr.type == record_type and rr.name == record_fqdn:
return rr
return None

def hastag(self, zone_name, zone_id, record_name):
"""
Check if stack_tag is in use
Args:
zone_name:
zone_id:
record_name:
Returns:
String if stack exists
None if not.
"""
hasrecord = self.get_record(zone_name, zone_id, record_name, 'TXT')
return hasrecord

0 comments on commit 38d2c75

Please sign in to comment.