Oleg Bondarev | 9250e61 | 2018-09-24 18:59:40 +0400 | [diff] [blame] | 1 | import logging |
| 2 | import random |
| 3 | |
| 4 | log = logging.getLogger(__name__) |
| 5 | |
| 6 | |
| 7 | def __virtual__(): |
| 8 | return 'octaviav2' if 'octaviav2.loadbalancer_list' in __salt__ else False |
| 9 | |
| 10 | |
| 11 | def _octaviav2_call(fname, *args, **kwargs): |
| 12 | return __salt__['octaviav2.{}'.format(fname)](*args, **kwargs) |
| 13 | |
| 14 | |
| 15 | def _resource_present(resource, name, changeable_params, cloud_name, **kwargs): |
| 16 | try: |
| 17 | method_name = '{}_get_details'.format(resource) |
| 18 | exact_resource = _octaviav2_call( |
| 19 | method_name, name, cloud_name=cloud_name |
| 20 | )[resource] |
| 21 | except Exception as e: |
| 22 | if 'ResourceNotFound' in repr(e): |
| 23 | try: |
| 24 | method_name = '{}_create'.format(resource) |
| 25 | resp = _octaviav2_call( |
| 26 | method_name, name=name, cloud_name=cloud_name, **kwargs |
| 27 | ) |
| 28 | except Exception as e: |
| 29 | log.exception('Octavia {0} create failed with {1}'. |
| 30 | format(resource, e)) |
| 31 | return _failed('create', name, resource) |
| 32 | return _succeeded('create', name, resource, resp) |
| 33 | elif 'MultipleResourcesFound' in repr(e): |
| 34 | return _failed('find', name, resource) |
| 35 | else: |
| 36 | raise |
| 37 | |
| 38 | to_update = {} |
| 39 | for key in kwargs: |
| 40 | if key in changeable_params and (key not in exact_resource |
| 41 | or kwargs[key] != exact_resource[key]): |
| 42 | to_update[key] = kwargs[key] |
| 43 | try: |
| 44 | method_name = '{}_update'.format(resource) |
| 45 | resp = _octaviav2_call( |
| 46 | method_name, name, cloud_name=cloud_name, **to_update |
| 47 | ) |
| 48 | except Exception as e: |
| 49 | log.exception('Octavia {0} update failed with {1}'.format(resource, e)) |
| 50 | return _failed('update', name, resource) |
| 51 | return _succeeded('update', name, resource, resp) |
| 52 | |
| 53 | |
| 54 | def _resource_absent(resource, name, cloud_name): |
| 55 | try: |
| 56 | method_name = '{}_get_details'.format(resource) |
| 57 | _octaviav2_call( |
| 58 | method_name, name, cloud_name=cloud_name |
| 59 | )[resource] |
| 60 | except Exception as e: |
| 61 | if 'ResourceNotFound' in repr(e): |
| 62 | return _succeeded('absent', name, resource) |
| 63 | if 'MultipleResourcesFound' in repr(e): |
| 64 | return _failed('find', name, resource) |
| 65 | try: |
| 66 | method_name = '{}_delete'.format(resource) |
| 67 | _octaviav2_call( |
| 68 | method_name, name, cloud_name=cloud_name |
| 69 | ) |
| 70 | except Exception as e: |
| 71 | log.error('Octavia delete {0} failed with {1}'.format(resource, e)) |
| 72 | return _failed('delete', name, resource) |
| 73 | return _succeeded('delete', name, resource) |
| 74 | |
| 75 | |
| 76 | def loadbalancer_present(name, cloud_name, **kwargs): |
| 77 | changeable = ( |
| 78 | 'admin_state_up', 'vip_qos_policy_id', 'description', |
| 79 | ) |
| 80 | |
| 81 | return _resource_present('loadbalancer', name, changeable, cloud_name, **kwargs) |
| 82 | |
| 83 | |
| 84 | def loadbalancer_absent(name, cloud_name): |
| 85 | return _resource_absent('loadbalancer', name, cloud_name) |
| 86 | |
| 87 | def _succeeded(op, name, resource, changes=None): |
| 88 | msg_map = { |
| 89 | 'create': '{0} {1} created', |
| 90 | 'delete': '{0} {1} removed', |
| 91 | 'update': '{0} {1} updated', |
| 92 | 'no_changes': '{0} {1} is in desired state', |
| 93 | 'absent': '{0} {1} not present', |
| 94 | } |
| 95 | changes_dict = { |
| 96 | 'name': name, |
| 97 | 'result': True, |
| 98 | 'comment': msg_map[op].format(resource, name), |
| 99 | 'changes': changes or {}, |
| 100 | } |
| 101 | return changes_dict |
| 102 | |
| 103 | |
| 104 | def _failed(op, name, resource): |
| 105 | msg_map = { |
| 106 | 'create': '{0} {1} failed to create', |
| 107 | 'delete': '{0} {1} failed to delete', |
| 108 | 'update': '{0} {1} failed to update', |
| 109 | 'find': '{0} {1} found multiple {0}', |
| 110 | } |
| 111 | changes_dict = { |
| 112 | 'name': name, |
| 113 | 'result': False, |
| 114 | 'comment': msg_map[op].format(resource, name), |
| 115 | 'changes': {}, |
| 116 | } |
| 117 | return changes_dict |