def execute_assets(changeplan): changed_assets = Asset.objects.filter(version=changeplan).order_by('itmodel__type') for changed_asset in changed_assets: try: live = ChangePlan.objects.get(id=0) live_asset = versioned_object(changed_asset, live, Asset.IDENTITY_FIELDS) if live_asset is None: live_asset = versioned_object(changed_asset, live, ['hostname']) if live_asset is None: Asset.objects.create( version=live, hostname=changed_asset.hostname, site=changed_asset.site, rack=add_rack(changed_asset.rack, live) if not changed_asset.site.offline else None, rack_position=changed_asset.rack_position if not changed_asset.site.offline and changed_asset.itmodel.type != ITModel.Type.BLADE else None, itmodel=changed_asset.itmodel, owner=changed_asset.owner, comment=changed_asset.comment, display_color=changed_asset.display_color if changed_asset.itmodel.type != ITModel.Type.CHASSIS else None, cpu=changed_asset.cpu if changed_asset.itmodel.type != ITModel.Type.CHASSIS else None, memory=changed_asset.memory if changed_asset.itmodel.type != ITModel.Type.CHASSIS else None, storage=changed_asset.storage if changed_asset.itmodel.type != ITModel.Type.CHASSIS else None, blade_chassis=add_asset(changed_asset.blade_chassis, live, Asset.IDENTITY_FIELDS)\ if changed_asset.itmodel.type == ITModel.Type.BLADE or changed_asset.itmodel.type == ITModel.Type.CHASSIS else None, slot=changed_asset.slot if changed_asset.itmodel.type == ITModel.Type.CHASSIS else None ) else: live_asset.asset_number = changed_asset.asset_number live_asset.hostname = changed_asset.hostname live_asset.site = changed_asset.site live_asset.rack = add_rack(changed_asset.rack, live) if not changed_asset.site.offline else None live_asset.rack_position = changed_asset.rack_position if not changed_asset.site.offline and changed_asset.itmodel.type != ITModel.Type.BLADE else None live_asset.itmodel = changed_asset.itmodel live_asset.owner = changed_asset.owner live_asset.comment = changed_asset.comment live_asset.display_color = changed_asset.display_color if changed_asset.itmodel.type != ITModel.Type.CHASSIS else None live_asset.cpu = changed_asset.cpu if changed_asset.itmodel.type != ITModel.Type.CHASSIS else None live_asset.memory = changed_asset.memory if changed_asset.itmodel.type != ITModel.Type.CHASSIS else None live_asset.storage = changed_asset.storage if changed_asset.itmodel.type != ITModel.Type.CHASSIS else None live_asset.blade_chassis = add_asset(changed_asset.blade_chassis, live, Asset.IDENTITY_FIELDS)\ if changed_asset.itmodel.type == ITModel.Type.BLADE else None live_asset.slot = changed_asset.slot if changed_asset.itmodel.type == ITModel.Type.BLADE else None live_asset.save() except Exception as e: pass
def create_asset_extra(asset, version, power_connections, net_ports): # Other objects if power_connections: order = 1 for connection in power_connections: if order > asset.itmodel.power_ports: break if isinstance(connection['pdu_id'], int): connection['pdu_id'] = PDU.objects.get(id=connection['pdu_id']) new_pdu = versioned_object(connection['pdu_id'], version, PDU.IDENTITY_FIELDS) if new_pdu is None: add_rack(connection['pdu_id'].rack, version) Powered.objects.create( pdu=versioned_object(connection['pdu_id'], version, PDU.IDENTITY_FIELDS), plug_number=connection['plug'], version=version, asset=asset, order=order ) order += 1 if net_ports: i = 1 for port in net_ports: if i > asset.itmodel.network_ports: break mac = port.get('mac_address') if mac and len(mac) == 0: mac = None if version.id != 0: if port['connection'] is not None: versioned_conn = add_network_conn(port['connection'], version) port['connection'] = versioned_conn NetworkPort.objects.create( asset=asset, label=NetworkPortLabel.objects.get( itmodel=asset.itmodel, name=port['label'] ), mac_address=mac, connection=add_network_conn(port.get('connection'), version), version=version, ) i += 1
def create(self, request, *args, **kwargs): version = ChangePlan.objects.get(id=get_version(request)) request.data['power_connections'] = [ entry for entry in request.data['power_connections'] if entry ] if version.id != 0 and request.data['location'][ 'tag'] != 'offline' and request.data['location']['rack']: rack = Rack.objects.get(id=request.data['location']['rack']) versioned_rack = add_rack(rack, version) request.data['location']['rack'] = versioned_rack.id return super(AssetCreate, self).create(request, *args, **kwargs)
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
def update(self, request, *args, **kwargs): # todo the logic in this looks fishy.. # Do you need to update data's id or is that done already? what about version? # Does the new asset get added to the new version? What about its network ports? # Make sure that network ports are not deleted in the offline case but connections are wiped version = ChangePlan.objects.get(id=get_version(request)) asset = self.get_object() asset_ver = asset.version request.data['power_connections'] = request.data.get( 'power_connections', []) request.data['network_ports'] = request.data.get('network_ports', []) request.data['power_connections'] = [ entry for entry in request.data['power_connections'] if entry ] site = Site.objects.get(id=request.data['location']['site']) check_asset_perm(request.user.username, site.abbr) check_asset_perm(request.user.username, asset.site.abbr) if version != asset_ver: data = request.data if not site.offline: rack = versioned_object(asset.rack, version, Rack.IDENTITY_FIELDS) if not rack: rack = add_rack(asset.rack, version) old_pdus = { port['id']: port['position'] for port in asset.rack.pdu_set.order_by('position').values( 'id', 'position') } new_pdus = { port['position']: port['id'] for port in rack.pdu_set.order_by('position').values( 'id', 'position') } data['location']['rack'] = rack.id for i, port in enumerate(request.data['power_connections']): data['power_connections'][i]['pdu_id'] = new_pdus[old_pdus[ int(port['pdu_id'])]] for i, port in enumerate(request.data['network_ports']): if port['connection'] is not None: versioned_conn = add_network_conn( NetworkPort.objects.get(id=port['connection']), version) data['network_ports'][i][ 'connection'] = versioned_conn.id else: request.data['power_connections'] = [] if request.data['location']['tag'] == 'chassis-mount': chassis = Asset.objects.get( id=request.data['location']['asset']) request.data['location']['asset'] = add_asset( chassis, version, Asset.IDENTITY_FIELDS).id pass serializer = AssetSerializer(data=data, context={ 'request': request, 'version': version.id }) serializer.is_valid(raise_exception=True) serializer.save() return Response(data=serializer.data, status=HTTP_200_OK) else: return super(AssetUpdate, self).update(request, *args, **kwargs)