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 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, params): """Create a SSHKey.""" form = SSHKeyForm(user=self.user, data=params) if form.is_valid(): try: obj = form.save() except ValidationError as e: try: raise HandlerValidationError(e.message_dict) except AttributeError: raise HandlerValidationError({"__all__": e.message}) return self.full_dehydrate(obj) else: raise HandlerValidationError(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)