From 1a33ca3184dbea838ea083a7cf026cf5884dc3f4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ga=C3=ABtan=20Duchaussois?= Date: Fri, 20 Apr 2018 10:04:41 +0200 Subject: [PATCH] initial version --- ansible.cfg | 2 ++ defaults/main.yml | 4 ++++ handlers/main.yml | 2 ++ meta/main.yml | 57 ++++++++++++++++++++++++++++++++++++++++++++++ tasks/kong-app.yml | 24 +++++++++++++++++++ tasks/main.yml | 5 ++++ tasks/plugins.yml | 28 +++++++++++++++++++++++ tasks/routes.yml | 32 ++++++++++++++++++++++++++ tests/inventory | 2 ++ tests/test.retry | 1 + tests/test.yml | 8 +++++++ vars/main.yml | 4 ++++ 12 files changed, 169 insertions(+) create mode 100644 ansible.cfg create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml 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 create mode 100644 tests/inventory create mode 100644 tests/test.retry create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..6462621 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +roles_path=../ diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..d75773b --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,4 @@ +--- +kong_app_admin_apikey: "" +kong_app_plugins: [] +kong_app_routes: [] diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..fd808d4 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for ansible-kong-app \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..e2b8be4 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,57 @@ +galaxy_info: + author: FretLink team + description: Love and trucks + company: FretLink + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Some suggested licenses: + # - BSD (default) + # - MIT + # - GPLv2 + # - GPLv3 + # - Apache + # - CC-BY + license: TBD + + min_ansible_version: 2.4 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # Optionally specify the branch Galaxy will use when accessing the GitHub + # repo for this role. During role install, if no tags are available, + # Galaxy will use this branch. During import Galaxy will access files on + # this branch. If Travis integration is configured, only notifications for this + # branch will be accepted. Otherwise, in all cases, the repo's default branch + # (usually master) will be used. + #github_branch: + + # + # platforms is a list of platforms, and each platform has a name and a list of versions. + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. 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 }}" diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.retry b/tests/test.retry new file mode 100644 index 0000000..2fbb50c --- /dev/null +++ b/tests/test.retry @@ -0,0 +1 @@ +localhost diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..fb0d9b8 --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,8 @@ +--- +- hosts: localhost + remote_user: root + roles: + - role: ansible-kong-app + kong_app_admin_url: http://127.0.0.1:8000 + kong_app_service_name: toto + kong_app_service_url: http://example.com diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..02f5cd6 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,4 @@ +--- +kong_app_service_body: + name: "{{ kong_app_service_name }}" + url: "{{ kong_app_service_url }}" -- 2.41.0