| 1 |
--- |
| 2 |
- name: Test API gateway creation using lookup=tag |
| 3 |
vars: |
| 4 |
api_name: "{{ api_names[0] }}" |
| 5 |
block: |
| 6 |
- name: Define API gateway configuration |
| 7 |
set_fact: |
| 8 |
apigateway_swagger_text: "{{ lookup('template', 'minimal-swagger-api.yml.j2') }}" |
| 9 |
|
| 10 |
# Test: create API gateway using check_mode = true |
| 11 |
- name: Create API gateway (check_mode=true) |
| 12 |
community.aws.api_gateway: |
| 13 |
name: "{{ api_name }}" |
| 14 |
swagger_text: "{{ apigateway_swagger_text }}" |
| 15 |
check_mode: true |
| 16 |
register: __create_check_mode |
| 17 |
|
| 18 |
- name: List existing API gateway |
| 19 |
community.aws.api_gateway_info: |
| 20 |
register: gateways |
| 21 |
|
| 22 |
- name: Ensure using check_mode=true, no API gateway was created |
| 23 |
assert: |
| 24 |
that: |
| 25 |
- __create_check_mode is changed |
| 26 |
- gateways.rest_apis | selectattr('name', 'equalto', api_name) | list | length == 0 |
| 27 |
|
| 28 |
# Test: create new API gateway using name and tags |
| 29 |
- name: Create new API gateway |
| 30 |
community.aws.api_gateway: |
| 31 |
name: "{{ api_name }}" |
| 32 |
swagger_text: "{{ apigateway_swagger_text }}" |
| 33 |
lookup: tag |
| 34 |
tags: "{{ resource_tags[0] }}" |
| 35 |
register: __create |
| 36 |
|
| 37 |
- name: List existing API gateway |
| 38 |
community.aws.api_gateway_info: |
| 39 |
register: gateways |
| 40 |
|
| 41 |
- name: Ensure new API was created |
| 42 |
assert: |
| 43 |
that: |
| 44 |
- __create is changed |
| 45 |
- gateways.rest_apis | selectattr('name', 'equalto', api_name) | list | length == 1 |
| 46 |
|
| 47 |
# Test: create API gateway idempotency (task reported changed but no new API created) |
| 48 |
- name: Create same API gateway once again |
| 49 |
community.aws.api_gateway: |
| 50 |
name: "{{ api_name }}" |
| 51 |
swagger_text: "{{ apigateway_swagger_text }}" |
| 52 |
lookup: tag |
| 53 |
tags: "{{ resource_tags[0] }}" |
| 54 |
|
| 55 |
- name: List existing API gateway |
| 56 |
community.aws.api_gateway_info: |
| 57 |
register: gateways |
| 58 |
|
| 59 |
- name: Ensure no new API was created |
| 60 |
assert: |
| 61 |
that: |
| 62 |
- gateways.rest_apis | selectattr('name', 'equalto', api_name) | list | length == 1 |
| 63 |
|
| 64 |
# Test: create new API using existing name but different tags (new API gateway should be created) |
| 65 |
- name: Create another API gateway with the same name but different tags |
| 66 |
community.aws.api_gateway: |
| 67 |
name: "{{ api_name }}" |
| 68 |
swagger_text: "{{ apigateway_swagger_text }}" |
| 69 |
lookup: tag |
| 70 |
tags: "{{ resource_tags[1] }}" |
| 71 |
|
| 72 |
- name: List existing API gateway |
| 73 |
community.aws.api_gateway_info: |
| 74 |
register: gateways |
| 75 |
|
| 76 |
- name: Ensure new API was created |
| 77 |
assert: |
| 78 |
that: |
| 79 |
- gateways.rest_apis | selectattr('name', 'equalto', api_name) | list | length == 2 |
| 80 |
|
| 81 |
rescue: |
| 82 |
- name: List existing API gateway |
| 83 |
community.aws.api_gateway_info: |
| 84 |
register: gateways |
| 85 |
|
| 86 |
- name: Delete remaining API gateway |
| 87 |
community.aws.api_gateway: |
| 88 |
api_id: '{{ item }}' |
| 89 |
state: absent |
| 90 |
ignore_errors: true |
| 91 |
with_items: "{{ gateways.rest_apis | selectattr('name', 'equalto', api_name) | map(attribute='id') | list }}" |
| 92 |
|
| 93 |
- name: Test API gateway deletion |
| 94 |
block: |
| 95 |
- name: "Create new API gateway name={{ api_name }}" |
| 96 |
community.aws.api_gateway: |
| 97 |
name: "{{ api_name }}" |
| 98 |
swagger_text: "{{ lookup('template', 'minimal-swagger-api.yml.j2') }}" |
| 99 |
lookup: tag |
| 100 |
tags: "{{ resource_tags[0] }}" |
| 101 |
vars: |
| 102 |
api_name: "{{ api_names[1] }}" |
| 103 |
|
| 104 |
- name: List existing API gateway |
| 105 |
community.aws.api_gateway_info: |
| 106 |
register: gateways |
| 107 |
|
| 108 |
- name: Ensure new API was created |
| 109 |
assert: |
| 110 |
that: |
| 111 |
- gateways.rest_apis | selectattr('name', 'equalto', api_names[1]) | list | length == 1 |
| 112 |
- gateways.rest_apis | selectattr('name', 'equalto', api_names[0]) | list | length == 2 |
| 113 |
|
| 114 |
# Test: Delete with lookup=tag (conflict), should failed |
| 115 |
- name: Delete API gateway |
| 116 |
community.aws.api_gateway: |
| 117 |
lookup: tag |
| 118 |
tags: "{{ resource_tags[0] }}" |
| 119 |
state: absent |
| 120 |
register: __delete_conflict |
| 121 |
ignore_errors: true |
| 122 |
|
| 123 |
- name: Ensure task failed |
| 124 |
assert: |
| 125 |
that: |
| 126 |
- __delete_conflict is failed |
| 127 |
- '__delete_conflict.msg == "Tags provided do not identify a unique API gateway"' |
| 128 |
|
| 129 |
# Test: Delete with name only (no api_id) |
| 130 |
- name: Create same API gateway once again |
| 131 |
community.aws.api_gateway: |
| 132 |
name: "{{ api_names[1] }}" |
| 133 |
state: absent |
| 134 |
register: __delete_missing_params |
| 135 |
ignore_errors: true |
| 136 |
|
| 137 |
- name: Ensure task failed |
| 138 |
assert: |
| 139 |
that: |
| 140 |
- __delete_missing_params is failed |
| 141 |
- '__delete_missing_params.msg == "API gateway id must be supplied to delete API gateway or provided tag with lookup=tag to identify API gateway id."' |
| 142 |
|
| 143 |
# Test: Delete (check_mode) |
| 144 |
- name: Delete API gateway - check mode |
| 145 |
community.aws.api_gateway: |
| 146 |
name: "{{ api_names[1] }}" |
| 147 |
lookup: tag |
| 148 |
tags: "{{ resource_tags[0] }}" |
| 149 |
state: absent |
| 150 |
register: __delete_check_mode |
| 151 |
check_mode: true |
| 152 |
|
| 153 |
- name: List existing API gateway |
| 154 |
community.aws.api_gateway_info: |
| 155 |
register: gateways |
| 156 |
|
| 157 |
- name: Ensure running in check mode, API was not deleted. |
| 158 |
assert: |
| 159 |
that: |
| 160 |
- __delete_check_mode is changed |
| 161 |
- gateways.rest_apis | selectattr('name', 'equalto', api_names[1]) | list | length == 1 |
| 162 |
- gateways.rest_apis | selectattr('name', 'equalto', api_names[0]) | list | length == 2 |
| 163 |
|
| 164 |
# Test: Delete using name and API gateway |
| 165 |
- name: Delete API gateway using name and lookup=tag |
| 166 |
community.aws.api_gateway: |
| 167 |
name: "{{ api_names[1] }}" |
| 168 |
lookup: tag |
| 169 |
tags: "{{ resource_tags[0] }}" |
| 170 |
state: absent |
| 171 |
register: __delete |
| 172 |
|
| 173 |
- name: List existing API gateway |
| 174 |
community.aws.api_gateway_info: |
| 175 |
register: gateways |
| 176 |
|
| 177 |
- name: Ensure matching API gateway was deleted |
| 178 |
assert: |
| 179 |
that: |
| 180 |
- __delete is changed |
| 181 |
- gateways.rest_apis | selectattr('name', 'equalto', api_names[1]) | list | length == 0 |
| 182 |
- gateways.rest_apis | selectattr('name', 'equalto', api_names[0]) | list | length == 2 |
| 183 |
|
| 184 |
# Test: Delete using api_id |
| 185 |
- name: Delete API gateway using api_id |
| 186 |
community.aws.api_gateway: |
| 187 |
api_id: "{{ gateways.rest_apis | selectattr('name', 'equalto', api_names[0]) | map(attribute='id') | first }}" |
| 188 |
state: absent |
| 189 |
register: __delete |
| 190 |
|
| 191 |
- name: List existing API gateway |
| 192 |
community.aws.api_gateway_info: |
| 193 |
register: gateways |
| 194 |
|
| 195 |
- name: Ensure matching API gateway was deleted |
| 196 |
assert: |
| 197 |
that: |
| 198 |
- __delete is changed |
| 199 |
- gateways.rest_apis | selectattr('name', 'equalto', api_names[0]) | list | length == 1 |
| 200 |
|
| 201 |
always: |
| 202 |
- name: List existing API gateway |
| 203 |
community.aws.api_gateway_info: |
| 204 |
register: gateways |
| 205 |
|
| 206 |
- name: Delete remaining API gateway |
| 207 |
community.aws.api_gateway: |
| 208 |
api_id: '{{ item }}' |
| 209 |
state: absent |
| 210 |
ignore_errors: true |
| 211 |
with_items: "{{ gateways.rest_apis | selectattr('name', 'in', api_names) | map(attribute='id') | list }}" |