From 86fe1360cfff115b42607de3ed22fd8c0c75ccc0 Mon Sep 17 00:00:00 2001 From: Paul Bonaud Date: Mon, 12 Oct 2020 17:11:48 +0200 Subject: 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). --- tasks/kong-app.yml | 15 +++------------ tasks/upstream.yml | 16 +++------------- 2 files changed, 6 insertions(+), 25 deletions(-) (limited to 'tasks') 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 @@ import_tasks: upstream.yml when: service.upstream is defined -- name: Check if service exists - uri: - url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}" - method: GET - headers: - apikey: "{{ server.kong_app_admin_apikey }}" - status_code: 200,404 - register: kong_app_check_service - +# https://docs.konghq.com/2.1.x/admin-api/#update-or-create-service - name: Update or Create service uri: - url: "{{ server.kong_app_admin_url }}/services/{{ exists_service | ternary('', service.name) }}" - method: "{{ exists_service | ternary('POST', 'PATCH') }}" + url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}" + method: PUT body: "{{ service_body | to_json }}" status_code: 200,201 headers: apikey: "{{ server.kong_app_admin_apikey }}" Content-Type: application/json vars: - exists_service: "{{ kong_app_check_service.status|default(404) == 404 }}" service_body: name: "{{ service.name }}" 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 @@ set_fact: upstream: "{{ service.upstream }}" -- name: Check if upstream exists - uri: - url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}" - method: GET - headers: - apikey: "{{ server.kong_app_admin_apikey }}" - status_code: 200,404 - register: kong_app_check_upstream - +# https://docs.konghq.com/2.1.x/admin-api/#update-or-create-upstream - name: Update or Create upstream uri: - url: "{{ server.kong_app_admin_url }}/upstreams/{{ exists_upstream | ternary('', upstream.conf.name) }}" - method: "{{ exists_upstream | ternary('POST', 'PATCH') }}" + url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name) }}" + method: PUT body: "{{ upstream.conf | to_json }}" status_code: 200,201 headers: apikey: "{{ server.kong_app_admin_apikey }}" Content-Type: application/json - vars: - exists_upstream: "{{ kong_app_check_upstream.status|default(404) == 404 }}" - name: Get upstream targets uri: -- cgit v1.2.3