Skip to content

Commit

Permalink
Fixed the unit unassociate workflow to work with mongoengine models a…
Browse files Browse the repository at this point in the history
…nd filters

fixes #1212
  • Loading branch information
mhrivnak committed Sep 4, 2015
1 parent 65c0e36 commit af2d621
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
23 changes: 11 additions & 12 deletions server/pulp/plugins/conduits/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,38 @@ def to_plugin_unit(pulp_unit, type_def):
return u


def to_plugin_associated_unit(pulp_unit, type_def):
def to_plugin_associated_unit(pulp_unit, unit_type_id, unit_key_fields):
"""
Parses the raw dictionary of content unit associated to a repository into
the plugin's object representation.
@param pulp_unit: raw dictionary of unit metadata
@type pulp_unit: dict
@param type_def: Pulp stored definition for the unit type
@type type_def: dict or BSON
:param pulp_unit: raw dictionary of unit metadata
:type pulp_unit: dict
:param unit_type_id: unique identifier for the type of unit
:type unit_type_id: str
:param unit_key_fields: collection of keys required for the type's unit key
:type unit_key_fields: list or tuple
@return: plugin unit representation of the given unit
@rtype: pulp.plugins.model.AssociatedUnit
:return: plugin unit representation of the given unit
:rtype: pulp.plugins.model.AssociatedUnit
"""

# Copy so we don't mangle the original unit
# pymongo on RHEL6 doesn't seem to like deepcopy, so do this instead
pulp_unit = dict(pulp_unit)
pulp_unit['metadata'] = dict(pulp_unit['metadata'])

key_list = type_def['unit_key']

unit_key = {}

for k in key_list:
for k in unit_key_fields:
unit_key[k] = pulp_unit['metadata'].pop(k)

storage_path = pulp_unit['metadata'].pop('_storage_path', None)
unit_id = pulp_unit.pop('unit_id', None)
created = pulp_unit.pop('created', None)
updated = pulp_unit.pop('updated', None)

u = AssociatedUnit(type_def['id'], unit_key, pulp_unit['metadata'], storage_path,
u = AssociatedUnit(unit_type_id, unit_key, pulp_unit['metadata'], storage_path,
created, updated)
u.id = unit_id

Expand Down
4 changes: 3 additions & 1 deletion server/pulp/plugins/conduits/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,9 @@ def do_get_repo_units(repo_id, criteria, exception_class, as_generator=False):
# Transfer object generator.
def _transfer_object_generator():
for u in units:
yield common_utils.to_plugin_associated_unit(u, type_defs[u['unit_type_id']])
type_id = u['unit_type_id']
type_def = type_defs[type_id]
yield common_utils.to_plugin_associated_unit(u, type_id, type_def['unit_key'])

if as_generator:
return _transfer_object_generator()
Expand Down
6 changes: 5 additions & 1 deletion server/pulp/server/db/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,12 @@ class ContentUnit(Document):
:type _ns: mongoengine.StringField
:ivar unit_type_id: content unit type
:type unit_type_id: mongoengine.StringField
:ivar unit_key_fields: required fields for the unit key. This must be defined by each subclass
:type unit_key_fields: tuple
"""

unit_key_fields = tuple()

id = StringField(primary_key=True)
last_updated = IntField(db_field='_last_updated', required=True)
user_metadata = DictField(db_field='pulp_user_metadata')
Expand All @@ -415,7 +419,7 @@ def attach_signals(cls):
signals.pre_save.connect(cls.pre_save_signal, sender=cls)

# Validate that the minimal set of fields has been defined
if not hasattr(cls, 'unit_key_fields'):
if len(cls.unit_key_fields) == 0:
class_name = cls.__name__
raise exceptions.PulpCodedException(error_codes.PLP0035, class_name=class_name)

Expand Down
16 changes: 11 additions & 5 deletions server/pulp/server/managers/repo/unit_association.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,21 @@ def calculate_associated_type_ids(source_repo_id, associated_units):


def create_transfer_units(associate_units, associated_unit_type_ids):
type_defs = {}
for def_id in associated_unit_type_ids:
type_def = types_db.type_definition(def_id)
type_defs[def_id] = type_def
unit_key_fields = {}
for unit_type_id in associated_unit_type_ids:
type_def = types_db.type_definition(unit_type_id)
if type_def is not None:
# this is an "old style" model
unit_key_fields[unit_type_id] = type_def['unit_key']
else:
# this must be a mongoengine model
unit_model = plugin_api.get_unit_model_by_id(unit_type_id)
unit_key_fields[unit_type_id] = unit_model.unit_key_fields

transfer_units = []
for unit in associate_units:
type_id = unit['unit_type_id']
u = conduit_common_utils.to_plugin_associated_unit(unit, type_defs[type_id])
u = conduit_common_utils.to_plugin_associated_unit(unit, type_id, unit_key_fields[type_id])
transfer_units.append(u)

return transfer_units
Expand Down

0 comments on commit af2d621

Please sign in to comment.