blob: 71d7c5c5da180a7cc90529fa801638cc6a5fddc2 [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'],
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030035 'auth_url': "{}://{}:{}/v2.0".format(
36 profile['protocol'],
37 profile['host'],
38 profile['port']
39 ),
Jiri Broulik47aa6b32017-07-10 18:39:15 +020040 'endpoint_type': profile['endpoint_type'],
41 'certificate': profile['certificate'],
Oleg Iurchenko86bf9e42018-01-18 20:58:21 +020042 'region_name': profile['region_name'],
43 'retries': profile.get('retries', 5)
Jiri Broulik47aa6b32017-07-10 18:39:15 +020044 }
45 return credentials
46
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030047
Jiri Broulik47aa6b32017-07-10 18:39:15 +020048def create_conn(cred=None):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030049 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020050 create connection
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030051 """
52 nt = Client(
53 '2',
54 username=cred['username'],
55 api_key=cred['password'],
56 project_id=cred['project_id'],
57 auth_url=cred['auth_url'],
58 endpoint_type=cred['endpoint_type'],
59 cacert=cred['certificate'],
Oleg Iurchenko86bf9e42018-01-18 20:58:21 +020060 region_name=cred['region_name'],
61 retries=cred['retries']
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030062 )
Jiri Broulik47aa6b32017-07-10 18:39:15 +020063 return nt
64
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030065
Jiri Broulik47aa6b32017-07-10 18:39:15 +020066def list_volumes(profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030067 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020068 Return list of cinder volumes.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030069 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020070 cred = _authng(profile)
71 nt = create_conn(cred)
72 return nt.volumes.list()
73
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030074
Jiri Broulik47aa6b32017-07-10 18:39:15 +020075def list_volume_type(profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030076 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020077 Return list of volume types
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030078 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020079 cred = _authng(profile)
80 nt = create_conn(cred)
81 return nt.volume_types.list()
82
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030083
Jiri Broulik47aa6b32017-07-10 18:39:15 +020084def get_volume_type(type_name, profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030085 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020086 Returns id of the specified volume type name
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030087 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020088 vt_id = None
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030089 vt_list = list_volume_type(profile)
Jiri Broulik47aa6b32017-07-10 18:39:15 +020090 for vt in vt_list:
91 if vt.name == type_name:
92 vt_id = vt.id
93
94 if vt_id:
95 cred = _authng(profile)
96 nt = create_conn(cred)
97 try:
98 vt = nt.volume_types.get(vt_id)
99 return vt
100 except:
101 return
102 else:
103 return
104
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300105
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200106def create_volume_type(type_name, profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300107 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200108 Create cinder volume type
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300109 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200110 vt = get_volume_type(type_name, profile)
111 if not vt:
112 cred = _authng(profile)
113 nt = create_conn(cred)
114 try:
115 nt.volume_types.create(type_name)
116 return 'created'
117 except:
118 return 'failed'
119 else:
120 return 'exists'
121
122
123def get_keys_volume_type(type_name, profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300124 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200125 Return extra specs of the specified volume type.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300126 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200127
128 vt = get_volume_type(type_name, profile)
129 if vt:
130 try:
131 return vt.get_keys()
132 except:
133 return 'failed'
134 else:
135 return
136
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300137
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200138def set_keys_volume_type(type_name, keys={}, profile=None, **kwargs):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300139 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200140 Set extra specs of the specified volume type.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300141 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200142 set_keys = False
143 vt = get_volume_type(type_name, profile)
144 if vt:
145 k = get_keys_volume_type(type_name, profile)
146 if not k:
147 set_keys = True
148 elif k:
149 for key in keys:
150 if k.get(key) != keys[key]:
151 set_keys = True
152 elif len(k) != len(keys):
153 set_keys = True
154 else:
155 return
156
157 if set_keys:
158 try:
159 vt.set_keys(keys)
160 return 'updated'
161 except:
162 return 'failed'
163 else:
164 return 'exist'
165 else:
166 return 'not found'