"Fossies" - the Fresh Open Source Software Archive

Member "cinder-17.1.0/cinder/volume/drivers/dell_emc/powerstore/utils.py" (8 Mar 2021, 4374 Bytes) of package /linux/misc/openstack/cinder-17.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "utils.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 17.0.1_vs_17.1.0.

    1 # Copyright (c) 2020 Dell Inc. or its subsidiaries.
    2 # All Rights Reserved.
    3 #
    4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 # not use this file except in compliance with the License. You may obtain
    6 # a copy of the License at
    7 #
    8 #      http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 # Unless required by applicable law or agreed to in writing, software
   11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 # License for the specific language governing permissions and limitations
   14 # under the License.
   15 
   16 """Utilities for Dell EMC PowerStore Cinder driver."""
   17 
   18 import re
   19 
   20 from oslo_log import log as logging
   21 from oslo_utils import units
   22 
   23 from cinder import exception
   24 from cinder.i18n import _
   25 from cinder.objects import fields
   26 from cinder.volume import volume_utils
   27 
   28 
   29 LOG = logging.getLogger(__name__)
   30 CHAP_DEFAULT_USERNAME = "PowerStore_iSCSI_CHAP_Username"
   31 CHAP_DEFAULT_SECRET_LENGTH = 60
   32 
   33 
   34 def bytes_to_gib(size_in_bytes):
   35     """Convert size in bytes to GiB.
   36 
   37     :param size_in_bytes: size in bytes
   38     :return: size in GiB
   39     """
   40 
   41     return size_in_bytes // units.Gi
   42 
   43 
   44 def gib_to_bytes(size_in_gb):
   45     """Convert size in GiB to bytes.
   46 
   47     :param size_in_gb: size in GiB
   48     :return: size in bytes
   49     """
   50 
   51     return size_in_gb * units.Gi
   52 
   53 
   54 def extract_fc_wwpns(connector):
   55     """Convert connector FC ports to appropriate format with colons.
   56 
   57     :param connector: connection properties
   58     :return: FC ports in appropriate format with colons
   59     """
   60 
   61     if "wwnns" not in connector or "wwpns" not in connector:
   62         msg = _("Host %s does not have FC initiators.") % connector["host"]
   63         LOG.error(msg)
   64         raise exception.VolumeBackendAPIException(data=msg)
   65     return [":".join(re.findall("..", wwpn)) for wwpn in connector["wwpns"]]
   66 
   67 
   68 def fc_wwn_to_string(wwn):
   69     """Convert FC WWN to string without colons.
   70 
   71     :param wwn: FC WWN
   72     :return: FC WWN without colons
   73     """
   74 
   75     return wwn.replace(":", "")
   76 
   77 
   78 def iscsi_portal_with_port(address):
   79     """Add default port 3260 to iSCSI portal
   80 
   81     :param address: iSCSI portal without port
   82     :return: iSCSI portal with default port 3260
   83     """
   84 
   85     return "%(address)s:3260" % {"address": address}
   86 
   87 
   88 def powerstore_host_name(connector, protocol):
   89     """Generate PowerStore host name for connector.
   90 
   91     :param connector: connection properties
   92     :param protocol: storage protocol (FC or iSCSI)
   93     :return: unique host name
   94     """
   95 
   96     return ("%(host)s-%(protocol)s" %
   97             {"host": connector["host"],
   98              "protocol": protocol, })
   99 
  100 
  101 def filter_hosts_by_initiators(hosts, initiators):
  102     """Filter hosts by given list of initiators.
  103 
  104     :param hosts: list of PowerStore host objects
  105     :param initiators: list of initiators
  106     :return: PowerStore hosts list
  107     """
  108 
  109     hosts_names_found = set()
  110     for host in hosts:
  111         for initiator in host["host_initiators"]:
  112             if initiator["port_name"] in initiators:
  113                 hosts_names_found.add(host["name"])
  114     return list(filter(lambda host: host["name"] in hosts_names_found, hosts))
  115 
  116 
  117 def is_multiattached_to_host(volume_attachment, host_name):
  118     """Check if volume is attached to multiple instances on one host.
  119 
  120     When multiattach is enabled, a volume could be attached to two or more
  121     instances which are hosted on one nova host.
  122     Because PowerStore cannot recognize the volume is attached to two or more
  123     instances, we should keep the volume attached to the nova host until
  124     the volume is detached from the last instance.
  125 
  126     :param volume_attachment: list of VolumeAttachment objects
  127     :param host_name: OpenStack host name
  128     :return: multiattach flag
  129     """
  130 
  131     if not volume_attachment:
  132         return False
  133 
  134     attachments = [
  135         attachment for attachment in volume_attachment
  136         if (attachment.attach_status == fields.VolumeAttachStatus.ATTACHED and
  137             attachment.attached_host == host_name)
  138     ]
  139     return len(attachments) > 1
  140 
  141 
  142 def get_chap_credentials():
  143     """Generate CHAP credentials.
  144 
  145     :return: CHAP username and secret
  146     """
  147 
  148     return {
  149         "chap_single_username": CHAP_DEFAULT_USERNAME,
  150         "chap_single_password": volume_utils.generate_password(
  151             CHAP_DEFAULT_SECRET_LENGTH
  152         )
  153     }