diff options
author | Paul Bonaud <paul.bonaud@fretlink.com> | 2020-10-12 17:11:48 +0200 |
---|---|---|
committer | Paul Bonaud <paul.bonaud@fretlink.com> | 2020-10-14 10:04:59 +0200 |
commit | 86fe1360cfff115b42607de3ed22fd8c0c75ccc0 (patch) | |
tree | 1e926825fba4b454765068d8ee7d77efeb14c3ce /tasks | |
parent | 2e423637ee780bd273094ad52e49906100724d08 (diff) | |
download | ansible-kong-app-86fe1360cfff115b42607de3ed22fd8c0c75ccc0.tar.gz ansible-kong-app-86fe1360cfff115b42607de3ed22fd8c0c75ccc0.tar.zst ansible-kong-app-86fe1360cfff115b42607de3ed22fd8c0c75ccc0.zip |
Use PUT methods instead of manually detecting if new or update
Kong supports resource idempotency by calling the `PUT` method on user
creation/update.
Kong will decide by itself whether to create or update the resource.
It's clearer and less code on our side. However we can only do this on
named resources (not possible on Plugins and Routes which are attached
to the `service` with an ID).
Diffstat (limited to 'tasks')
-rw-r--r-- | tasks/kong-app.yml | 15 | ||||
-rw-r--r-- | tasks/upstream.yml | 16 |
2 files changed, 6 insertions, 25 deletions
diff --git a/tasks/kong-app.yml b/tasks/kong-app.yml index 975e2cd..3c51e7e 100644 --- a/tasks/kong-app.yml +++ b/tasks/kong-app.yml | |||
@@ -2,26 +2,17 @@ | |||
2 | import_tasks: upstream.yml | 2 | import_tasks: upstream.yml |
3 | when: service.upstream is defined | 3 | when: service.upstream is defined |
4 | 4 | ||
5 | - name: Check if service exists | 5 | # https://docs.konghq.com/2.1.x/admin-api/#update-or-create-service |
6 | uri: | ||
7 | url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}" | ||
8 | method: GET | ||
9 | headers: | ||
10 | apikey: "{{ server.kong_app_admin_apikey }}" | ||
11 | status_code: 200,404 | ||
12 | register: kong_app_check_service | ||
13 | |||
14 | - name: Update or Create service | 6 | - name: Update or Create service |
15 | uri: | 7 | uri: |
16 | url: "{{ server.kong_app_admin_url }}/services/{{ exists_service | ternary('', service.name) }}" | 8 | url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}" |
17 | method: "{{ exists_service | ternary('POST', 'PATCH') }}" | 9 | method: PUT |
18 | body: "{{ service_body | to_json }}" | 10 | body: "{{ service_body | to_json }}" |
19 | status_code: 200,201 | 11 | status_code: 200,201 |
20 | headers: | 12 | headers: |
21 | apikey: "{{ server.kong_app_admin_apikey }}" | 13 | apikey: "{{ server.kong_app_admin_apikey }}" |
22 | Content-Type: application/json | 14 | Content-Type: application/json |
23 | vars: | 15 | vars: |
24 | exists_service: "{{ kong_app_check_service.status|default(404) == 404 }}" | ||
25 | service_body: | 16 | service_body: |
26 | name: "{{ service.name }}" | 17 | name: "{{ service.name }}" |
27 | url: "{{ service.url }}" | 18 | url: "{{ service.url }}" |
diff --git a/tasks/upstream.yml b/tasks/upstream.yml index 43c1baf..47f2e89 100644 --- a/tasks/upstream.yml +++ b/tasks/upstream.yml | |||
@@ -2,26 +2,16 @@ | |||
2 | set_fact: | 2 | set_fact: |
3 | upstream: "{{ service.upstream }}" | 3 | upstream: "{{ service.upstream }}" |
4 | 4 | ||
5 | - name: Check if upstream exists | 5 | # https://docs.konghq.com/2.1.x/admin-api/#update-or-create-upstream |
6 | uri: | ||
7 | url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}" | ||
8 | method: GET | ||
9 | headers: | ||
10 | apikey: "{{ server.kong_app_admin_apikey }}" | ||
11 | status_code: 200,404 | ||
12 | register: kong_app_check_upstream | ||
13 | |||
14 | - name: Update or Create upstream | 6 | - name: Update or Create upstream |
15 | uri: | 7 | uri: |
16 | url: "{{ server.kong_app_admin_url }}/upstreams/{{ exists_upstream | ternary('', upstream.conf.name) }}" | 8 | url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name) }}" |
17 | method: "{{ exists_upstream | ternary('POST', 'PATCH') }}" | 9 | method: PUT |
18 | body: "{{ upstream.conf | to_json }}" | 10 | body: "{{ upstream.conf | to_json }}" |
19 | status_code: 200,201 | 11 | status_code: 200,201 |
20 | headers: | 12 | headers: |
21 | apikey: "{{ server.kong_app_admin_apikey }}" | 13 | apikey: "{{ server.kong_app_admin_apikey }}" |
22 | Content-Type: application/json | 14 | Content-Type: application/json |
23 | vars: | ||
24 | exists_upstream: "{{ kong_app_check_upstream.status|default(404) == 404 }}" | ||
25 | 15 | ||
26 | - name: Get upstream targets | 16 | - name: Get upstream targets |
27 | uri: | 17 | uri: |