Add method for DPM object-id validation

The added method allows checking if an argument is a valid dpm
object id or not. It only checks the format. It does not check
if the corresponding object exists on the HMC.

Change-Id: I68583044b5a32f9873635dc8517269b8ee82a303
This commit is contained in:
Andreas Scheuring 2017-03-08 10:23:17 +01:00
parent e222399f9f
commit 038e3cbffa
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,37 @@
# 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.tests.unit.config.test_config import VALID_DPM_OBJECT_ID
from os_dpm.tests.unit.config.test_config import VALID_DPM_OBJECT_ID_UC
from os_dpm import utils
class TestUtils(base.BaseTestCase):
def test_is_valid_object_id(self):
self.assertTrue(utils.is_valid_dpm_object_id(VALID_DPM_OBJECT_ID))
def test_is_valid_object_id_invalid(self):
invalid = ["foo",
# Missing -
"fa1f246612df311a804c4ed2cc1d6564",
# Invalid character 'g'
"ga1f2466-12df-311a-804c-4ed2cc1d6564",
# Too long
"fa1f2466-12df-311a-804c-4ed2cc1d65641",
# Upper case is not considered as valid by the HMC
VALID_DPM_OBJECT_ID_UC]
for oid in invalid:
self.assertFalse(utils.is_valid_dpm_object_id(oid))

34
os_dpm/utils.py Normal file
View File

@ -0,0 +1,34 @@
# 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.
import re
from os_dpm import constants as const
def is_valid_dpm_object_id(object_id):
"""Validates if the argument is a valid object-id
Returns True if the argument has the format of a DPM object-id. It's not
being checked whether the object-id exists on the HMC.
:param object_id: potential object id to test
:return: True if argument is a valid object-id, else False
"""
match = re.search("^" + const.OBJECT_ID_REGEX + "$", object_id)
if match:
return True
return False