Oleksiy Petrenko | e2c8da2 | 2018-03-30 18:27:58 +0300 | [diff] [blame] | 1 | # Copyright 2018 Mirantis Inc |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import logging |
| 17 | log = logging.getLogger(__name__) |
| 18 | |
| 19 | |
| 20 | def __virtual__(): |
| 21 | ''' |
| 22 | Only load if manila module is present in __salt__ |
| 23 | ''' |
| 24 | return 'manilang' if 'manilang.list_share_types' in __salt__ else False |
| 25 | |
| 26 | |
| 27 | manilang_func = { |
| 28 | 'list_types': 'manilang.list_share_types', |
| 29 | 'create_type': 'manilang.create_share_type', |
| 30 | 'set_type_specs': 'manilang.set_share_type_extra_specs', |
| 31 | 'unset_type_specs': 'manilang.unset_share_type_extra_specs', |
| 32 | 'delete_type': 'manilang.delete_share_type', |
| 33 | } |
| 34 | |
| 35 | |
| 36 | def share_type_present(name, extra_specs, cloud_name, **kwargs): |
| 37 | """ |
| 38 | Ensure that share_type is present and has desired parameters |
| 39 | |
| 40 | This function will create the desired share type if one with the requested |
| 41 | name does not exist. If it does, it will be updated to correspond to |
| 42 | parameters passed to this function. |
| 43 | |
| 44 | :param name: name of the share type. |
| 45 | :param extra_specs: dictionary of extra_specs that share type should have. |
| 46 | It contains one required parameter - driver_handles_share_servers. |
| 47 | :param kwargs: other arguments that will be pushed into share_type |
| 48 | dictionary to be POSTed, if specified. |
| 49 | |
| 50 | """ |
| 51 | origin_share_types = __salt__[ |
| 52 | manilang_func['list_types'] |
| 53 | ](cloud_name=cloud_name)['share_types'] |
| 54 | share_types = [ |
| 55 | share_type |
| 56 | for share_type in origin_share_types if share_type['name'] == name |
| 57 | ] |
| 58 | if not share_types: |
| 59 | try: |
| 60 | res = __salt__[ |
| 61 | manilang_func['create_type'] |
| 62 | ](name, extra_specs, cloud_name=cloud_name, **kwargs) |
| 63 | except Exception as e: |
| 64 | log.error('Manila share type create failed with {}'.format(e)) |
| 65 | return _create_failed(name, 'resource') |
| 66 | return _created(name, 'share_type', res) |
| 67 | |
| 68 | elif len(share_types) == 1: |
| 69 | exact_share_type = share_types[0] |
| 70 | |
| 71 | api_extra_specs = exact_share_type['extra_specs'] |
| 72 | api_keys = set(api_extra_specs) |
| 73 | sls_keys = set(extra_specs) |
| 74 | |
| 75 | to_delete = api_keys - sls_keys |
| 76 | to_add = sls_keys - api_keys |
| 77 | to_update = sls_keys & api_keys |
| 78 | resp = {} |
| 79 | |
| 80 | for key in to_delete: |
| 81 | try: |
| 82 | __salt__[ |
| 83 | manilang_func['unset_type_specs'] |
| 84 | ](exact_share_type['id'], key, cloud_name=cloud_name) |
| 85 | except Exception as e: |
| 86 | log.error( |
| 87 | 'Manila share type delete ' |
| 88 | 'extra specs failed with {}'.format(e) |
| 89 | ) |
| 90 | return _update_failed(name, 'share_type_extra_specs') |
| 91 | resp.update({'deleted_extra_specs': to_delete}) |
| 92 | |
| 93 | diff = {} |
| 94 | |
| 95 | for key in to_add: |
| 96 | diff[key] = extra_specs[key] |
| 97 | for key in to_update: |
| 98 | if extra_specs[key] != api_extra_specs[key]: |
| 99 | diff[key] = extra_specs[key] |
| 100 | if diff: |
| 101 | try: |
| 102 | resp.update( |
| 103 | __salt__[ |
| 104 | manilang_func['set_type_specs'] |
| 105 | ](exact_share_type['id'], {'extra_specs': diff}, |
| 106 | cloud_name=cloud_name) |
| 107 | ) |
| 108 | except Exception as e: |
| 109 | log.error( |
| 110 | 'Manila share type update ' |
| 111 | 'extra specs failed with {}'.format(e) |
| 112 | ) |
| 113 | return _update_failed(name, 'share_type_extra_specs') |
| 114 | if to_delete or diff: |
| 115 | return _updated(name, 'share_type', resp) |
| 116 | return _no_changes(name, 'share_type') |
| 117 | else: |
| 118 | return _find_failed(name, 'share_type') |
| 119 | |
| 120 | |
| 121 | def share_type_absent(name, cloud_name): |
| 122 | origin_share_types = __salt__[ |
| 123 | manilang_func['list_types'] |
| 124 | ](cloud_name=cloud_name)['share_types'] |
| 125 | share_types = [ |
| 126 | share_type |
| 127 | for share_type in origin_share_types if share_type['name'] == name |
| 128 | ] |
| 129 | if not share_types: |
| 130 | return _absent(name, 'share_type') |
| 131 | elif len(share_types) == 1: |
| 132 | try: |
| 133 | __salt__[manilang_func['delete_type']](share_types[0]['id'], |
| 134 | cloud_name=cloud_name) |
| 135 | except Exception as e: |
| 136 | log.error('Manila share type delete failed with {}'.format(e)) |
| 137 | return _delete_failed(name, 'share_type') |
| 138 | return _deleted(name, 'share_type') |
| 139 | else: |
| 140 | return _find_failed(name, 'share_type') |
| 141 | |
| 142 | |
| 143 | def _created(name, resource, resource_definition): |
| 144 | changes_dict = { |
| 145 | 'name': name, |
| 146 | 'changes': resource_definition, |
| 147 | 'result': True, |
| 148 | 'comment': '{}{} created'.format(resource, name) |
| 149 | } |
| 150 | return changes_dict |
| 151 | |
| 152 | |
| 153 | def _updated(name, resource, resource_definition): |
| 154 | changes_dict = { |
| 155 | 'name': name, |
| 156 | 'changes': resource_definition, |
| 157 | 'result': True, |
| 158 | 'comment': '{}{} updated'.format(resource, name) |
| 159 | } |
| 160 | return changes_dict |
| 161 | |
| 162 | |
| 163 | def _no_changes(name, resource): |
| 164 | changes_dict = { |
| 165 | 'name': name, |
| 166 | 'changes': {}, |
| 167 | 'result': True, |
| 168 | 'comment': '{}{} is in desired state'.format(resource, name) |
| 169 | } |
| 170 | return changes_dict |
| 171 | |
| 172 | |
| 173 | def _deleted(name, resource): |
| 174 | changes_dict = { |
| 175 | 'name': name, |
| 176 | 'changes': {}, |
| 177 | 'result': True, |
| 178 | 'comment': '{}{} removed'.format(resource, name) |
| 179 | } |
| 180 | return changes_dict |
| 181 | |
| 182 | |
| 183 | def _absent(name, resource): |
| 184 | changes_dict = {'name': name, |
| 185 | 'changes': {}, |
| 186 | 'comment': '{0} {1} not present'.format(resource, name), |
| 187 | 'result': True} |
| 188 | return changes_dict |
| 189 | |
| 190 | |
| 191 | def _delete_failed(name, resource): |
| 192 | changes_dict = {'name': name, |
| 193 | 'changes': {}, |
| 194 | 'comment': '{0} {1} failed to delete'.format(resource, |
| 195 | name), |
| 196 | 'result': False} |
| 197 | return changes_dict |
| 198 | |
| 199 | |
| 200 | def _create_failed(name, resource): |
| 201 | changes_dict = {'name': name, |
| 202 | 'changes': {}, |
| 203 | 'comment': '{0} {1} failed to create'.format(resource, |
| 204 | name), |
| 205 | 'result': False} |
| 206 | return changes_dict |
| 207 | |
| 208 | |
| 209 | def _update_failed(name, resource): |
| 210 | changes_dict = {'name': name, |
| 211 | 'changes': {}, |
| 212 | 'comment': '{0} {1} failed to update'.format(resource, |
| 213 | name), |
| 214 | 'result': False} |
| 215 | return changes_dict |
| 216 | |
| 217 | |
| 218 | def _find_failed(name, resource): |
| 219 | changes_dict = { |
| 220 | 'name': name, |
| 221 | 'changes': {}, |
| 222 | 'comment': '{0} {1} found multiple {0}'.format(resource, name), |
| 223 | 'result': False, |
| 224 | } |
| 225 | return changes_dict |