Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | import logging |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 3 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 4 | LOG = logging.getLogger(__name__) |
| 5 | |
| 6 | # Import third party libs |
| 7 | HAS_CINDER = False |
| 8 | try: |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 9 | from cinderclient.client import Client |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 10 | HAS_CINDER = True |
| 11 | except ImportError: |
| 12 | pass |
| 13 | |
| 14 | __opts__ = {} |
| 15 | |
| 16 | |
| 17 | def __virtual__(): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 18 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 19 | Only load this module if cinder |
| 20 | is installed on this minion. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 21 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 22 | if HAS_CINDER: |
| 23 | return 'cinderng' |
| 24 | return False |
| 25 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 26 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 27 | def _authng(profile=None): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 28 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 29 | Set up cinder credentials |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 30 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 31 | credentials = { |
| 32 | 'username': profile['user'], |
| 33 | 'password': profile['password'], |
| 34 | 'project_id': profile['project_id'], |
Oleg Iurchenko | 6fe8e5d | 2018-02-20 14:26:04 +0200 | [diff] [blame^] | 35 | 'auth_url': "{}://{}:{}/{}".format( |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 36 | profile['protocol'], |
| 37 | profile['host'], |
Oleg Iurchenko | 6fe8e5d | 2018-02-20 14:26:04 +0200 | [diff] [blame^] | 38 | profile['port'], |
| 39 | profile.get('api_version', 'v3') |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 40 | ), |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 41 | 'endpoint_type': profile['endpoint_type'], |
| 42 | 'certificate': profile['certificate'], |
Oleg Iurchenko | 86bf9e4 | 2018-01-18 20:58:21 +0200 | [diff] [blame] | 43 | 'region_name': profile['region_name'], |
| 44 | 'retries': profile.get('retries', 5) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 45 | } |
| 46 | return credentials |
| 47 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 48 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 49 | def create_conn(cred=None): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 50 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 51 | create connection |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 52 | """ |
| 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 Iurchenko | 86bf9e4 | 2018-01-18 20:58:21 +0200 | [diff] [blame] | 61 | region_name=cred['region_name'], |
| 62 | retries=cred['retries'] |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 63 | ) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 64 | return nt |
| 65 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 66 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 67 | def list_volumes(profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 68 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 69 | Return list of cinder volumes. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 70 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 71 | cred = _authng(profile) |
| 72 | nt = create_conn(cred) |
| 73 | return nt.volumes.list() |
| 74 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 75 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 76 | def list_volume_type(profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 77 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 78 | Return list of volume types |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 79 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 80 | cred = _authng(profile) |
| 81 | nt = create_conn(cred) |
| 82 | return nt.volume_types.list() |
| 83 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 84 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 85 | def get_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 86 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 87 | Returns id of the specified volume type name |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 88 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 89 | vt_id = None |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 90 | vt_list = list_volume_type(profile) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 91 | 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. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 106 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 107 | def create_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 108 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 109 | Create cinder volume type |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 110 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 111 | 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 | |
| 124 | def get_keys_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 125 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 126 | Return extra specs of the specified volume type. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 127 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 128 | |
| 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. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 138 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 139 | def set_keys_volume_type(type_name, keys={}, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 140 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 141 | Set extra specs of the specified volume type. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 142 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 143 | 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' |