blob: a5c7d425480072b07fd728d3a4d9ce89069157d4 [file] [log] [blame]
# -*- coding: utf-8 -*-
import logging
LOG = logging.getLogger(__name__)
# Import third party libs
HAS_CINDER = False
try:
from cinderclient.client import Client
HAS_CINDER = True
except ImportError:
pass
__opts__ = {}
def __virtual__():
"""
Only load this module if cinder
is installed on this minion.
"""
if HAS_CINDER:
return 'cinderng'
return False
def _authng(profile=None):
"""
Set up cinder credentials
"""
credentials = {
'username': profile['user'],
'password': profile['password'],
'project_id': profile['project_id'],
'auth_url': "{}://{}:{}/v2.0".format(
profile['protocol'],
profile['host'],
profile['port']
),
'endpoint_type': profile['endpoint_type'],
'certificate': profile['certificate'],
'region_name': profile['region_name']
}
return credentials
def create_conn(cred=None):
"""
create connection
"""
nt = Client(
'2',
username=cred['username'],
api_key=cred['password'],
project_id=cred['project_id'],
auth_url=cred['auth_url'],
endpoint_type=cred['endpoint_type'],
cacert=cred['certificate'],
region_name=cred['region_name']
)
return nt
def list_volumes(profile=None, **kwargs):
"""
Return list of cinder volumes.
"""
cred = _authng(profile)
nt = create_conn(cred)
return nt.volumes.list()
def list_volume_type(profile=None, **kwargs):
"""
Return list of volume types
"""
cred = _authng(profile)
nt = create_conn(cred)
return nt.volume_types.list()
def get_volume_type(type_name, profile=None, **kwargs):
"""
Returns id of the specified volume type name
"""
vt_id = None
vt_list = list_volume_type(profile)
for vt in vt_list:
if vt.name == type_name:
vt_id = vt.id
if vt_id:
cred = _authng(profile)
nt = create_conn(cred)
try:
vt = nt.volume_types.get(vt_id)
return vt
except:
return
else:
return
def create_volume_type(type_name, profile=None, **kwargs):
"""
Create cinder volume type
"""
vt = get_volume_type(type_name, profile)
if not vt:
cred = _authng(profile)
nt = create_conn(cred)
try:
nt.volume_types.create(type_name)
return 'created'
except:
return 'failed'
else:
return 'exists'
def get_keys_volume_type(type_name, profile=None, **kwargs):
"""
Return extra specs of the specified volume type.
"""
vt = get_volume_type(type_name, profile)
if vt:
try:
return vt.get_keys()
except:
return 'failed'
else:
return
def set_keys_volume_type(type_name, keys={}, profile=None, **kwargs):
"""
Set extra specs of the specified volume type.
"""
set_keys = False
vt = get_volume_type(type_name, profile)
if vt:
k = get_keys_volume_type(type_name, profile)
if not k:
set_keys = True
elif k:
for key in keys:
if k.get(key) != keys[key]:
set_keys = True
elif len(k) != len(keys):
set_keys = True
else:
return
if set_keys:
try:
vt.set_keys(keys)
return 'updated'
except:
return 'failed'
else:
return 'exist'
else:
return 'not found'