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 From ad5e125af59a34020943158999564af4f3a41a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Fri, 20 Apr 2018 12:14:40 +0200 Subject: After testing --- tasks/kong-app.yml | 18 +++++++++--------- tasks/plugins.yml | 16 +++++++--------- tasks/routes.yml | 10 ++++------ 3 files changed, 20 insertions(+), 24 deletions(-) (limited to 'tasks') diff --git a/tasks/kong-app.yml b/tasks/kong-app.yml index d50c193..e162c09 100644 --- a/tasks/kong-app.yml +++ b/tasks/kong-app.yml @@ -7,15 +7,15 @@ 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: Update or Create service + uri: + url: "{{ kong_app_admin_url }}/services/{{ (kong_app_check_service.status == 404) | ternary('', kong_app_service_name) }}" + method: "{{ (kong_app_check_service.status == 404) | ternary('POST', 'PATCH') }}" + body: "{{ kong_app_service_body | to_json }}" + status_code: 200,201 + headers: + apikey: "{{ kong_app_admin_apikey }}" + Content-Type: application/json - name: Setup plugins import_tasks: plugins.yml diff --git a/tasks/plugins.yml b/tasks/plugins.yml index c8f165c..84f3d49 100644 --- a/tasks/plugins.yml +++ b/tasks/plugins.yml @@ -3,16 +3,14 @@ url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/plugins" method: GET validate_certs: no + headers: + apikey: "{{ kong_app_admin_apikey }}" 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 }} +- name: Setup plugin uri: - url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/plugins" - method: "{{ (current_config == {}) | ternary ('POST', 'PATCH') }}" + url: "{{ kong_app_admin_url }}/{{ (current_config.id == '') | ternary('services/' ~ kong_app_service_name ~ '/plugins','plugins/' ~ current_config.id) }}" + method: "{{ (current_config.id == '') | ternary ('POST', 'PATCH') }}" body: "{{ plugin | combine(current_body) | to_json }}" headers: apikey: "{{ kong_app_admin_apikey }}" @@ -22,7 +20,7 @@ loop_control: loop_var: plugin vars: - current_config: "{{ kong_app_current_plugins.data | selectattr('name', plugin.name) | first |default({}) }}" + current_config: "{{ kong_app_service_plugins_check.json.data | selectattr('name', 'equalto', plugin.name) | first |default({\"id\": ''}) }}" current_id_hash: - id: "{{ current_config.id | default('')}}" + id: "{{ current_config.id }}" current_body: "{{ (current_id_hash.id == '') | ternary({}, current_id_hash) }}" diff --git a/tasks/routes.yml b/tasks/routes.yml index 1a9f6db..bbcdb76 100644 --- a/tasks/routes.yml +++ b/tasks/routes.yml @@ -3,12 +3,10 @@ url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/routes" method: GET validate_certs: no + headers: + apikey: "{{ kong_app_admin_apikey }}" 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" @@ -24,9 +22,9 @@ - name: Delete old routes uri: - url: "{{ kong_app_admin_url }}/services/routes/{{ item.id }}" + url: "{{ kong_app_admin_url }}/routes/{{ item.id }}" method: DELETE headers: apikey: "{{ kong_app_admin_apikey }}" status_code: 204 - with_items: "{{ kong_app_current_routes.data }}" + with_items: "{{ kong_app_service_routes_check.json.data }}" -- cgit v1.2.3 From d213edd127a145821e419e80104a838da421e7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Fri, 20 Apr 2018 14:40:33 +0200 Subject: add travis configuration and fix tests --- tasks/kong-app.yml | 6 ++++-- tasks/plugins.yml | 6 ++++-- tasks/routes.yml | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) (limited to 'tasks') diff --git a/tasks/kong-app.yml b/tasks/kong-app.yml index e162c09..df95f2e 100644 --- a/tasks/kong-app.yml +++ b/tasks/kong-app.yml @@ -9,13 +9,15 @@ - name: Update or Create service uri: - url: "{{ kong_app_admin_url }}/services/{{ (kong_app_check_service.status == 404) | ternary('', kong_app_service_name) }}" - method: "{{ (kong_app_check_service.status == 404) | ternary('POST', 'PATCH') }}" + url: "{{ kong_app_admin_url }}/services/{{ exists_service | ternary('', kong_app_service_name) }}" + method: "{{ exists_service | ternary('POST', 'PATCH') }}" body: "{{ kong_app_service_body | to_json }}" status_code: 200,201 headers: apikey: "{{ kong_app_admin_apikey }}" Content-Type: application/json + vars: + exists_service: "{{ kong_app_check_service.status|default(404) == 404 }}" - name: Setup plugins import_tasks: plugins.yml diff --git a/tasks/plugins.yml b/tasks/plugins.yml index 84f3d49..c03bca7 100644 --- a/tasks/plugins.yml +++ b/tasks/plugins.yml @@ -9,8 +9,8 @@ - name: Setup plugin uri: - url: "{{ kong_app_admin_url }}/{{ (current_config.id == '') | ternary('services/' ~ kong_app_service_name ~ '/plugins','plugins/' ~ current_config.id) }}" - method: "{{ (current_config.id == '') | ternary ('POST', 'PATCH') }}" + url: "{{ kong_app_admin_url }}/{{ exists_plugin | ternary('services/' ~ kong_app_service_name ~ '/plugins','plugins/' ~ current_config.id) }}" + method: "{{ exists_plugin | ternary ('POST', 'PATCH') }}" body: "{{ plugin | combine(current_body) | to_json }}" headers: apikey: "{{ kong_app_admin_apikey }}" @@ -24,3 +24,5 @@ current_id_hash: id: "{{ current_config.id }}" current_body: "{{ (current_id_hash.id == '') | ternary({}, current_id_hash) }}" + exists_plugin: "{{ current_id_hash.id == '' }}" + when: not kong_app_service_plugins_check|skipped diff --git a/tasks/routes.yml b/tasks/routes.yml index bbcdb76..d4d912f 100644 --- a/tasks/routes.yml +++ b/tasks/routes.yml @@ -28,3 +28,4 @@ apikey: "{{ kong_app_admin_apikey }}" status_code: 204 with_items: "{{ kong_app_service_routes_check.json.data }}" + when: not kong_app_service_routes_check|skipped -- cgit v1.2.3 From 0682b86504faf606b9017bc82bdbabd3af237db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Fri, 20 Apr 2018 15:00:52 +0200 Subject: fix deprecation warning --- tasks/plugins.yml | 2 +- tasks/routes.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'tasks') diff --git a/tasks/plugins.yml b/tasks/plugins.yml index c03bca7..f096d4b 100644 --- a/tasks/plugins.yml +++ b/tasks/plugins.yml @@ -25,4 +25,4 @@ id: "{{ current_config.id }}" current_body: "{{ (current_id_hash.id == '') | ternary({}, current_id_hash) }}" exists_plugin: "{{ current_id_hash.id == '' }}" - when: not kong_app_service_plugins_check|skipped + when: not kong_app_service_plugins_check is skipped diff --git a/tasks/routes.yml b/tasks/routes.yml index d4d912f..4581e8c 100644 --- a/tasks/routes.yml +++ b/tasks/routes.yml @@ -28,4 +28,4 @@ apikey: "{{ kong_app_admin_apikey }}" status_code: 204 with_items: "{{ kong_app_service_routes_check.json.data }}" - when: not kong_app_service_routes_check|skipped + when: not kong_app_service_routes_check is skipped -- cgit v1.2.3 From 767c0e538b6b7c751784444fd6c676668e3a1b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Mon, 23 Apr 2018 10:35:56 +0200 Subject: setup an array of services --- tasks/kong-app.yml | 9 ++++++--- tasks/main.yml | 5 ++++- tasks/plugins.yml | 6 +++--- tasks/routes.yml | 6 +++--- 4 files changed, 16 insertions(+), 10 deletions(-) (limited to 'tasks') diff --git a/tasks/kong-app.yml b/tasks/kong-app.yml index df95f2e..f94f044 100644 --- a/tasks/kong-app.yml +++ b/tasks/kong-app.yml @@ -1,6 +1,6 @@ - name: Check if service exists uri: - url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}" + url: "{{ kong_app_admin_url }}/services/{{ service.name }}" method: GET headers: apikey: "{{ kong_app_admin_apikey }}" @@ -9,15 +9,18 @@ - name: Update or Create service uri: - url: "{{ kong_app_admin_url }}/services/{{ exists_service | ternary('', kong_app_service_name) }}" + url: "{{ kong_app_admin_url }}/services/{{ exists_service | ternary('', service.name) }}" method: "{{ exists_service | ternary('POST', 'PATCH') }}" - body: "{{ kong_app_service_body | to_json }}" + body: "{{ service_body | to_json }}" status_code: 200,201 headers: apikey: "{{ 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 }}" - name: Setup plugins import_tasks: plugins.yml diff --git a/tasks/main.yml b/tasks/main.yml index 4bb562f..4bd42c8 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,5 +1,8 @@ --- - name: Include kong-app tasks - import_tasks: kong-app.yml + include_tasks: kong-app.yml tags: - kong-app + with_items: "{{ kong_services }}" + loop_control: + loop_var: service diff --git a/tasks/plugins.yml b/tasks/plugins.yml index f096d4b..3ae0fbe 100644 --- a/tasks/plugins.yml +++ b/tasks/plugins.yml @@ -1,6 +1,6 @@ - name: Get plugins uri: - url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/plugins" + url: "{{ kong_app_admin_url }}/services/{{ service.name }}/plugins" method: GET validate_certs: no headers: @@ -9,14 +9,14 @@ - name: Setup plugin uri: - url: "{{ kong_app_admin_url }}/{{ exists_plugin | ternary('services/' ~ kong_app_service_name ~ '/plugins','plugins/' ~ current_config.id) }}" + url: "{{ kong_app_admin_url }}/{{ exists_plugin | ternary('services/' ~ service.name ~ '/plugins','plugins/' ~ current_config.id) }}" method: "{{ exists_plugin | 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 }}" + with_items: "{{ service.plugins | default([]) }}" loop_control: loop_var: plugin vars: diff --git a/tasks/routes.yml b/tasks/routes.yml index 4581e8c..881fc96 100644 --- a/tasks/routes.yml +++ b/tasks/routes.yml @@ -1,6 +1,6 @@ - name: Get app routes uri: - url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/routes" + url: "{{ kong_app_admin_url }}/services/{{ service.name }}/routes" method: GET validate_certs: no headers: @@ -9,14 +9,14 @@ - name: Setup route uri: - url: "{{ kong_app_admin_url }}/services/{{ kong_app_service_name }}/routes" + url: "{{ kong_app_admin_url }}/services/{{ 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 }}" + with_items: "{{ service.routes | default([]) }}" loop_control: loop_var: route -- cgit v1.2.3