def _default(self, req): """Return default share group type.""" context = req.environ['manila.context'] share_group_type = share_group_types.get_default(context) if not share_group_type: msg = _("Default share group type not found.") raise exc.HTTPNotFound(explanation=msg) share_group_type['id'] = six.text_type(share_group_type['id']) return self._view_builder.show(req, share_group_type)
def default(self, req): """Return default share group type.""" context = req.environ['manila.context'] share_group_type = share_group_types.get_default(context) if not share_group_type: msg = _("Default share group type not found.") raise exc.HTTPNotFound(explanation=msg) share_group_type['id'] = six.text_type(share_group_type['id']) return self._view_builder.show(req, share_group_type)
def create(self, req, body): """Creates a new share group.""" context = req.environ['manila.context'] if not self.is_valid_body(body, 'share_group'): msg = _("'share_group' is missing from the request body.") raise exc.HTTPBadRequest(explanation=msg) share_group = body['share_group'] valid_fields = { 'name', 'description', 'share_types', 'share_group_type_id', 'source_share_group_snapshot_id', 'share_network_id', 'availability_zone', } invalid_fields = set(share_group.keys()) - valid_fields if invalid_fields: msg = _("The fields %s are invalid.") % invalid_fields raise exc.HTTPBadRequest(explanation=msg) if ('share_types' in share_group and 'source_share_group_snapshot_id' in share_group): msg = _("Cannot supply both 'share_types' and " "'source_share_group_snapshot_id' attributes.") raise exc.HTTPBadRequest(explanation=msg) if not (share_group.get('share_types') or 'source_share_group_snapshot_id' in share_group): default_share_type = share_types.get_default_share_type() if default_share_type: share_group['share_types'] = [default_share_type['id']] else: msg = _("Must specify at least one share type as a default " "share type has not been configured.") raise exc.HTTPBadRequest(explanation=msg) kwargs = {} if 'name' in share_group: kwargs['name'] = share_group.get('name') if 'description' in share_group: kwargs['description'] = share_group.get('description') _share_types = share_group.get('share_types') if _share_types: if not all([uuidutils.is_uuid_like(st) for st in _share_types]): msg = _("The 'share_types' attribute must be a list of uuids") raise exc.HTTPBadRequest(explanation=msg) kwargs['share_type_ids'] = _share_types if ('share_network_id' in share_group and 'source_share_group_snapshot_id' in share_group): msg = _("Cannot supply both 'share_network_id' and " "'source_share_group_snapshot_id' attributes as the share " "network is inherited from the source.") raise exc.HTTPBadRequest(explanation=msg) availability_zone = share_group.get('availability_zone') if availability_zone: if 'source_share_group_snapshot_id' in share_group: msg = _( "Cannot supply both 'availability_zone' and " "'source_share_group_snapshot_id' attributes as the " "availability zone is inherited from the source.") raise exc.HTTPBadRequest(explanation=msg) try: az = db.availability_zone_get(context, availability_zone) kwargs['availability_zone_id'] = az.id kwargs['availability_zone'] = az.name except exception.AvailabilityZoneNotFound as e: raise exc.HTTPNotFound(explanation=six.text_type(e)) if 'source_share_group_snapshot_id' in share_group: source_share_group_snapshot_id = share_group.get( 'source_share_group_snapshot_id') if not uuidutils.is_uuid_like(source_share_group_snapshot_id): msg = _("The 'source_share_group_snapshot_id' attribute " "must be a uuid.") raise exc.HTTPBadRequest(explanation=six.text_type(msg)) kwargs['source_share_group_snapshot_id'] = ( source_share_group_snapshot_id) elif 'share_network_id' in share_group: share_network_id = share_group.get('share_network_id') if not uuidutils.is_uuid_like(share_network_id): msg = _("The 'share_network_id' attribute must be a uuid.") raise exc.HTTPBadRequest(explanation=six.text_type(msg)) kwargs['share_network_id'] = share_network_id if 'share_group_type_id' in share_group: share_group_type_id = share_group.get('share_group_type_id') if not uuidutils.is_uuid_like(share_group_type_id): msg = _("The 'share_group_type_id' attribute must be a uuid.") raise exc.HTTPBadRequest(explanation=six.text_type(msg)) kwargs['share_group_type_id'] = share_group_type_id else: # get default def_share_group_type = share_group_types.get_default() if def_share_group_type: kwargs['share_group_type_id'] = def_share_group_type['id'] else: msg = _("Must specify a share group type as a default " "share group type has not been configured.") raise exc.HTTPBadRequest(explanation=msg) try: new_share_group = self.share_group_api.create(context, **kwargs) except exception.InvalidShareGroupSnapshot as e: raise exc.HTTPConflict(explanation=six.text_type(e)) except (exception.ShareGroupSnapshotNotFound, exception.InvalidInput) as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) return self._view_builder.detail( req, {k: v for k, v in new_share_group.items()})