Skip to content

Commit

Permalink
Merge branch 'develop' into ofir/Renaming_Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ofirvardi committed Mar 28, 2018
2 parents 4332d7a + 003d7ce commit f33cd6f
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 8 deletions.
2 changes: 1 addition & 1 deletion drivers/aws_shell/src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cloudshell-automation-api>=8.3.0.0,<8.3.1.0
cloudshell-shell-core>=3.1.0,<3.2.0
cloudshell-cp-aws>=2.2.0,<2.3.0
cloudshell-cp-aws>=2.3.0,<2.4.0
jsonpickle==0.9.3
10 changes: 10 additions & 0 deletions drivers/aws_shellPackage/DataModel/datamodel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@
<Rule Name="Setting" />
</Rules>
</AttributeInfo>
<AttributeInfo Name="IAM Role Name" Type="String" DefaultValue="" Description="Provide the IAM role name to be used in this instance." IsReadOnly="false" >
<Rules>
<Rule Name="Configuration" />
<Rule Name="Setting" />
</Rules>
</AttributeInfo>
<AttributeInfo DefaultValue="0" Description="The max number of I/O operations per second that the volume can support. For Provisioned IOPS (SSD) volumes, you can provision up to 30 IOPS per GiB. If left empty the default in the AMI will be used. For example 240." IsReadOnly="false" Name="Max Storage IOPS" Type="Numeric">
<Rules>
<Rule Name="Configuration" />
Expand Down Expand Up @@ -226,6 +232,7 @@
<DriverName>AWS Shell Driver</DriverName>
</Drivers>
<Scripts>

</Scripts>
</ResourceModel>
</Models>
Expand Down Expand Up @@ -269,6 +276,9 @@
<AttachedAttribute IsLocal="true" IsOverridable="true" Name="Instance Type" UserInput="true">
<AllowedValues />
</AttachedAttribute>
<AttachedAttribute IsLocal="true" IsOverridable="true" Name="IAM Role Name" UserInput="true">
<AllowedValues />
</AttachedAttribute>
<AttachedAttribute IsLocal="true" IsOverridable="true" Name="Storage Size" UserInput="false">
<AllowedValues />
</AttachedAttribute>
Expand Down
2 changes: 1 addition & 1 deletion drivers/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.1
2.3.0
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ def _create_deployment_parameters(self, ec2_session, aws_ec2_resource_model, ami
self._validate_image_available(image, ami_deployment_model.aws_ami_id)

aws_model.aws_ami_id = ami_deployment_model.aws_ami_id
aws_model.iam_role = self._get_iam_instance_profile_request(ami_deployment_model)
aws_model.min_count = 1
aws_model.max_count = 1
aws_model.instance_type = self._get_instance_item(ami_deployment_model, aws_ec2_resource_model)
Expand All @@ -373,6 +374,14 @@ def _create_deployment_parameters(self, ec2_session, aws_ec2_resource_model, ami

return aws_model

def _get_iam_instance_profile_request(self, ami_deployment_model):
role = ami_deployment_model.iam_role
if not role:
return dict()
if role.startswith('arn:'):
return {"Arn": role}
return {"Name": role}

def _prepare_network_interfaces(self, vpc, ami_deployment_model, security_group_ids, network_config_results,
logger):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ def _get_vm_instance_data(self, instance, vpc_id):
AdditionalData('Storage Type', self._get_volume_type(volume)),
AdditionalData('Storage Size', self._get_volume_size(volume)),
AdditionalData('VPC ID', vpc_id, hidden=True),
AdditionalData('Availability Zone', self._get_availability_zone(instance), hidden=True)
AdditionalData('Availability Zone', self._get_availability_zone(instance), hidden=True),
]

if instance.iam_instance_profile:
data.append(AdditionalData('IAM Role', instance.iam_instance_profile["Arn"]))

return data

def _get_vm_network_data(self, instance):
Expand Down
3 changes: 2 additions & 1 deletion package/cloudshell/cp/aws/domain/services/ec2/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def create_instance(self, ec2_session, name, reservation, ami_deployment_info, e
InstanceType=ami_deployment_info.instance_type,
KeyName=ami_deployment_info.aws_key,
BlockDeviceMappings=ami_deployment_info.block_device_mappings,
NetworkInterfaces=ami_deployment_info.network_interfaces
NetworkInterfaces=ami_deployment_info.network_interfaces,
IamInstanceProfile=ami_deployment_info.iam_role
# PrivateIpAddress=ami_deployment_info.private_ip_address
)[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def convert_to_deployment_resource_model(deployment_request, resource):
deployment_resource_model.storage_iops = data["Attributes"]['Storage IOPS']
deployment_resource_model.storage_type = data["Attributes"]['Storage Type']
deployment_resource_model.instance_type = data["Attributes"]['Instance Type']
deployment_resource_model.iam_role = data["Attributes"]['IAM Role Name']
deployment_resource_model.root_volume_name = data["Attributes"]['Root Volume Name']
deployment_resource_model.wait_for_ip = convert_to_bool(data["Attributes"]['Wait for IP'])
deployment_resource_model.wait_for_status_check = convert_to_bool(
Expand Down
1 change: 1 addition & 0 deletions package/cloudshell/cp/aws/models/ami_deployment_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def __init__(self):
self.min_count = 0 # type: int
self.max_count = 0 # type: int
self.instance_type = '' # type: str
self.iam_role = '' # type: str
self.private_ip_address = '' # type: str
self.security_group_ids = [] # type: list[str]
self.block_device_mappings = [] # type: list[dict]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(self):
self.min_count = 0 # type: int
self.max_count = 0 # type: int
self.instance_type = '' # type: str
self.iam_role = '' # type: str
self.security_group_ids = None # type: str
self.private_ip_address = '' # type: str
self.root_volume_name = '' # type: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ def test_create_deployment_parameters_single_subnet(self):
ami_model = Mock()
ami_model.aws_ami_id = 'asd'
ami_model.storage_size = '0'
ami_model.iam_role = ""
ami_model.network_configurations = None
vpc = Mock()
self.deploy_operation._get_block_device_mappings = Mock()
Expand All @@ -355,6 +356,78 @@ def test_create_deployment_parameters_single_subnet(self):
self.assertTrue(len(aws_model.security_group_ids) == 1)
self.assertTrue(len(aws_model.network_interfaces) == 1)

def test_create_deployment_parameters_no_iam_role(self):
image = Mock()
image.state = 'available'
ec2_session = Mock()
ec2_session.Image = Mock(return_value=image)
ami_model = Mock()
ami_model.iam_role = ""
ami_model.network_configurations = None
vpc = Mock()
self.deploy_operation._get_block_device_mappings = Mock()

aws_model = self.deploy_operation._create_deployment_parameters(ec2_session=ec2_session,
aws_ec2_resource_model=self.ec2_datamodel,
ami_deployment_model=ami_model,
vpc=vpc,
security_group=None,
key_pair='keypair',
reservation=Mock(),
network_config_results=MagicMock(),
logger=self.logger,)

# if instance doesnt have iam role, the deployment params will have an empty iam instance profile dict
self.assertTrue(not any(aws_model.iam_role)) # not any(some_dict) => is empty dictionary

def test_create_deployment_parameters_iam_role_not_arn(self):
image = Mock()
image.state = 'available'
ec2_session = Mock()
ec2_session.Image = Mock(return_value=image)
ami_model = Mock()
ami_model.iam_role = "admin_role"
ami_model.network_configurations = None
vpc = Mock()
self.deploy_operation._get_block_device_mappings = Mock()

aws_model = self.deploy_operation._create_deployment_parameters(ec2_session=ec2_session,
aws_ec2_resource_model=self.ec2_datamodel,
ami_deployment_model=ami_model,
vpc=vpc,
security_group=None,
key_pair='keypair',
reservation=Mock(),
network_config_results=MagicMock(),
logger=self.logger,)

# if instance has iam role, but not in the form of arn, will return dict with iam role name
self.assertTrue(aws_model.iam_role['Name'] == ami_model.iam_role)

def test_create_deployment_parameters_iam_role_arn(self):
image = Mock()
image.state = 'available'
ec2_session = Mock()
ec2_session.Image = Mock(return_value=image)
ami_model = Mock()
ami_model.network_configurations = None
ami_model.iam_role = "arn:aws:iam::admin_role"
vpc = Mock()
self.deploy_operation._get_block_device_mappings = Mock()

aws_model = self.deploy_operation._create_deployment_parameters(ec2_session=ec2_session,
aws_ec2_resource_model=self.ec2_datamodel,
ami_deployment_model=ami_model,
vpc=vpc,
security_group=None,
key_pair='keypair',
reservation=Mock(),
network_config_results=MagicMock(),
logger=self.logger,)

# if instance has iam role, but not in the form of arn, will return dict with iam role name
self.assertTrue(aws_model.iam_role['Arn'] == ami_model.iam_role)

def test_prepare_network_interfaces_multi_subnets_with_public_ip(self):
ami_model = Mock()
ami_model.add_public_ip = True
Expand Down
2 changes: 2 additions & 0 deletions package/tests/test_common/test_vm_details_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ def test_prepare_vm_details(self):
instance.platform = 'instance_platform'
instance.network_interfaces = []
instance.volumes.all = lambda: []
instance.iam_instance_profile = {"Arn": "arn:aws:iam::admin_role"}

vm_instance_data = self.vm_details_provider.create(instance).vm_instance_data

self.assertTrue(self._get_value(vm_instance_data, 'AMI ID') == instance.image_id)
self.assertTrue(self._get_value(vm_instance_data, 'instance type') == instance.instance_type)
self.assertTrue(self._get_value(vm_instance_data, 'platform') == instance.platform)
self.assertTrue(self._get_value(vm_instance_data, 'IAM Role') == instance.iam_instance_profile['Arn'])

def test_prepare_network_interface_objects_with_elastic_ip(self):
# elastic_ip
Expand Down
3 changes: 1 addition & 2 deletions package/tests/test_domain_services/test_instance_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,15 @@ def test_create_instance(self):
cancellation_context=cancellation_context,
logger=Mock())


self.ec2_session.create_instances.assert_called_once_with(ImageId=ami_dep.aws_ami_id,
MinCount=ami_dep.min_count,
MaxCount=ami_dep.max_count,
InstanceType=ami_dep.instance_type,
IamInstanceProfile=ami_dep.iam_role,
KeyName=ami_dep.aws_key,
BlockDeviceMappings=ami_dep.block_device_mappings,
NetworkInterfaces=ami_dep.network_interfaces)


self.instance_waiter.wait.assert_called_once_with(instance=new_instance,
state=self.instance_waiter.RUNNING,
cancellation_context=cancellation_context)
Expand Down
3 changes: 3 additions & 0 deletions package/tests/test_domain_services/test_model_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def test_convert_to_deployment_resource_model(self):
'"Storage IOPS":"0",' \
'"Storage Type":"storage_type",' \
'"Instance Type":"t.nano",' \
'"IAM Role Name":"top secret",'\
'"Root Volume Name":"root_vol_name",' \
'"Wait for IP":"False",' \
'"Wait for Status Check":"True",' \
Expand All @@ -78,6 +79,7 @@ def test_convert_to_deployment_resource_model(self):
self.assertEquals(model.storage_iops, "0")
self.assertEquals(model.storage_type, "storage_type")
self.assertEquals(model.instance_type, "t.nano")
self.assertEquals(model.iam_role, "top secret")
self.assertEquals(model.root_volume_name, "root_vol_name")
self.assertFalse(model.wait_for_ip)
self.assertTrue(model.wait_for_status_check)
Expand All @@ -98,6 +100,7 @@ def test_convert_to_deployment_resource_model_with_network(self):
'"Storage IOPS":"0",' \
'"Storage Type":"storage_type",' \
'"Instance Type":"t.nano",' \
'"IAM Role Name":"top secret",'\
'"Root Volume Name":"root_vol_name",' \
'"Wait for IP":"False",' \
'"Wait for Status Check":"True",' \
Expand Down
2 changes: 1 addition & 1 deletion package/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.1
2.3.0
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.1
2.3.0

0 comments on commit f33cd6f

Please sign in to comment.