From 1a33ca3184dbea838ea083a7cf026cf5884dc3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Fri, 20 Apr 2018 10:04:41 +0200 Subject: initial version --- tasks/kong-app.yml | 24 ++++++++++++++++++++++++ tasks/main.yml | 5 +++++ tasks/plugins.yml | 28 ++++++++++++++++++++++++++++ tasks/routes.yml | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 tasks/kong-app.yml create mode 100644 tasks/main.yml create mode 100644 tasks/plugins.yml create mode 100644 tasks/routes.yml (limited to 'tasks') diff --git a/tasks/kong-app.yml b/tasks/kong-app.yml new file mode 100644 index 0000000..d50c193 --- /dev/null +++ b/tasks/kong-app.yml @@ -0,0 +1,24 @@ +- name: Check if service exists + uri: + url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}" + method: GET + headers: + apikey: "{{ kong_app_admin_apikey }}" + status_code: 200,404 + register: kong_app_check_service + +#- name: Update or Create service +# uri: +# url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}" +# method: "{{ (kong_app_check_service.status_code == 404) | ternary ('POST', 'PATCH') }}" +# body: "{{ kong_app_service_body | to_json }}" +# headers: +# apikey: "{{ kong_app_admin_apikey }}" +# Content-Type: application/json +# status_code: 200,201 + +- name: Setup plugins + import_tasks: plugins.yml + +- name: Setup routes + import_tasks: routes.yml diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..4bb562f --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: Include kong-app tasks + import_tasks: kong-app.yml + tags: + - kong-app diff --git a/tasks/plugins.yml b/tasks/plugins.yml new file mode 100644 index 0000000..c8f165c --- /dev/null +++ b/tasks/plugins.yml @@ -0,0 +1,28 @@ +- name: Get plugins + uri: + url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/plugins" + method: GET + validate_certs: no + register: kong_app_service_plugins_check + +- name: Set plugins facts + set_fact: + kong_app_current_plugins: "{{ kong_app_service_plugins_check.json | default('{}') | from_json }}" + +- name: Setup plugin {{ plugin.name }} + uri: + url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/plugins" + method: "{{ (current_config == {}) | ternary ('POST', 'PATCH') }}" + body: "{{ plugin | combine(current_body) | to_json }}" + headers: + apikey: "{{ kong_app_admin_apikey }}" + Content-Type: application/json + status_code: 200,201 + with_items: "{{ kong_app_plugins }}" + loop_control: + loop_var: plugin + vars: + current_config: "{{ kong_app_current_plugins.data | selectattr('name', plugin.name) | first |default({}) }}" + current_id_hash: + id: "{{ current_config.id | default('')}}" + current_body: "{{ (current_id_hash.id == '') | ternary({}, current_id_hash) }}" diff --git a/tasks/routes.yml b/tasks/routes.yml new file mode 100644 index 0000000..1a9f6db --- /dev/null +++ b/tasks/routes.yml @@ -0,0 +1,32 @@ +- name: Get app routes + uri: + url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/routes" + method: GET + validate_certs: no + register: kong_app_service_routes_check + +- name: Set routes facts + set_fact: + kong_app_current_routes: "{{ kong_app_service_routes_check.json |default('{\"data\": []}') | from_json }}" + +- name: Setup route + uri: + url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/routes" + method: POST + body: "{{ route | to_json }}" + headers: + apikey: "{{ kong_app_admin_apikey }}" + Content-Type: application/json + status_code: 201 + with_items: "{{ kong_app_routes }}" + loop_control: + loop_var: route + +- name: Delete old routes + uri: + url: "{{ kong_app_admin_url }}/services/routes/{{ item.id }}" + method: DELETE + headers: + apikey: "{{ kong_app_admin_apikey }}" + status_code: 204 + with_items: "{{ kong_app_current_routes.data }}" -- cgit v1.2.3