def test_get_works(self): self.fake_client_set.compute.keypairs.find(name='kp')\ .AndReturn('K1') my_ssh_keys.keypair_to_view('K1').AndReturn('REPLY') self.mox.ReplayAll() rv = self.client.get('/v1/me/ssh-keys/kp') data = self.check_and_parse_response(rv) self.assertEquals(data, 'REPLY')
def test_upload_public(self): kp = doubles.make(self.mox, doubles.Keypair, name='TestKP', public_key='PUBKEY', fingerprint='FP') self.fake_client_set.compute.keypairs.create(kp.name, 'PUBLIC')\ .AndReturn(kp) my_ssh_keys.keypair_to_view(kp).AndReturn('REPLY') self.mox.ReplayAll() data = self.interact({'name': kp.name, 'public-key': 'PUBLIC'}) self.assertEquals(data, 'REPLY')
def test_generate_pair(self): kp = doubles.make(self.mox, doubles.Keypair, name='TestKP', public_key='PUBKEY', fingerprint='FP', private_key='PRIVATE') self.fake_client_set.compute.keypairs.create(kp.name, None)\ .AndReturn(kp) my_ssh_keys.keypair_to_view(kp).AndReturn({'FAKE': 1}) self.mox.ReplayAll() data = self.interact({'name': kp.name}) self.assertEquals(data, {'FAKE': 1, 'private-key': 'PRIVATE'})
def get_users_ssh_key(user_id, key_name): if not g.is_admin: fetch_user(user_id, False) # check that user is visible try: mgr = auth.admin_client_set().compute_ext.user_keypairs keypair = mgr.get(user_id, key_name) except osc_exc.NotFound: abort(404) return make_json_response(keypair_to_view(keypair))
def test_list_works(self): expected = { 'collection': { 'name': 'ssh-keys', 'parent-href': '/v1/me', 'size': 2 }, 'ssh-keys': ['REPLY1', 'REPLY2'] } self.fake_client_set.compute.keypairs.list()\ .AndReturn(['K1', 'K2']) my_ssh_keys.keypair_to_view('K1').AndReturn('REPLY1') my_ssh_keys.keypair_to_view('K2').AndReturn('REPLY2') self.mox.ReplayAll() rv = self.client.get('/v1/me/ssh-keys/') data = self.check_and_parse_response(rv) self.assertEquals(data, expected)
def list_users_ssh_keys(user_id): parse_collection_request(_SCHEMA) fetch_user(user_id, g.is_admin) # check that user exists and is visible mgr = auth.admin_client_set().compute_ext.user_keypairs result = [keypair_to_view(keypair) for keypair in mgr.list(user_id)] parent_href = url_for('users.get_user', user_id=user_id) return make_collection_response('ssh-keys', result, parent_href=parent_href)
def test_keypair_to_view_works(self): kp = doubles.make(self.mox, doubles.Keypair, name='Test KP', public_key='PUBKEY', fingerprint='FP') expected = { 'name': 'Test KP', 'public-key': 'PUBKEY', 'fingerprint': 'FP', 'href': '/v1/me/ssh-keys/Test%20KP' } self.mox.ReplayAll() with self.app.test_request_context(): data = my_ssh_keys.keypair_to_view(kp) self.assertEquals(data, expected)
@BP.route('/', methods=('POST',)) @user_endpoint def create_users_ssh_key(user_id): data = parse_request_data(required=_SCHEMA.required) if user_id != auth.current_user_id(): auth.assert_admin() fetch_user(user_id, g.is_admin) # check that user exists and is visible mgr = auth.admin_client_set().compute_ext.user_keypairs try: kp = mgr.create(user_id, data['name'], data['public-key']) except osc_exc.BadRequest, e: raise exc.InvalidRequest(str(e)) set_audit_resource_id(kp.name) return make_json_response(keypair_to_view(kp)) @BP.route('/<key_name>', methods=('DELETE',)) def delete_users_ssh_key(user_id, key_name): if user_id != auth.current_user_id(): auth.assert_admin() mgr = auth.admin_client_set().compute_ext.user_keypairs try: mgr.delete(user_id, key_name) except osc_exc.NotFound: abort(404) return make_json_response(None, 204)
@BP.route('/', methods=('POST', )) @user_endpoint def create_users_ssh_key(user_id): data = parse_request_data(required=_SCHEMA.required) if user_id != auth.current_user_id(): auth.assert_admin() fetch_user(user_id, g.is_admin) # check that user exists and is visible mgr = auth.admin_client_set().compute_ext.user_keypairs try: kp = mgr.create(user_id, data['name'], data['public-key']) except osc_exc.BadRequest, e: raise exc.InvalidRequest(str(e)) set_audit_resource_id(kp.name) return make_json_response(keypair_to_view(kp)) @BP.route('/<key_name>', methods=('DELETE', )) def delete_users_ssh_key(user_id, key_name): if user_id != auth.current_user_id(): auth.assert_admin() mgr = auth.admin_client_set().compute_ext.user_keypairs try: mgr.delete(user_id, key_name) except osc_exc.NotFound: abort(404) return make_json_response(None, 204)