File: uuid.py

package info (click to toggle)
python-pypowervm 1.1.24%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,400 kB
  • sloc: python: 29,780; xml: 174; makefile: 14
file content (54 lines) | stat: -rw-r--r-- 1,786 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Copyright 2015, 2018 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.

"""Utilities around Universally-Unique Identifiers (UUIDs)."""

from oslo_utils import uuidutils


def convert_uuid_to_pvm(uuid):
    """Converts a standard UUID to PowerVM format

    PowerVM uuids always set the byte 0, bit 0 to 0.

    :param uuid: A standard format uuid string
    :returns: A PowerVM compliant uuid
    """
    return "%x%s" % (int(uuid[0], 16) & 7, uuid[1:])


def id_or_uuid(an_id):
    """Sanitizes a short ID or string UUID, and indicates which was used.

    Use as:
        is_uuid, lpar_id = id_or_uuid(lpar_id)
        if is_uuid:  # lpar_id is a string UUID
        else:  # lpar_id is LPAR short ID of type int

    :param an_id: Short ID (may be string or int) or string UUID of, e.g., an
                  LPAR.
    :return: Boolean.  If True, the other return is a UUID string.  If False,
                       it is an integer.
    :return: The input ID, either converted to int, or in its original string
             form if a UUID.
    """
    if uuidutils.is_uuid_like(an_id):
        is_uuid = True
        ret_id = an_id
    else:
        is_uuid = False
        ret_id = int(an_id)
    return is_uuid, ret_id