Skip to content

Commit

Permalink
Added 3 extension methods to the aws shell driver:
Browse files Browse the repository at this point in the history
- remote_set_aim_role
- remote_attach_volume
- remote_detach_volume
  • Loading branch information
Alex Azarh authored and Alex Azarh committed Nov 13, 2017
1 parent d54c177 commit ebf3dd4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
90 changes: 89 additions & 1 deletion drivers/aws_shell/src/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,92 @@ def SetAppSecurityGroups(self, context, request):
return self.aws_shell.set_app_security_groups(context, request)

def GetVmDetails(self, context, ports):
return self.aws_shell.get_vm_details(context)
return self.aws_shell.get_vm_details(context)

def remote_set_aim_role(self, context, iam_instance_profile_arn, ports):
"""
:param cloudshell.shell.core.context.ResourceCommandContext context:
:param str iam_instance_profile_arn:
:param ports:
:return:
"""
from cloudshell.cp.aws.domain.context.aws_shell import AwsShellContext
with AwsShellContext(context=context, aws_session_manager=self.aws_shell.aws_session_manager) as shell_context:
from cloudshell.core.context.error_handling_context import ErrorHandlingContext
with ErrorHandlingContext(shell_context.logger):
shell_context.logger.info('Set AIM Role')

# Get instance id
deployed_instance_id = \
self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id(context)

# Get IAM Instance profile associations
result = shell_context.aws_api.ec2_client.describe_iam_instance_profile_associations(
Filters=[{'Name': 'instance-id', 'Values': [deployed_instance_id]},
{'Name': 'state', 'Values': ['associating', 'associated']}]
)

# Check if current associations contain the requested ARN
if self._instance_profile_associations_exists(result):
from cloudshell.cp.aws.domain.common.list_helper import first_or_default
association = first_or_default(result['IamInstanceProfileAssociations'],
lambda x: x['IamInstanceProfile']['Arn'] == iam_instance_profile_arn)
if not association:
# Remove current IAM associations
for ass in result['IamInstanceProfileAssociations']:
shell_context.aws_api.ec2_client.disassociate_iam_instance_profile(
AssociationId=ass['AssociationId'])
else:
shell_context.logger.info('Requested IAM ARN {0} already associated with instance'
.format(iam_instance_profile_arn))
return

# Associate IAM ARN with instance
shell_context.aws_api.ec2_client.associate_iam_instance_profile(
InstanceId=deployed_instance_id,
IamInstanceProfile={
'Arn': iam_instance_profile_arn
}
)

def remote_attach_volume(self, context, device, volume_id, ports):
from cloudshell.cp.aws.domain.context.aws_shell import AwsShellContext
with AwsShellContext(context=context, aws_session_manager=self.aws_shell.aws_session_manager) as shell_context:
from cloudshell.core.context.error_handling_context import ErrorHandlingContext
with ErrorHandlingContext(shell_context.logger):
shell_context.logger.info('Attach volume {1} to device {0}'.format(device, volume_id))

# Get instance id
deployed_instance_id = \
self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id(context)

# Get instance object
instance = shell_context.aws_api.ec2_session.Instance(deployed_instance_id)

# Attach volume
instance.attach_volume(Device=device, VolumeId=volume_id)

def remote_detach_volume(self, context, device, volume_id, force, ports):
from cloudshell.cp.aws.domain.context.aws_shell import AwsShellContext
with AwsShellContext(context=context, aws_session_manager=self.aws_shell.aws_session_manager) as shell_context:
from cloudshell.core.context.error_handling_context import ErrorHandlingContext
with ErrorHandlingContext(shell_context.logger):
from cloudshell.cp.aws.common.converters import convert_to_bool
force = convert_to_bool(force)
shell_context.logger.info('Detaching volume {1} from device {0}. Force: {2}'
.format(device, volume_id, force))

# Get instance id
deployed_instance_id = \
self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id(context)

# Get instance object
instance = shell_context.aws_api.ec2_session.Instance(deployed_instance_id)

# Detach volume
instance.detach_volume(Device=device, VolumeId=volume_id, Force=force)

@staticmethod
def _instance_profile_associations_exists(result):
return result and 'IamInstanceProfileAssociations' in result and len(
result['IamInstanceProfileAssociations']) > 0
3 changes: 3 additions & 0 deletions drivers/aws_shell/src/drivermetadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<Command Description="" DisplayName="Deploy From AMI" EnableCancellation="true" Name="deploy_ami" Tags="allow_unreserved" />
</Category>
<Category Name="Connectivity">
<Command Description="" DisplayName="Set AIM Roles" Name="remote_set_aim_role" Tags="remote_connectivity,allow_shared" />
<Command Description="" DisplayName="Attach Volume" Name="remote_attach_volume" Tags="remote_connectivity,allow_shared" />
<Command Description="" DisplayName="Detach Volume" Name="remote_detach_volume" Tags="remote_connectivity,allow_shared" />
<Command Description="" DisplayName="Refresh IP" EnableCancellation="true" Name="remote_refresh_ip" Tags="remote_connectivity,allow_shared" />
<Command Description="" DisplayName="Get Application Ports" Name="GetApplicationPorts" Tags="remote_connectivity,allow_shared" />
<Command Description="" DisplayName="Prepare Connectivity" EnableCancellation="true" Name="PrepareConnectivity" Tags="allow_unreserved" />
Expand Down

0 comments on commit ebf3dd4

Please sign in to comment.