From b66d960537b8dbba421125b49e5c541ccfde47bc Mon Sep 17 00:00:00 2001 From: Andreas Scheuring Date: Wed, 8 Mar 2017 09:35:00 +0100 Subject: [PATCH] Add DPMObjectId configuration object This patch adds a new DPMObjectId configuraiton object and corresponding type to os-dpm. This new configuration object represents a DPM object id. It inlcudes validation if the given config value has the correct object-id format. It allows upper and lower case values to be defined, however internally it converts everything to lower case (which is required by the hmc webservices). Example for a valid object-id: fa1f2466-12df-311a-804c-4ed2cc1d656b Change-Id: I6b446f9634a018e005e112cf1e3f9cc7f6fb4a1b --- os_dpm/config/cfg.py | 30 ++++++++++++++++ os_dpm/config/types.py | 36 +++++++++++++++++++ os_dpm/constants.py | 17 +++++++++ os_dpm/tests/unit/config/test_cfg.py | 26 ++++++++++++++ os_dpm/tests/unit/config/test_types.py | 48 ++++++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 os_dpm/config/cfg.py create mode 100644 os_dpm/config/types.py create mode 100644 os_dpm/constants.py create mode 100644 os_dpm/tests/unit/config/test_cfg.py create mode 100644 os_dpm/tests/unit/config/test_types.py diff --git a/os_dpm/config/cfg.py b/os_dpm/config/cfg.py new file mode 100644 index 0000000..65a82d9 --- /dev/null +++ b/os_dpm/config/cfg.py @@ -0,0 +1,30 @@ +# Copyright 2017 IBM Corp. All Rights Reserved. +# +# 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 oslo_config import cfg + +from os_dpm.config.types import DPMObjectIdType + + +class DPMObjectIdOpt(cfg.Opt): + def __init__(self, name, help=None): + """Option with DPM Object-Id type + + Option with ``type`` :class:`DPMObjectIdType` + + :param name: the option's name + :param help: an explanation of how the option is used + """ + super(DPMObjectIdOpt, self).__init__(name, type=DPMObjectIdType(), + help=help) diff --git a/os_dpm/config/types.py b/os_dpm/config/types.py new file mode 100644 index 0000000..63ca482 --- /dev/null +++ b/os_dpm/config/types.py @@ -0,0 +1,36 @@ +# Copyright 2017 IBM Corp. All Rights Reserved. +# +# 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 oslo_config import cfg + +from os_dpm import constants as const + + +class DPMObjectIdType(cfg.types.String): + def __init__(self): + """DPM Object-Id Type. + + DPM Object-Id values are returned as lower case str objects. + If the configuration value is provided as upper case, this type + converts it to lower case. + """ + + super(DPMObjectIdType, self).__init__( + type_name='DPM Object-Id value', ignore_case=True, max_length=36, + regex="^" + const.OBJECT_ID_REGEX + "$") + + def __call__(self, value): + val = super(DPMObjectIdType, self).__call__(value) + return val.lower() diff --git a/os_dpm/constants.py b/os_dpm/constants.py new file mode 100644 index 0000000..259ff2f --- /dev/null +++ b/os_dpm/constants.py @@ -0,0 +1,17 @@ +# Copyright (c) 2017 IBM Corp. +# +# All Rights Reserved. +# +# 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. + +OBJECT_ID_REGEX = "[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}" diff --git a/os_dpm/tests/unit/config/test_cfg.py b/os_dpm/tests/unit/config/test_cfg.py new file mode 100644 index 0000000..980283a --- /dev/null +++ b/os_dpm/tests/unit/config/test_cfg.py @@ -0,0 +1,26 @@ +# Copyright 2017 IBM Corp. All Rights Reserved. +# +# 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 oslotest import base + +from os_dpm.config import cfg + + +class TestDPMObjectIdOpt(base.BaseTestCase): + def test_object_id_opt(self): + opt = cfg.DPMObjectIdOpt("foo-name", help="foo-help") + self.assertEqual("foo-help", opt.help) + self.assertEqual("foo-name", opt.name) + self.assertEqual(cfg.DPMObjectIdType, type(opt.type)) diff --git a/os_dpm/tests/unit/config/test_types.py b/os_dpm/tests/unit/config/test_types.py new file mode 100644 index 0000000..b4beae2 --- /dev/null +++ b/os_dpm/tests/unit/config/test_types.py @@ -0,0 +1,48 @@ +# Copyright 2017 IBM Corp. All Rights Reserved. +# +# 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 oslotest import base + +from os_dpm.config import types + +VALID_DPM_OBJECT_ID = "fa1f2466-12df-311a-804c-4ed2cc1d656b" +VALID_DPM_OBJECT_ID_UC = "FA1F2466-12DF-311A-804C-4ED2CC1D656B" + + +class TestDPMObjectIdType(base.BaseTestCase): + conf_type = types.DPMObjectIdType() + + def test_empty_value_fail(self): + self.assertRaises(ValueError, self.conf_type, '') + + def test_invalid_value_fail(self): + self.assertRaises(ValueError, self.conf_type, 'foobar') + + def test_valid_object_id(self): + self.assertEqual(VALID_DPM_OBJECT_ID, + self.conf_type(VALID_DPM_OBJECT_ID)) + + def test_valid_object_id_ignore_case(self): + self.assertEqual(VALID_DPM_OBJECT_ID, + self.conf_type(VALID_DPM_OBJECT_ID_UC)) + + def test_object_id_too_long(self): + self.assertRaises(ValueError, self.conf_type, + VALID_DPM_OBJECT_ID + "1") + + def test_repr(self): + t = types.DPMObjectIdType() + self.assertEqual( + "String(regex='^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$')", + repr(t))