aboutsummaryrefslogtreecommitdiffhomepage
path: root/tasks/plugins.yml
blob: 90a038667db479d8adf82c84f8dac338403b929a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
- name: Get plugins
  uri:
    url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}/plugins"
    method: GET
    validate_certs: no
    headers:
      apikey: "{{ server.kong_app_admin_apikey }}"
  register: kong_app_service_plugins_check

- name: Setup plugin
  uri:
    url: "{{ server.kong_app_admin_url }}/{{ exists_plugin | ternary(create_url, update_url) }}"
    method: "{{ exists_plugin | ternary ('POST', 'PATCH') }}"
    body: "{{ plugin | combine(current_body)  | to_json }}"
    headers:
      apikey: "{{ server.kong_app_admin_apikey }}"
      Content-Type: application/json
    status_code: 200,201
  with_items: "{{ service.plugins | default([]) }}"
  loop_control:
    loop_var: plugin
  vars:
    create_url: "services/{{ service.name }}/plugins"
    update_url: "plugins/{{ current_config.id }}"
    plugins_list: "{{ kong_app_service_plugins_check.json.data }}"
    current_config: "{{ plugins_list | selectattr('name', 'equalto', plugin.name) | first |default({\"id\": ''}) }}"
    current_id_hash:
      id: "{{ current_config.id }}"
    current_body: "{{ (not current_id_hash.id)  | ternary({}, current_id_hash) }}"
    exists_plugin: "{{ not current_id_hash.id  }}"
  when: not kong_app_service_plugins_check is skipped
  ignore_errors: true
  no_log: true
  register: kong_plugin_update_st

- name: Retry failed (deletion)
  uri:
    url: "{{ url }}"
    method: "DELETE"
    headers: "{{ headers }}"
    status_code: 204
  loop: "{{ kong_plugin_update_st.results }}"
  loop_control:
    loop_var: result
    label: "{{ label_name }}"
  vars:
    method: "{{ result.invocation.module_args.method }}"
    url: "{{ result.invocation.module_args.url }}"
    headers: "{{ result.invocation.module_args.headers }}"
    label_name: "{{ result.plugin.name }}"
  when:
    - kong_plugin_update_st is failed
    - result is failed
    - method == "PATCH"
  no_log: true

- name: Retry failed (recreation)
  uri:
    url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}/plugins"
    method: "POST"
    body: "{{ body | to_json }}"
    headers: "{{ headers }}"
    status_code: 200,201
  loop: "{{ kong_plugin_update_st.results }}"
  loop_control:
    loop_var: result
    label: "{{ label_name }}"
  vars:
    method: "{{ result.invocation.module_args.method }}"
    headers: "{{ result.invocation.module_args.headers }}"
    body: "{{ result.invocation.module_args.body }}"
    label_name: "{{ result.plugin.name }}"
  when:
    - kong_plugin_update_st is failed
    - result is failed
    - method == "PATCH"
  no_log: true

- name: Give info on errors
  debug:
      msg: "{{ info }}"
  loop: "{{ kong_plugin_update_st.results }}"
  loop_control:
    label: "{{ item.plugin.name }}"
  when:
    - kong_plugin_update_st is failed
    - item is failed
    - item.plugin is defined
    - item.plugin.name is defined
    - item.json is defined
    - item.json.message is defined
  vars:
    info: "{{ item.json.message }}"

- name: Fail otherwise
  fail:
    msg: "This plugin {{ result.plugin.name }} failed to be created"
  loop: "{{ kong_plugin_update_st.results }}"
  loop_control:
    loop_var: result
    label: "{{ label_name }}"
  vars:
    method: "{{ result.invocation.module_args.method }}"
    label_name: "{{ result.plugin.name }}"
  when:
    - kong_plugin_update_st is failed
    - result is failed
    - method == "POST"
  no_log: true