Browse Source

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-4ed2cc1d65

Change-Id: I6b446f9634a018e005e112cf1e3f9cc7f6fb4a1b
Andreas Scheuring 2 years ago
parent
commit
b66d960537

+ 30
- 0
os_dpm/config/cfg.py View File

@@ -0,0 +1,30 @@
1
+# Copyright 2017 IBM Corp. All Rights Reserved.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+#    http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS,
11
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+# See the License for the specific language governing permissions and
13
+# limitations under the License.
14
+
15
+from oslo_config import cfg
16
+
17
+from os_dpm.config.types import DPMObjectIdType
18
+
19
+
20
+class DPMObjectIdOpt(cfg.Opt):
21
+    def __init__(self, name, help=None):
22
+        """Option with DPM Object-Id type
23
+
24
+           Option with ``type`` :class:`DPMObjectIdType`
25
+
26
+        :param name: the option's name
27
+        :param help: an explanation of how the option is used
28
+        """
29
+        super(DPMObjectIdOpt, self).__init__(name, type=DPMObjectIdType(),
30
+                                             help=help)

+ 36
- 0
os_dpm/config/types.py View File

@@ -0,0 +1,36 @@
1
+# Copyright 2017 IBM Corp. All Rights Reserved.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+#    http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS,
11
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+# See the License for the specific language governing permissions and
13
+# limitations under the License.
14
+
15
+
16
+from oslo_config import cfg
17
+
18
+from os_dpm import constants as const
19
+
20
+
21
+class DPMObjectIdType(cfg.types.String):
22
+    def __init__(self):
23
+        """DPM Object-Id Type.
24
+
25
+        DPM Object-Id values are returned as lower case str objects.
26
+        If the configuration value is provided as upper case, this type
27
+        converts it to lower case.
28
+        """
29
+
30
+        super(DPMObjectIdType, self).__init__(
31
+            type_name='DPM Object-Id value', ignore_case=True, max_length=36,
32
+            regex="^" + const.OBJECT_ID_REGEX + "$")
33
+
34
+    def __call__(self, value):
35
+        val = super(DPMObjectIdType, self).__call__(value)
36
+        return val.lower()

+ 17
- 0
os_dpm/constants.py View File

@@ -0,0 +1,17 @@
1
+# Copyright (c) 2017 IBM Corp.
2
+#
3
+# All Rights Reserved.
4
+#
5
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
6
+#    not use this file except in compliance with the License. You may obtain
7
+#    a copy of the License at
8
+#
9
+#         http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#    Unless required by applicable law or agreed to in writing, software
12
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+#    License for the specific language governing permissions and limitations
15
+#    under the License.
16
+
17
+OBJECT_ID_REGEX = "[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}"

+ 26
- 0
os_dpm/tests/unit/config/test_cfg.py View File

@@ -0,0 +1,26 @@
1
+# Copyright 2017 IBM Corp. All Rights Reserved.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+#    http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS,
11
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+# See the License for the specific language governing permissions and
13
+# limitations under the License.
14
+
15
+
16
+from oslotest import base
17
+
18
+from os_dpm.config import cfg
19
+
20
+
21
+class TestDPMObjectIdOpt(base.BaseTestCase):
22
+    def test_object_id_opt(self):
23
+        opt = cfg.DPMObjectIdOpt("foo-name", help="foo-help")
24
+        self.assertEqual("foo-help", opt.help)
25
+        self.assertEqual("foo-name", opt.name)
26
+        self.assertEqual(cfg.DPMObjectIdType, type(opt.type))

+ 48
- 0
os_dpm/tests/unit/config/test_types.py View File

@@ -0,0 +1,48 @@
1
+# Copyright 2017 IBM Corp. All Rights Reserved.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+#    http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS,
11
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+# See the License for the specific language governing permissions and
13
+# limitations under the License.
14
+
15
+from oslotest import base
16
+
17
+from os_dpm.config import types
18
+
19
+VALID_DPM_OBJECT_ID = "fa1f2466-12df-311a-804c-4ed2cc1d656b"
20
+VALID_DPM_OBJECT_ID_UC = "FA1F2466-12DF-311A-804C-4ED2CC1D656B"
21
+
22
+
23
+class TestDPMObjectIdType(base.BaseTestCase):
24
+    conf_type = types.DPMObjectIdType()
25
+
26
+    def test_empty_value_fail(self):
27
+        self.assertRaises(ValueError, self.conf_type, '')
28
+
29
+    def test_invalid_value_fail(self):
30
+        self.assertRaises(ValueError, self.conf_type, 'foobar')
31
+
32
+    def test_valid_object_id(self):
33
+        self.assertEqual(VALID_DPM_OBJECT_ID,
34
+                         self.conf_type(VALID_DPM_OBJECT_ID))
35
+
36
+    def test_valid_object_id_ignore_case(self):
37
+        self.assertEqual(VALID_DPM_OBJECT_ID,
38
+                         self.conf_type(VALID_DPM_OBJECT_ID_UC))
39
+
40
+    def test_object_id_too_long(self):
41
+        self.assertRaises(ValueError, self.conf_type,
42
+                          VALID_DPM_OBJECT_ID + "1")
43
+
44
+    def test_repr(self):
45
+        t = types.DPMObjectIdType()
46
+        self.assertEqual(
47
+            "String(regex='^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$')",
48
+            repr(t))

Loading…
Cancel
Save