示例#1
0
    def skip_row(self, instance, original):
        check_asset_perm(self.owner.username, instance.asset.site.abbr)
        if instance.asset.site.offline and getattr(instance, 'dest_hostname'):
            raise serializers.ValidationError(
                "Cannot set network connections in offline storage.")
        dest = newest_object(NetworkPort,
                             self.version,
                             asset__hostname=getattr(instance,
                                                     'dest_hostname'),
                             label__name=getattr(instance, 'dest_port'))
        if dest:
            check_asset_perm(self.owner.username, dest.asset.site.abbr)
            if dest.asset.site.offline and getattr(instance, 'dest_hostname'):
                raise serializers.ValidationError(
                    "Cannot set network connections in offline storage.")

        if original.mac_address != instance.mac_address:
            return False

        if not dest and not original.connection:
            return True

        if dest and original.connection:
            return dest.connection.asset.hostname == instance.asset.hostname

        return False
示例#2
0
    def after_import_row(self, row, row_result, **kwargs):
        if row_result.import_type == 'skip':
            return
        try:
            port = NetworkPort.objects.get(id=row_result.object_id)
        except:
            return

        if (row['dest_hostname'] == ''
                and row['dest_port'] != '') or (row['dest_hostname'] != ''
                                                and row['dest_port'] == ''):
            raise ValidationError("These fields must both be empty or set")

        if row['dest_hostname'] != '' and row['dest_port'] != '':
            dest_conn = newest_object(NetworkPort,
                                      self.version,
                                      asset__hostname=row['dest_hostname'],
                                      label__name=row['dest_port'])

            dest_conn = add_network_conn(dest_conn, self.version)
            port.connection = dest_conn
            port.save()
        else:
            port.connection = None
            port.save()
示例#3
0
def create_asset_diffs(changeplan, target):
    changed_assets = Asset.objects.filter(version=changeplan).order_by('id')
    for changed_asset in changed_assets:
        try:
            live_asset = versioned_object(changed_asset, target, Asset.IDENTITY_FIELDS)
            if live_asset is None:
                live_asset = versioned_object(changed_asset, target, ['hostname'])
            AssetDiff.objects.create(
                changeplan=changeplan,
                live_asset=live_asset,
                changed_asset=changed_asset,
            )
        except Asset.DoesNotExist:
            AssetDiff.objects.create(
                changeplan=changeplan,
                changed_asset=changed_asset,
            )
    for child in changeplan.changeplan_set.all():
        asset = Asset.objects.filter(commissioned=None, version=child).first()
        if asset:
            old = newest_object(Asset, changeplan, asset_number=asset.asset_number, hostname=asset.hostname)
            AssetDiff.objects.create(
                changeplan=changeplan,
                live_asset=old,
                changed_asset=asset
            )
示例#4
0
 def clean(self, value, row=None, *args, **kwargs):
     my_asset = newest_object(Asset,
                              ChangePlan.objects.get(id=row['version']),
                              hostname=row['src_hostname'])
     return NetworkPortLabel.objects.get(
         name__iexact=row['src_port'],
         itmodel__vendor__iexact=my_asset.itmodel.vendor,
         itmodel__model_number__iexact=my_asset.itmodel.model_number)
示例#5
0
def get_site(row):
    if row:
        if row['datacenter'] and row['rack'] and row['rack_position']:
            return row['datacenter']
        elif row['offline_site']:
            return row['offline_site']
        elif row['chassis_number']:
            chassis = newest_object(Asset,
                                    ChangePlan.objects.get(id=row['version']),
                                    asset_number=row['chassis_number'])
            if not chassis:
                raise serializers.ValidationError("Chassis does not exist.")
            return chassis.site
    return None
示例#6
0
 def clean(self, value, row=None, *args, **kwargs):
     if row['datacenter']:
         my_rack = row['rack']
         my_site = get_site(row)
         version = ChangePlan.objects.get(id=row['version'])
         if my_site:
             site = Site.objects.get(abbr=my_site)
             rack = newest_object(Rack,
                                  version,
                                  rack=my_rack,
                                  site=site)
             if not rack:
                 raise serializers.ValidationError(
                     "Rack does not exist.")
             return add_rack(rack, version)
     return None
示例#7
0
 def clean(self, value, row=None, *args, **kwargs):
     if value:
         version = ChangePlan.objects.get(id=row['version'])
         chassis = newest_object(Asset, version, asset_number=value)
         return add_asset(chassis, version)
     return None
示例#8
0
 def clean(self, value, row=None, *args, **kwargs):
     version = ChangePlan.objects.get(id=row['version'])
     asset = newest_object(Asset, version, hostname=row['src_hostname'])
     return add_asset(asset, version, ['hostname'])