|
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 root 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 | 1 | - name: Create resource on API gateway. |
26 | 2 | ansible.builtin.command: >- |
27 | 3 | aws apigateway create-resource |
|
64 | 40 | --region "{{ _aws_region }}" |
65 | 41 | when: item.url_params is defined and item.url_params | length > 0 |
66 | 42 |
|
67 | | -- name: Add Lambda for method without URL params. |
| 43 | +- name: Build arn for S3 bucket. |
| 44 | + ansible.builtin.set_fact: |
| 45 | + _target_arn: "arn:aws:apigateway:{{ _aws_region }}:s3:path/{{ item.bucket }}/{{ item.object }}" |
| 46 | + when: item.resource == "s3" |
| 47 | + |
| 48 | +- name: List Objects in S3 bucket. |
| 49 | + amazon.aws.s3_object: |
| 50 | + bucket: "{{ item.bucket }}" |
| 51 | + mode: list |
| 52 | + register: _objects_in_s3_bucket |
| 53 | + when: item.resource == "s3" |
| 54 | + |
| 55 | +- name: Set _object_missing to false if object exist. |
| 56 | + ansible.builtin.set_fact: |
| 57 | + _object_missing: false |
| 58 | + when: item.resource == "s3" and item.object in _objects_in_s3_bucket.s3_keys |
| 59 | + |
| 60 | +- name: Set _object_missing to true if object doesn't exist. |
| 61 | + ansible.builtin.set_fact: |
| 62 | + _object_missing: true |
| 63 | + when: item.resource == "s3" and item.object not in _objects_in_s3_bucket.s3_keys |
| 64 | + |
| 65 | +- name: Create default file for S3. |
| 66 | + ansible.builtin.template: |
| 67 | + src: default_s3_object.j2 |
| 68 | + dest: default_s3_object |
| 69 | + when: _object_missing is defined and _object_missing |
| 70 | + |
| 71 | +- name: Add default file to S3 bucket if it doesn't exist. |
| 72 | + amazon.aws.s3_object: |
| 73 | + bucket: "{{ item.bucket }}" |
| 74 | + object: "{{ item.object }}" |
| 75 | + src: "default_s3_object" |
| 76 | + mode: put |
| 77 | + when: _object_missing is defined and _object_missing |
| 78 | + |
| 79 | +- name: Build arn for Lambda. |
| 80 | + ansible.builtin.set_fact: |
| 81 | + _target_arn: "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:api_{{ item.name }}/invocations" |
| 82 | + when: item.resource == "api" |
| 83 | + |
| 84 | +- name: Add Service for method without URL params. |
68 | 85 | ansible.builtin.command: >- |
69 | 86 | aws apigateway put-integration |
70 | 87 | --rest-api-id "{{ _api_gate.id }}" |
71 | 88 | --resource-id "{{ _api_resource.id }}" |
72 | 89 | --http-method "{{ item.type }}" |
73 | 90 | --type AWS |
74 | 91 | --content-handling CONVERT_TO_TEXT |
75 | | - --integration-http-method POST |
76 | | - --uri "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:api_{{ item.name }}/invocations" |
| 92 | + --integration-http-method {{ 'GET' if item.resource == 's3' else 'POST' }} |
| 93 | + --uri "{{ _target_arn }}" |
77 | 94 | --region {{ _aws_region }} |
| 95 | + {{ '--credentials "arn:aws:iam::' + _acc_id + ':role/api_get_s3"' if item.resource == 's3' else '' }} |
78 | 96 | when: item.url_params is not defined or item.url_params | length == 0 |
79 | 97 |
|
80 | 98 | - name: Generate template parts for each param |
|
93 | 111 | dest: integration_template |
94 | 112 | when: item.url_params is defined and item.url_params | length > 0 |
95 | 113 |
|
96 | | -- name: Add Lambda for method with URL params. |
| 114 | +- name: Add Service for method with URL params. |
97 | 115 | ansible.builtin.command: >- |
98 | 116 | aws apigateway put-integration |
99 | 117 | --rest-api-id "{{ _api_gate.id }}" |
100 | 118 | --resource-id "{{ _api_resource.id }}" |
101 | 119 | --http-method "{{ item.type }}" |
102 | 120 | --type AWS |
103 | 121 | --content-handling CONVERT_TO_TEXT |
104 | | - --integration-http-method POST |
| 122 | + --integration-http-method {{ 'GET' if item.resource == 's3' else 'POST' }} |
105 | 123 | --passthrough-behavior WHEN_NO_TEMPLATES |
106 | 124 | --request-template file://integration_template |
107 | | - --uri "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:api_{{ item.name }}/invocations" |
| 125 | + --uri "{{ _target_arn }}" |
108 | 126 | --region {{ _aws_region }} |
| 127 | + {{ '--credentials "arn:aws:iam::' + _acc_id + ':role/api_get_s3"' if item.resource == 's3' else '' }} |
109 | 128 | when: item.url_params is defined and item.url_params | length > 0 |
110 | 129 |
|
111 | 130 | - name: Add method response. |
|
0 commit comments