def create(self, request): """Add a new SSH key to the requesting or supplied user's account. The request payload should contain the public SSH key data in form data whose name is "key". """ user = request.user username = get_optional_param(request.POST, 'user') if username is not None and request.user.is_superuser: supplied_user = get_one(User.objects.filter(username=username)) if supplied_user is not None: user = supplied_user else: # Raise an error so that the user can know that their # attempt at specifying a user did not work. raise MAASAPIValidationError( "Supplied username does not match any current users.") elif username is not None and not request.user.is_superuser: raise MAASAPIValidationError( "Only administrators can specify a user" " when creating an SSH key.") form = SSHKeyForm(user=user, data=request.data) if form.is_valid(): sshkey = form.save(ENDPOINT.API, request) emitter = JSONEmitter(sshkey, typemapper, None, DISPLAY_SSHKEY_FIELDS) stream = emitter.render(request) return HttpResponse(stream, content_type='application/json; charset=utf-8', status=int(http.client.CREATED)) else: raise MAASAPIValidationError(form.errors)
def create(self, request): """@description-title Add a new SSL key @description Add a new SSL key to the requesting user's account. @param (string) "key" [required=true,formatting=true] An SSL key should be provided in the request payload as form data with the name 'key': key: "key data" - ``key data``: The contents of a pem file. @success (http-status-code) "201" 201 @success (json) "success-json" A JSON object containing the new key. @success-example "success-json" [exkey=ssl-keys-create] placeholder text """ form = SSLKeyForm(user=request.user, data=request.data) if form.is_valid(): sslkey = form.save(ENDPOINT.API, request) emitter = JSONEmitter(sslkey, typemapper, None, DISPLAY_SSLKEY_FIELDS) stream = emitter.render(request) return HttpResponse( stream, content_type="application/json; charset=utf-8", status=int(http.client.CREATED), ) else: raise MAASAPIValidationError(form.errors)
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)
def create(self, request): """@description-title Add a new SSH key @description Add a new SSH key to the requesting or supplied user's account. @param (string) "key" [required=true,formatting=true] A public SSH key should be provided in the request payload as form data with the name 'key': key: "key-type public-key-data" - ``key-type``: ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, ecdsa-sha2-nistp521, ssh-dss, ssh-ed25519, ssh-rsa - ``public key data``: Base64-encoded key data. @success (http-status-code) "201" 201 @success (json) "success-json" A JSON object containing the new key. @success-example "success-json" [exkey=ssh-keys-create] placeholder text """ user = request.user username = get_optional_param(request.POST, "user") if username is not None and request.user.is_superuser: supplied_user = get_one(User.objects.filter(username=username)) if supplied_user is not None: user = supplied_user else: # Raise an error so that the user can know that their # attempt at specifying a user did not work. raise MAASAPIValidationError( "Supplied username does not match any current users." ) elif username is not None and not request.user.is_superuser: raise MAASAPIValidationError( "Only administrators can specify a user" " when creating an SSH key." ) form = SSHKeyForm(user=user, data=request.data) if form.is_valid(): sshkey = form.save(ENDPOINT.API, request) emitter = JSONEmitter( sshkey, typemapper, None, DISPLAY_SSHKEY_FIELDS ) stream = emitter.render(request) return HttpResponse( stream, content_type="application/json; charset=utf-8", status=int(http.client.CREATED), ) else: raise MAASAPIValidationError(form.errors)
def create(self, request): """Add a new SSH key to the requesting user's account. The request payload should contain the public SSH key data in form data whose name is "key". """ form = SSHKeyForm(user=request.user, data=request.data) if form.is_valid(): sshkey = form.save() emitter = JSONEmitter(sshkey, typemapper, None, DISPLAY_SSHKEY_FIELDS) stream = emitter.render(request) return HttpResponse(stream, content_type='application/json; charset=utf-8', status=int(http.client.CREATED)) else: raise MAASAPIValidationError(form.errors)
def json_file_storage(stored_file, request): # Convert stored_file into a json object: use the same fields used # when serialising lists of object, plus the base64-encoded content. dict_representation = { fieldname: getattr(stored_file, fieldname) for fieldname in DISPLAYED_FILES_FIELDS } # Encode the content as base64. dict_representation["content"] = b64encode(getattr(stored_file, "content")) dict_representation["resource_uri"] = reverse("file_handler", args=[stored_file.filename]) # Emit the json for this object manually because, no matter what the # piston documentation says, once a type is associated with a list # of fields by piston's typemapper mechanism, there is no way to # override that in a specific handler with 'fields' or 'exclude'. emitter = JSONEmitter(dict_representation, typemapper, None) stream = emitter.render(request) return stream
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)
def json_object(obj, request): """Convert object into a json object.""" emitter = JSONEmitter(obj, typemapper, None) stream = emitter.render(request) return stream