diff --git a/nova/objects/__init__.py b/nova/objects/__init__.py index 088a00d4c6ff..d62752ffdc2d 100644 --- a/nova/objects/__init__.py +++ b/nova/objects/__init__.py @@ -17,6 +17,7 @@ def register_all(): # NOTE(danms): You must make sure your object gets imported in this # function in order for it to be registered by services that may # need to receive it via RPC. + __import__('nova.objects.dns_domain') __import__('nova.objects.instance') __import__('nova.objects.instance_info_cache') __import__('nova.objects.security_group') diff --git a/nova/objects/dns_domain.py b/nova/objects/dns_domain.py new file mode 100644 index 000000000000..6b81dd4b8068 --- /dev/null +++ b/nova/objects/dns_domain.py @@ -0,0 +1,71 @@ +# Copyright (C) 2014, Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from nova import db +from nova.objects import base +from nova.objects import fields + + +class DNSDomain(base.NovaPersistentObject, base.NovaObject): + # Version 1.0: Initial version + VERSION = '1.0' + + fields = { + 'domain': fields.StringField(), + 'scope': fields.StringField(nullable=True), + 'availability_zone': fields.StringField(nullable=True), + 'project_id': fields.StringField(nullable=True), + } + + @staticmethod + def _from_db_object(context, vif, db_vif): + for field in vif.fields: + vif[field] = db_vif[field] + vif._context = context + vif.obj_reset_changes() + return vif + + @base.remotable_classmethod + def get_by_domain(cls, context, domain): + db_dnsd = db.dnsdomain_get(context, domain) + if db_dnsd: + return cls._from_db_object(context, cls(), db_dnsd) + + @base.remotable_classmethod + def register_for_zone(cls, context, domain, zone): + db.dnsdomain_register_for_zone(context, domain, zone) + + @base.remotable_classmethod + def register_for_project(cls, context, domain, project): + db.dnsdomain_register_for_project(context, domain, project) + + @base.remotable_classmethod + def delete_by_domain(cls, context, domain): + db.dnsdomain_unregister(context, domain) + + +class DNSDomainList(base.ObjectListBase, base.NovaObject): + # Version 1.0: Initial version + VERSION = '1.0' + fields = { + 'objects': fields.ListOfObjectsField('DNSDomain'), + } + child_versions = { + '1.0': '1.0', + } + + @base.remotable_classmethod + def get_all(cls, context): + db_domains = db.dnsdomain_get_all(context) + return base.obj_make_list(context, cls(), DNSDomain, db_domains) diff --git a/nova/tests/objects/test_dns_domain.py b/nova/tests/objects/test_dns_domain.py new file mode 100644 index 000000000000..3791d172092d --- /dev/null +++ b/nova/tests/objects/test_dns_domain.py @@ -0,0 +1,85 @@ +# Copyright (C) 2014, Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from nova import db +from nova.objects import dns_domain +from nova.tests.objects import test_objects + + +fake_dnsd = { + 'created_at': None, + 'updated_at': None, + 'deleted_at': None, + 'deleted': 0, + 'domain': 'blah.example.com', + 'scope': 'private', + 'availability_zone': 'overthere', + 'project_id': '867530niner', +} + + +class _TestDNSDomain(object): + @staticmethod + def _compare(test, db, obj): + for field, value in db.items(): + test.assertEqual(db[field], obj[field]) + + def test_get_by_domain(self): + with mock.patch.object(db, 'dnsdomain_get') as get: + get.return_value = fake_dnsd + dnsd = dns_domain.DNSDomain.get_by_domain(self.context, 'domain') + self._compare(self, fake_dnsd, dnsd) + + def test_register_for_zone(self): + dns_domain.DNSDomain.register_for_zone(self.context.elevated(), + 'domain', 'zone') + dnsd = dns_domain.DNSDomain.get_by_domain(self.context, 'domain') + self.assertEqual('domain', dnsd.domain) + self.assertEqual('zone', dnsd.availability_zone) + + def test_register_for_project(self): + dns_domain.DNSDomain.register_for_project(self.context.elevated(), + 'domain', 'project') + dnsd = dns_domain.DNSDomain.get_by_domain(self.context, 'domain') + self.assertEqual('domain', dnsd.domain) + self.assertEqual('project', dnsd.project_id) + + def test_delete_by_domain(self): + dns_domain.DNSDomain.register_for_zone(self.context.elevated(), + 'domain', 'zone') + dnsd = dns_domain.DNSDomain.get_by_domain(self.context, 'domain') + self.assertEqual('domain', dnsd.domain) + self.assertEqual('zone', dnsd.availability_zone) + + dns_domain.DNSDomain.delete_by_domain(self.context.elevated(), + 'domain') + dnsd = dns_domain.DNSDomain.get_by_domain(self.context, 'domain') + self.assertEqual(None, dnsd) + + def test_get_all(self): + with mock.patch.object(db, 'dnsdomain_get_all') as get: + get.return_value = [fake_dnsd] + dns_domains = dns_domain.DNSDomainList.get_all(self.context) + + +class TestDNSDomainObject(test_objects._LocalTest, + _TestDNSDomain): + pass + + +class TestRemoteDNSDomainObject(test_objects._RemoteTest, + _TestDNSDomain): + pass