def setUpTestData(cls): clustertype = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1') clustergroup = ClusterGroup.objects.create(name='Cluster Group 1', slug='cluster-group-1') clusters = ( Cluster(name='Cluster 1', type=clustertype, group=clustergroup), Cluster(name='Cluster 2', type=clustertype, group=clustergroup), ) Cluster.objects.bulk_create(clusters) virtual_machines = ( VirtualMachine(name='Virtual Machine 1', cluster=clusters[0], local_context_data={'A': 1}), VirtualMachine(name='Virtual Machine 2', cluster=clusters[0], local_context_data={'B': 2}), VirtualMachine(name='Virtual Machine 3', cluster=clusters[0], local_context_data={'C': 3}), ) VirtualMachine.objects.bulk_create(virtual_machines) cls.create_data = [ { 'name': 'Virtual Machine 4', 'cluster': clusters[1].pk, }, { 'name': 'Virtual Machine 5', 'cluster': clusters[1].pk, }, { 'name': 'Virtual Machine 6', 'cluster': clusters[1].pk, }, ]
def setUpTestData(cls): cluster_types = ( ClusterType(name='Cluster Type 1', slug='cluster-type-1'), ClusterType(name='Cluster Type 2', slug='cluster-type-2'), ClusterType(name='Cluster Type 3', slug='cluster-type-3'), ) ClusterType.objects.bulk_create(cluster_types) clusters = ( Cluster(name='Cluster 1', type=cluster_types[0]), Cluster(name='Cluster 2', type=cluster_types[1]), Cluster(name='Cluster 3', type=cluster_types[2]), ) Cluster.objects.bulk_create(clusters) vms = ( VirtualMachine(name='Virtual Machine 1', cluster=clusters[0]), VirtualMachine(name='Virtual Machine 2', cluster=clusters[1]), VirtualMachine(name='Virtual Machine 3', cluster=clusters[2]), ) VirtualMachine.objects.bulk_create(vms) interfaces = ( VMInterface(virtual_machine=vms[0], name='Interface 1', enabled=True, mtu=100, mac_address='00-00-00-00-00-01'), VMInterface(virtual_machine=vms[1], name='Interface 2', enabled=True, mtu=200, mac_address='00-00-00-00-00-02'), VMInterface(virtual_machine=vms[2], name='Interface 3', enabled=False, mtu=300, mac_address='00-00-00-00-00-03'), ) VMInterface.objects.bulk_create(interfaces)
def setUpTestData(cls): sites = ( Site(name='Site 1', slug='site-1'), Site(name='Site 2', slug='site-2'), ) Site.objects.bulk_create(sites) clustergroups = ( ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'), ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'), ) ClusterGroup.objects.bulk_create(clustergroups) clustertypes = ( ClusterType(name='Cluster Type 1', slug='cluster-type-1'), ClusterType(name='Cluster Type 2', slug='cluster-type-2'), ) ClusterType.objects.bulk_create(clustertypes) Cluster.objects.bulk_create([ Cluster(name='Cluster 1', group=clustergroups[0], type=clustertypes[0], site=sites[0]), Cluster(name='Cluster 2', group=clustergroups[0], type=clustertypes[0], site=sites[0]), Cluster(name='Cluster 3', group=clustergroups[0], type=clustertypes[0], site=sites[0]), ]) cls.form_data = { 'name': 'Cluster X', 'group': clustergroups[1].pk, 'type': clustertypes[1].pk, 'tenant': None, 'site': sites[1].pk, 'comments': 'Some comments', 'tags': 'Alpha,Bravo,Charlie', } cls.csv_data = ( "name,type", "Cluster 4,Cluster Type 1", "Cluster 5,Cluster Type 1", "Cluster 6,Cluster Type 1", ) cls.bulk_edit_data = { 'group': clustergroups[1].pk, 'type': clustertypes[1].pk, 'tenant': None, 'site': sites[1].pk, 'comments': 'New comments', }
def setUpTestData(cls): cluster_types = ( ClusterType(name='Cluster Type 1', slug='cluster-type-1'), ClusterType(name='Cluster Type 2', slug='cluster-type-2'), ClusterType(name='Cluster Type 3', slug='cluster-type-3'), ) ClusterType.objects.bulk_create(cluster_types) cluster_groups = ( ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'), ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'), ClusterGroup(name='Cluster Group 3', slug='cluster-group-3'), ) ClusterGroup.objects.bulk_create(cluster_groups) regions = ( Region(name='Test Region 1', slug='test-region-1'), Region(name='Test Region 2', slug='test-region-2'), Region(name='Test Region 3', slug='test-region-3'), ) # Can't use bulk_create for models with MPTT fields for r in regions: r.save() sites = ( Site(name='Test Site 1', slug='test-site-1', region=regions[0]), Site(name='Test Site 2', slug='test-site-2', region=regions[1]), Site(name='Test Site 3', slug='test-site-3', region=regions[2]), ) Site.objects.bulk_create(sites) tenant_groups = ( TenantGroup(name='Tenant group 1', slug='tenant-group-1'), TenantGroup(name='Tenant group 2', slug='tenant-group-2'), TenantGroup(name='Tenant group 3', slug='tenant-group-3'), ) for tenantgroup in tenant_groups: tenantgroup.save() tenants = ( Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]), Tenant(name='Tenant 2', slug='tenant-2', group=tenant_groups[1]), Tenant(name='Tenant 3', slug='tenant-3', group=tenant_groups[2]), ) Tenant.objects.bulk_create(tenants) clusters = ( Cluster(name='Cluster 1', type=cluster_types[0], group=cluster_groups[0], site=sites[0], tenant=tenants[0]), Cluster(name='Cluster 2', type=cluster_types[1], group=cluster_groups[1], site=sites[1], tenant=tenants[1]), Cluster(name='Cluster 3', type=cluster_types[2], group=cluster_groups[2], site=sites[2], tenant=tenants[2]), ) Cluster.objects.bulk_create(clusters)
def setUp(self): self.client = Client() clustertype = ClusterType(name='Cluster Type 1', slug='cluster-type-1') clustertype.save() cluster = Cluster(name='Cluster 1', type=clustertype) cluster.save() VirtualMachine.objects.bulk_create([ VirtualMachine(name='Virtual Machine 1', cluster=cluster), VirtualMachine(name='Virtual Machine 2', cluster=cluster), VirtualMachine(name='Virtual Machine 3', cluster=cluster), ])
def setUp(self): self.client = Client() clustertype = ClusterType(name='Cluster Type 1', slug='cluster-type-1') clustertype.save() cluster = Cluster(name='Cluster 1', type=clustertype) cluster.save() VirtualMachine.objects.bulk_create([ VirtualMachine(name='Virtual Machine 1', cluster=cluster), VirtualMachine(name='Virtual Machine 2', cluster=cluster), VirtualMachine(name='Virtual Machine 3', cluster=cluster), ])
def setUp(self): self.client = Client() clustergroup = ClusterGroup(name='Cluster Group 1', slug='cluster-group-1') clustergroup.save() clustertype = ClusterType(name='Cluster Type 1', slug='cluster-type-1') clustertype.save() Cluster.objects.bulk_create([ Cluster(name='Cluster 1', group=clustergroup, type=clustertype), Cluster(name='Cluster 2', group=clustergroup, type=clustertype), Cluster(name='Cluster 3', group=clustergroup, type=clustertype), ])
def setUp(self): user = create_test_user(permissions=['virtualization.view_cluster']) self.client = Client() self.client.force_login(user) clustergroup = ClusterGroup(name='Cluster Group 1', slug='cluster-group-1') clustergroup.save() clustertype = ClusterType(name='Cluster Type 1', slug='cluster-type-1') clustertype.save() Cluster.objects.bulk_create([ Cluster(name='Cluster 1', group=clustergroup, type=clustertype), Cluster(name='Cluster 2', group=clustergroup, type=clustertype), Cluster(name='Cluster 3', group=clustergroup, type=clustertype), ])
def setUp(self): user = create_test_user( permissions=['virtualization.view_virtualmachine']) self.client = Client() self.client.force_login(user) clustertype = ClusterType(name='Cluster Type 1', slug='cluster-type-1') clustertype.save() cluster = Cluster(name='Cluster 1', type=clustertype) cluster.save() VirtualMachine.objects.bulk_create([ VirtualMachine(name='Virtual Machine 1', cluster=cluster), VirtualMachine(name='Virtual Machine 2', cluster=cluster), VirtualMachine(name='Virtual Machine 3', cluster=cluster), ])
def mutate_and_get_payload(cls, input, context, info): type = input.get('type') group = input.get('group') site = input.get('site') temp = Cluster() if not_none(type): temp.type = ClusterType.objects.get(pk=from_global_id(type)[1]) if not_none(group): temp.group = ClusterGroup.objects.get(pk=from_global_id(group)[1]) if not_none(site): temp.site = Site.objects.get(pk=from_global_id(site)[1]) fields = ['name', 'comments'] return NewCluster(cluster=set_and_save(fields, input, temp))
def setUpTestData(cls): cluster_types = ( ClusterType(name='Cluster Type 1', slug='cluster-type-1'), ClusterType(name='Cluster Type 2', slug='cluster-type-2'), ) ClusterType.objects.bulk_create(cluster_types) cluster_groups = ( ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'), ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'), ) ClusterGroup.objects.bulk_create(cluster_groups) clusters = ( Cluster(name='Cluster 1', type=cluster_types[0], group=cluster_groups[0]), Cluster(name='Cluster 2', type=cluster_types[0], group=cluster_groups[0]), Cluster(name='Cluster 3', type=cluster_types[0], group=cluster_groups[0]), ) Cluster.objects.bulk_create(clusters) cls.create_data = [ { 'name': 'Cluster 4', 'type': cluster_types[1].pk, 'group': cluster_groups[1].pk, }, { 'name': 'Cluster 5', 'type': cluster_types[1].pk, 'group': cluster_groups[1].pk, }, { 'name': 'Cluster 6', 'type': cluster_types[1].pk, 'group': cluster_groups[1].pk, }, ]
def setUpTestData(cls): regions = ( Region(name='Test Region 1', slug='test-region-1'), Region(name='Test Region 2', slug='test-region-2'), Region(name='Test Region 3', slug='test-region-3'), ) # Can't use bulk_create for models with MPTT fields for r in regions: r.save() sites = ( Site(name='Test Site 1', slug='test-site-1'), Site(name='Test Site 2', slug='test-site-2'), Site(name='Test Site 3', slug='test-site-3'), ) Site.objects.bulk_create(sites) device_roles = ( DeviceRole(name='Device Role 1', slug='device-role-1'), DeviceRole(name='Device Role 2', slug='device-role-2'), DeviceRole(name='Device Role 3', slug='device-role-3'), ) DeviceRole.objects.bulk_create(device_roles) platforms = ( Platform(name='Platform 1', slug='platform-1'), Platform(name='Platform 2', slug='platform-2'), Platform(name='Platform 3', slug='platform-3'), ) Platform.objects.bulk_create(platforms) cluster_groups = ( ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'), ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'), ClusterGroup(name='Cluster Group 3', slug='cluster-group-3'), ) ClusterGroup.objects.bulk_create(cluster_groups) cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1') clusters = ( Cluster(name='Cluster 1', type=cluster_type), Cluster(name='Cluster 2', type=cluster_type), Cluster(name='Cluster 3', type=cluster_type), ) Cluster.objects.bulk_create(clusters) tenant_groups = ( TenantGroup(name='Tenant Group 1', slug='tenant-group-1'), TenantGroup(name='Tenant Group 2', slug='tenant-group-2'), TenantGroup(name='Tenant Group 3', slug='tenant-group-3'), ) for tenantgroup in tenant_groups: tenantgroup.save() tenants = ( Tenant(name='Tenant 1', slug='tenant-1'), Tenant(name='Tenant 2', slug='tenant-2'), Tenant(name='Tenant 3', slug='tenant-3'), ) Tenant.objects.bulk_create(tenants) for i in range(0, 3): is_active = bool(i % 2) c = ConfigContext.objects.create( name='Config Context {}'.format(i + 1), is_active=is_active, data='{"foo": 123}') c.regions.set([regions[i]]) c.sites.set([sites[i]]) c.roles.set([device_roles[i]]) c.platforms.set([platforms[i]]) c.cluster_groups.set([cluster_groups[i]]) c.clusters.set([clusters[i]]) c.tenant_groups.set([tenant_groups[i]]) c.tenants.set([tenants[i]])
def setUpTestData(cls): deviceroles = ( DeviceRole(name='Device Role 1', slug='device-role-1'), DeviceRole(name='Device Role 2', slug='device-role-2'), ) DeviceRole.objects.bulk_create(deviceroles) platforms = ( Platform(name='Platform 1', slug='platform-1'), Platform(name='Platform 2', slug='platform-2'), ) Platform.objects.bulk_create(platforms) clustertype = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1') clusters = ( Cluster(name='Cluster 1', type=clustertype), Cluster(name='Cluster 2', type=clustertype), ) Cluster.objects.bulk_create(clusters) VirtualMachine.objects.bulk_create([ VirtualMachine(name='Virtual Machine 1', cluster=clusters[0], role=deviceroles[0], platform=platforms[0]), VirtualMachine(name='Virtual Machine 2', cluster=clusters[0], role=deviceroles[0], platform=platforms[0]), VirtualMachine(name='Virtual Machine 3', cluster=clusters[0], role=deviceroles[0], platform=platforms[0]), ]) tags = create_tags('Alpha', 'Bravo', 'Charlie') cls.form_data = { 'cluster': clusters[1].pk, 'tenant': None, 'platform': platforms[1].pk, 'name': 'Virtual Machine X', 'status': VirtualMachineStatusChoices.STATUS_STAGED, 'role': deviceroles[1].pk, 'primary_ip4': None, 'primary_ip6': None, 'vcpus': 4, 'memory': 32768, 'disk': 4000, 'comments': 'Some comments', 'tags': [t.pk for t in tags], 'local_context_data': None, } cls.csv_data = ( "name,cluster", "Virtual Machine 4,Cluster 1", "Virtual Machine 5,Cluster 1", "Virtual Machine 6,Cluster 1", ) cls.bulk_edit_data = { 'cluster': clusters[1].pk, 'tenant': None, 'platform': platforms[1].pk, 'status': VirtualMachineStatusChoices.STATUS_STAGED, 'role': deviceroles[1].pk, 'vcpus': 8, 'memory': 65535, 'disk': 8000, 'comments': 'New comments', }
def setUpTestData(cls): cluster_types = ( ClusterType(name='Cluster Type 1', slug='cluster-type-1'), ClusterType(name='Cluster Type 2', slug='cluster-type-2'), ClusterType(name='Cluster Type 3', slug='cluster-type-3'), ) ClusterType.objects.bulk_create(cluster_types) cluster_groups = ( ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'), ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'), ClusterGroup(name='Cluster Group 3', slug='cluster-group-3'), ) ClusterGroup.objects.bulk_create(cluster_groups) regions = ( Region(name='Test Region 1', slug='test-region-1'), Region(name='Test Region 2', slug='test-region-2'), Region(name='Test Region 3', slug='test-region-3'), ) # Can't use bulk_create for models with MPTT fields for r in regions: r.save() sites = ( Site(name='Test Site 1', slug='test-site-1', region=regions[0]), Site(name='Test Site 2', slug='test-site-2', region=regions[1]), Site(name='Test Site 3', slug='test-site-3', region=regions[2]), ) Site.objects.bulk_create(sites) clusters = ( Cluster(name='Cluster 1', type=cluster_types[0], group=cluster_groups[0], site=sites[0]), Cluster(name='Cluster 2', type=cluster_types[1], group=cluster_groups[1], site=sites[1]), Cluster(name='Cluster 3', type=cluster_types[2], group=cluster_groups[2], site=sites[2]), ) Cluster.objects.bulk_create(clusters) platforms = ( Platform(name='Platform 1', slug='platform-1'), Platform(name='Platform 2', slug='platform-2'), Platform(name='Platform 3', slug='platform-3'), ) Platform.objects.bulk_create(platforms) roles = ( DeviceRole(name='Device Role 1', slug='device-role-1'), DeviceRole(name='Device Role 2', slug='device-role-2'), DeviceRole(name='Device Role 3', slug='device-role-3'), ) DeviceRole.objects.bulk_create(roles) tenant_groups = ( TenantGroup(name='Tenant group 1', slug='tenant-group-1'), TenantGroup(name='Tenant group 2', slug='tenant-group-2'), TenantGroup(name='Tenant group 3', slug='tenant-group-3'), ) for tenantgroup in tenant_groups: tenantgroup.save() tenants = ( Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]), Tenant(name='Tenant 2', slug='tenant-2', group=tenant_groups[1]), Tenant(name='Tenant 3', slug='tenant-3', group=tenant_groups[2]), ) Tenant.objects.bulk_create(tenants) vms = ( VirtualMachine(name='Virtual Machine 1', cluster=clusters[0], platform=platforms[0], role=roles[0], tenant=tenants[0], status=VirtualMachineStatusChoices.STATUS_ACTIVE, vcpus=1, memory=1, disk=1, local_context_data={"foo": 123}), VirtualMachine(name='Virtual Machine 2', cluster=clusters[1], platform=platforms[1], role=roles[1], tenant=tenants[1], status=VirtualMachineStatusChoices.STATUS_STAGED, vcpus=2, memory=2, disk=2), VirtualMachine(name='Virtual Machine 3', cluster=clusters[2], platform=platforms[2], role=roles[2], tenant=tenants[2], status=VirtualMachineStatusChoices.STATUS_OFFLINE, vcpus=3, memory=3, disk=3), ) VirtualMachine.objects.bulk_create(vms) interfaces = ( Interface(virtual_machine=vms[0], name='Interface 1', mac_address='00-00-00-00-00-01'), Interface(virtual_machine=vms[1], name='Interface 2', mac_address='00-00-00-00-00-02'), Interface(virtual_machine=vms[2], name='Interface 3', mac_address='00-00-00-00-00-03'), ) Interface.objects.bulk_create(interfaces)
def sync_vms(device, vms): """ Syncing VirtualMachines from device :param device: :param vms: :return: """ if not vms: return False, "There is no VM to update" # TODO: cluster tenancy cluster = device.cluster if not cluster: # Will create a new cluster logger.debug("Creating a new cluster {}".format(device.name)) cluster = Cluster() cluster.name = device.name cluster.type = ClusterType.objects.get(name=DEFAULT_CLUSTER_TYPE) cluster.site = device.site logger.debug("Saving a cluster {}".format(device.name)) cluster.save() cluster.devices.add(device) cluster.save() logger.debug("Device was added to cluster {}".format(device.name)) # Determine non-exists VMs and put it offline new_vm_names = [vm_data['NAME'] for vm_data in vms] if new_vm_names: logger.debug("New VMS: {}".format(new_vm_names)) non_exist_vms = VirtualMachine.objects.filter( cluster_id=cluster.id).exclude(name__in=new_vm_names) logger.debug( "VM which located in netbox, but non-exist on current cluster: {}". format(non_exist_vms)) if non_exist_vms: for vm in non_exist_vms: if vm.status != DEVICE_STATUS_STAGED: vm.status = DEVICE_STATUS_STAGED # vm.comments = vm.comments + "\n\nVM not exist on current cluster!\n" vm.save() # TODO: role and platform # By default all VMS - is a linux VMs platform = Platform.objects.get(name='Linux') # with server roles role = DeviceRole.objects.get(name='Server') # TODO: Need a get vm disks from vm objects # for vm_instance in vms: # pass for vm_data in vms: temp_vm = __TempVm(vm_data.get('NAME')) logger.debug("Will sync VM named: {}".format(temp_vm)) temp_vm.cluster_id = cluster.id temp_vm.platform_id = platform.id temp_vm.role_id = role.id temp_vm.memory = int( vm_data['MEM']) / 1024 # its a simple method - we need a MB temp_vm.vcpus = int(vm_data['VCPU']) # Determine VM state if int(vm_data['STATE']) == DEVICE_STATUS_ACTIVE: temp_vm.status = DEVICE_STATUS_ACTIVE # get disks names = vm_data.get('DISKNAMES') sizes = vm_data.get('DISKSIZES') paths = vm_data.get('DISKPATHS') # TODO: govnocode style - rewrite it for pythonic true way # This code parse a strings, like "{'diskindex': 1, 'name': 'sda'}" # Convert it to pythonic code # and split to one dict disks = [] total_size = 0 for name in names: name = ast.literal_eval(name) temp = name.copy() index = temp.get('diskindex') for size in sizes: size = ast.literal_eval(size) size_index = size.get('diskindex') if size_index == index: temp.update(size) for path in paths: path = ast.literal_eval(path) path_index = path.get('diskindex') if path_index == index: temp.update(path) disks.append(temp) del temp # non-urgent logger.info("Disks is: {}, len is {}".format(disks, len(disks))) # Filling VM comments with Disk Section separator = SEPARATOR # saving comments # TODO: fixing adding comments with multiple separator # TODO: Change this try: original_comments = VirtualMachine.objects.get( name=temp_vm.name).comments except: logger.debug("Cannot find a VM for original comments") original_comments = "" try: logger.debug("Tries to detect if comments exist") comments = separator.join(original_comments.split(separator)[1:]) except: logger.debug("Still no any other comments...") comments = original_comments disk_string = "" # and adding disks for disk in disks: try: logger.debug("Size of disk {} is {}".format( disk.get('name'), disk.get('size'))) size = int(disk.get('size')) / pow(1024, 3) disk_string += "**Disk:** {}\t**Size:** {} GB\t**Path:** {}\n".format( disk.get('name'), int(size), disk.get('path')) total_size += size except Exception as e: logger.warning("Cannot add disk {} - error is {}".format( disk.get('name'), e)) disk_string += separator temp_vm.comments = disk_string + comments temp_vm.disk = int(total_size) vm = VirtualMachine.objects.filter(name=temp_vm.name) logger.debug("found VM: {}".format(vm)) logger.debug("VM Data: {}".format(vm.__dict__)) if vm: logger.info("VM {} already exists. Will update this VM".format( temp_vm.name)) vm = vm[0] if _diff_objects(temp_vm, vm) == 0: vm.cluster_id = temp_vm.cluster_id vm.status = temp_vm.status vm.platform_id = temp_vm.platform_id vm.role_id = temp_vm.role_id vm.memory = temp_vm.memory vm.vcpus = temp_vm.vcpus vm.comments = temp_vm.comments vm.disk = temp_vm.disk vm.save() logger.info("VM {} was updated".format(vm.name)) else: logger.info("VM {} was not changed - nothing to change".format( temp_vm.name)) else: logger.info("VM {} is not exist. Will create new VM".format( temp_vm.name)) vm = VirtualMachine() vm.name = temp_vm.name vm.status = temp_vm.status vm.cluster_id = temp_vm.cluster_id vm.platform_id = temp_vm.platform_id vm.role_id = temp_vm.role_id vm.memory = temp_vm.memory vm.vcpus = temp_vm.vcpus vm.comments = temp_vm.comments vm.disk = temp_vm.disk vm.save() logger.info("Saved new VM: {}".format(vm.name)) return True, "VMs successfully synced"
def setUpTestData(cls): cluster_types = ( ClusterType(name='Cluster Type 1', slug='cluster-type-1'), ClusterType(name='Cluster Type 2', slug='cluster-type-2'), ClusterType(name='Cluster Type 3', slug='cluster-type-3'), ) ClusterType.objects.bulk_create(cluster_types) cluster_groups = ( ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'), ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'), ClusterGroup(name='Cluster Group 3', slug='cluster-group-3'), ) ClusterGroup.objects.bulk_create(cluster_groups) regions = ( Region(name='Test Region 1', slug='test-region-1'), Region(name='Test Region 2', slug='test-region-2'), Region(name='Test Region 3', slug='test-region-3'), ) for r in regions: r.save() site_groups = ( SiteGroup(name='Site Group 1', slug='site-group-1'), SiteGroup(name='Site Group 2', slug='site-group-2'), SiteGroup(name='Site Group 3', slug='site-group-3'), ) for site_group in site_groups: site_group.save() sites = ( Site(name='Test Site 1', slug='test-site-1', region=regions[0], group=site_groups[0]), Site(name='Test Site 2', slug='test-site-2', region=regions[1], group=site_groups[1]), Site(name='Test Site 3', slug='test-site-3', region=regions[2], group=site_groups[2]), ) Site.objects.bulk_create(sites) clusters = ( Cluster(name='Cluster 1', type=cluster_types[0], group=cluster_groups[0], site=sites[0]), Cluster(name='Cluster 2', type=cluster_types[1], group=cluster_groups[1], site=sites[1]), Cluster(name='Cluster 3', type=cluster_types[2], group=cluster_groups[2], site=sites[2]), ) Cluster.objects.bulk_create(clusters) platforms = ( Platform(name='Platform 1', slug='platform-1'), Platform(name='Platform 2', slug='platform-2'), Platform(name='Platform 3', slug='platform-3'), ) Platform.objects.bulk_create(platforms) roles = ( DeviceRole(name='Device Role 1', slug='device-role-1'), DeviceRole(name='Device Role 2', slug='device-role-2'), DeviceRole(name='Device Role 3', slug='device-role-3'), ) DeviceRole.objects.bulk_create(roles) tenant_groups = ( TenantGroup(name='Tenant group 1', slug='tenant-group-1'), TenantGroup(name='Tenant group 2', slug='tenant-group-2'), TenantGroup(name='Tenant group 3', slug='tenant-group-3'), ) for tenantgroup in tenant_groups: tenantgroup.save() tenants = ( Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]), Tenant(name='Tenant 2', slug='tenant-2', group=tenant_groups[1]), Tenant(name='Tenant 3', slug='tenant-3', group=tenant_groups[2]), ) Tenant.objects.bulk_create(tenants) vms = ( VirtualMachine(name='Virtual Machine 1', cluster=clusters[0], platform=platforms[0], role=roles[0], tenant=tenants[0], status=VirtualMachineStatusChoices.STATUS_ACTIVE, vcpus=1, memory=1, disk=1, local_context_data={"foo": 123}), VirtualMachine(name='Virtual Machine 2', cluster=clusters[1], platform=platforms[1], role=roles[1], tenant=tenants[1], status=VirtualMachineStatusChoices.STATUS_STAGED, vcpus=2, memory=2, disk=2), VirtualMachine(name='Virtual Machine 3', cluster=clusters[2], platform=platforms[2], role=roles[2], tenant=tenants[2], status=VirtualMachineStatusChoices.STATUS_OFFLINE, vcpus=3, memory=3, disk=3), ) VirtualMachine.objects.bulk_create(vms) interfaces = ( VMInterface(virtual_machine=vms[0], name='Interface 1', mac_address='00-00-00-00-00-01'), VMInterface(virtual_machine=vms[1], name='Interface 2', mac_address='00-00-00-00-00-02'), VMInterface(virtual_machine=vms[2], name='Interface 3', mac_address='00-00-00-00-00-03'), ) VMInterface.objects.bulk_create(interfaces) # Assign primary IPs for filtering ipaddresses = ( IPAddress(address='192.0.2.1/24', assigned_object=interfaces[0]), IPAddress(address='192.0.2.2/24', assigned_object=interfaces[1]), ) IPAddress.objects.bulk_create(ipaddresses) VirtualMachine.objects.filter(pk=vms[0].pk).update( primary_ip4=ipaddresses[0]) VirtualMachine.objects.filter(pk=vms[1].pk).update( primary_ip4=ipaddresses[1])
def setUpTestData(cls): regions = ( Region(name='Test Region 1', slug='test-region-1'), Region(name='Test Region 2', slug='test-region-2'), Region(name='Test Region 3', slug='test-region-3'), ) for r in regions: r.save() site_groups = ( SiteGroup(name='Site Group 1', slug='site-group-1'), SiteGroup(name='Site Group 2', slug='site-group-2'), SiteGroup(name='Site Group 3', slug='site-group-3'), ) for site_group in site_groups: site_group.save() sites = ( Site(name='Test Site 1', slug='test-site-1'), Site(name='Test Site 2', slug='test-site-2'), Site(name='Test Site 3', slug='test-site-3'), ) Site.objects.bulk_create(sites) manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1') device_types = ( DeviceType(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'), DeviceType(manufacturer=manufacturer, model='Device Type 2', slug='device-type-3'), DeviceType(manufacturer=manufacturer, model='Device Type 3', slug='device-type-4'), ) DeviceType.objects.bulk_create(device_types) device_roles = ( DeviceRole(name='Device Role 1', slug='device-role-1'), DeviceRole(name='Device Role 2', slug='device-role-2'), DeviceRole(name='Device Role 3', slug='device-role-3'), ) DeviceRole.objects.bulk_create(device_roles) platforms = ( Platform(name='Platform 1', slug='platform-1'), Platform(name='Platform 2', slug='platform-2'), Platform(name='Platform 3', slug='platform-3'), ) Platform.objects.bulk_create(platforms) cluster_groups = ( ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'), ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'), ClusterGroup(name='Cluster Group 3', slug='cluster-group-3'), ) ClusterGroup.objects.bulk_create(cluster_groups) cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1') clusters = ( Cluster(name='Cluster 1', type=cluster_type), Cluster(name='Cluster 2', type=cluster_type), Cluster(name='Cluster 3', type=cluster_type), ) Cluster.objects.bulk_create(clusters) tenant_groups = ( TenantGroup(name='Tenant Group 1', slug='tenant-group-1'), TenantGroup(name='Tenant Group 2', slug='tenant-group-2'), TenantGroup(name='Tenant Group 3', slug='tenant-group-3'), ) for tenantgroup in tenant_groups: tenantgroup.save() tenants = ( Tenant(name='Tenant 1', slug='tenant-1'), Tenant(name='Tenant 2', slug='tenant-2'), Tenant(name='Tenant 3', slug='tenant-3'), ) Tenant.objects.bulk_create(tenants) tags = create_tags('Alpha', 'Bravo', 'Charlie') for i in range(0, 3): is_active = bool(i % 2) c = ConfigContext.objects.create( name='Config Context {}'.format(i + 1), is_active=is_active, data='{"foo": 123}') c.regions.set([regions[i]]) c.site_groups.set([site_groups[i]]) c.sites.set([sites[i]]) c.device_types.set([device_types[i]]) c.roles.set([device_roles[i]]) c.platforms.set([platforms[i]]) c.cluster_groups.set([cluster_groups[i]]) c.clusters.set([clusters[i]]) c.tenant_groups.set([tenant_groups[i]]) c.tenants.set([tenants[i]]) c.tags.set([tags[i]])