def share_server_migration_start(self, req, id, body): """Migrate a share server to the specified host.""" context = req.environ['manila.context'] try: share_server = db_api.share_server_get(context, id) except exception.ShareServerNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) params = body.get('migration_start') if not params: raise exc.HTTPBadRequest(explanation=_("Request is missing body.")) bool_params = ['writable', 'nondisruptive', 'preserve_snapshots'] mandatory_params = bool_params + ['host'] utils.check_params_exist(mandatory_params, params) bool_param_values = utils.check_params_are_boolean(bool_params, params) pool_was_specified = len(params['host'].split('#')) > 1 if pool_was_specified: msg = _('The destination host can not contain pool information.') raise exc.HTTPBadRequest(explanation=msg) new_share_network = None new_share_network_id = params.get('new_share_network_id', None) if new_share_network_id: try: new_share_network = db_api.share_network_get( context, new_share_network_id) except exception.NotFound: msg = _("Share network %s not " "found.") % new_share_network_id raise exc.HTTPBadRequest(explanation=msg) common.check_share_network_is_active(new_share_network) else: share_network_id = ( share_server['share_network_subnet']['share_network_id']) current_share_network = db_api.share_network_get( context, share_network_id) common.check_share_network_is_active(current_share_network) try: self.share_api.share_server_migration_start( context, share_server, params['host'], bool_param_values['writable'], bool_param_values['nondisruptive'], bool_param_values['preserve_snapshots'], new_share_network=new_share_network) except exception.ServiceIsDown as e: # NOTE(dviroel): user should check if the host is healthy raise exc.HTTPBadRequest(explanation=e.msg) except exception.InvalidShareServer as e: # NOTE(dviroel): invalid share server meaning that some internal # resource have a invalid state. raise exc.HTTPConflict(explanation=e.msg)
def update(self, req, id, body): """Update specified share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'update') if not body or RESOURCE_NAME not in body: raise exc.HTTPUnprocessableEntity() try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) update_values = body[RESOURCE_NAME] self._verify_no_mutually_exclusive_data(share_network, update_values) if share_network['share_servers']: for value in update_values: if value not in ['name', 'description']: msg = _("Cannot update share network %s. It is used by " "share servers. Only 'name' and 'description' " "fields are available for update")\ % share_network['id'] raise exc.HTTPForbidden(explanation=msg) try: share_network = db_api.share_network_update(context, id, update_values) except db_exception.DBError: msg = "Could not save supplied data due to database error" raise exc.HTTPBadRequest(explanation=msg) return self._view_builder.build_share_network(share_network)
def _add_security_service(self, req, id, data): """Associate share network with a given security service.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'add_security_service') share_network = db_api.share_network_get(context, id) if share_network['share_servers']: msg = _("Cannot add security services. Share network is used.") raise exc.HTTPForbidden(explanation=msg) security_service = db_api.security_service_get( context, data['security_service_id']) for attached_service in share_network['security_services']: if attached_service['type'] == security_service['type']: msg = _("Cannot add security service to share network. " "Security service with '%(ss_type)s' type already " "added to '%(sn_id)s' share network") % { 'ss_type': security_service['type'], 'sn_id': share_network['id']} raise exc.HTTPConflict(explanation=msg) try: share_network = db_api.share_network_add_security_service( context, id, data['security_service_id']) except KeyError: msg = "Malformed request body" raise exc.HTTPBadRequest(explanation=msg) except exception.NotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) except exception.ShareNetworkSecurityServiceAssociationError as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) return self._view_builder.build_share_network(share_network)
def check_add_security_service(self, req, id, body): """Check the feasibility of associate a new security service.""" context = req.environ['manila.context'] share_network = db_api.share_network_get(context, id) policy.check_policy(context, RESOURCE_NAME, 'add_security_service_check', target_obj=share_network) data = body['add_security_service_check'] try: security_service = db_api.security_service_get( context, data['security_service_id']) except KeyError: msg = "Malformed request body." raise exc.HTTPBadRequest(explanation=msg) except exception.NotFound: msg = ("Security service %s doesn't exist." ) % data['security_service_id'] raise exc.HTTPBadRequest(explanation=msg) reset_check = utils.get_bool_from_api_params('reset_operation', data) try: result = ( self.share_api.check_share_network_security_service_update( context, share_network, security_service, reset_operation=reset_check)) except exception.ServiceIsDown as e: raise exc.HTTPConflict(explanation=e.msg) except exception.InvalidShareNetwork as e: raise exc.HTTPBadRequest(explanation=e.msg) except exception.InvalidSecurityService as e: raise exc.HTTPConflict(explanation=e.msg) return self._view_builder.build_security_service_update_check( req, data, result)
def delete(self, req, id): """Delete specified share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'delete') try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: msg = "%s" % e raise exc.HTTPNotFound(explanation=msg) if share_network['share_servers']: msg = _("Cannot delete share network %s. " "There are share servers using it") % id raise exc.HTTPForbidden(explanation=msg) db_api.share_network_delete(context, id) try: reservations = QUOTAS.reserve( context, project_id=share_network['project_id'], share_networks=-1) except Exception: msg = _("Failed to update usages deleting share-network.") LOG.exception(msg) else: QUOTAS.commit(context, reservations, project_id=share_network['project_id']) return webob.Response(status_int=202)
def update(self, req, id, body): """Update specified share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'update') if not body or RESOURCE_NAME not in body: raise exc.HTTPUnprocessableEntity() try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: msg = "%s" % e raise exc.HTTPNotFound(explanation=msg) if share_network['share_servers']: msg = _("Cannot update share network %s." " It is used by share servers") % share_network['id'] raise exc.HTTPForbidden(explanation=msg) update_values = body[RESOURCE_NAME] try: share_network = db_api.share_network_update(context, id, update_values) except db_exception.DBError: msg = "Could not save supplied data due to database error" raise exc.HTTPBadRequest(explanation=msg) return self._view_builder.build_share_network(share_network)
def remove_security_service(self, req, id, body): """Dissociate share network from a given security service.""" context = req.environ['manila.context'] share_network = db_api.share_network_get(context, id) policy.check_policy(context, RESOURCE_NAME, 'remove_security_service', target_obj=share_network) data = body['remove_security_service'] if self._share_network_subnets_contain_share_servers(share_network): msg = _("Cannot remove security services. Share network is used.") raise exc.HTTPForbidden(explanation=msg) try: share_network = db_api.share_network_remove_security_service( context, id, data['security_service_id']) except KeyError: msg = "Malformed request body" raise exc.HTTPBadRequest(explanation=msg) except exception.NotFound as e: raise exc.HTTPNotFound(explanation=e.msg) except exception.ShareNetworkSecurityServiceDissociationError as e: raise exc.HTTPBadRequest(explanation=e.msg) return self._view_builder.build_share_network(req, share_network)
def test_get_with_two_security_services(self): security_dict1 = { 'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type' } security_dict2 = { 'id': 'fake security service id2', 'project_id': self.fake_context.project_id, 'type': 'fake type' } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.security_service_create(self.fake_context, security_dict2) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict2['id']) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['security_services']), 2)
def test_remove_security_service(self): security_dict1 = { 'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type' } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) db_api.share_network_remove_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) result = sqlalchemy_api.model_query( self.fake_context, models.ShareNetworkSecurityServiceAssociation).\ filter_by(security_service_id=security_dict1['id']).\ filter_by(share_network_id=self.share_nw_dict['id']).first() self.assertTrue(result is None) share_nw_ref = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(share_nw_ref['security_services']), 0)
def test_remove_security_service(self): security_dict1 = {'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) db_api.share_network_remove_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) result = sqlalchemy_api.model_query( self.fake_context, models.ShareNetworkSecurityServiceAssociation).\ filter_by(security_service_id=security_dict1['id']).\ filter_by(share_network_id=self.share_nw_dict['id']).first() self.assertTrue(result is None) share_nw_ref = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(share_nw_ref['security_services']), 0)
def update(self, req, id, body): """Update specified share network.""" context = req.environ["manila.context"] policy.check_policy(context, RESOURCE_NAME, "update") if not body or RESOURCE_NAME not in body: raise exc.HTTPUnprocessableEntity() try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: msg = "%s" % e raise exc.HTTPNotFound(explanation=msg) if share_network["status"] == constants.STATUS_ACTIVE: msg = "Network %s is in use" % id raise exc.HTTPBadRequest(explanation=msg) update_values = body[RESOURCE_NAME] try: share_network = db_api.share_network_update(context, id, update_values) except exception.DBError: msg = "Could not save supplied data due to database error" raise exc.HTTPBadRequest(explanation=msg) return self._view_builder.build_share_network(share_network)
def delete(self, req, id): """Delete specified share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'delete') try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) shares = db_api.share_get_all_by_share_network(context, id) if shares: msg = _("Can not delete share network %(id)s, it has " "%(len)s share(s).") % {'id': id, 'len': len(shares)} LOG.error(msg) raise exc.HTTPConflict(explanation=msg) for share_server in share_network['share_servers']: self.share_rpcapi.delete_share_server(context, share_server) db_api.share_network_delete(context, id) try: reservations = QUOTAS.reserve( context, project_id=share_network['project_id'], share_networks=-1) except Exception: LOG.exception(_LE("Failed to update usages deleting " "share-network.")) else: QUOTAS.commit(context, reservations, project_id=share_network['project_id']) return webob.Response(status_int=202)
def test_get(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self._check_fields(expected=self.share_nw_dict, actual=result) self.assertEqual(len(result['shares']), 0) self.assertEqual(len(result['security_services']), 0)
def test_get_with_one_allocation(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.network_allocation_create(self.fake_context, self.allocation_dict) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["network_allocations"]), 1) self._check_fields(expected=self.allocation_dict, actual=result["network_allocations"][0])
def show(self, req, share_network_id, share_network_subnet_id): """Show share network subnet.""" context = req.environ['manila.context'] try: db_api.share_network_get(context, share_network_id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) try: share_network_subnet = db_api.share_network_subnet_get( context, share_network_subnet_id) except exception.ShareNetworkSubnetNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) return self._view_builder.build_share_network_subnet( req, share_network_subnet)
def test_update(self): new_status = constants.STATUS_ERROR db_api.share_network_create(self.fake_context, self.share_nw_dict) result_update = db_api.share_network_update(self.fake_context, self.share_nw_dict["id"], {"status": new_status}) result_get = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(result_update["status"], new_status) self._check_fields(expected=dict(result_update.iteritems()), actual=dict(result_get.iteritems()))
def create(self, req, share_network_id, body): """Add a new share network subnet into the share network.""" context = req.environ['manila.context'] if not self.is_valid_body(body, 'share-network-subnet'): msg = _("Share Network Subnet is missing from the request body.") raise exc.HTTPBadRequest(explanation=msg) data = body['share-network-subnet'] data['share_network_id'] = share_network_id common.check_net_id_and_subnet_id(data) try: db_api.share_network_get(context, share_network_id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) availability_zone = data.pop('availability_zone', None) subnet_az = None if availability_zone: try: subnet_az = db_api.availability_zone_get( context, availability_zone) except exception.AvailabilityZoneNotFound: msg = _("The provided availability zone %s does not " "exist.") % availability_zone raise exc.HTTPBadRequest(explanation=msg) self._validate_subnet(context, share_network_id, az=subnet_az) try: data['availability_zone_id'] = (subnet_az['id'] if subnet_az is not None else None) share_network_subnet = db_api.share_network_subnet_create( context, data) except db_exception.DBError as e: msg = _('Could not create the share network subnet.') LOG.error(e) raise exc.HTTPInternalServerError(explanation=msg) share_network_subnet = db_api.share_network_subnet_get( context, share_network_subnet['id']) return self._view_builder.build_share_network_subnet( req, share_network_subnet)
def test_network_allocations_relation(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.network_allocation_create(self.fake_context, self.allocation_dict) db_api.network_allocation_delete(self.fake_context, self.allocation_dict["id"]) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["network_allocations"]), 0)
def test_shares_relation(self): share_dict = {"id": "fake share id1"} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["shares"]), 0)
def delete(self, req, share_network_id, share_network_subnet_id): """Delete specified share network subnet.""" context = req.environ['manila.context'] try: db_api.share_network_get(context, share_network_id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) try: share_network_subnet = db_api.share_network_subnet_get( context, share_network_subnet_id) except exception.ShareNetworkSubnetNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) for share_server in share_network_subnet['share_servers'] or []: shares = db_api.share_instances_get_all_by_share_server( context, share_server['id']) if shares: msg = _("Cannot delete share network subnet %(id)s, it has " "one or more shares.") % { 'id': share_network_subnet_id } LOG.error(msg) raise exc.HTTPConflict(explanation=msg) # NOTE(silvacarlose): Do not allow the deletion of any share server # if any of them has the flag is_auto_deletable = False if not self._all_share_servers_are_auto_deletable( share_network_subnet): msg = _("The service cannot determine if there are any " "non-managed shares on the share network subnet %(id)s," "so it cannot be deleted. Please contact the cloud " "administrator to rectify.") % { 'id': share_network_subnet_id } LOG.error(msg) raise exc.HTTPConflict(explanation=msg) for share_server in share_network_subnet['share_servers']: self.share_rpcapi.delete_share_server(context, share_server) db_api.share_network_subnet_delete(context, share_network_subnet_id) return webob.Response(status_int=http_client.ACCEPTED)
def test_get_with_one_share(self): share_dict1 = {"id": "fake share id1", "share_network_id": self.share_nw_dict["id"]} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict1) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["shares"]), 1) self._check_fields(expected=share_dict1, actual=result["shares"][0])
def test_get_with_two_allocations(self): allocation_dict2 = dict(self.allocation_dict) allocation_dict2["id"] = "fake port id2" db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.network_allocation_create(self.fake_context, self.allocation_dict) db_api.network_allocation_create(self.fake_context, allocation_dict2) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["network_allocations"]), 2)
def test_shares_relation(self): share_dict = {'id': 'fake share id1'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['shares']), 0)
def test_get_with_two_shares(self): share_dict1 = {"id": "fake share id1", "share_network_id": self.share_nw_dict["id"]} share_dict2 = {"id": "fake share id2", "share_network_id": self.share_nw_dict["id"]} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict1) db_api.share_create(self.fake_context, share_dict2) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["shares"]), 2)
def delete(self, req, id): """Delete specified share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'delete') try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) share_instances = ( db_api.share_instances_get_all_by_share_network(context, id) ) if share_instances: msg = _("Can not delete share network %(id)s, it has " "%(len)s share(s).") % {'id': id, 'len': len(share_instances)} LOG.error(msg) raise exc.HTTPConflict(explanation=msg) # NOTE(ameade): Do not allow deletion of share network used by share # group sg_count = db_api.count_share_groups_in_share_network(context, id) if sg_count: msg = _("Can not delete share network %(id)s, it has %(len)s " "share group(s).") % {'id': id, 'len': sg_count} LOG.error(msg) raise exc.HTTPConflict(explanation=msg) # NOTE(silvacarlose): Do not allow the deletion of any share server # if one of them has the flag is_auto_deletable = False if not self._all_share_servers_are_auto_deletable(share_network): msg = _("The service cannot determine if there are any " "non-managed shares on the share network %(id)s, so it " "cannot be deleted. Please contact the cloud " "administrator to rectify.") % {'id': id} LOG.error(msg) raise exc.HTTPConflict(explanation=msg) for share_server in share_network['share_servers']: self.share_rpcapi.delete_share_server(context, share_server) db_api.share_network_delete(context, id) try: reservations = QUOTAS.reserve( context, project_id=share_network['project_id'], share_networks=-1, user_id=share_network['user_id']) except Exception: LOG.exception("Failed to update usages deleting " "share-network.") else: QUOTAS.commit(context, reservations, project_id=share_network['project_id'], user_id=share_network['user_id']) return webob.Response(status_int=http_client.ACCEPTED)
def show(self, req, id): """Return data about the requested network info.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'show') try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) return self._view_builder.build_share_network(share_network)
def index(self, req, share_network_id): """Returns a list of share network subnets.""" context = req.environ['manila.context'] try: share_network = db_api.share_network_get(context, share_network_id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) return self._view_builder.build_share_network_subnets( req, share_network.get('share_network_subnets'))
def show(self, req, id): """Return data about the requested network info.""" context = req.environ["manila.context"] policy.check_policy(context, RESOURCE_NAME, "show") try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: msg = "%s" % e raise exc.HTTPNotFound(explanation=msg) return self._view_builder.build_share_network(share_network)
def test_security_services_relation(self): security_dict1 = {'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['security_services']), 0)
def delete(self, req, id): """Delete specified share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'delete') try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) share_instances = (db_api.share_instances_get_all_by_share_network( context, id)) if share_instances: msg = _("Can not delete share network %(id)s, it has " "%(len)s share(s).") % { 'id': id, 'len': len(share_instances) } LOG.error(msg) raise exc.HTTPConflict(explanation=msg) # NOTE(ameade): Do not allow deletion of share network used by share # group sg_count = db_api.count_share_groups_in_share_network(context, id) if sg_count: msg = _("Can not delete share network %(id)s, it has %(len)s " "share group(s).") % { 'id': id, 'len': sg_count } LOG.error(msg) raise exc.HTTPConflict(explanation=msg) for share_server in share_network['share_servers']: self.share_rpcapi.delete_share_server(context, share_server) db_api.share_network_delete(context, id) try: reservations = QUOTAS.reserve( context, project_id=share_network['project_id'], share_networks=-1, user_id=share_network['user_id']) except Exception: LOG.exception( _LE("Failed to update usages deleting " "share-network.")) else: QUOTAS.commit(context, reservations, project_id=share_network['project_id'], user_id=share_network['user_id']) return webob.Response(status_int=202)
def test_update(self): new_name = 'fake_new_name' db_api.share_network_create(self.fake_context, self.share_nw_dict) result_update = db_api.share_network_update(self.fake_context, self.share_nw_dict['id'], {'name': new_name}) result_get = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(result_update['name'], new_name) self._check_fields(expected=dict(six.iteritems(result_update)), actual=dict(six.iteritems(result_get)))
def test_get_with_one_share(self): share_dict1 = {'id': 'fake share id1', 'share_network_id': self.share_nw_dict['id']} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict1) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['shares']), 1) self._check_fields(expected=share_dict1, actual=result['shares'][0])
def test_security_services_relation(self): security_dict1 = { "id": "fake security service id1", "project_id": self.fake_context.project_id, "type": "fake type", } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["security_services"]), 0)
def test_get_with_two_shares(self): share_dict1 = {'id': 'fake share id1', 'share_network_id': self.share_nw_dict['id']} share_dict2 = {'id': 'fake share id2', 'share_network_id': self.share_nw_dict['id']} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict1) db_api.share_create(self.fake_context, share_dict2) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['shares']), 2)
def test_get_with_one_security_service(self): security_dict1 = { "id": "fake security service id1", "project_id": self.fake_context.project_id, "type": "fake type", } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict["id"], security_dict1["id"]) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["security_services"]), 1) self._check_fields(expected=security_dict1, actual=result["security_services"][0])
def _validate_manage_share_server_parameters(self, context, body): if not (body and self.is_valid_body(body, 'share_server')): msg = _("Share Server entity not found in request body") raise exc.HTTPUnprocessableEntity(explanation=msg) required_parameters = ('host', 'share_network_id', 'identifier') data = body['share_server'] for parameter in required_parameters: if parameter not in data: msg = _("Required parameter %s not found") % parameter raise exc.HTTPBadRequest(explanation=msg) if not data.get(parameter): msg = _("Required parameter %s is empty") % parameter raise exc.HTTPBadRequest(explanation=msg) identifier = data['identifier'] host, share_network_id = data['host'], data['share_network_id'] if share_utils.extract_host(host, 'pool'): msg = _("Host parameter should not contain pool.") raise exc.HTTPBadRequest(explanation=msg) try: utils.validate_service_host( context, share_utils.extract_host(host)) except exception.ServiceNotFound as e: raise exc.HTTPBadRequest(explanation=e) except exception.PolicyNotAuthorized as e: raise exc.HTTPForbidden(explanation=e) except exception.AdminRequired as e: raise exc.HTTPForbidden(explanation=e) except exception.ServiceIsDown as e: raise exc.HTTPBadRequest(explanation=e) try: share_network = db_api.share_network_get( context, share_network_id) except exception.ShareNetworkNotFound as e: raise exc.HTTPBadRequest(explanation=e) driver_opts = data.get('driver_options') if driver_opts is not None and not isinstance(driver_opts, dict): msg = _("Driver options must be in dictionary format.") raise exc.HTTPBadRequest(explanation=msg) return identifier, host, share_network, driver_opts
def _unmanage(self, req, id, body=None): context = req.environ['manila.context'] LOG.debug("Unmanage Share Server with id: %s", id) # force's default value is False # force will be True if body is {'unmanage': {'force': True}} force = (body.get('unmanage') or {}).get('force', False) or False try: share_server = db_api.share_server_get(context, id) except exception.ShareServerNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) network_subnet_id = share_server.get('share_network_subnet_id', None) if network_subnet_id: subnet = db_api.share_network_subnet_get(context, network_subnet_id) share_network_id = subnet['share_network_id'] else: share_network_id = share_server.get('share_network_id') share_network = db_api.share_network_get(context, share_network_id) common.check_share_network_is_active(share_network) allowed_statuses = [ constants.STATUS_ERROR, constants.STATUS_ACTIVE, constants.STATUS_MANAGE_ERROR, constants.STATUS_UNMANAGE_ERROR ] if share_server['status'] not in allowed_statuses: data = { 'status': share_server['status'], 'allowed_statuses': ', '.join(allowed_statuses), } msg = _("Share server's actual status is %(status)s, allowed " "statuses for unmanaging are " "%(allowed_statuses)s.") % data raise exc.HTTPBadRequest(explanation=msg) try: self.share_api.unmanage_share_server(context, share_server, force=force) except (exception.ShareServerInUse, exception.PolicyNotAuthorized) as e: raise exc.HTTPBadRequest(explanation=e.msg) return webob.Response(status_int=http_client.ACCEPTED)
def update_security_service(self, req, id, body): """Update security service parameters from a given share network.""" context = req.environ['manila.context'] share_network = db_api.share_network_get(context, id) policy.check_policy(context, RESOURCE_NAME, 'update_security_service', target_obj=share_network) try: data = body['update_security_service'] current_security_service = db_api.security_service_get( context, data['current_service_id'] ) new_security_service = db_api.security_service_get( context, data['new_service_id'] ) except KeyError: msg = "Malformed request body." raise exc.HTTPBadRequest(explanation=msg) except exception.NotFound: msg = ("The current security service or the new security service " "doesn't exist.") raise exc.HTTPBadRequest(explanation=msg) try: self.share_api.update_share_network_security_service( context, share_network, new_security_service, current_security_service=current_security_service) except exception.ServiceIsDown as e: raise exc.HTTPConflict(explanation=e.msg) except exception.InvalidShareNetwork as e: raise exc.HTTPBadRequest(explanation=e.msg) except exception.InvalidSecurityService as e: raise exc.HTTPConflict(explanation=e.msg) try: share_network = db_api.share_network_update_security_service( context, id, data['current_service_id'], data['new_service_id']) except exception.NotFound as e: raise exc.HTTPNotFound(explanation=e.msg) except (exception.ShareNetworkSecurityServiceDissociationError, exception.ShareNetworkSecurityServiceAssociationError) as e: raise exc.HTTPBadRequest(explanation=e.msg) return self._view_builder.build_share_network(req, share_network)
def test_get_with_one_security_service(self): security_dict1 = {'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['security_services']), 1) self._check_fields(expected=security_dict1, actual=result['security_services'][0])
def add_security_service(self, req, id, body): """Associate share network with a given security service.""" context = req.environ['manila.context'] share_network = db_api.share_network_get(context, id) policy.check_policy(context, RESOURCE_NAME, 'add_security_service', target_obj=share_network) try: data = body['add_security_service'] security_service = db_api.security_service_get( context, data['security_service_id']) except KeyError: msg = "Malformed request body" raise exc.HTTPBadRequest(explanation=msg) contain_share_servers = ( self._share_network_subnets_contain_share_servers(share_network)) support_adding_to_in_use_networks = ( req.api_version_request >= api_version.APIVersionRequest("2.63")) if contain_share_servers: if not support_adding_to_in_use_networks: msg = _("Cannot add security services. Share network is used.") raise exc.HTTPForbidden(explanation=msg) try: self.share_api.update_share_network_security_service( context, share_network, security_service) except exception.ServiceIsDown as e: raise exc.HTTPConflict(explanation=e.msg) except exception.InvalidShareNetwork as e: raise exc.HTTPBadRequest(explanation=e.msg) except exception.InvalidSecurityService as e: raise exc.HTTPConflict(explanation=e.msg) try: share_network = db_api.share_network_add_security_service( context, id, data['security_service_id']) except exception.NotFound as e: raise exc.HTTPNotFound(explanation=e.msg) except exception.ShareNetworkSecurityServiceAssociationError as e: raise exc.HTTPBadRequest(explanation=e.msg) return self._view_builder.build_share_network(req, share_network)
def _activate(self, req, id, data): """Activate share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'activate') try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: msg = _("Share-network was not found. %s") % e raise exc.HTTPNotFound(explanation=msg) if share_network['status'] != constants.STATUS_INACTIVE: msg = _("Share network should be 'INACTIVE'.") raise exc.HTTPBadRequest(explanation=msg) self.share_rpcapi.activate_network(context, id, data) return self._view_builder.build_share_network(share_network)
def delete(self, req, id): """Delete specified share network.""" context = req.environ["manila.context"] policy.check_policy(context, RESOURCE_NAME, "delete") try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: msg = "%s" % e raise exc.HTTPNotFound(explanation=msg) if share_network["status"] == constants.STATUS_ACTIVE: msg = "Network %s is in use" % id raise exc.HTTPBadRequest(explanation=msg) db_api.share_network_delete(context, id) return webob.Response(status_int=202)
def _activate(self, req, id, data): """Activate share network.""" context = req.environ["manila.context"] policy.check_policy(context, RESOURCE_NAME, "activate") try: reservations = QUOTAS.reserve(context, share_networks=1) except exception.OverQuota as e: overs = e.kwargs["overs"] usages = e.kwargs["usages"] quotas = e.kwargs["quotas"] def _consumed(name): return usages[name]["reserved"] + usages[name]["in_use"] if "share_networks" in overs: msg = _( "Quota exceeded for %(s_pid)s, tried to activate" " share-network (%(d_consumed)d of %(d_quota)d " "already consumed)" ) LOG.warn( msg % { "s_pid": context.project_id, "d_consumed": _consumed("share_networks"), "d_quota": quotas["share_networks"], } ) raise exception.ActivatedShareNetworksLimitExceeded(allowed=quotas["share_networks"]) else: try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: msg = _("Share-network was not found. %s") % e raise exc.HTTPNotFound(explanation=msg) if share_network["status"] != constants.STATUS_INACTIVE: msg = _("Share network should be 'INACTIVE'.") raise exc.HTTPBadRequest(explanation=msg) self.share_rpcapi.activate_network(context, id, data) QUOTAS.commit(context, reservations) return self._view_builder.build_share_network(share_network)
def _remove_security_service(self, req, id, data): """Dissociate share network from a given security service.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'remove_security_service') share_network = db_api.share_network_get(context, id) if share_network['share_servers']: msg = _("Cannot remove security services. Share network is used.") raise exc.HTTPForbidden(explanation=msg) try: share_network = db_api.share_network_remove_security_service( context, id, data['security_service_id']) except KeyError: msg = "Malformed request body" raise exc.HTTPBadRequest(explanation=msg) except exception.NotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) except exception.ShareNetworkSecurityServiceDissociationError as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) return self._view_builder.build_share_network(req, share_network)
def show(self, req, id): """Return data about the requested share server.""" context = req.environ['manila.context'] try: server = db_api.share_server_get(context, id) share_network = db_api.share_network_get( context, server.share_network_subnet['share_network_id']) server.share_network_id = share_network['id'] server.project_id = share_network['project_id'] if share_network['name']: server.share_network_name = share_network['name'] else: server.share_network_name = share_network['id'] except exception.ShareServerNotFound as e: raise exc.HTTPNotFound(explanation=e.msg) except exception.ShareNetworkNotFound: msg = _("Share server %s could not be found. Its associated " "share network does not " "exist.") % server.share_network_subnet['share_network_id'] raise exc.HTTPNotFound(explanation=msg) return self._view_builder.build_share_server(req, server)
def index(self, req): """Returns a list of share servers.""" context = req.environ['manila.context'] search_opts = {} search_opts.update(req.GET) share_servers = db_api.share_server_get_all(context) for s in share_servers: try: s.share_network_id = s.share_network_subnet['share_network_id'] share_network = db_api.share_network_get( context, s.share_network_id) s.project_id = share_network['project_id'] if share_network['name']: s.share_network_name = share_network['name'] else: s.share_network_name = share_network['id'] except exception.ShareNetworkNotFound: # NOTE(dviroel): The share-network may already be deleted while # the share-server is in 'deleting' state. In this scenario, # we will return some empty values. LOG.debug( "Unable to retrieve share network details for share " "server %(server)s, the network %(network)s was " "not found.", { 'server': s.id, 'network': s.share_network_id }) s.project_id = '' s.share_network_name = '' if search_opts: for k, v in search_opts.items(): share_servers = [ s for s in share_servers if (hasattr(s, k) and s[k] == v or k == 'share_network' and v in [s.share_network_name, s.share_network_id]) ] return self._view_builder.build_share_servers(req, share_servers)
def delete(self, req, id): """Delete specified share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'delete') try: share_network = db_api.share_network_get(context, id) except exception.ShareNetworkNotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) share_instances = (db_api.share_instances_get_all_by_share_network( context, id)) if share_instances: msg = _("Can not delete share network %(id)s, it has " "%(len)s share(s).") % { 'id': id, 'len': len(share_instances) } LOG.error(msg) raise exc.HTTPConflict(explanation=msg) # NOTE(ameade): Do not allow deletion of share network used by share # group sg_count = db_api.count_share_groups_in_share_network(context, id) if sg_count: msg = _("Can not delete share network %(id)s, it has %(len)s " "share group(s).") % { 'id': id, 'len': sg_count } LOG.error(msg) raise exc.HTTPConflict(explanation=msg) # NOTE(silvacarlose): Do not allow the deletion of share networks # if it still contains two or more subnets if self._share_network_contains_subnets(share_network): msg = _("The share network %(id)s has more than one subnet " "attached. Please remove the subnets untill you have one " "or no subnets remaining.") % { 'id': id } LOG.error(msg) raise exc.HTTPConflict(explanation=msg) for subnet in share_network['share_network_subnets']: if not self._all_share_servers_are_auto_deletable(subnet): msg = _("The service cannot determine if there are any " "non-managed shares on the share network subnet " "%(id)s, so it cannot be deleted. Please contact the " "cloud administrator to rectify.") % { 'id': subnet['id'] } LOG.error(msg) raise exc.HTTPConflict(explanation=msg) for subnet in share_network['share_network_subnets']: for share_server in subnet['share_servers']: self.share_rpcapi.delete_share_server(context, share_server) db_api.share_network_delete(context, id) try: reservations = QUOTAS.reserve( context, project_id=share_network['project_id'], share_networks=-1, user_id=share_network['user_id']) except Exception: LOG.exception("Failed to update usages deleting " "share-network.") else: QUOTAS.commit(context, reservations, project_id=share_network['project_id'], user_id=share_network['user_id']) return webob.Response(status_int=http_client.ACCEPTED)