예제 #1
0
 def ping(self, raise_exception=False):
     try:
         self.client.list_locations()
     except AzureBackendError as e:
         if raise_exception:
             raise AzureBackendError(e)
         return False
     else:
         return True
예제 #2
0
    def pull_images(self, location):
        cached_images = {
            image.backend_id: image
            for image in models.Image.objects.filter(settings=self.settings,
                                                     location=location)
        }

        try:
            backend_images = {
                image_wrapper.image.id: image_wrapper
                for image_wrapper in islice(
                    self.client.list_virtual_machine_images(
                        location.backend_id,
                        ['MicrosoftSQLServer', 'Debian', 'Canonical'],
                    ),
                    10,
                )
            }
        except HttpResponseError as e:
            if e.error.code == 'NoRegisteredProviderFound':
                backend_images = {}
            else:
                raise AzureBackendError(e)

        new_images = {
            backend_id: image_wrapper
            for backend_id, image_wrapper in backend_images.items()
            if backend_id not in cached_images
        }

        stale_images = {
            backend_id: image
            for backend_id, image in cached_images.items()
            if backend_id not in backend_images
        }

        for backend_image_name in new_images:
            backend_image: AzureImage = new_images[backend_image_name]
            models.Image.objects.create(
                backend_id=backend_image.image.id,
                offer=backend_image.offer_name,
                publisher=backend_image.publisher_name,
                sku=backend_image.sku_name,
                version=backend_image.version_name,
                name=f'{backend_image.offer_name} {backend_image.version_name}',
                settings=self.settings,
                location=location,
            )

        for cached_image_name in stale_images:
            stale_images[cached_image_name].delete()
예제 #3
0
    def pull_sizes(self, location):
        cached_sizes = {
            size.backend_id: size
            for size in models.Size.objects.filter(settings=self.settings)
        }

        try:
            backend_sizes = {
                size.name: size
                for size in self.client.list_virtual_machine_sizes(
                    location.backend_id)
            }
        except HttpResponseError as e:
            if e.error.code == 'NoRegisteredProviderFound':
                backend_sizes = {}
            else:
                raise AzureBackendError(e)

        new_sizes = {
            name: size
            for name, size in backend_sizes.items() if name not in cached_sizes
        }

        stale_sizes = {
            name: size
            for name, size in cached_sizes.items() if name not in backend_sizes
        }

        for backend_size_name in new_sizes:
            backend_size = new_sizes[backend_size_name]
            models.Size.objects.create(
                backend_id=backend_size.name,
                settings=self.settings,
                **backend_size.as_dict(),
            )

        for cached_size_name in stale_sizes:
            stale_sizes[cached_size_name].delete()
예제 #4
0
 def test_invalid_credentials_are_not_accepted(self, mocked_backend):
     mocked_backend().ping.side_effect = AzureBackendError(
         'Invalid credentials')
     self.client.force_authenticate(self.fixture.owner)
     response = self.client.post(self.url, self.get_valid_payload())
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)