diff --git a/salt/modules/boto_sns.py b/salt/modules/boto_sns.py index 4df7d80603..2326dd6739 100644 --- a/salt/modules/boto_sns.py +++ b/salt/modules/boto_sns.py @@ -24,6 +24,7 @@ def __virtual__(): return False return True + def get_all_topics(region=None, key=None, keyid=None, profile=None): cache_key = 'boto_sns.topics_cache' try: @@ -34,12 +35,13 @@ def get_all_topics(region=None, key=None, keyid=None, profile=None): conn = _get_conn(region, key, keyid, profile) __context__[cache_key] = {} # TODO: support >100 SNS topics (via NextToken) - for t in conn.get_all_topics()['ListTopicsResponse']\ - ['ListTopicsResult']['Topics']: + topics = conn.get_all_topics() + for t in topics['ListTopicsResponse']['ListTopicsResult']['Topics']: short_name = t['TopicArn'].split(':')[-1] __context__[cache_key][short_name] = t['TopicArn'] return __context__[cache_key] + def exists(name, region=None, key=None, keyid=None, profile=None): ''' Check to see if an SNS topic exists. @@ -68,6 +70,7 @@ def create(name, region=None, key=None, keyid=None, profile=None): log.info('Created SNS topic {0}'.format(name)) return True + def delete(name, region=None, key=None, keyid=None, profile=None): ''' Delete an SNS topic. @@ -81,11 +84,13 @@ def delete(name, region=None, key=None, keyid=None, profile=None): log.info('Deleted SNS topic {0}'.format(name)) return True + def get_arn(name, region=None, key=None, keyid=None, profile=None): if name.startswith('arn:aws:sns:'): return name account_id = __salt__['boto_iam.get_account_id']() - return 'arn:aws:sns:{}:{}:{}'.format(_get_region(region), account_id, name) + return 'arn:aws:sns:{0}:{1}:{2}'.format(_get_region(region), account_id, name) + def _get_region(region=None): if not region and __salt__['config.option']('sns.region'): @@ -94,6 +99,7 @@ def _get_region(region=None): region = 'us-east-1' return region + def _get_conn(region, key, keyid, profile): ''' Get a boto connection to SNS. @@ -117,4 +123,4 @@ def _get_conn(region, key, keyid, profile): conn = boto.sns.connect_to_region(region, aws_access_key_id=keyid, aws_secret_access_key=key) - return conn \ No newline at end of file + return conn diff --git a/salt/states/boto_sns.py b/salt/states/boto_sns.py index 4419729625..38d9871f58 100644 --- a/salt/states/boto_sns.py +++ b/salt/states/boto_sns.py @@ -56,12 +56,14 @@ passed in as a dict, or as a string to pull from pillars or minion config: ''' from __future__ import absolute_import + def __virtual__(): ''' Only load if boto is available. ''' return 'boto_sns' if 'boto_sns.exists' in __salt__ else False + def present( name, region=None, @@ -89,7 +91,6 @@ def present( ''' ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} - is_present = __salt__['boto_sns.exists'](name, region, key, keyid, profile) if is_present: ret['comment'] = 'AWS SNS topic {0} present.'.format(name) diff --git a/tests/integration/modules/boto_sns.py b/tests/integration/modules/boto_sns.py index 5b718e02f5..3ffc994548 100644 --- a/tests/integration/modules/boto_sns.py +++ b/tests/integration/modules/boto_sns.py @@ -55,7 +55,6 @@ class BotoSNSTest(integration.ModuleCase): self.assertIn('my-second-test-topic', ret.keys()) self.assertIn(self._get_arn('my-second-test-topic'), ret.values()) - def _get_arn(self, name): return 'arn:aws:sns:us-east-1:{}:{}'.format(self.account_id, name) diff --git a/tests/integration/states/boto_sns.py b/tests/integration/states/boto_sns.py index 38a330588f..8d5e827b61 100644 --- a/tests/integration/states/boto_sns.py +++ b/tests/integration/states/boto_sns.py @@ -44,17 +44,21 @@ class BotoSNSTest(integration.ModuleCase, name='my-state-test-topic') self.assertSaltTrueReturn(ret) self.assertInSaltReturn('my-state-test-topic', ret, 'name') - self.assertInSaltComment('AWS SNS topic my-state-test-topic created.', ret) - self.assertSaltStateChangesEqual(ret, {'old': None, 'new': {'topic': 'my-state-test-topic'}}) + self.assertInSaltComment('AWS SNS topic my-state-test-topic created.', + ret) + self.assertSaltStateChangesEqual( + ret, {'old': None, 'new': {'topic': 'my-state-test-topic'}} + ) def test_present_already_exist(self): self.run_state('boto_sns.present', name='my-state-test-topic') ret = self.run_state('boto_sns.present', - name='my-state-test-topic') + name='my-state-test-topic') self.assertSaltTrueReturn(ret) self.assertInSaltReturn('my-state-test-topic', ret, 'name') - self.assertInSaltComment('AWS SNS topic my-state-test-topic present.', ret) + self.assertInSaltComment('AWS SNS topic my-state-test-topic present.', + ret) self.assertSaltStateChangesEqual(ret, {}) def test_present_test_mode(self): @@ -63,7 +67,8 @@ class BotoSNSTest(integration.ModuleCase, test=True) self.assertSaltNoneReturn(ret) self.assertInSaltReturn('my-state-test-topic', ret, 'name') - self.assertInSaltComment('AWS SNS topic my-state-test-topic is set to be created.', ret) + self.assertInSaltComment( + 'AWS SNS topic my-state-test-topic is set to be created.', ret) self.assertSaltStateChangesEqual(ret, {}) ret = self.run_function('boto_sns.exists', name='my-state-test-topic') self.assertFalse(ret) @@ -73,7 +78,8 @@ class BotoSNSTest(integration.ModuleCase, name='my-state-test-topic') self.assertSaltTrueReturn(ret) self.assertInSaltReturn('my-state-test-topic', ret, 'name') - self.assertInSaltComment('AWS SNS topic my-state-test-topic does not exist.', ret) + self.assertInSaltComment( + 'AWS SNS topic my-state-test-topic does not exist.', ret) self.assertSaltStateChangesEqual(ret, {}) def test_absent_already_exists(self): @@ -83,18 +89,20 @@ class BotoSNSTest(integration.ModuleCase, name='my-state-test-topic') self.assertSaltTrueReturn(ret) self.assertInSaltReturn('my-state-test-topic', ret, 'name') - self.assertInSaltComment('AWS SNS topic my-state-test-topic does not exist.', ret) - self.assertSaltStateChangesEqual(ret, {'new': None, 'old': {'topic': 'my-state-test-topic'}}) + self.assertInSaltComment( + 'AWS SNS topic my-state-test-topic does not exist.', ret) + self.assertSaltStateChangesEqual( + ret, {'new': None, 'old': {'topic': 'my-state-test-topic'}}) def test_absent_test_mode(self): - self.run_state('boto_sns.present', - name='my-state-test-topic') + self.run_state('boto_sns.present', name='my-state-test-topic') ret = self.run_state('boto_sns.absent', name='my-state-test-topic', test=True) self.assertSaltNoneReturn(ret) self.assertInSaltReturn('my-state-test-topic', ret, 'name') - self.assertInSaltComment('AWS SNS topic my-state-test-topic is set to be removed.', ret) + self.assertInSaltComment( + 'AWS SNS topic my-state-test-topic is set to be removed.', ret) self.assertSaltStateChangesEqual(ret, {}) ret = self.run_function('boto_sns.exists', name='my-state-test-topic') self.assertTrue(ret)