Esempio n. 1
0
    def update(self, request, id):
        """@description-title Update a boot source
        @description Update a boot source with the given id.

        @param (string) "{id}" [required=true] A boot-source id.

        @param (string) "url" [required=false] The URL of the BootSource.

        @param (string) "keyring_filename" [required=false] The path to the
        keyring file for this BootSource.

        @param (string) "keyring_data" [required=false] The GPG keyring for
        this BootSource, base64-encoded data.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the updated
        boot-source object.
        @success-example "success-json" [exkey=boot-sources-update] placeholder
        text

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested boot-source is not found.
        @error-example "not-found"
            Not Found
        """
        boot_source = get_object_or_404(BootSource, id=id)
        form = BootSourceForm(data=request.data,
                              files=request.FILES,
                              instance=boot_source)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
Esempio n. 2
0
    def create(self, request):
        """@description-title Create a boot source
        @description Create a new boot source. Note that in addition to
        ``url``, you must supply either ``keyring_data`` or
        ``keyring_filename``.

        @param (string) "url" [required=true] The URL of the BootSource.

        @param (string) "keyring_filename" [required=false] The path to the
        keyring file for this BootSource.

        @param (string) "keyring_data" [required=false] The GPG keyring for
        this BootSource, base64-encoded.

        @success (http-status-code) "server-success" 201
        @success (json) "success-json" A JSON object containing the new boot
        source.
        @success-example "success-json" [exkey=boot-sources-create] placeholder
        text
        """
        form = BootSourceForm(data=request.data, files=request.FILES)
        if form.is_valid():
            boot_source = form.save()
            handler = BootSourceHandler()
            emitter = JSONEmitter(boot_source, typemapper, handler,
                                  handler.fields, False)
            return HttpResponse(
                emitter.render(request),
                content_type="application/json; charset=utf-8",
                status=int(http.client.CREATED),
            )
        else:
            raise MAASAPIValidationError(form.errors)
Esempio n. 3
0
 def test_creates_boot_source_object_with_keyring_filename(self):
     params = {
         "url": "http://example.com/",
         "keyring_filename": factory.make_name("keyring_filename"),
     }
     form = BootSourceForm(data=params)
     self.assertTrue(form.is_valid(), form._errors)
     boot_source = form.save()
     self.assertAttributes(boot_source, params)
Esempio n. 4
0
 def test_edits_boot_source_object(self):
     boot_source = factory.make_BootSource()
     params = {
         "url": "http://example.com/",
         "keyring_filename": factory.make_name("keyring_filename"),
     }
     form = BootSourceForm(instance=boot_source, data=params)
     self.assertTrue(form.is_valid(), form._errors)
     form.save()
     boot_source = reload_object(boot_source)
     self.assertAttributes(boot_source, params)
Esempio n. 5
0
 def test_creates_boot_source_object_with_keyring_data(self):
     in_mem_file = InMemoryUploadedFile(
         BytesIO(sample_binary_data),
         name=factory.make_name('name'),
         field_name=factory.make_name('field-name'),
         content_type='application/octet-stream',
         size=len(sample_binary_data),
         charset=None)
     params = {'url': 'http://example.com/'}
     form = BootSourceForm(data=params, files={'keyring_data': in_mem_file})
     self.assertTrue(form.is_valid(), form._errors)
     boot_source = form.save()
     self.assertEqual(sample_binary_data, bytes(boot_source.keyring_data))
     self.assertAttributes(boot_source, params)
Esempio n. 6
0
    def update(self, request, id):
        """Update a specific boot source.

        :param url: The URL of the BootSource.
        :param keyring_filename: The path to the keyring file for this
            BootSource.
        :param keyring_data: The GPG keyring for this BootSource,
            base64-encoded data.
        """
        boot_source = get_object_or_404(BootSource, id=id)
        form = BootSourceForm(data=request.data,
                              files=request.FILES,
                              instance=boot_source)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
Esempio n. 7
0
    def create(self, request):
        """Create a new boot source.

        :param url: The URL of the BootSource.
        :param keyring_filename: The path to the keyring file for
            this BootSource.
        :param keyring_data: The GPG keyring for this BootSource,
            base64-encoded.
        """
        form = BootSourceForm(data=request.data, files=request.FILES)
        if form.is_valid():
            boot_source = form.save()
            handler = BootSourceHandler()
            emitter = JSONEmitter(boot_source, typemapper, handler,
                                  handler.fields, False)
            return HttpResponse(emitter.render(request),
                                content_type="application/json; charset=utf-8",
                                status=int(http.client.CREATED))
        else:
            raise MAASAPIValidationError(form.errors)