Ejemplo n.º 1
0
    def get_inventory(self, command_context):
        """Validate Cloud Provider.

        :param command_context: ResourceCommandContext
        """
        try:
            with AwsShellContext(context=command_context,
                                 aws_session_manager=self.aws_session_manager
                                 ) as shell_context:
                shell_context.logger.info("Starting Autoload Operation...")
                result = self.autoload_operation.get_inventory(
                    cloud_provider_model=shell_context.aws_ec2_resource_model,
                    logger=shell_context.logger,
                    ec2_client=shell_context.aws_api.ec2_client,
                    ec2_session=shell_context.aws_api.ec2_session,
                    s3_session=shell_context.aws_api.s3_session,
                )
                shell_context.logger.info("End Autoload Operation...")
                return result

        except ClientError as ce:
            if "AuthorizationHeaderMalformed" in str(ce):
                raise Exception(self.CREDENTIALS_ERROR_MESSAGE)
            raise ce

        except NoCredentialsError:
            raise Exception(self.CREDENTIALS_ERROR_MESSAGE)

        except ValueError as ve:
            if "Invalid endpoint" in str(ve):
                raise Exception(
                    "Oops, like you didnt configure Region correctly. Please select "
                    "Region and try again ")
            else:
                raise ve
Ejemplo n.º 2
0
    def create_traffic_mirroring(self, context, cancellation_context, request):
        """# noqa
        Will create a vpc for the reservation and will peer it with the management vpc
        :param request:
        :param ResourceCommandContext context:

        :return: json string response
        :param CancellationContext cancellation_context:
        :rtype: list[ActionResultBase]
        """
        with AwsShellContext(
                context=context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Create traffic mirroring")
            actions = self._parse_request(request, shell_context)
            self.traffic_mirroring_operation.validate_create_actions(
                actions, request, shell_context.logger)
            results = self.traffic_mirroring_operation.create(
                ec2_client=shell_context.aws_api.ec2_client,
                reservation=self.model_parser.convert_to_reservation_model(
                    context.reservation),
                actions=actions,
                cancellation_context=cancellation_context,
                logger=shell_context.logger,
                cloudshell=shell_context.cloudshell_session,
            )

            return results
Ejemplo n.º 3
0
    def refresh_ip(self, command_context):
        """
        :param ResourceRemoteCommandContext command_context:
        """
        with AwsShellContext(context=command_context, aws_session_manager=self.aws_session_manager) as shell_context:
            with ErrorHandlingContext(shell_context.logger):
                shell_context.logger.info('Refresh IP')

                # Get Private Ip on deployed resource
                private_ip_on_resource = self.model_parser.get_private_ip_from_connected_resource_details(
                    command_context)
                # Get Public IP on deployed resource
                public_ip_on_resource = self.model_parser.get_public_ip_from_connected_resource_details(command_context)
                # Get instance id
                deployed_instance_id = self.model_parser.try_get_deployed_connected_resource_instance_id(
                    command_context)
                # Get connected resource name
                resource_fullname = self.model_parser.get_connectd_resource_fullname(command_context)

                self.refresh_ip_operation.refresh_ip(cloudshell_session=shell_context.cloudshell_session,
                                                     ec2_session=shell_context.aws_api.ec2_session,
                                                     deployed_instance_id=deployed_instance_id,
                                                     private_ip_on_resource=private_ip_on_resource,
                                                     public_ip_on_resource=public_ip_on_resource,
                                                     resource_fullname=resource_fullname)
Ejemplo n.º 4
0
    def deploy_ami(self, command_context, deployment_request, cancellation_context):
        """
        Will deploy Amazon Image on the cloud provider
        :param ResourceCommandContext command_context:
        :param JSON Obj deployment_request:
        :param CancellationContext cancellation_context:
        """
        with AwsShellContext(context=command_context, aws_session_manager=self.aws_session_manager) as shell_context:
            with ErrorHandlingContext(shell_context.logger):
                shell_context.logger.info('Deploying AMI')

                aws_ami_deployment_model = self.model_parser.convert_to_deployment_resource_model(deployment_request,
                                                                                                  command_context.resource)

                deploy_data = self.deploy_ami_operation \
                    .deploy(ec2_session=shell_context.aws_api.ec2_session,
                            s3_session=shell_context.aws_api.s3_session,
                            name=aws_ami_deployment_model.app_name,
                            reservation=self.model_parser.convert_to_reservation_model(command_context.reservation),
                            aws_ec2_cp_resource_model=shell_context.aws_ec2_resource_model,
                            ami_deployment_model=aws_ami_deployment_model,
                            ec2_client=shell_context.aws_api.ec2_client,
                            cancellation_context=cancellation_context,
                            logger=shell_context.logger)

                return self.command_result_parser.set_command_result(deploy_data)
Ejemplo n.º 5
0
    def prepare_connectivity(self, command_context, request, cancellation_context):
        """
        Will create a vpc for the reservation and will peer it with the management vpc
        :param ResourceCommandContext command_context: The Command Context
        :param request: The json request
        :return: json string response
        :param CancellationContext cancellation_context:
        :rtype: str
        """
        with AwsShellContext(context=command_context, aws_session_manager=self.aws_session_manager) as shell_context:
            with ErrorHandlingContext(shell_context.logger):
                shell_context.logger.info('Prepare Connectivity')

                # parse request
                connectivity_actions = self._request_str_to_actions_list(request)

                results = self.prepare_connectivity_operation.prepare_connectivity(
                    ec2_client=shell_context.aws_api.ec2_client,
                    ec2_session=shell_context.aws_api.ec2_session,
                    s3_session=shell_context.aws_api.s3_session,
                    reservation=self.model_parser.convert_to_reservation_model(command_context.reservation),
                    aws_ec2_datamodel=shell_context.aws_ec2_resource_model,
                    actions=connectivity_actions,
                    cancellation_context=cancellation_context,
                    logger=shell_context.logger)

                return self.command_result_parser.set_command_result({'driverResponse': {'actionResults': results}})
Ejemplo n.º 6
0
    def cleanup_connectivity(self, command_context, request):
        """
        Will delete the reservation vpc and all related resources including all remaining instances
        :param ResourceCommandContext command_context:
        :param request: The json request
        :return: json string response
        :rtype: str
        """

        with AwsShellContext(context=command_context, aws_session_manager=self.aws_session_manager) as shell_context:
            with ErrorHandlingContext(shell_context.logger):
                shell_context.logger.info('Cleanup Connectivity')

                connectivity_actions = self._request_str_to_actions_list(request)

                result = self.clean_up_operation \
                    .cleanup(ec2_client=shell_context.aws_api.ec2_client,
                             ec2_session=shell_context.aws_api.ec2_session,
                             s3_session=shell_context.aws_api.s3_session,
                             aws_ec2_data_model=shell_context.aws_ec2_resource_model,
                             reservation_id=command_context.reservation.reservation_id,
                             actions=connectivity_actions,
                             logger=shell_context.logger)
                return self.command_result_parser.set_command_result(
                    {'driverResponse': {'actionResults': [result]}})
Ejemplo n.º 7
0
    def save_app(self, context, cancellation_context):
        """# noqa
        :param context:
        :param cancellation_context:
        :return:
        """
        with AwsShellContext(
                context=context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Save Snapshot")

            resource = context.remote_endpoints[0]

            data_holder = self.model_parser.convert_app_resource_to_deployed_app(
                resource)
            resource_fullname = self.model_parser.get_connectd_resource_fullname(
                context)

            image_id = self.snapshot_operation.save(
                logger=shell_context.logger,
                ec2_session=shell_context.aws_api.ec2_session,
                instance_id=data_holder.vmdetails.uid,
                deployed_app_name=resource_fullname,
                snapshot_prefix="",
                no_reboot=True,
            )

            return json.dumps({"AWS EC2 Instance.AWS AMI Id": image_id})
Ejemplo n.º 8
0
    def add_custom_tags(self, context, request):
        """# noqa
        :param ResourceCommandContext context:
        :param str request:
        :return:
        """
        with AwsShellContext(
                context=context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Add custom tags")

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

            # Expected request syntax:
            # [{  # noqa
            #     'Key': 'string',  # noqa
            #     'Value': 'string'  # noqa
            # }]  # noqa
            tags = json.loads(request)

            instance = self.instance_service.get_instance_by_id(
                shell_context.aws_api.ec2_session, deployed_instance_id)
            instance.create_tags(Tags=tags)
Ejemplo n.º 9
0
    def set_app_security_groups(self, context, request):
        """# noqa
        Set security groups (inbound rules only)
        :param context: todo - set the type of the parameter
        :param request: The json request
        :return:
        """
        with AwsShellContext(
                context=context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Set App Security Groups")

            reservation = self.model_parser.convert_to_reservation_model(
                context.reservation)
            app_security_group_models = (
                self.model_parser.convert_to_app_security_group_models(request)
            )

            result = self.set_app_security_groups_operation.set_apps_security_groups(
                app_security_group_models=app_security_group_models,
                reservation=reservation,
                ec2_session=shell_context.aws_api.ec2_session,
                logger=shell_context.logger,
            )

            json_result = SetAppSecurityGroupActionResult.to_json(result)

            return json_result
Ejemplo n.º 10
0
    def get_vm_details(self, context, cancellation_context, requests_json):
        """# noqa
        Get vm details for specific deployed app
        :type context: ResourceCommandContext
        :rtype str
        """
        results = []
        vm_details_requests = [
            VmDetailsRequest(item) for item in DeployDataHolder(
                jsonpickle.decode(requests_json)).items
        ]

        for request in vm_details_requests:
            if cancellation_context.is_cancelled:
                break

            try:
                with AwsShellContext(context=context,
                                     aws_session_manager=self.
                                     aws_session_manager) as shell_context:
                    shell_context.logger.info("Get VmDetails")
                    vm_details = self.vm_details_operation.get_vm_details(
                        request.uuid, shell_context.aws_api.ec2_session)
                    vm_details.appName = request.app_name
                    results.append(vm_details)
            except Exception as e:
                result = VmDetailsData()
                result.appName = request.app_name
                result.error = str(e)
                results.append(result)

        return self.command_result_parser.set_command_result(results)
Ejemplo n.º 11
0
    def get_application_ports(self, command_context):
        """# noqa
        Will return the application ports in a nicely formated manner
        :param ResourceRemoteCommandContext command_context:
        :rtype: str
        """
        with AwsShellContext(
                context=command_context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Get Application Ports")
            resource = command_context.remote_endpoints[0]

            # Get instance id
            deployed_instance_id = (
                self.model_parser.
                try_get_deployed_connected_resource_instance_id(
                    command_context))

            # Get Allow all Storage Traffic on deployed resource
            allow_all_storage_traffic = self.model_parser.get_allow_all_storage_traffic_from_connected_resource_details(  # noqa
                command_context)

            return self.deployed_app_ports_operation.get_app_ports_from_cloud_provider(
                ec2_session=shell_context.aws_api.ec2_session,
                instance_id=deployed_instance_id,
                resource=resource,
                allow_all_storage_traffic=allow_all_storage_traffic,
            )
Ejemplo n.º 12
0
    def deploy_ami(self, command_context, actions, cancellation_context):
        """# noqa
        Will deploy Amazon Image on the cloud provider
        :param ResourceCommandContext command_context:
        :param list[RequestActionBase] actions::
        :param CancellationContext cancellation_context:
        """
        with AwsShellContext(
                context=command_context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Deploying AMI")

            deploy_action = single(actions, lambda x: isinstance(x, DeployApp))
            network_actions = [
                a for a in actions if isinstance(a, ConnectSubnet)
            ]

            deploy_data = self.deploy_ami_operation.deploy(
                ec2_session=shell_context.aws_api.ec2_session,
                s3_session=shell_context.aws_api.s3_session,
                name=deploy_action.actionParams.appName,
                reservation=self.model_parser.convert_to_reservation_model(
                    command_context.reservation),
                aws_ec2_cp_resource_model=shell_context.aws_ec2_resource_model,
                ami_deploy_action=deploy_action,
                network_actions=network_actions,
                ec2_client=shell_context.aws_api.ec2_client,
                cancellation_context=cancellation_context,
                logger=shell_context.logger,
            )

            return deploy_data
Ejemplo n.º 13
0
    def remove_traffic_mirroring(self, context, request):
        """# noqa
        Can remove traffic mirroring sessions by session id, or all sessions associated with a traffic mirror target (by target nic id)
        :param str request:
        :param ResourceCommandContext context:
        :param ResourceCommandContext context:

        :return: json string response
        :rtype: list[ActionResultBase]
        """
        with AwsShellContext(
                context=context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Create traffic mirroring")

            self.traffic_mirroring_operation.validate_remove_request(
                request, shell_context.logger)

            actions = self._parse_request(request, shell_context)

            results = self.traffic_mirroring_operation.remove(
                ec2_client=shell_context.aws_api.ec2_client,
                reservation=self.model_parser.convert_to_reservation_model(
                    context.reservation),
                actions=actions,
                logger=shell_context.logger,
                cloudshell=shell_context.cloudshell_session,
            )

            return results
Ejemplo n.º 14
0
    def prepare_connectivity(self, command_context, actions,
                             cancellation_context):
        """# noqa
        Will create a vpc for the reservation and will peer it with the management vpc
        :param ResourceCommandContext command_context: The Command Context
        :param list[RequestActionBase] actions:
        :return: json string response
        :param CancellationContext cancellation_context:
        :rtype: list[ActionResultBase]
        """
        with AwsShellContext(
                context=command_context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Prepare Connectivity")
            reservation = self.model_parser.convert_to_reservation_model(
                command_context.reservation)
            cs_subnet_service = CsSubnetService(
                shell_context.cloudshell_session, reservation.reservation_id)

            results = self.prepare_connectivity_operation.prepare_connectivity(
                ec2_client=shell_context.aws_api.ec2_client,
                ec2_session=shell_context.aws_api.ec2_session,
                s3_session=shell_context.aws_api.s3_session,
                reservation=reservation,
                aws_ec2_datamodel=shell_context.aws_ec2_resource_model,
                actions=actions,
                cancellation_context=cancellation_context,
                cs_subnet_service=cs_subnet_service,
                logger=shell_context.logger,
            )

            return results
Ejemplo n.º 15
0
    def cleanup_connectivity(self, command_context, actions):
        """# noqa
        Will delete the reservation vpc and all related resources including all remaining instances
        :param ResourceCommandContext command_context:
        :param list[RequestActionBase] actions::
        :return: json string response
        :rtype: str
        """
        with AwsShellContext(
                context=command_context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Cleanup Connectivity")

            result = self.clean_up_operation.cleanup(
                ec2_client=shell_context.aws_api.ec2_client,
                ec2_session=shell_context.aws_api.ec2_session,
                s3_session=shell_context.aws_api.s3_session,
                aws_ec2_data_model=shell_context.aws_ec2_resource_model,
                reservation_id=command_context.reservation.reservation_id,
                actions=actions,
                logger=shell_context.logger,
            )
            return self.command_result_parser.set_command_result(
                {"driverResponse": {
                    "actionResults": [result]
                }})
Ejemplo n.º 16
0
 def get_vm_details(self, context):
     """
     Get vm details for specific deployed app
     :type context: ResourceRemoteCommandContext
     :rtype str
     """
     with AwsShellContext(context=context, aws_session_manager=self.aws_session_manager) as shell_context:
         with ErrorHandlingContext(shell_context.logger):
             shell_context.logger.info('Get VmDetails')
             instance_id = self.model_parser.try_get_deployed_connected_resource_instance_id(context)
             vm_details = self.vm_details_operation.get_vm_details(instance_id, shell_context.aws_api.ec2_session)
             return self.command_result_parser.set_command_result(vm_details)
Ejemplo n.º 17
0
    def remote_get_snapshots(self, context):
        with AwsShellContext(
                context=context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Get Snapshots")

            resource = context.remote_endpoints[0]
            data_holder = self.model_parser.convert_app_resource_to_deployed_app(
                resource)

            return self.snapshot_operation.get_snapshots(
                shell_context.aws_api.ec2_client,
                instance_id=data_holder.vmdetails.uid)
Ejemplo n.º 18
0
 def get_access_key(self, command_context):
     """
     Returns the pem file for the connected resource
     :param ResourceRemoteCommandContext command_context:
     :rtype str:
     """
     with AwsShellContext(context=command_context, aws_session_manager=self.aws_session_manager) as shell_context:
         with ErrorHandlingContext(shell_context.logger):
             shell_context.logger.info('GetAccessKey')
             reservation_id = self._get_reservation_id(command_context)
             return self.access_key_operation.get_access_key(s3_session=shell_context.aws_api.s3_session,
                                                             aws_ec2_resource_model=shell_context.aws_ec2_resource_model,
                                                             reservation_id=reservation_id)
Ejemplo n.º 19
0
    def delete_instance(self, command_context):
        """
        Will delete the ami instance
        :param ResourceRemoteCommandContext command_context:
        """
        with AwsShellContext(context=command_context, aws_session_manager=self.aws_session_manager) as shell_context:
            with ErrorHandlingContext(shell_context.logger):
                shell_context.logger.info('Delete instance')

                resource = command_context.remote_endpoints[0]
                data_holder = self.model_parser.convert_app_resource_to_deployed_app(resource)

                self.delete_ami_operation.delete_instance(logger=shell_context.logger,
                                                          ec2_session=shell_context.aws_api.ec2_session,
                                                          instance_id=data_holder.vmdetails.uid)
Ejemplo n.º 20
0
    def power_off_ami(self, command_context):
        """
        Will power on the ami
        :param ResourceRemoteCommandContext command_context:
        """
        with AwsShellContext(context=command_context, aws_session_manager=self.aws_session_manager) as shell_context:
            with ErrorHandlingContext(shell_context.logger):
                shell_context.logger.info('Power Off')

                resource = command_context.remote_endpoints[0]
                data_holder = self.model_parser.convert_app_resource_to_deployed_app(resource)

                self.power_management_operation.power_off(ec2_session=shell_context.aws_api.ec2_session,
                                                          ami_id=data_holder.vmdetails.uid)

                shell_context.cloudshell_session.SetResourceLiveStatus(resource.fullname, "Offline", "Powered Off")
    def test_aws_shell_context_proper_initialized(self):
        # Arrange
        aws_session_manager = Mock()
        command_context = Mock()
        with patch(
                'cloudshell.cp.aws.domain.context.aws_shell.LoggingSessionContext'
        ) as logger:
            with mock.patch(
                    'cloudshell.cp.aws.domain.context.aws_shell.ErrorHandlingContext'
            ):
                with mock.patch(
                        'cloudshell.cp.aws.domain.context.aws_shell.CloudShellSessionContext'
                ) as cloudshell_session:
                    with mock.patch(
                            'cloudshell.cp.aws.domain.context.aws_shell.AwsResourceModelContext'
                    ) as aws_ec2_resource_model:
                        with mock.patch(
                                'cloudshell.cp.aws.domain.context.aws_shell.AwsApiSessionContext'
                        ) as aws_api:
                            expected_context = AwsShellContextModel(
                                logger=logger().__enter__(),
                                cloudshell_session=cloudshell_session(
                                ).__enter__(),
                                aws_ec2_resource_model=aws_ec2_resource_model(
                                ).__enter__(),
                                aws_api=aws_api().__enter__())

                            # Act
                            with AwsShellContext(
                                    context=command_context,
                                    aws_session_manager=aws_session_manager
                            ) as aws_shell_context:
                                # Assert
                                self.assertIsInstance(aws_shell_context,
                                                      AwsShellContextModel)

                                self.assertEquals(aws_shell_context.logger,
                                                  expected_context.logger)
                                self.assertEquals(
                                    aws_shell_context.cloudshell_session,
                                    expected_context.cloudshell_session)
                                self.assertEquals(
                                    aws_shell_context.aws_ec2_resource_model,
                                    expected_context.aws_ec2_resource_model)
                                self.assertEquals(aws_shell_context.aws_api,
                                                  expected_context.aws_api)
Ejemplo n.º 22
0
 def remote_restore_snapshot(self, context, snapshot_name):
     with AwsShellContext(
             context=context,
             aws_session_manager=self.aws_session_manager) as shell_context:
         shell_context.logger.info("Save Snapshot")
         resource = context.remote_endpoints[0]
         reservation = ReservationModel(context.remote_reservation)
         tags = self.tag_service.get_default_tags(snapshot_name,
                                                  reservation)
         data_holder = self.model_parser.convert_app_resource_to_deployed_app(
             resource)
         self.snapshot_operation.save_snapshot(
             ec2_client=shell_context.aws_api.ec2_client,
             ec2_session=shell_context.aws_api.ec2_session,
             instance_id=data_holder.vmdetails.uid,
             snapshot_name=snapshot_name,
             tags=tags,
         )
Ejemplo n.º 23
0
    def power_off_ami(self, command_context):
        """# noqa
        Will power on the ami
        :param ResourceRemoteCommandContext command_context:
        """
        with AwsShellContext(
                context=command_context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Power Off")

            resource = command_context.remote_endpoints[0]
            data_holder = self.model_parser.convert_app_resource_to_deployed_app(
                resource)

            self.power_management_operation.power_off(
                ec2_session=shell_context.aws_api.ec2_session,
                ami_id=data_holder.vmdetails.uid,
            )
Ejemplo n.º 24
0
    def assign_additional_private_ipv4s(self, context, vnic_id, new_ips):
        with AwsShellContext(
                context=context,
                aws_session_manager=self.aws_session_manager) as shell_context:
            shell_context.logger.info("Assign additional IP Addresses")

            ips = map(str.strip, new_ips.split(";"))
            try:
                response = shell_context.aws_api.ec2_client.assign_private_ip_addresses(
                    AllowReassignment=True,
                    NetworkInterfaceId=vnic_id,
                    PrivateIpAddresses=ips,
                )
                assigned_ips_response = response.get(
                    "AssignedPrivateIpAddresses", [])
                return ";".join([
                    ip.get("PrivateIpAddress") for ip in assigned_ips_response
                    if ip.get("PrivateIpAddress")
                ])
            except Exception:
                shell_context.logger.error("Failed to add ips", exc_info=1)
                return None