Skip to content

Commit

Permalink
Merge pull request #501 from cloudify-cosmo/add-some-good-stuff
Browse files Browse the repository at this point in the history
add some good stuff
  • Loading branch information
EarthmanT authored Dec 27, 2022
2 parents 8008fac + b2906a7 commit ecf9774
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 89 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
3.1.3: Add account ID property to roles in precreate.
3.1.2:
- RD-6237 Calculated drift in non-JSON serializable format
3.1.1:
Expand Down
11 changes: 10 additions & 1 deletion cloudify_aws/common/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ def __init__(self, node, aws_config=None):
'No plugin properties were provided. '
'Defaulting client_config credentials.')

def get_sts_client(self, config):
return boto3.client("sts", **config)

def get_sts_credentials(self, role, config):
sts_client = boto3.client("sts", **config)
sts_client = self.get_sts_client(config)

sts_credentials = sts_client.assume_role(
RoleArn=role,
Expand All @@ -103,6 +106,12 @@ def get_sts_credentials(self, role, config):
"region_name": self.aws_config["region_name"]
}

def get_account_id(self):
sts_client = self.get_sts_client(self.aws_config)
caller_id = sts_client.get_caller_identity()
if 'Account' in caller_id:
return caller_id['Account']

def client(self, service_name):
'''
Builds an AWS connection client
Expand Down
5 changes: 4 additions & 1 deletion cloudify_aws/common/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,10 @@ def _prepare_create_raises_UnknownServiceError(
)
)

fake_boto.assert_called_with(type_name, **CLIENT_CONFIG)
if type_name == 'iam':
return fake_boto
else:
fake_boto.assert_called_with(type_name, **CLIENT_CONFIG)

def _create_common_relationships(self,
node_id,
Expand Down
1 change: 1 addition & 0 deletions cloudify_aws/common/tests/test_iface_requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def perform_operation(self, operation_callable, args, kwargs):
@patch('cloudify_aws.common.decorators._wait_for_delete')
@patch('cloudify_aws.common.AWSResourceBase.make_client_call')
@patch('cloudify_aws.common.connection.Boto3Connection.client')
@patch('cloudify_aws.common.connection.Boto3Connection.get_account_id')
@patch('cloudify.context.CloudifyContext._verify_in_relationship_context')
def test_iface_requirement(self, *_):
plugin_yaml = self.get_plugin_yaml()
Expand Down
1 change: 1 addition & 0 deletions cloudify_aws/iam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, ctx_node, resource_id=None, client=None, logger=None):
AWSResourceBase.__init__(
self, client or Boto3Connection(ctx_node).client('iam'),
resource_id=resource_id, logger=logger)
self.account_id = Boto3Connection(ctx_node).get_account_id()

@property
def properties(self):
Expand Down
6 changes: 6 additions & 0 deletions cloudify_aws/iam/resources/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ def validate_polices(policies, policies_from_properties):
return policies


@decorators.aws_resource(IAMRole, RESOURCE_TYPE, waits_for_status=False)
@decorators.aws_params(RESOURCE_NAME)
def precreate(ctx, iface, **_):
ctx.instance.runtime_properties['account_id'] = iface.account_id


@decorators.aws_resource(IAMRole, RESOURCE_TYPE, waits_for_status=False)
@decorators.aws_params(RESOURCE_NAME)
def create(ctx, iface, resource_config, params, **_):
Expand Down
35 changes: 21 additions & 14 deletions cloudify_aws/iam/tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@
'aws_resource_id': 'group_name_id',
'resource_config': {}
}
ctx_node = MagicMock(
properties=NODE_PROPERTIES,
plugin=MagicMock(properties={})
)


@patch('cloudify_aws.common.connection.ctx')
@patch('cloudify_aws.common.connection.Boto3Connection.get_account_id')
class TestIAMGroup(TestBase):

def setUp(self):
Expand All @@ -67,14 +73,15 @@ def tearDown(self):

super(TestIAMGroup, self).tearDown()

def test_create_raises_UnknownServiceError(self):
self._prepare_create_raises_UnknownServiceError(
def test_create_raises_UnknownServiceError(self, *_):
fake_boto = self._prepare_create_raises_UnknownServiceError(
type_hierarchy=GROUP_TH,
type_name='iam',
type_class=group
)
fake_boto.assert_called_with('iam')

def test_create(self):
def test_create(self, *_):
_ctx = self.get_mock_ctx(
'test_create',
test_properties=NODE_PROPERTIES,
Expand All @@ -94,7 +101,7 @@ def test_create(self):

group.create(ctx=_ctx, resource_config=None, iface=None, params=None)

self.fake_boto.assert_called_with('iam', **CLIENT_CONFIG)
self.fake_boto.assert_called_with('iam')

self.fake_client.create_group.assert_called_with(
GroupName='group_name_id', Path='some_path'
Expand All @@ -105,7 +112,7 @@ def test_create(self):
RUNTIME_PROPERTIES_AFTER_CREATE
)

def test_delete(self):
def test_delete(self, *_):
_ctx = self.get_mock_ctx(
'test_delete',
test_properties=NODE_PROPERTIES,
Expand All @@ -120,7 +127,7 @@ def test_delete(self):

group.delete(ctx=_ctx, resource_config=None, iface=None)

self.fake_boto.assert_called_with('iam', **CLIENT_CONFIG)
self.fake_boto.assert_called_with('iam')

self.fake_client.delete_group.assert_called_with(
GroupName='group_name_id'
Expand All @@ -133,7 +140,7 @@ def test_delete(self):
}
)

def test_attach_to_User(self):
def test_attach_to_User(self, *_):
_source_ctx, _target_ctx, _ctx = self._create_common_relationships(
'test_attach_to',
GROUP_TH,
Expand Down Expand Up @@ -161,7 +168,7 @@ def test_attach_to_User(self):
}
)

def test_detach_from_User(self):
def test_detach_from_User(self, *_):
_source_ctx, _target_ctx, _ctx = self._create_common_relationships(
'test_detach_from',
GROUP_TH,
Expand Down Expand Up @@ -191,7 +198,7 @@ def test_detach_from_User(self):
}
)

def test_detach_from_Policy(self):
def test_detach_from_Policy(self, *_):
_source_ctx, _target_ctx, _ctx = self._create_common_relationships(
'test_detach_from',
GROUP_TH,
Expand Down Expand Up @@ -219,7 +226,7 @@ def test_detach_from_Policy(self):
}
)

def test_attach_to_Policy(self):
def test_attach_to_Policy(self, *_):
_source_ctx, _target_ctx, _ctx = self._create_common_relationships(
'test_attach_to',
GROUP_TH,
Expand Down Expand Up @@ -247,15 +254,15 @@ def test_attach_to_Policy(self):
}
)

def test_IAMGroupClass_properties(self):
def test_IAMGroupClass_properties(self, *_):
self.fake_client.get_group = MagicMock(return_value={
'Group': {
'GroupName': "group_name_id",
'Arn': 'arn_id'
}
})

test_instance = group.IAMGroup("ctx_node", resource_id='group_id',
test_instance = group.IAMGroup(ctx_node, resource_id='group_id',
client=self.fake_client,
logger=None)

Expand All @@ -268,15 +275,15 @@ def test_IAMGroupClass_properties(self):
GroupName='group_id'
)

def test_IAMGroupClass_status(self):
def test_IAMGroupClass_status(self, *_):
self.fake_client.get_group = MagicMock(return_value={
'Group': {
'GroupName': "group_name_id",
'Arn': 'arn_id'
}
})

test_instance = group.IAMGroup("ctx_node", resource_id='group_id',
test_instance = group.IAMGroup(ctx_node, resource_id='group_id',
client=self.fake_client,
logger=None)

Expand Down
18 changes: 14 additions & 4 deletions cloudify_aws/iam/tests/test_iambase.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@

# Standard imports
import unittest
from mock import patch, MagicMock

# Local imports
from cloudify_aws.common.tests.test_base import TestServiceBase
from cloudify_aws.iam import IAMBase
from cloudify_aws.common.tests.test_base import TestServiceBase

ctx_node = MagicMock(
properties={},
plugin=MagicMock(properties={})
)


class TestIAMBase(TestServiceBase):

def setUp(self):
@patch('cloudify_aws.common.connection.ctx')
@patch('cloudify_aws.common.connection.Boto3Connection.get_account_id')
def setUp(self, _, _ctx):
_ctx = MagicMock( # noqa
node=ctx_node, plugin=MagicMock(properties={}))
super(TestIAMBase, self).setUp()
self.base = IAMBase("ctx_node", resource_id=True,
client=True, logger=None)
self.base = IAMBase(
ctx_node, resource_id=True, client=True, logger=None)


if __name__ == '__main__':
Expand Down
18 changes: 13 additions & 5 deletions cloudify_aws/iam/tests/test_instance_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,28 @@
},
'client_config': CLIENT_CONFIG
}
ctx_node = MagicMock(
properties=NODE_PROPERTIES,
plugin=MagicMock(properties={})
)


class TestIAMInstanceProfile(TestBase):

def setUp(self):
@patch('cloudify_aws.common.connection.ctx')
@patch('cloudify_aws.common.connection.Boto3Connection.get_account_id')
def setUp(self, _, _ctx):
super(TestIAMInstanceProfile, self).setUp()
_ctx = MagicMock( # noqa
node=ctx_node, plugin=MagicMock(properties={}))

self.fake_boto, self.fake_client = self.fake_boto_client('iam')

self.mock_patch = patch('boto3.client', self.fake_boto)
self.mock_patch.start()
self.instance_profile = \
instance_profile.IAMInstanceProfile(
"ctx_node",
ctx_node,
resource_id=True,
client=MagicMock(),
logger=None)
Expand Down Expand Up @@ -89,7 +97,7 @@ def test_create(self):

instance_profile.create(ctx=_ctx, resource_config=None, iface=None)

self.fake_boto.assert_called_with('iam', **CLIENT_CONFIG)
self.fake_boto.assert_called_with('sts', **CLIENT_CONFIG)

# This is just because I'm not interested in the content
# of remote_configuration right now.
Expand Down Expand Up @@ -124,7 +132,7 @@ def test_create_no_role(self):

instance_profile.create(ctx=_ctx, resource_config=None, iface=None)

self.fake_boto.assert_called_with('iam', **CLIENT_CONFIG)
self.fake_boto.assert_called_with('sts', **CLIENT_CONFIG)

# This is just because I'm not interested in the content
# of remote_configuration right now.
Expand Down Expand Up @@ -152,7 +160,7 @@ def test_delete(self):

instance_profile.delete(ctx=_ctx, resource_config=None, iface=None)

self.fake_boto.assert_called_with('iam', **CLIENT_CONFIG)
self.fake_boto.assert_called_with('sts', **CLIENT_CONFIG)

self.assertEqual(_ctx.instance.runtime_properties, {})

Expand Down
Loading

0 comments on commit ecf9774

Please sign in to comment.