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'], |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 35 | 'auth_url': "{}://{}:{}/v2.0".format( |
| 36 | profile['protocol'], |
| 37 | profile['host'], |
| 38 | profile['port'] |
| 39 | ), |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 40 | 'endpoint_type': profile['endpoint_type'], |
| 41 | 'certificate': profile['certificate'], |
Oleg Iurchenko | 86bf9e4 | 2018-01-18 20:58:21 +0200 | [diff] [blame] | 42 | 'region_name': profile['region_name'], |
| 43 | 'retries': profile.get('retries', 5) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 44 | } |
| 45 | return credentials |
| 46 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 47 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 48 | def create_conn(cred=None): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 49 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 50 | create connection |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 51 | """ |
| 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 Iurchenko | 86bf9e4 | 2018-01-18 20:58:21 +0200 | [diff] [blame] | 60 | region_name=cred['region_name'], |
| 61 | retries=cred['retries'] |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 62 | ) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 63 | return nt |
| 64 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 65 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 66 | def list_volumes(profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 67 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 68 | Return list of cinder volumes. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 69 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 70 | cred = _authng(profile) |
| 71 | nt = create_conn(cred) |
| 72 | return nt.volumes.list() |
| 73 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 74 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 75 | def list_volume_type(profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 76 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 77 | Return list of volume types |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 78 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 79 | cred = _authng(profile) |
| 80 | nt = create_conn(cred) |
| 81 | return nt.volume_types.list() |
| 82 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 83 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 84 | def get_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 85 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 86 | Returns id of the specified volume type name |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 87 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 88 | vt_id = None |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 89 | vt_list = list_volume_type(profile) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 90 | 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. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 105 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 106 | def create_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 107 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 108 | Create cinder volume type |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 109 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 110 | 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 | |
| 123 | def get_keys_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 124 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 125 | Return extra specs of the specified volume type. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 126 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 127 | |
| 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. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 137 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 138 | def set_keys_volume_type(type_name, keys={}, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 139 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 140 | Set extra specs of the specified volume type. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 141 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 142 | 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' |