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(-) 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 From 1443410095d3b4a2a560997e8a0662a1deed1d57 Mon Sep 17 00:00:00 2001 From: Paul Bonaud Date: Mon, 12 Oct 2020 17:31:15 +0200 Subject: tests: allow fake Kong server to receive PUT requests --- tests/kong.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/kong.py b/tests/kong.py index a1f9cec..c39542f 100755 --- a/tests/kong.py +++ b/tests/kong.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from http.server import HTTPServer, CGIHTTPRequestHandler +import random class SimpleHTTPRequestHandler(CGIHTTPRequestHandler): def do_GET(self): @@ -17,6 +18,14 @@ class SimpleHTTPRequestHandler(CGIHTTPRequestHandler): self.end_headers() self.wfile.write(b'Hello, world!') + def do_PUT(self): + if bool(random.getrandbits(1)): + self.send_response(200) + else: + self.send_response(201) + self.end_headers() + self.wfile.write(b'Hello, world!') + httpd = HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler) httpd.serve_forever() -- cgit v1.2.3