blob: 08aaad923b9a2b18414fcb65d0c55ec0a612f533 [file] [log] [blame]
Jiri Broulik47aa6b32017-07-10 18:39:15 +02001# -*- coding: utf-8 -*-
2import logging
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +03003
Jiri Broulik47aa6b32017-07-10 18:39:15 +02004LOG = logging.getLogger(__name__)
5
6# Import third party libs
7HAS_CINDER = False
8try:
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +03009 from cinderclient.client import Client
Jiri Broulik47aa6b32017-07-10 18:39:15 +020010 HAS_CINDER = True
11except ImportError:
12 pass
13
14__opts__ = {}
15
16
17def __virtual__():
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030018 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020019 Only load this module if cinder
20 is installed on this minion.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030021 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020022 if HAS_CINDER:
23 return 'cinderng'
24 return False
25
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030026
Jiri Broulik47aa6b32017-07-10 18:39:15 +020027def _authng(profile=None):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030028 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020029 Set up cinder credentials
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030030 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020031 credentials = {
32 'username': profile['user'],
33 'password': profile['password'],
34 'project_id': profile['project_id'],
Oleg Iurchenko6fe8e5d2018-02-20 14:26:04 +020035 'auth_url': "{}://{}:{}/{}".format(
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030036 profile['protocol'],
37 profile['host'],
Oleg Iurchenko6fe8e5d2018-02-20 14:26:04 +020038 profile['port'],
39 profile.get('api_version', 'v3')
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030040 ),
Jiri Broulik47aa6b32017-07-10 18:39:15 +020041 'endpoint_type': profile['endpoint_type'],
42 'certificate': profile['certificate'],
Oleg Iurchenko86bf9e42018-01-18 20:58:21 +020043 'region_name': profile['region_name'],
44 'retries': profile.get('retries', 5)
Jiri Broulik47aa6b32017-07-10 18:39:15 +020045 }
46 return credentials
47
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030048
Jiri Broulik47aa6b32017-07-10 18:39:15 +020049def create_conn(cred=None):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030050 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020051 create connection
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030052 """
53 nt = Client(
54 '2',
55 username=cred['username'],
56 api_key=cred['password'],
57 project_id=cred['project_id'],
58 auth_url=cred['auth_url'],
59 endpoint_type=cred['endpoint_type'],
60 cacert=cred['certificate'],
Oleg Iurchenko86bf9e42018-01-18 20:58:21 +020061 region_name=cred['region_name'],
62 retries=cred['retries']
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030063 )
Jiri Broulik47aa6b32017-07-10 18:39:15 +020064 return nt
65
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030066
Jiri Broulik47aa6b32017-07-10 18:39:15 +020067def list_volumes(profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030068 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020069 Return list of cinder volumes.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030070 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020071 cred = _authng(profile)
72 nt = create_conn(cred)
73 return nt.volumes.list()
74
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030075
Jiri Broulik47aa6b32017-07-10 18:39:15 +020076def list_volume_type(profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030077 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020078 Return list of volume types
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030079 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020080 cred = _authng(profile)
81 nt = create_conn(cred)
82 return nt.volume_types.list()
83
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030084
Jiri Broulik47aa6b32017-07-10 18:39:15 +020085def get_volume_type(type_name, profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030086 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020087 Returns id of the specified volume type name
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030088 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020089 vt_id = None
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030090 vt_list = list_volume_type(profile)
Jiri Broulik47aa6b32017-07-10 18:39:15 +020091 for vt in vt_list:
92 if vt.name == type_name:
93 vt_id = vt.id
94
95 if vt_id:
96 cred = _authng(profile)
97 nt = create_conn(cred)
98 try:
99 vt = nt.volume_types.get(vt_id)
100 return vt
101 except:
102 return
103 else:
104 return
105
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300106
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200107def create_volume_type(type_name, profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300108 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200109 Create cinder volume type
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300110 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200111 vt = get_volume_type(type_name, profile)
112 if not vt:
113 cred = _authng(profile)
114 nt = create_conn(cred)
115 try:
116 nt.volume_types.create(type_name)
117 return 'created'
118 except:
119 return 'failed'
120 else:
121 return 'exists'
122
123
124def get_keys_volume_type(type_name, profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300125 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200126 Return extra specs of the specified volume type.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300127 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200128
129 vt = get_volume_type(type_name, profile)
130 if vt:
131 try:
132 return vt.get_keys()
133 except:
134 return 'failed'
135 else:
136 return
137
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300138
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200139def set_keys_volume_type(type_name, keys={}, profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300140 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200141 Set extra specs of the specified volume type.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300142 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200143 set_keys = False
144 vt = get_volume_type(type_name, profile)
145 if vt:
146 k = get_keys_volume_type(type_name, profile)
147 if not k:
148 set_keys = True
149 elif k:
150 for key in keys:
151 if k.get(key) != keys[key]:
152 set_keys = True
153 elif len(k) != len(keys):
154 set_keys = True
155 else:
156 return
157
158 if set_keys:
159 try:
160 vt.set_keys(keys)
161 return 'updated'
162 except:
163 return 'failed'
164 else:
165 return 'exist'
166 else:
167 return 'not found'