def test_metadata_download(self):
        IMAGE_ID = u'1'
        image_dict = {
            'name': 'visibleImage',
            'container_format': 'novaImage',
            'location': 'swift://localhost/',
            'container_format': '',
            'disk_format': '',
            'size': '',
            'image.min_disk': '',
            'image.min_ram': ''
        }
        image = api.Image(image_dict)

        self.visibleImage = api.Image(image_dict)

        self.mox.StubOutWithMock(api, 'image_get_meta')
        api.image_get_meta(IsA(http.HttpRequest), IMAGE_ID).AndReturn(image)

        self.mox.ReplayAll()

        res = self.client.get(
            reverse('dash_metadata_download',
                    args=[self.TEST_TENANT, IMAGE_ID]))

        #self.assertIn('images', res.context)
        #images = res.context['images']
        #self.assertEqual(len(images), 1)
        #self.assertEqual(images[0].name, 'visibleImage')

        self.mox.VerifyAll()
    def test_metadata_download_noimage(self):
        IMAGE_ID = u'1'
        self.mox.StubOutWithMock(api, 'image_get_meta')
        api.image_get_meta(IsA(http.HttpRequest), IMAGE_ID).AndReturn(None)

        self.mox.ReplayAll()

        res = self.client.post(
            reverse('dash_metadata_download',
                    args=[self.TEST_TENANT, IMAGE_ID]))

        self.assertRedirectsNoFollow(
            res, reverse('dash_images_metadata', args=[self.TEST_TENANT]))

        self.mox.VerifyAll()
    def test_launch_get(self):
        IMAGE_ID = '1'

        self.mox.StubOutWithMock(api, 'image_get_meta')
        api.image_get_meta(IsA(http.HttpRequest),
                      IMAGE_ID).AndReturn(self.visibleImage)

        self.mox.StubOutWithMock(api, 'tenant_quota_get')
        api.tenant_quota_get(IsA(http.HttpRequest),
                             self.TEST_TENANT).AndReturn(FakeQuota)

        self.mox.StubOutWithMock(api, 'token_get_tenant')
        api.token_get_tenant(IsA(http.HttpRequest),
                             self.TEST_TENANT).AndReturn(self.TEST_TENANT)

        self.mox.StubOutWithMock(api, 'flavor_list')
        api.flavor_list(IsA(http.HttpRequest)).AndReturn(self.flavors)

        self.mox.StubOutWithMock(api, 'keypair_list')
        api.keypair_list(IsA(http.HttpRequest)).AndReturn(self.keypairs)

        self.mox.StubOutWithMock(api, 'security_group_list')
        api.security_group_list(IsA(http.HttpRequest)).AndReturn(
                                    self.security_groups)

        self.mox.ReplayAll()

        res = self.client.get(reverse('dash_images_launch',
                              args=[self.TEST_TENANT, IMAGE_ID]))

        self.assertTemplateUsed(res,
                'django_openstack/dash/images/launch.html')

        image = res.context['image']
        self.assertEqual(image.name, self.visibleImage.name)

        self.assertEqual(res.context['tenant'], self.TEST_TENANT)

        form = res.context['form']

        form_flavorfield = form.fields['flavor']
        self.assertIn('m1.massive', form_flavorfield.choices[0][1])

        form_keyfield = form.fields['key_name']
        self.assertEqual(form_keyfield.choices[0][0],
                         self.keypairs[0].name)

        self.mox.VerifyAll()
示例#4
0
def download(request, tenant_id, image_id):
    image = None
    try:
        image = api.image_get_meta(request, image_id)
    except glance_exception.ClientConnectionError, e:
        LOG.exception("Error connecting to glance")
        messages.error(request, "Error connecting to glance: %s" % str(e))
示例#5
0
def update(request, tenant_id, image_id):
    try:
        image = api.image_get_meta(request, image_id)
    except glance_exception.ClientConnectionError, e:
        LOG.exception("Error connecting to glance")
        messages.error(request, "Error connecting to glance: %s"
                                 % e.message)
示例#6
0
def download(request, tenant_id, image_id):
    image = None
    try:
        image = api.image_get_meta(request, image_id)
    except glance_exception.ClientConnectionError, e:
        LOG.exception("Error connecting to glance")
        messages.error(request, "Error connecting to glance: %s" % str(e))
def update(request, tenant_id, image_id):
    try:
        image = api.image_get_meta(request, image_id)
    except glance_exception.ClientConnectionError, e:
        LOG.exception("Error connecting to glance")
        messages.error(request, "Error connecting to glance: %s"
                                 % e.message)
    def test_download_metadata_api_error(self):
        IMAGE_ID = '1'

        self.mox.StubOutWithMock(api, 'image_get_meta')
        exception = api_exceptions.ApiException('ApiError')
        api.image_get_meta(IsA(http.HttpRequest), IMAGE_ID).AndRaise(exception)

        self.mox.StubOutWithMock(messages, 'error')
        messages.error(IsA(http.HttpRequest), IsA(str))

        self.mox.ReplayAll()

        res = self.client.get(
            reverse('dash_metadata_download',
                    args=[self.TEST_TENANT, IMAGE_ID]))

        self.assertRedirectsNoFollow(
            res, reverse('dash_images_metadata', args=[self.TEST_TENANT]))

        self.mox.VerifyAll()
    def handle(self, request, data):
        image_id = data['image_id']
        tenant_id = request.user.tenant_id
        error_retrieving = _('Unable to retreive image info from glance: %s' % image_id)
        error_updating = _('Error updating image with id: %s' % image_id)

        try:
            image = api.image_get_meta(request, image_id)
        except glance_exception.ClientConnectionError, e:
            LOG.exception(_('Error connecting to glance'))
            messages.error(request, error_retrieving)
    def test_launch_keypairlist_error(self):
        IMAGE_ID = '2'

        self.mox.StubOutWithMock(api, 'image_get_meta')
        api.image_get_meta(IsA(http.HttpRequest),
                      IMAGE_ID).AndReturn(self.visibleImage)

        self.mox.StubOutWithMock(api, 'token_get_tenant')
        api.token_get_tenant(IsA(http.HttpRequest),
                             self.TEST_TENANT).AndReturn(self.TEST_TENANT)

        self.mox.StubOutWithMock(api, 'tenant_quota_get')
        api.tenant_quota_get(IsA(http.HttpRequest),
                             self.TEST_TENANT).AndReturn(FakeQuota)

        self.mox.StubOutWithMock(api, 'flavor_list')
        api.flavor_list(IsA(http.HttpRequest)).AndReturn(self.flavors)

        exception = api_exceptions.ApiException('apiException')
        self.mox.StubOutWithMock(api, 'keypair_list')
        api.keypair_list(IsA(http.HttpRequest)).AndRaise(exception)

        self.mox.StubOutWithMock(api, 'security_group_list')
        api.security_group_list(IsA(http.HttpRequest)).AndReturn(
                                    self.security_groups)

        self.mox.ReplayAll()

        res = self.client.get(reverse('dash_images_launch',
                              args=[self.TEST_TENANT, IMAGE_ID]))

        self.assertTemplateUsed(res,
                'django_openstack/dash/images/launch.html')

        form = res.context['form']

        form_keyfield = form.fields['key_name']
        self.assertEqual(len(form_keyfield.choices), 0)

        self.mox.VerifyAll()
示例#11
0
    def test_metadata_update_post(self):
        self.setActiveUser(self.TEST_TOKEN, self.TEST_USER, self.TEST_TENANT)
        IMAGE_ID = u'1'
        image_dict = {
            'name': 'visibleImage',
            'container_format': 'novaImage',
            'location': 'swift://localhost/',
            'container_format': '',
            'disk_format': '',
            'image.size': '',
            'image.min_disk': '',
            'image.min_ram': ''
        }
        image = api.Image(image_dict)
        form_data = {
            'method': 'UpdateImageForm',
            'name': 'visibleImage',
            'image_id': IMAGE_ID,
            'location': 'swift://localhost/',
            'user': '******',
            'password': '******'
        }

        self.visibleImage = api.Image(image_dict)

        self.mox.StubOutWithMock(api, 'image_get_meta')
        api.image_get_meta(IsA(http.HttpRequest), IMAGE_ID).AndReturn(image)

        self.mox.ReplayAll()

        res = self.client.post(
            reverse('dash_metadata_update', args=[self.TEST_TENANT, IMAGE_ID]),
            form_data)

        #self.assertIn('images', res.context)
        #images = res.context['images']
        #self.assertEqual(len(images), 1)
        #self.assertEqual(images[0].name, 'visibleImage')

        self.mox.VerifyAll()
示例#12
0
    def test_metadata_download_invalid(self):
        IMAGE_ID = u'1'
        image_dict = {
            'name': 'visibleImage',
            'container_format': 'novaImage',
            'location': 'swift://localhost/'
        }
        image = api.Image(image_dict)

        self.visibleImage = api.Image(image_dict)

        self.mox.StubOutWithMock(api, 'image_get_meta')
        api.image_get_meta(IsA(http.HttpRequest), IMAGE_ID).AndReturn(image)

        self.mox.ReplayAll()

        res = self.client.post(
            reverse('dash_metadata_download',
                    args=[self.TEST_TENANT, IMAGE_ID]))

        self.assertRedirectsNoFollow(
            res, reverse('dash_images_metadata', args=[self.TEST_TENANT]))
        self.mox.VerifyAll()
示例#13
0
 def handle(self, request, data):
     image_id = data['image_id']
     tenant_id = request.user.tenant_id
     try:
         image = api.image_get_meta(request, image_id)
         if image.owner == request.user.tenant_id:
             api.image_delete(request, image_id)
         else:
             messages.info(request, "Unable to delete image, you are not \
                                    its owner.")
             return redirect('dash_images_update', tenant_id, image_id)
     except glance_exception.ClientConnectionError, e:
         LOG.exception("Error connecting to glance")
         messages.error(request, "Error connecting to glance: %s"
                                 % e.message)
示例#14
0
    def handle(self, request, data):
        image_id = data['image_id']
        tenant_id = request.user.tenant_id
        error_retrieving = _('Unable to retreive image info from glance: %s' % image_id)
        error_updating = _('Error updating image with id: %s' % image_id)

        scheme, location = _parse_location(data['location'])
        auth = ":".join([data['user'] , data['password']])

        try:
            image = api.image_get_meta(request, image_id)
            #image = api.image_get(request, image_id)
        except glance_exception.ClientConnectionError, e:
            LOG.exception(_('Error connecting to glance'))
            messages.error(request, error_retrieving)
示例#15
0
class UpdateImageForm(forms.SelfHandlingForm):
    image_id = forms.CharField(widget=forms.HiddenInput())
    name = forms.CharField(max_length="25", label="Name")
    location = forms.CharField(label="Location")
    user = forms.CharField(label="User")
    password = forms.CharField(widget=forms.PasswordInput, label="Password")

    def handle(self, request, data):
        image_id = data['image_id']
        tenant_id = request.user.tenant_id
        error_retrieving = _('Unable to retreive image info from glance: %s' %
                             image_id)
        error_updating = _('Error updating image with id: %s' % image_id)

        scheme, loc, path = _parse_location(data['location'])
        auth = ":".join([data['user'], data['password']])

        image_name = data['name']
        try:
            check_name = "Image name"
            image_name.encode('ascii')
            check_name = "User"
            data['user'].encode('ascii')
            check_name = "Password"
            data['password'].encode('ascii')
        except UnicodeEncodeError, e:
            messages.error(
                request,
                "%s contains non ASCII character: %s" % (check_name, str(e)))
            return

        location = "%s://%s@%s%s" % (scheme, auth, loc, quote(path))

        try:
            image = api.image_get_meta(request, image_id)
            #image = api.image_get(request, image_id)
        except glance_exception.ClientConnectionError, e:
            LOG.exception(_('Error connecting to glance'))
            messages.error(request, error_retrieving)
示例#16
0
    def handle(self, request, data):
        image_id = data['image_id']
        tenant_id = data['tenant_id']
        try:
            image = api.image_get_meta(request, image_id)
            flavor = api.flavor_get(request, data['flavor'])
            api.server_create(request,
                              data['name'],
                              image,
                              flavor,
                              data.get('key_name'),
                              normalize_newlines(data.get('user_data')),
                              data.get('security_groups'))

            msg = 'Instance was successfully launched'
            LOG.info(msg)
            messages.success(request, msg)
            return redirect('dash_instances', tenant_id)

        except api_exceptions.ApiException, e:
            LOG.exception('ApiException while creating instances of image "%s"'
                           % image_id)
            messages.error(request,
                           'Unable to launch instance: %s' % e.message)
    def test_launch_post(self):
        FLAVOR_ID = self.flavors[0].id
        IMAGE_ID = '1'
        KEY_NAME = self.keypairs[0].name
        SERVER_NAME = 'serverName'
        USER_DATA = 'userData'

        form_data = {'method': 'LaunchForm',
                     'flavor': FLAVOR_ID,
                     'image_id': IMAGE_ID,
                     'key_name': KEY_NAME,
                     'name': SERVER_NAME,
                     'user_data': USER_DATA,
                     'tenant_id': self.TEST_TENANT,
                     'security_groups': 'default',
                     }

        self.mox.StubOutWithMock(api, 'image_get_meta')
        api.image_get_meta(IsA(http.HttpRequest),
                      IMAGE_ID).AndReturn(self.visibleImage)

        self.mox.StubOutWithMock(api, 'token_get_tenant')
        api.token_get_tenant(IsA(http.HttpRequest),
                             self.TEST_TENANT).AndReturn(self.TEST_TENANT)

        self.mox.StubOutWithMock(api, 'tenant_quota_get')
        api.tenant_quota_get(IsA(http.HttpRequest),
                             self.TEST_TENANT).AndReturn(FakeQuota)

        self.mox.StubOutWithMock(api, 'flavor_list')
        api.flavor_list(IsA(http.HttpRequest)).AndReturn(self.flavors)

        self.mox.StubOutWithMock(api, 'keypair_list')
        api.keypair_list(IsA(http.HttpRequest)).AndReturn(self.keypairs)

        self.mox.StubOutWithMock(api, 'security_group_list')
        api.security_group_list(IsA(http.HttpRequest)).AndReturn(
                                    self.security_groups)

        # called again by the form
        api.image_get_meta(IsA(http.HttpRequest),
                      IMAGE_ID).AndReturn(self.visibleImage)

        self.mox.StubOutWithMock(api, 'flavor_get')
        api.flavor_get(IsA(http.HttpRequest),
                       IsA(unicode)).AndReturn(self.flavors[0])

        self.mox.StubOutWithMock(api, 'server_create')

        api.server_create(IsA(http.HttpRequest), SERVER_NAME,
                          self.visibleImage, self.flavors[0],
                          KEY_NAME, USER_DATA, [self.security_groups[0].name])

        self.mox.StubOutWithMock(messages, 'success')
        messages.success(IsA(http.HttpRequest), IsA(str))

        self.mox.ReplayAll()

        res = self.client.post(reverse('dash_images_launch',
                                        args=[self.TEST_TENANT, IMAGE_ID]),
                               form_data)

        self.assertRedirectsNoFollow(res, reverse('dash_instances',
                                          args=[self.TEST_TENANT]))

        self.mox.VerifyAll()
示例#18
0
        except api_exceptions.ApiException:
            LOG.exception('Unable to retrieve list of keypairs')
            return []

    def securitygrouplist():
        try:
            fl = api.security_group_list(request)
            sel = [(f.name, f.name) for f in fl]
            return sel
        except novaclient_exceptions.ClientException, e:
            LOG.exception('Unable to retrieve list of security groups')
            return []

    # TODO(mgius): Any reason why these can't be after the launchform logic?
    # If The form is valid, we've just wasted these two api calls
    image = api.image_get_meta(request, image_id)
    tenant = api.token_get_tenant(request, request.user.tenant_id)
    quotas = api.tenant_quota_get(request, request.user.tenant_id)
    try:
        quotas.ram = int(quotas.ram) / 100
    except Exception, e:
        messages.error(request, 'Error parsing quota  for %s: %s' %
                                 (image_id, e.message))
        return redirect('dash_instances', tenant_id)

    form, handled = LaunchForm.maybe_handle(
            request, initial={'flavorlist': flavorlist(),
                              'keynamelist': keynamelist(),
                              'securitygrouplist': securitygrouplist(),
                              'image_id': image_id,
                              'tenant_id': tenant_id})
    def test_launch_form_apiexception(self):
        FLAVOR_ID = self.flavors[0].id
        IMAGE_ID = '1'
        KEY_NAME = self.keypairs[0].name
        SERVER_NAME = 'serverName'
        USER_DATA = 'userData'

        form_data = {'method': 'LaunchForm',
                     'flavor': FLAVOR_ID,
                     'image_id': IMAGE_ID,
                     'key_name': KEY_NAME,
                     'name': SERVER_NAME,
                     'tenant_id': self.TEST_TENANT,
                     'user_data': USER_DATA,
                     'security_groups': 'default',
                     }

        self.mox.StubOutWithMock(api, 'image_get_meta')
        api.image_get_meta(IgnoreArg(),
                      IMAGE_ID).AndReturn(self.visibleImage)

        self.mox.StubOutWithMock(api, 'token_get_tenant')
        api.token_get_tenant(IgnoreArg(),
                             self.TEST_TENANT).AndReturn(self.TEST_TENANT)

        self.mox.StubOutWithMock(api, 'tenant_quota_get')
        api.tenant_quota_get(IsA(http.HttpRequest),
                             self.TEST_TENANT).AndReturn(FakeQuota)

        self.mox.StubOutWithMock(api, 'flavor_list')
        api.flavor_list(IgnoreArg()).AndReturn(self.flavors)

        self.mox.StubOutWithMock(api, 'keypair_list')
        api.keypair_list(IgnoreArg()).AndReturn(self.keypairs)

        self.mox.StubOutWithMock(api, 'security_group_list')
        api.security_group_list(IsA(http.HttpRequest)).AndReturn(
                                    self.security_groups)

        # called again by the form
        api.image_get_meta(IgnoreArg(),
                      IMAGE_ID).AndReturn(self.visibleImage)

        self.mox.StubOutWithMock(api, 'flavor_get')
        api.flavor_get(IgnoreArg(),
                       IsA(unicode)).AndReturn(self.flavors[0])

        self.mox.StubOutWithMock(api, 'server_create')

        exception = api_exceptions.ApiException('apiException')
        api.server_create(IsA(http.HttpRequest), SERVER_NAME,
                          self.visibleImage, self.flavors[0],
                          KEY_NAME, USER_DATA,
                          self.security_groups).AndRaise(exception)

        self.mox.StubOutWithMock(messages, 'error')
        messages.error(IsA(http.HttpRequest), IsA(str))

        self.mox.ReplayAll()
        url = reverse('dash_images_launch',
                      args=[self.TEST_TENANT, IMAGE_ID])
        res = self.client.post(url, form_data)

        self.assertTemplateUsed(res,
                'django_openstack/dash/images/launch.html')

        self.mox.VerifyAll()