Пример #1
0
 def test_last_image_sync(self):
     owner = factory.make_admin()
     handler = ControllerHandler(owner, {})
     node = factory.make_RackController(owner=owner)
     result = handler.list({})
     self.assertEqual(1, len(result))
     self.assertEqual(NODE_TYPE.RACK_CONTROLLER, result[0].get('node_type'))
     self.assertEqual(result[0].get(
         'last_image_sync'), dehydrate_datetime(node.last_image_sync))
     data = handler.get({"system_id": node.system_id})
     self.assertEqual(data.get("last_image_sync"), dehydrate_datetime(
         node.last_image_sync))
Пример #2
0
 def dehydrate_space(self, space):
     data = {
         "id": space.id,
         "name": space.get_name(),
         "description": space.description,
         "updated": dehydrate_datetime(space.updated),
         "created": dehydrate_datetime(space.created),
         "subnet_ids":
         sorted(subnet.id for subnet in space.subnet_set.all()),
         "vlan_ids": sorted(vlan.id for vlan in space.vlan_set.all()),
     }
     return data
Пример #3
0
 def dehydrate_fabric(self, fabric):
     data = {
         "id": fabric.id,
         "name": fabric.get_name(),
         "description": fabric.description,
         "class_type": fabric.class_type,
         "updated": dehydrate_datetime(fabric.updated),
         "created": dehydrate_datetime(fabric.created),
         "vlan_ids": sorted([vlan.id for vlan in fabric.vlan_set.all()]),
     }
     data["default_vlan_id"] = data["vlan_ids"][0]
     return data
Пример #4
0
 def dehydrate_domain(self, domain, for_list=False):
     if domain.id == 0:
         displayname = "%s (default)" % domain.name
     else:
         displayname = domain.name
     data = {
         "id": domain.id,
         "name": domain.name,
         "displayname": displayname,
         "authoritative": domain.authoritative,
         "ttl": domain.ttl,
         "updated": dehydrate_datetime(domain.updated),
         "created": dehydrate_datetime(domain.created),
         }
     ip_map = StaticIPAddress.objects.get_hostname_ip_mapping(
         domain, raw_ttl=True)
     rr_map = DNSData.objects.get_hostname_dnsdata_mapping(
         domain, raw_ttl=True)
     domainname_len = len(domain.name)
     for name, info in ip_map.items():
         name = name[:-domainname_len - 1]
         if info.system_id is not None:
             rr_map[name].system_id = info.system_id
         for ip in info.ips:
             if IPAddress(ip).version == 4:
                 rr_map[name].rrset.add((info.ttl, 'A', ip))
             else:
                 rr_map[name].rrset.add((info.ttl, 'AAAA', ip))
     rrsets = [
         {
             'name': hostname,
             'system_id': info.system_id,
             'node_type': info.node_type,
             'ttl': ttl,
             'rrtype': rrtype,
             'rrdata': rrdata,
         }
         for hostname, info in rr_map.items()
         for ttl, rrtype, rrdata in info.rrset
     ]
     data['resource_count'] = len(rrsets)
     hosts = set()
     for record in rrsets:
         if record['system_id'] is not None:
             hosts.add(record['system_id'])
     data['hosts'] = len(hosts)
     if not for_list:
         data.update({
             "rrsets": rrsets,
         })
     return data
Пример #5
0
    def dehydrate_script_result(self, script_result, handler):
        results = script_result.read_results().get("results", {})
        data = {
            "id":
            script_result.id,
            "updated":
            dehydrate_datetime(script_result.updated),
            "script":
            script_result.script_id,
            "parameters":
            script_result.parameters,
            "physical_blockdevice":
            script_result.physical_blockdevice_id,
            "script_version":
            script_result.script_version_id,
            "status":
            script_result.status,
            "status_name":
            script_result.status_name,
            "exit_status":
            script_result.exit_status,
            "started":
            dehydrate_datetime(script_result.started),
            "ended":
            dehydrate_datetime(script_result.ended),
            "runtime":
            script_result.runtime,
            "starttime":
            script_result.starttime,
            "endtime":
            script_result.endtime,
            "estimated_runtime":
            script_result.estimated_runtime,
            "name":
            script_result.name,
            "result_type":
            script_result.script_set.result_type,
            "hardware_type":
            script_result.script.hardware_type,
            "tags":
            ", ".join(script_result.script.tags),
            "results": [{
                "name": key,
                "title": key,
                "description": "",
                "value": value,
                "surfaced": False,
            } for key, value in results.items()],
        }

        return data
Пример #6
0
 def dehydrate_iprange(self, iprange, for_list=False):
     data = {
         "id": iprange.id,
         "created": dehydrate_datetime(iprange.created),
         "updated": dehydrate_datetime(iprange.updated),
         "subnet": iprange.subnet_id,
         "start_ip": iprange.start_ip,
         "end_ip": iprange.end_ip,
         "comment": iprange.comment,
         "user": iprange.user.username if iprange.user else "",
         "type": iprange.type,
         "vlan": iprange.subnet.vlan_id if iprange.subnet else None,
     }
     return data
Пример #7
0
def MatchesRenderedNotification(ntfn):
    """Return a matcher for a rendered notification."""
    return MatchesDict(
        {
            "id": Equals(ntfn.id),
            "ident": Is(None) if ntfn.ident is None else Equals(ntfn.ident),
            "user": Is(None) if ntfn.user_id is None else Equals(ntfn.user_id),
            "users": Is(ntfn.users),
            "admins": Is(ntfn.admins),
            "updated": Equals(dehydrate_datetime(ntfn.updated)),
            "created": Equals(dehydrate_datetime(ntfn.created)),
            "message": Equals(ntfn.render()),
            "category": Equals(ntfn.category),
        }
    )
Пример #8
0
 def dehydrate_event(self, event):
     data = {
         "id": event.id,
         "node_id": event.node.id,
         "action": event.action,
         "description": event.description,
         "type": {
             "level": dehydrate_event_type_level(event.type.level),
             "name": event.type.name,
             "description": event.type.description,
         },
         "updated": dehydrate_datetime(event.updated),
         "created": dehydrate_datetime(event.created),
     }
     return data
Пример #9
0
 def dehydrate_script(self, script):
     return {
         'id': script.id,
         'name': script.name,
         'title': script.title,
         'description': script.description,
         'script_type': script.script_type,
         'tags': script.tags,
         'timeout': '0%s' % str(script.timeout),
         'destructive': script.destructive,
         'default': script.default,
         'script': script.script_id,
         'created': dehydrate_datetime(script.created),
         'updated': dehydrate_datetime(script.updated),
     }
Пример #10
0
 def dehydrate_service(self, service, for_list=False):
     data = {
         "id": service.id,
         "node": service.node.id,
         "name": service.name,
         "status": service.status,
         "status_info": service.status_info,
         "updated": dehydrate_datetime(service.updated),
         "created": dehydrate_datetime(service.created),
     }
     if for_list:
         del data["node"]
         del data["updated"]
         del data["created"]
     return data
Пример #11
0
 def dehydrate(self, package_repository):
     return {
         'id': package_repository.id,
         'name': package_repository.name,
         'url': package_repository.url,
         'distributions': package_repository.distributions,
         'disabled_pockets': package_repository.disabled_pockets,
         'disabled_components': package_repository.disabled_components,
         'components': package_repository.components,
         'arches': package_repository.arches,
         'key': package_repository.key,
         'default': package_repository.default,
         'enabled': package_repository.enabled,
         'updated': dehydrate_datetime(package_repository.updated),
         'created': dehydrate_datetime(package_repository.created),
     }
Пример #12
0
 def dehydrate_discovery(self, discovery: Discovery, for_list=False):
     data = {
         "discovery_id": discovery.discovery_id,
         "fabric": discovery.fabric_id,
         "fabric_name": discovery.fabric_name,
         "hostname": discovery.hostname,
         "id": discovery.id,
         "ip": discovery.ip,
         "is_external_dhcp": discovery.is_external_dhcp,
         "mdns": discovery.mdns_id,
         "mac_address": discovery.mac_address,
         "mac_organization": discovery.mac_organization,
         "neighbour": discovery.neighbour_id,
         "observer": discovery.observer_id,
         "observer_hostname": discovery.observer_hostname,
         "observer_interface": discovery.observer_interface_id,
         "observer_interface_name": discovery.observer_interface_name,
         "observer_system_id": discovery.observer_system_id,
         "subnet": discovery.subnet_id,
         "subnet_cidr": discovery.subnet_cidr,
         "vid": discovery.vid,
         "vlan": discovery.vlan_id,
         "first_seen": str(
             time.mktime(discovery.first_seen.timetuple())
             + discovery.first_seen.microsecond / 1e6
         ),
         "last_seen": dehydrate_datetime(discovery.last_seen),
     }
     return data
Пример #13
0
 def test_get_history(self):
     user = factory.make_User()
     handler = NodeResultHandler(user, {}, None)
     node = factory.make_Node(owner=user)
     script = factory.make_Script()
     script_results = []
     for _ in range(10):
         script_set = factory.make_ScriptSet(node=node)
         script_results.append(factory.make_ScriptResult(
             script=script, script_set=script_set,
             status=SCRIPT_STATUS.PASSED))
     latest_script_result = script_results[-1]
     script_results = sorted(
         script_results, key=lambda i: i.id, reverse=True)
     queries = CountQueries()
     with queries:
         ret = handler.get_history({'id': latest_script_result.id})
     self.assertEqual(4, queries.num_queries)
     for script_result, out in zip(script_results, ret):
         self.assertDictEqual({
             'id': script_result.id,
             'updated': dehydrate_datetime(script_result.updated),
             'status': script_result.status,
             'status_name': script_result.status_name,
             'runtime': script_result.runtime,
             'starttime': script_result.starttime,
             'endtime': script_result.endtime,
             'estimated_runtime': script_result.estimated_runtime,
             'suppressed': script_result.suppressed,
         }, out)
Пример #14
0
 def dehydrate_last_image_sync(self, last_image_sync):
     """Return formatted datetime."""
     return (
         dehydrate_datetime(last_image_sync)
         if last_image_sync is not None
         else None
     )
Пример #15
0
 def test_get(self):
     user = factory.make_User()
     handler = ResourcePoolHandler(user, {}, None)
     pool = factory.make_ResourcePool()
     result = handler.get({"id": pool.id})
     self.assertEqual(
         {'id': pool.id,
          'name': pool.name,
          'description': pool.description,
          'is_default': False,
          'created': dehydrate_datetime(pool.created),
          'updated': dehydrate_datetime(pool.updated),
          'machine_total_count': 0,
          'machine_ready_count': 0,
          'permissions': []},
         result)
Пример #16
0
    def dehydrate_events(self, obj):
        """Dehydrate the node events.

        The latests 50 not including DEBUG events will be dehydrated. The
        `EventsHandler` needs to be used if more are required.
        """
        events = (
            Event.objects.filter(node=obj)
            .exclude(type__level=logging.DEBUG)
            .select_related("type")
            .order_by("-id")[:50]
        )
        return [
            {
                "id": event.id,
                "type": {
                    "id": event.type.id,
                    "name": event.type.name,
                    "description": event.type.description,
                    "level": dehydrate_event_type_level(event.type.level),
                },
                "description": event.description,
                "created": dehydrate_datetime(event.created),
            }
            for event in events
        ]
Пример #17
0
 def get_history(self, params):
     """Return a list of historic results."""
     id = params.get("id")
     script_result = (ScriptResult.objects.filter(id=id).only(
         "status",
         "script_id",
         "script_set_id",
         "physical_blockdevice_id",
         "interface_id",
         "script_name",
     ).first())
     history_qs = script_result.history.only(
         "id",
         "updated",
         "status",
         "started",
         "ended",
         "script_id",
         "script_name",
         "script_set_id",
         "physical_blockdevice_id",
         "interface_id",
         "suppressed",
     )
     return [{
         "id": history.id,
         "updated": dehydrate_datetime(history.updated),
         "status": history.status,
         "status_name": history.status_name,
         "runtime": history.runtime,
         "starttime": history.starttime,
         "endtime": history.endtime,
         "estimated_runtime": history.estimated_runtime,
         "suppressed": history.suppressed,
     } for history in history_qs]
Пример #18
0
 def dehydrate_user(self, user, sshkeys_count=0, for_self=False):
     data = {
         "id": user.id,
         "username": user.username,
         "first_name": user.first_name,
         "last_name": user.last_name,
         "email": user.email,
         "is_superuser": user.is_superuser,
         "sshkeys_count": sshkeys_count,
         "last_login": dehydrate_datetime(user.last_login),
         "is_local": user.userprofile.is_local,
         "completed_intro": user.userprofile.completed_intro,
         "machines_count": user.node_set.count(),
     }
     if for_self:
         permissions = []
         if user.has_perm(NodePermission.admin):
             permissions.append("machine_create")
         if user.has_perm(NodePermission.view):
             permissions.append("device_create")
         if user.has_perm(ResourcePoolPermission.create):
             permissions.append("resource_pool_create")
         if user.has_perm(ResourcePoolPermission.delete):
             permissions.append("resource_pool_delete")
         if user.has_perm(PodPermission.create):
             permissions.append("pod_create")
         data["global_permissions"] = permissions
     return data
Пример #19
0
 def dehydrate(self, package_repository):
     return {
         "id": package_repository.id,
         "name": package_repository.name,
         "url": package_repository.url,
         "distributions": package_repository.distributions,
         "disabled_pockets": package_repository.disabled_pockets,
         "disabled_components": package_repository.disabled_components,
         "disable_sources": package_repository.disable_sources,
         "components": package_repository.components,
         "arches": package_repository.arches,
         "key": package_repository.key,
         "default": package_repository.default,
         "enabled": package_repository.enabled,
         "updated": dehydrate_datetime(package_repository.updated),
         "created": dehydrate_datetime(package_repository.created),
     }
Пример #20
0
 def test_get(self):
     user = factory.make_User()
     handler = ResourcePoolHandler(user, {}, None)
     pool = factory.make_ResourcePool()
     result = handler.get({"id": pool.id})
     self.assertEqual(
         {
             "id": pool.id,
             "name": pool.name,
             "description": pool.description,
             "is_default": False,
             "created": dehydrate_datetime(pool.created),
             "updated": dehydrate_datetime(pool.updated),
             "machine_total_count": 0,
             "machine_ready_count": 0,
             "permissions": [],
         },
         result,
     )
Пример #21
0
 def dehydrate_subnet(self, subnet, for_list=False):
     data = {
         "id":
         subnet.id,
         "updated":
         dehydrate_datetime(subnet.updated),
         "created":
         dehydrate_datetime(subnet.created),
         "name":
         subnet.name,
         "description":
         subnet.description,
         "dns_servers": (" ".join(sorted(subnet.dns_servers))
                         if subnet.dns_servers is not None else ""),
         "vlan":
         subnet.vlan_id,
         "space":
         subnet.vlan.space_id,
         "rdns_mode":
         subnet.rdns_mode,
         "allow_dns":
         subnet.allow_dns,
         "allow_proxy":
         subnet.allow_proxy,
         "cidr":
         subnet.cidr,
         "gateway_ip":
         subnet.gateway_ip,
         "active_discovery":
         subnet.active_discovery,
         "managed":
         subnet.managed,
     }
     full_range = subnet.get_iprange_usage()
     metadata = IPRangeStatistics(full_range)
     data["statistics"] = metadata.render_json(include_ranges=True,
                                               include_suggestions=True)
     data["version"] = IPNetwork(subnet.cidr).version
     if not for_list:
         data["ip_addresses"] = subnet.render_json_for_related_ips(
             with_username=True, with_summary=True)
     return data
Пример #22
0
 def dehydrate_script(self, script):
     return {
         'id': script.id,
         'name': script.name,
         'title': script.title,
         'description': script.description,
         'tags': script.tags,
         'script_type': script.script_type,
         'hardware_type': script.hardware_type,
         'parallel': script.parallel,
         'results': json.dumps(script.results),
         'parameters': json.dumps(script.parameters),
         'packages': json.dumps(script.packages),
         'timeout': '0%s' % str(script.timeout),
         'destructive': script.destructive,
         'default': script.default,
         'script': script.script_id,
         'created': dehydrate_datetime(script.created),
         'updated': dehydrate_datetime(script.updated),
     }
Пример #23
0
 def dehydrate_node_device(self, node_device):
     return {
         "id": node_device.id,
         "created": dehydrate_datetime(node_device.created),
         "updated": dehydrate_datetime(node_device.updated),
         "bus": node_device.bus,
         "hardware_type": node_device.hardware_type,
         "vendor_id": node_device.vendor_id,
         "product_id": node_device.product_id,
         "vendor_name": node_device.vendor_name,
         "product_name": node_device.product_name,
         "commissioning_driver": node_device.commissioning_driver,
         "bus_number": node_device.bus_number,
         "device_number": node_device.device_number,
         "pci_address": node_device.pci_address,
         "physical_blockdevice_id": node_device.physical_blockdevice_id,
         "physical_interface_id": node_device.physical_interface_id,
         "numa_node_id": node_device.numa_node_id,
         "node_id": node_device.node_id,
     }
Пример #24
0
 def dehydrate_vlan(self, vlan, for_list=False):
     data = {
         "id": vlan.id,
         "name": vlan.name,
         "description": vlan.description,
         "vid": vlan.vid,
         "mtu": vlan.mtu,
         "fabric": vlan.fabric_id,
         "space": vlan.space_id,
         "updated": dehydrate_datetime(vlan.updated),
         "created": dehydrate_datetime(vlan.created),
         "dhcp_on": vlan.dhcp_on,
         "external_dhcp": vlan.external_dhcp,
         "primary_rack": vlan.primary_rack,
         "secondary_rack": vlan.secondary_rack,
         "relay_vlan": vlan.relay_vlan_id,
     }
     data["rack_sids"] = sorted(
         list(
             {
                 interface.node.system_id
                 for interface in vlan.interface_set.all()
                 if interface.node_id is not None
                 and interface.node.is_rack_controller
             }
         )
     )
     if not for_list:
         data["node_ids"] = sorted(
             list(
                 {
                     interface.node_id
                     for interface in vlan.interface_set.all()
                     if interface.node_id is not None
                 }
             )
         )
         data["space_ids"] = sorted(
             {subnet.space.id for subnet in vlan.subnet_set.all()}
         )
     return data
Пример #25
0
 def dehydrate(self, obj, data, for_list=False):
     data.update({
         'sshkeys_count': obj.sshkeys_count,
         'is_local': obj.userprofile.is_local,
         'machines_count': obj.machines_count,
         'last_login': dehydrate_datetime(obj.last_login)
     })
     if obj.id == self.user.id:
         # User is reading information about itself, so provide the global
         # permissions.
         data['global_permissions'] = self._get_global_permissions()
     return data
Пример #26
0
 def dehydrate_event(self, event):
     data = {
         "id": event.id,
         "node_id": event.node.id,
         "node_hostname": event.node_hostname,
         "user": event.user.id,
         "username": event.username,
         "ip_address": event.ip_address,
         "endpoint": event.endpoint,
         "user_agent": event.user_agent,
         "action": event.action,
         "description": event.description,
         "type": {
             "level": dehydrate_event_type_level(event.type.level),
             "name": event.type.name,
             "description": event.type.description,
             },
         "updated": dehydrate_datetime(event.updated),
         "created": dehydrate_datetime(event.created),
         }
     return data
Пример #27
0
 def dehydrate(self, obj, data, for_list=False):
     data.update({
         "sshkeys_count": obj.sshkeys_count,
         "is_local": obj.userprofile.is_local,
         "completed_intro": obj.userprofile.completed_intro,
         "machines_count": obj.machines_count,
         "last_login": dehydrate_datetime(obj.last_login),
     })
     if obj.id == self.user.id:
         # User is reading information about itself, so provide the global
         # permissions.
         data["global_permissions"] = self._get_global_permissions()
     return data
Пример #28
0
 def dehydrate_dhcp_snippet(self, dhcp_snippet):
     node_system_id = None
     subnet_id = None
     iprange_id = None
     if dhcp_snippet.subnet is not None:
         subnet_id = dhcp_snippet.subnet.id
         if dhcp_snippet.iprange is not None:
             iprange_id = dhcp_snippet.iprange.id
     elif dhcp_snippet.node is not None:
         node_system_id = dhcp_snippet.node.system_id
     return {
         "id":
         dhcp_snippet.id,
         "name":
         dhcp_snippet.name,
         "description":
         dhcp_snippet.description,
         "value":
         dhcp_snippet.value.data,
         "history": [{
             "id": value.id,
             "value": value.data,
             "created": format_datetime(value.created),
         } for value in dhcp_snippet.value.previous_versions()],
         "enabled":
         dhcp_snippet.enabled,
         "node":
         node_system_id,
         "subnet":
         subnet_id,
         "iprange":
         iprange_id,
         "updated":
         dehydrate_datetime(dhcp_snippet.updated),
         "created":
         dehydrate_datetime(dhcp_snippet.created),
     }
Пример #29
0
 def dehydrate_script(self, script):
     return {
         "id": script.id,
         "name": script.name,
         "title": script.title,
         "description": script.description,
         "tags": script.tags,
         "script_type": script.script_type,
         "hardware_type": script.hardware_type,
         "parallel": script.parallel,
         "results": script.results,
         "parameters": script.parameters,
         "packages": script.packages,
         "timeout": "0%s" % str(script.timeout),
         "destructive": script.destructive,
         "default": script.default,
         "script": script.script_id,
         "for_hardware": script.for_hardware,
         "may_reboot": script.may_reboot,
         "recommission": script.recommission,
         "apply_configured_networking": script.apply_configured_networking,
         "created": dehydrate_datetime(script.created),
         "updated": dehydrate_datetime(script.updated),
     }
Пример #30
0
 def dehydrate_script(self, script):
     return {
         'id': script.id,
         'name': script.name,
         'title': script.title,
         'description': script.description,
         'tags': script.tags,
         'script_type': script.script_type,
         'hardware_type': script.hardware_type,
         'parallel': script.parallel,
         'results': json.dumps(script.results),
         'parameters': json.dumps(script.parameters),
         'packages': json.dumps(script.packages),
         'timeout': '0%s' % str(script.timeout),
         'destructive': script.destructive,
         'default': script.default,
         'script': script.script_id,
         'for_hardware': script.for_hardware,
         'may_reboot': script.may_reboot,
         'recommission': script.recommission,
         'apply_configured_networking': script.apply_configured_networking,
         'created': dehydrate_datetime(script.created),
         'updated': dehydrate_datetime(script.updated),
     }