|
| 1 | +- name: Get resources. |
| 2 | + ansible.builtin.command: >- |
| 3 | + aws apigateway get-resources |
| 4 | + --rest-api-id "{{ _api_gate.id }}" |
| 5 | + --region "{{ _aws_region }}" |
| 6 | + register: _api_old_resource |
| 7 | + |
| 8 | +- name: Setting previous command output into variable. |
| 9 | + ansible.builtin.set_fact: |
| 10 | + _api_old_resource: "{{ _api_old_resource.stdout | from_json }}" |
| 11 | + |
| 12 | +- name: Find the index of existing resource. |
| 13 | + ansible.builtin.set_fact: |
| 14 | + _api_old_resource_index: "{{ lookup('ansible.utils.index_of', _api_old_resource['items'], 'eq', '/' + item.name, 'path', wantlist=True) }}" |
| 15 | + |
| 16 | +- name: Delete resource. |
| 17 | + ansible.builtin.command: >- |
| 18 | + aws apigateway delete-resource |
| 19 | + --rest-api-id "{{ _api_gate.id }}" |
| 20 | + --resource-id "{{ _api_old_resource['items'][_api_old_resource_index[0]].id }}" |
| 21 | + --region "{{ _aws_region }}" |
| 22 | + register: _api_old_resource |
| 23 | + when: _api_old_resource_index | length > 0 |
| 24 | + |
| 25 | +- name: Create resource on API gateway. |
| 26 | + ansible.builtin.command: >- |
| 27 | + aws apigateway create-resource |
| 28 | + --rest-api-id "{{ _api_gate.id }}" |
| 29 | + --parent-id "{{ _api_res_list[_api_res_index_list[0]].id }}" |
| 30 | + --path-part "{{ item.name }}" |
| 31 | + --region "{{ _aws_region }}" |
| 32 | + register: _api_resource |
| 33 | + |
| 34 | +- name: Setting previous command output into variable. |
| 35 | + ansible.builtin.set_fact: |
| 36 | + _api_resource: "{{ _api_resource.stdout | from_json }}" |
| 37 | + |
| 38 | +- name: Put method on API gateway |
| 39 | + ansible.builtin.command: >- |
| 40 | + aws apigateway put-method |
| 41 | + --rest-api-id "{{ _api_gate.id }}" |
| 42 | + --resource-id "{{ _api_resource.id }}" |
| 43 | + --http-method "{{ item.type }}" |
| 44 | + --authorization-type "NONE" |
| 45 | + --no-api-key-required |
| 46 | + --region "{{ _aws_region }}" |
| 47 | +
|
| 48 | +- name: Add Lambda for method. |
| 49 | + ansible.builtin.command: >- |
| 50 | + aws apigateway put-integration |
| 51 | + --rest-api-id "{{ _api_gate.id }}" |
| 52 | + --resource-id "{{ _api_resource.id }}" |
| 53 | + --http-method "{{ item.type }}" |
| 54 | + --type AWS |
| 55 | + --content-handling CONVERT_TO_TEXT |
| 56 | + --request-templates '{ "application/json": "{\"statusCode\": 200}" }' |
| 57 | + --integration-http-method POST |
| 58 | + --uri "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:API_{{ item.name }}/invocations" |
| 59 | + --region {{ _aws_region }} |
| 60 | +
|
| 61 | +- name: Add method response. |
| 62 | + ansible.builtin.command: >- |
| 63 | + aws apigateway put-method-response |
| 64 | + --rest-api-id "{{ _api_gate.id }}" |
| 65 | + --resource-id "{{ _api_resource.id }}" |
| 66 | + --http-method "{{ item.type }}" |
| 67 | + --status-code "200" |
| 68 | + --response-models '{"application/json":"Empty"}' |
| 69 | + --region {{ _aws_region }} |
| 70 | +
|
| 71 | +- name: Add integration response. |
| 72 | + ansible.builtin.command: >- |
| 73 | + aws apigateway put-integration-response |
| 74 | + --rest-api-id "{{ _api_gate.id }}" |
| 75 | + --resource-id "{{ _api_resource.id }}" |
| 76 | + --http-method "{{ item.type }}" |
| 77 | + --status-code "200" |
| 78 | + --selection-pattern "" |
| 79 | + --content-handling "CONVERT_TO_TEXT" |
| 80 | + --region {{ _aws_region }} |
0 commit comments