]> git.immae.eu Git - github/fretlink/ansible-kong-app.git/commitdiff
Use PUT methods instead of manually detecting if new or update
authorPaul Bonaud <paul.bonaud@fretlink.com>
Mon, 12 Oct 2020 15:11:48 +0000 (17:11 +0200)
committerPaul Bonaud <paul.bonaud@fretlink.com>
Wed, 14 Oct 2020 08:04:59 +0000 (10:04 +0200)
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
tasks/upstream.yml

index 975e2cdffb99458c51f75f5a98fb3accc53b89d1..3c51e7e3092a351341657864f66fc9c9e1a04cba 100644 (file)
@@ -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 }}"
index 43c1baf905660e75b1a3779ac695ed37b95161c6..47f2e89ff1cd099cd56b632f4cf6bf737feb9af8 100644 (file)
@@ -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: