示例#1
0
    def get_vm_details(self, command_context, cancellation_context,
                       requests_json):
        """Get vm details for specific deployed app

        :param requests_json:
        :param cancellation_context:
        :param command_context: ResourceRemoteCommandContext
        """

        with LoggingSessionContext(command_context) as logger:
            with ErrorHandlingContext(logger):
                logger.info("Starting get_vm_details operation...")

                requests = DeployDataHolder(
                    jsonpickle.decode(requests_json)).items

                group_name = self.model_parser.convert_to_reservation_model(command_context.reservation) \
                    .reservation_id

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

                # data_holder_request = self.model_parser.convert_app_resource_to_request(resource)
                # deployment_service = data_holder_request.deploymentService
                # is_market_place = filter(lambda x: x.name == "Image SKU", deployment_service.attributes)

                with CloudShellSessionContext(
                        command_context) as cloudshell_session:
                    cloud_provider_model = self.model_parser.convert_to_cloud_provider_resource_model(
                        resource=command_context.resource,
                        cloudshell_session=cloudshell_session)

                azure_clients = AzureClientsManager(cloud_provider_model)

                vm_details = self.vm_details_operation.get_vm_details(
                    compute_client=azure_clients.compute_client,
                    group_name=group_name,
                    requests=requests,
                    logger=logger,
                    network_client=azure_clients.network_client,
                    model_parser=self.model_parser,
                    cancellation_context=cancellation_context)
                return self.command_result_parser.set_command_result(
                    vm_details)
示例#2
0
    def refresh_ip(self, command_context):
        """Refresh private and public IPs on the Cloudshell resource

        :param ResourceRemoteCommandContext command_context:
        """
        with LoggingSessionContext(command_context) as logger:
            with ErrorHandlingContext(logger):
                logger.info("Starting Refresh IP operation...")

                resource = command_context.remote_endpoints[0]
                data_holder = self.model_parser.convert_app_resource_to_deployed_app(
                    resource)
                vm_name = data_holder.name
                group_name = self.model_parser.convert_to_reservation_model(command_context.remote_reservation) \
                    .reservation_id
                private_ip = self.model_parser.get_private_ip_from_connected_resource_details(
                    command_context)
                public_ip = self.model_parser.get_public_ip_from_connected_resource_details(
                    command_context)
                resource_fullname = self.model_parser.get_connected_resource_fullname(
                    command_context)

                with CloudShellSessionContext(
                        command_context) as cloudshell_session:
                    cloud_provider_model = self.model_parser.convert_to_cloud_provider_resource_model(
                        resource=command_context.resource,
                        cloudshell_session=cloudshell_session)

                    azure_clients = AzureClientsManager(cloud_provider_model)

                    self.refresh_ip_operation.refresh_ip(
                        cloudshell_session=cloudshell_session,
                        compute_client=azure_clients.compute_client,
                        network_client=azure_clients.network_client,
                        resource_group_name=group_name,
                        vm_name=vm_name,
                        private_ip_on_resource=private_ip,
                        public_ip_on_resource=public_ip,
                        resource_fullname=resource_fullname,
                        logger=logger)

                logger.info(
                    'Azure VM IPs were successfully refreshed'.format(vm_name))
示例#3
0
    def deploy_vm_from_custom_image(self, command_context, deploy_action,
                                    cancellation_context):
        """Deploy Azure Image from given Image URN

        :param ResourceCommandContext command_context: ResourceCommandContext instance
        :param cloudshell.cp.core.models.DeployApp deploy_action: describes the desired deployment
        :param CancellationContext cancellation_context:
        :return:
        """
        with LoggingSessionContext(command_context) as logger:
            with ErrorHandlingContext(logger):
                logger.info('Deploying Azure VM From Custom Image...')

                with CloudShellSessionContext(
                        command_context) as cloudshell_session:
                    azure_vm_deployment_model = self.model_parser. \
                        convert_to_deploy_azure_vm_from_custom_image_resource_model(
                        deploy_action=deploy_action,
                        cloudshell_session=cloudshell_session,
                        logger=logger)

                    cloud_provider_model = self.model_parser.convert_to_cloud_provider_resource_model(
                        resource=command_context.resource,
                        cloudshell_session=cloudshell_session)

                azure_clients = AzureClientsManager(cloud_provider_model)

                result = self.deploy_azure_vm_operation.deploy_from_custom_image(
                    deployment_model=azure_vm_deployment_model,
                    cloud_provider_model=cloud_provider_model,
                    reservation=self.model_parser.convert_to_reservation_model(
                        command_context.reservation),
                    network_client=azure_clients.network_client,
                    compute_client=azure_clients.compute_client,
                    storage_client=azure_clients.storage_client,
                    cancellation_context=cancellation_context,
                    logger=logger,
                    cloudshell_session=cloudshell_session)

                logger.info('End deploying Azure VM From Custom Image')

                return result
示例#4
0
    def get_access_key(self, command_context):
        """Returns public key
        :param ResourceRemoteCommandContext command_context:
        :rtype str:
        """
        with LoggingSessionContext(command_context) as logger:
            with ErrorHandlingContext(logger):
                logger.info("Starting GetAccessKey...")

                with CloudShellSessionContext(
                        command_context) as cloudshell_session:
                    cloud_provider_model = self.model_parser.convert_to_cloud_provider_resource_model(
                        resource=command_context.resource,
                        cloudshell_session=cloudshell_session)

                azure_clients = AzureClientsManager(cloud_provider_model)
                resource_group_name = \
                    self.model_parser.convert_to_reservation_model(command_context.remote_reservation).reservation_id

                return self.access_key_operation.get_access_key(
                    storage_client=azure_clients.storage_client,
                    group_name=resource_group_name)
示例#5
0
    def prepare_connectivity(self, context, actions, cancellation_context):
        """
        Creates a connectivity for the Sandbox:
        1.Resource group
        2.Storage account
        3.Key pair
        4.Network Security Group
        5.Creating a subnet under the

        :param context:
        :param actions: list[cloudshell.cp.core.models.RequestActionBase]
        :param cancellation_context cloudshell.shell.core.driver_context.CancellationContext instance
        :return:
        """
        with LoggingSessionContext(context) as logger:
            with ErrorHandlingContext(logger):
                logger.info('Preparing Connectivity for Azure VM...')

                with CloudShellSessionContext(context) as cloudshell_session:
                    cloud_provider_model = self.model_parser.convert_to_cloud_provider_resource_model(
                        resource=context.resource,
                        cloudshell_session=cloudshell_session)

                azure_clients = AzureClientsManager(cloud_provider_model)

                result = self.prepare_connectivity_operation.prepare_connectivity(
                    reservation=self.model_parser.convert_to_reservation_model(
                        context.reservation),
                    cloud_provider_model=cloud_provider_model,
                    storage_client=azure_clients.storage_client,
                    resource_client=azure_clients.resource_client,
                    network_client=azure_clients.network_client,
                    logger=logger,
                    actions=actions,
                    cancellation_context=cancellation_context)

                logger.info('End Preparing Connectivity for Azure VM')
                return result
    def setUp(self):
        self.cloud_provider = mock.MagicMock()

        with mock.patch("cloudshell.cp.azure.common.azure_clients.ServicePrincipalCredentials"):
            self.azure_clients_manager = AzureClientsManager(cloud_provider=self.cloud_provider)
class TesAzureClientsManager(TestCase):
    def setUp(self):
        self.cloud_provider = mock.MagicMock()

        with mock.patch("cloudshell.cp.azure.common.azure_clients.ServicePrincipalCredentials"):
            self.azure_clients_manager = AzureClientsManager(cloud_provider=self.cloud_provider)

    def test_init(self):
        """Check that __init__ method sets correct params for the instance"""
        cloud_provider = mock.MagicMock()
        subscription_id = mock.MagicMock()
        application_id = mock.MagicMock()
        application_key = mock.MagicMock()
        tenant = mock.MagicMock()
        service_credentials = mock.MagicMock()

        with mock.patch.object(AzureClientsManager, "_get_subscription", return_value=subscription_id):
            with mock.patch.object(AzureClientsManager, "_get_azure_application_id", return_value=application_id):
                with mock.patch.object(AzureClientsManager, "_get_azure_application_key", return_value=application_key):
                    with mock.patch.object(AzureClientsManager, "_get_azure_tenant", return_value=tenant):
                        with mock.patch.object(AzureClientsManager, "_get_service_credentials",
                                               return_value=service_credentials):
                            # Act
                            azure_clients_manager = AzureClientsManager(cloud_provider=cloud_provider)

                            # Verify
                            self.assertEqual(azure_clients_manager._subscription_id, subscription_id)
                            self.assertEqual(azure_clients_manager._application_id, application_id)
                            self.assertEqual(azure_clients_manager._application_key, application_key)
                            self.assertEqual(azure_clients_manager._tenant, tenant)
                            self.assertEqual(azure_clients_manager._service_credentials, service_credentials)
                            self.assertIsNone(azure_clients_manager._compute_client)
                            self.assertIsNone(azure_clients_manager._network_client)
                            self.assertIsNone(azure_clients_manager._storage_client)
                            self.assertIsNone(azure_clients_manager._resource_client)

    def test_check_params_equality_returns_true(self):
        """Check that method will return True if cloud_provider attributes are the same as in current model"""
        # Act
        with mock.patch("cloudshell.cp.azure.common.azure_clients.ServicePrincipalCredentials"):
            azure_clients_manager = AzureClientsManager(cloud_provider=self.cloud_provider)

        # Verify
        self.assertIs(self.azure_clients_manager, azure_clients_manager)

    def test_check_params_equality_returns_false(self):
        """Check that method will return False if cloud_provider attributes aren't the same as in current model"""
        cloud_provider = mock.MagicMock()
        # Act
        with mock.patch("cloudshell.cp.azure.common.azure_clients.ServicePrincipalCredentials"):
            azure_clients_manager = AzureClientsManager(cloud_provider=cloud_provider)

        # Verify
        self.assertIsNot(self.azure_clients_manager, azure_clients_manager)

    @mock.patch("cloudshell.cp.azure.common.azure_clients.ServicePrincipalCredentials")
    def test_get_service_credentials(self, service_credentials_class):
        """Check that method returns ServicePrincipalCredentials instance"""
        mocked_service_credentials = mock.MagicMock()
        service_credentials_class.return_value = mocked_service_credentials

        # Act
        service_credentials = self.azure_clients_manager._get_service_credentials()

        # Verify
        service_credentials_class.assert_called_once_with(client_id=self.azure_clients_manager._application_id,
                                                          secret=self.azure_clients_manager._application_key,
                                                          tenant=self.azure_clients_manager._tenant)
        self.assertIs(service_credentials, mocked_service_credentials)

    def test_get_subscription(self):
        """"""
        # Act
        subscription = self.azure_clients_manager._get_subscription(self.cloud_provider)
        # Verify
        self.assertEqual(subscription, self.cloud_provider.azure_subscription_id)

    def test_get_azure_application_id(self):
        """"""
        # Act
        azure_application_id = self.azure_clients_manager._get_azure_application_id(self.cloud_provider)
        # Verify
        self.assertEqual(azure_application_id, self.cloud_provider.azure_application_id)

    def test_get_azure_application_key(self):
        """"""
        # Act
        azure_application_key = self.azure_clients_manager._get_azure_application_key(self.cloud_provider)
        # Verify
        self.assertEqual(azure_application_key, self.cloud_provider.azure_application_key)

    def test_get_azure_tenant(self):
        """"""
        # Act
        azure_tenant = self.azure_clients_manager._get_azure_tenant(self.cloud_provider)
        # Verify
        self.assertEqual(azure_tenant, self.cloud_provider.azure_tenant)

    @mock.patch("cloudshell.cp.azure.common.azure_clients.ComputeManagementClient")
    def test_compute_client(self, compute_client_class):
        """Check that property will get ComputeManagementClient client and initialize client only once"""
        mocked_compute_client = mock.MagicMock()
        compute_client_class.return_value = mocked_compute_client
        # Act
        compute_client = self.azure_clients_manager.compute_client
        # repeat property call to verify that method will not create one more instance
        compute_client = self.azure_clients_manager.compute_client

        # Verify
        self.assertIs(compute_client, mocked_compute_client)
        self.assertIs(compute_client, self.azure_clients_manager._compute_client)
        compute_client_class.assert_called_once_with(self.azure_clients_manager._service_credentials,
                                                     self.azure_clients_manager._subscription_id)

    @mock.patch("cloudshell.cp.azure.common.azure_clients.NetworkManagementClient")
    def test_network_client(self, network_client_class):
        """Check that property will get NetworkManagementClient client and initialize client only once"""
        mocked_compute_client = mock.MagicMock()
        network_client_class.return_value = mocked_compute_client
        # Act
        network_client = self.azure_clients_manager.network_client
        # repeat property call to verify that method will not create one more instance
        network_client = self.azure_clients_manager.network_client

        # Verify
        self.assertIs(network_client, mocked_compute_client)
        self.assertIs(network_client, self.azure_clients_manager._network_client)
        network_client_class.assert_called_once_with(self.azure_clients_manager._service_credentials,
                                                     self.azure_clients_manager._subscription_id)

    @mock.patch("cloudshell.cp.azure.common.azure_clients.ResourceManagementClient")
    def test_storage_client(self, resource_client_class):
        """Check that property will get ResourceManagementClient client and initialize client only once"""
        mocked_compute_client = mock.MagicMock()
        resource_client_class.return_value = mocked_compute_client
        # Act
        resource_client = self.azure_clients_manager.resource_client
        # repeat property call to verify that method will not create one more instance
        resource_client = self.azure_clients_manager.resource_client

        # Verify
        self.assertIs(resource_client, mocked_compute_client)
        self.assertIs(resource_client, self.azure_clients_manager._resource_client)
        resource_client_class.assert_called_once_with(self.azure_clients_manager._service_credentials,
                                                      self.azure_clients_manager._subscription_id)

    @mock.patch("cloudshell.cp.azure.common.azure_clients.StorageManagementClient")
    def test_resource_client(self, storage_client_class):
        """Check that property will get StorageManagementClient client and initialize client only once"""
        mocked_compute_client = mock.MagicMock()
        storage_client_class.return_value = mocked_compute_client
        # Act
        storage_client = self.azure_clients_manager.storage_client
        # repeat property call to verify that method will not create one more instance
        storage_client = self.azure_clients_manager.storage_client

        # Verify
        self.assertIs(storage_client, mocked_compute_client)
        self.assertIs(storage_client, self.azure_clients_manager._storage_client)
        storage_client_class.assert_called_once_with(self.azure_clients_manager._service_credentials,
                                                     self.azure_clients_manager._subscription_id)

    @mock.patch("cloudshell.cp.azure.common.azure_clients.SubscriptionClient")
    def test_subscription_client(self, subscription_client_class):
        """Check that property will get SubscriptionClient client and initialize client only once"""
        mocked_subscription_client = mock.MagicMock()
        subscription_client_class.return_value = mocked_subscription_client
        # Act
        subscription_client = self.azure_clients_manager.subscription_client
        # repeat property call to verify that method will not create one more instance
        subscription_client = self.azure_clients_manager.subscription_client

        # Verify
        self.assertIs(subscription_client, mocked_subscription_client)
        self.assertIs(subscription_client, self.azure_clients_manager._subscription_client)
        subscription_client_class.assert_called_once_with(self.azure_clients_manager._service_credentials)