From 767c0e538b6b7c751784444fd6c676668e3a1b01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ga=C3=ABtan=20Duchaussois?= Date: Mon, 23 Apr 2018 10:35:56 +0200 Subject: [PATCH] setup an array of services --- README.md | 29 +++++++++++++++-------------- defaults/main.yml | 3 +-- tasks/kong-app.yml | 9 ++++++--- tasks/main.yml | 5 ++++- tasks/plugins.yml | 6 +++--- tasks/routes.yml | 6 +++--- tests/test.retry | 1 - vars/main.yml | 3 --- 8 files changed, 32 insertions(+), 30 deletions(-) delete mode 100644 tests/test.retry diff --git a/README.md b/README.md index 5cf5f18..074e830 100644 --- a/README.md +++ b/README.md @@ -13,16 +13,17 @@ Role Variables * `kong_app_admin_url` the kong admin url (mandatory). * `kong_app_admin_apikey` the apikey to use kong admin api. Default to "" -* `kong_app_service_name` the nameof the service to create for this app, mandatory -* `kong_app_service_url` the url of the backend of the app, mandatory -* `kong_app_plugins` An array of plugins to activate with their name and config in a dict - * `name` - * `config` -* `kong_app_routes` An array of routes to create for this app. - * `hosts` - * `paths` - * `protocols` - * `methods` +* `kong_services` an array of services to setup (default to []) + * `name` the name of the service to create for this app, mandatory + * `url` the url of the backend of the app, mandatory + * `plugins` An array of plugins to activate with their name and config in a dict + * `name` + * `config` + * `routes` An array of routes to create for this service. + * `hosts` + * `paths` + * `protocols` + * `methods` Dependencies ------------ @@ -35,10 +36,10 @@ Example Playbook - hosts: localhost roles: - { role: kong-app, kong_app_admin_url: http://localhost:8001, - kong_app_service_name: example, - kong_app_service_url: http://example.com, - kong_app_plugins: [], - kong_app_routes: [ { hosts: [my.kong.example], paths: [/] } ] + kong_app_services: [ name:example, + url: http://example.com, + plugins: [], + routes: [ { hosts: [my.kong.example], paths: [/] } ]] } License diff --git a/defaults/main.yml b/defaults/main.yml index d75773b..e2de8f5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,4 +1,3 @@ --- kong_app_admin_apikey: "" -kong_app_plugins: [] -kong_app_routes: [] +kong_services: [] 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 diff --git a/tests/test.retry b/tests/test.retry deleted file mode 100644 index 2fbb50c..0000000 --- a/tests/test.retry +++ /dev/null @@ -1 +0,0 @@ -localhost diff --git a/vars/main.yml b/vars/main.yml index 02f5cd6..ed97d53 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,4 +1 @@ --- -kong_app_service_body: - name: "{{ kong_app_service_name }}" - url: "{{ kong_app_service_url }}" -- 2.41.0