Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 1 | import functools |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 2 | import logging |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 3 | import time |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 4 | |
| 5 | |
| 6 | def __virtual__(): |
| 7 | return 'designatev2' if 'designatev2.zone_list' in __salt__ else False # noqa |
| 8 | |
| 9 | |
| 10 | log = logging.getLogger(__name__) |
| 11 | |
| 12 | |
| 13 | def _designatev2_call(fname, *args, **kwargs): |
| 14 | return __salt__['designatev2.{}'.format(fname)](*args, **kwargs) # noqa |
| 15 | |
| 16 | |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 17 | def _resource_present(resource, name, cloud_name, waiter=None, **kwargs): |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 18 | try: |
| 19 | method_name = '{}_get_details'.format(resource) |
| 20 | exact_resource = _designatev2_call( |
| 21 | method_name, name, cloud_name=cloud_name |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 22 | ) |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 23 | except Exception as e: |
| 24 | if 'ResourceNotFound' in repr(e): |
| 25 | try: |
| 26 | method_name = '{}_create'.format(resource) |
| 27 | resp = _designatev2_call( |
| 28 | method_name, name=name, cloud_name=cloud_name, **kwargs |
| 29 | ) |
| 30 | except Exception as e: |
| 31 | log.exception('Designate {0} create failed with {1}'. |
| 32 | format(resource, e)) |
| 33 | return _failed('create', name, resource) |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 34 | if waiter: |
| 35 | if not waiter(name, cloud_name): |
| 36 | return _failed('create', name, resource) |
| 37 | resp['status'] = 'ACTIVE' |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 38 | return _succeeded('create', name, resource, resp) |
| 39 | elif 'MultipleResourcesFound' in repr(e): |
| 40 | return _failed('find', name, resource) |
| 41 | else: |
| 42 | raise |
| 43 | |
| 44 | to_update = {} |
| 45 | for key in kwargs: |
| 46 | if key not in exact_resource or kwargs[key] != exact_resource[key]: |
| 47 | to_update[key] = kwargs[key] |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 48 | if to_update: |
| 49 | try: |
| 50 | method_name = '{}_update'.format(resource) |
| 51 | resp = _designatev2_call( |
| 52 | method_name, name, cloud_name=cloud_name, **to_update |
| 53 | ) |
| 54 | except Exception as e: |
| 55 | log.exception('Designate {0} update failed with {1}'.format(resource, e)) |
| 56 | return _failed('update', name, resource) |
| 57 | return _succeeded('update', name, resource, resp) |
| 58 | return _succeeded('no_changes', name, resource) |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 59 | |
| 60 | |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 61 | def _resource_absent(resource, name, cloud_name, waiter=None): |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 62 | try: |
| 63 | method_name = '{}_get_details'.format(resource) |
| 64 | _designatev2_call( |
| 65 | method_name, name, cloud_name=cloud_name |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 66 | ) |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 67 | except Exception as e: |
| 68 | if 'ResourceNotFound' in repr(e): |
| 69 | return _succeeded('absent', name, resource) |
| 70 | if 'MultipleResourcesFound' in repr(e): |
| 71 | return _failed('find', name, resource) |
| 72 | try: |
| 73 | method_name = '{}_delete'.format(resource) |
| 74 | _designatev2_call( |
| 75 | method_name, name, cloud_name=cloud_name |
| 76 | ) |
| 77 | except Exception as e: |
| 78 | log.error('Designate delete {0} failed with {1}'.format(resource, e)) |
| 79 | return _failed('delete', name, resource) |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 80 | if waiter: |
| 81 | if not waiter: |
| 82 | return _failed('delete', name, resource) |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 83 | return _succeeded('delete', name, resource) |
| 84 | |
| 85 | |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 86 | def _poll_for_zone(zone_name, cloud_name, checker, retry=5, timeout=2): |
| 87 | while retry: |
| 88 | zones = _designatev2_call('zone_list', name=zone_name, |
| 89 | cloud_name=cloud_name)['zones'] |
| 90 | if checker(zones): |
| 91 | return True |
| 92 | time.sleep(timeout) |
| 93 | retry -= 1 |
| 94 | return False |
| 95 | |
| 96 | |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 97 | def zone_present(name, cloud_name, email, **kwargs): |
| 98 | kwargs.update({'email': email}) |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 99 | return _resource_present( |
| 100 | 'zone', name, cloud_name, |
| 101 | functools.partial( |
| 102 | _poll_for_zone, |
| 103 | checker=lambda zones: len(zones) and |
| 104 | zones[0]['status'] == 'ACTIVE', |
| 105 | **kwargs.pop('waiter_config', {})), |
| 106 | **kwargs) |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 107 | |
| 108 | |
Oleksiy Petrenko | 62330b6 | 2018-11-20 16:54:02 +0200 | [diff] [blame^] | 109 | def zone_absent(name, cloud_name, **kwargs): |
| 110 | return _resource_absent( |
| 111 | 'zone', name, cloud_name, |
| 112 | waiter=functools.partial( |
| 113 | _poll_for_zone, checker=lambda zones: not len(zones), |
| 114 | **kwargs.pop('waiter_config', {}))) |
Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def _succeeded(op, name, resource, changes=None): |
| 118 | msg_map = { |
| 119 | 'create': '{0} {1} created', |
| 120 | 'delete': '{0} {1} removed', |
| 121 | 'update': '{0} {1} updated', |
| 122 | 'no_changes': '{0} {1} is in desired state', |
| 123 | 'absent': '{0} {1} not present', |
| 124 | 'resources_moved': '{1} resources were moved from {0}', |
| 125 | } |
| 126 | changes_dict = { |
| 127 | 'name': name, |
| 128 | 'result': True, |
| 129 | 'comment': msg_map[op].format(resource, name), |
| 130 | 'changes': changes or {}, |
| 131 | } |
| 132 | return changes_dict |
| 133 | |
| 134 | |
| 135 | def _failed(op, name, resource): |
| 136 | msg_map = { |
| 137 | 'create': '{0} {1} failed to create', |
| 138 | 'delete': '{0} {1} failed to delete', |
| 139 | 'update': '{0} {1} failed to update', |
| 140 | 'find': '{0} {1} found multiple {0}', |
| 141 | 'resources_moved': 'failed to move {1} from {0}', |
| 142 | } |
| 143 | changes_dict = { |
| 144 | 'name': name, |
| 145 | 'result': False, |
| 146 | 'comment': msg_map[op].format(resource, name), |
| 147 | 'changes': {}, |
| 148 | } |
| 149 | return changes_dict |