Skip to content

Commit

Permalink
Add read objects for dns related MAAS entities (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis authored Sep 22, 2023
1 parent 7c1374b commit fec3b50
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 1 deletion.
20 changes: 20 additions & 0 deletions maas/client/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ def devices(origin):
"list": origin.Devices.read,
}

@facade
def dnsresources(origin):
return {
"get": origin.DNSResource.read,
"list": origin.DNSResources.read,
}

@facade
def dnsresourcerecords(origin):
return {
"get": origin.DNSResourceRecord.read,
"list": origin.DNSResourceRecords.read,
}

@facade
def domains(origin):
return {
Expand Down Expand Up @@ -172,6 +186,12 @@ def ip_ranges(origin):
"list": origin.IPRanges.read,
}

@facade
def ip_addresses(origin):
return {
"list": origin.IPAddresses.read,
}

@facade
def maas(origin):
attrs = (
Expand Down
3 changes: 3 additions & 0 deletions maas/client/viscera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,8 @@ def __init__(self, session):
".boot_sources",
".controllers",
".devices",
".dnsresources",
".dnsresourcerecords",
".domains",
".events",
".subnets",
Expand All @@ -1178,6 +1180,7 @@ def __init__(self, session):
".filesystems",
".interfaces",
".ipranges",
".ip_addresses",
".logical_volumes",
".maas",
".machines",
Expand Down
38 changes: 38 additions & 0 deletions maas/client/viscera/dnsresourcerecords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Objects for dnsresourcerecords."""

__all__ = ["DNSResourceRecord", "DNSResourceRecords"]

from . import check, check_optional, Object, ObjectField, ObjectSet, ObjectType


class DNSResourceRecordType(ObjectType):
"""Metaclass for `DNSResourceRecords`."""

async def read(cls):
data = await cls._handler.read()
return cls(map(cls._object, data))


class DNSResourceRecords(ObjectSet, metaclass=DNSResourceRecordType):
"""The set of dnsresourcerecords stored in MAAS."""


class DNSResourceRecordType(ObjectType):
async def read(cls, id):
data = await cls._handler.read(id=id)
return cls(data)


class DNSResourceRecord(Object, metaclass=DNSResourceRecordType):
"""A dnsresourcerecord stored in MAAS."""

id = ObjectField.Checked("id", check(int), readonly=True, pk=True)
ttl = ObjectField.Checked("ttl", check_optional(int), check_optional(int))
rrtype = ObjectField.Checked("rrtype", check(str), check(str))
rrdata = ObjectField.Checked("rrdata", check(str), check(str))
fqdn = ObjectField.Checked("fqdn", check(str), check(str))

def __repr__(self):
return super(DNSResourceRecord, self).__repr__(
fields={"ttl", "rrtype", "rrdata", "fqdn"}
)
48 changes: 48 additions & 0 deletions maas/client/viscera/dnsresources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Objects for dnsresources."""

__all__ = ["DNSResource", "DNSResources"]

from . import (
check,
check_optional,
Object,
ObjectField,
ObjectSet,
ObjectType,
ObjectFieldRelatedSet,
)


class DNSResourceType(ObjectType):
"""Metaclass for `DNSResources`."""

async def read(cls):
data = await cls._handler.read()
return cls(map(cls._object, data))


class DNSResources(ObjectSet, metaclass=DNSResourceType):
"""The set of dnsresources stored in MAAS."""


class DNSResourceType(ObjectType):
async def read(cls, id):
data = await cls._handler.read(id=id)
return cls(data)


class DNSResource(Object, metaclass=DNSResourceType):
"""A dnsresource stored in MAAS."""

id = ObjectField.Checked("id", check(int), readonly=True, pk=True)
address_ttl = ObjectField.Checked(
"address_ttl", check_optional(int), check_optional(int)
)
fqdn = ObjectField.Checked("fqdn", check(str), check(str))
ip_addresses = ObjectFieldRelatedSet("ip_addresses", "IPAddresses")
resource_records = ObjectFieldRelatedSet("resource_records", "DNSResourceRecords")

def __repr__(self):
return super(DNSResource, self).__repr__(
fields={"address_ttl", "fqdn", "ip_addresses", "resource_records"}
)
54 changes: 54 additions & 0 deletions maas/client/viscera/ip_addresses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Objects for ipaddresses."""

__all__ = ["IPAddress", "IPAddresses"]

from . import (
check,
parse_timestamp,
Object,
ObjectField,
ObjectSet,
ObjectType,
ObjectFieldRelatedSet,
ObjectFieldRelated,
OriginObjectRef,
)


class IPAddressType(ObjectType):
"""Metaclass for `IPAddresses`."""

async def read(cls):
data = await cls._handler.read()
return cls(map(cls._object, data))


class IPAddresses(ObjectSet, metaclass=IPAddressType):
"""The set of ipaddresses stored in MAAS."""

_object = OriginObjectRef(name="IPAddress")


class IPAddress(Object):
"""An ipaddress stored in MAAS."""

alloc_type = ObjectField.Checked("alloc_type", check(int), check(int))
alloc_type_name = ObjectField.Checked("alloc_type_name", check(str), check(str))
created = ObjectField.Checked("created", parse_timestamp, readonly=True)
ip = ObjectField.Checked("ip", check(str))
owner = ObjectFieldRelated("owner", "User")
interface_set = ObjectFieldRelatedSet("interface_set", "Interfaces")
subnet = ObjectFieldRelated("subnet", "Subnet", readonly=True, default=None)

def __repr__(self):
return super(IPAddress, self).__repr__(
fields={
"alloc_type",
"alloc_type_name",
"created",
"ip",
"owner",
"interface_set",
"subnet",
}
)
50 changes: 50 additions & 0 deletions maas/client/viscera/tests/test_dnsresourcerecords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Tests for `maas.client.viscera.dnsresourcerecords`."""

import random

from testtools.matchers import Equals

from .. import dnsresourcerecords

from ..testing import bind
from ...testing import make_string_without_spaces, TestCase


def make_origin():
"""
Create a new origin with DNSResourceRecord and DNSResourceRecords. The former
refers to the latter via the origin, hence why it must be bound.
"""
return bind(
dnsresourcerecords.DNSResourceRecords, dnsresourcerecords.DNSResourceRecord
)


class TestDNSResourceRecords(TestCase):
def test__dnsresourcerecords_read(self):
"""DNSResourceRecords.read() returns a list of DNSResourceRecords."""
DNSResourceRecords = make_origin().DNSResourceRecords
dnsresourcerecords = [
{"id": random.randint(0, 100), "fqdn": make_string_without_spaces()}
for _ in range(3)
]
DNSResourceRecords._handler.read.return_value = dnsresourcerecords
dnsresourcerecords = DNSResourceRecords.read()
self.assertThat(len(dnsresourcerecords), Equals(3))


class TestDNSResourceRecord(TestCase):
def test__dnsresourcerecord_read(self):
DNSResourceRecord = make_origin().DNSResourceRecord
dnsresourcerecord = {
"id": random.randint(0, 100),
"fqdn": make_string_without_spaces(),
}
DNSResourceRecord._handler.read.return_value = dnsresourcerecord
self.assertThat(
DNSResourceRecord.read(id=dnsresourcerecord["id"]),
Equals(DNSResourceRecord(dnsresourcerecord)),
)
DNSResourceRecord._handler.read.assert_called_once_with(
id=dnsresourcerecord["id"]
)
45 changes: 45 additions & 0 deletions maas/client/viscera/tests/test_dnsresources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Tests for `maas.client.viscera.dnsresources`."""

import random

from testtools.matchers import Equals

from .. import dnsresources

from ..testing import bind
from ...testing import make_string_without_spaces, TestCase


def make_origin():
"""
Create a new origin with DNSResource and DNSResources. The former
refers to the latter via the origin, hence why it must be bound.
"""
return bind(dnsresources.DNSResources, dnsresources.DNSResource)


class TestDNSResources(TestCase):
def test__dnsresources_read(self):
"""DNSResources.read() returns a list of DNSResources."""
DNSResources = make_origin().DNSResources
dnsresources = [
{"id": random.randint(0, 100), "fqdn": make_string_without_spaces()}
for _ in range(3)
]
DNSResources._handler.read.return_value = dnsresources
dnsresources = DNSResources.read()
self.assertThat(len(dnsresources), Equals(3))


class TestDNSResource(TestCase):
def test__dnsresource_read(self):
DNSResource = make_origin().DNSResource
dnsresource = {
"id": random.randint(0, 100),
"fqdn": make_string_without_spaces(),
}
DNSResource._handler.read.return_value = dnsresource
self.assertThat(
DNSResource.read(id=dnsresource["id"]), Equals(DNSResource(dnsresource))
)
DNSResource._handler.read.assert_called_once_with(id=dnsresource["id"])
29 changes: 29 additions & 0 deletions maas/client/viscera/tests/test_ip_addresses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Tests for `maas.client.viscera.ip_addresses`."""

from testtools.matchers import Equals

from .. import ip_addresses

from ..testing import bind
from ...testing import TestCase


def make_origin():
"""
Create a new origin with IPAddress and IPAddresses. The former
refers to the latter via the origin, hence why it must be bound.
"""
return bind(ip_addresses.IPAddresses, ip_addresses.IPAddress)


class TestIPAddresses(TestCase):
def test__ip_addresses_read(self):
"""IPAddresses.read() returns a list of IPAddresses."""
IPAddresses = make_origin().IPAddresses
ip_addresses = [
{"ip": "10.0.0.%s" % (i + 1), "alloc_type_name": "User reserved"}
for i in range(3)
]
IPAddresses._handler.read.return_value = ip_addresses
ip_addresses = IPAddresses.read()
self.assertThat(len(ip_addresses), Equals(3))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def read(filename):
"setuptools",
"testscenarios",
"testtools",
"Twisted",
"Twisted<23.0.0",
],
description="A client API library specially for MAAS.",
long_description=read('README'),
Expand Down

0 comments on commit fec3b50

Please sign in to comment.