Browse Source

Merge pull request #27 from lbroudoux/cors-policy

Adding support for adding CORS policy
pull/38/head
Nicolas Massé 7 years ago
committed by GitHub
parent
commit
f3a92a03a2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      README.md
  2. 3
      defaults/main.yml
  3. 23
      tasks/api-calls/update_policies.yml
  4. 3
      tasks/main.yml
  5. 13
      tasks/steps/policies.yml
  6. 7
      templates/api-calls/update_policies.j2
  7. 5
      templates/existing_policies.j2
  8. 5
      templates/wanted_policies.j2
  9. 11
      vars/main.yml

11
README.md

@ -447,6 +447,17 @@ Defines the 3scale Private Base URL.
- **Default value:** `<threescale_cicd_api_backend_scheme>://<threescale_cicd_api_backend_hostname>` - **Default value:** `<threescale_cicd_api_backend_scheme>://<threescale_cicd_api_backend_hostname>`
- **Example:** `http://mybackend.acme.corp:8080` - **Example:** `http://mybackend.acme.corp:8080`
### `threescale_cicd_apicast_policies_cors`
Allows to enable the CORS policy onto APICast gateway. In case your API should support cross-origin
and browser based invocations and you do not have included the `OPTIONS` verb on correct path into
your OpenAPI Specification file...
- **Syntax:** boolean `yes` or `no`
- **Required:** no
- **Default value:** `no`
- **Example:** `yes` if you want to activate CORS policy on APICast
### `threescale_cicd_openapi_smoketest_operation` ### `threescale_cicd_openapi_smoketest_operation`
Defines the OpenAPI Specification method to use for smoke tests. Defines the OpenAPI Specification method to use for smoke tests.

3
defaults/main.yml

@ -23,6 +23,9 @@ threescale_cicd_local_bin_path: '{{ playbook_dir }}/bin'
# Enable the OpenAPI Specification validation # Enable the OpenAPI Specification validation
threescale_cicd_validate_openapi: yes threescale_cicd_validate_openapi: yes
# APIcast policies
threescale_cicd_apicast_policies_cors: no
# APIcast public base URLs # APIcast public base URLs
threescale_cicd_apicast_sandbox_endpoint: '{{ lookup(''template'', ''openapi/apicast_sandbox_endpoint.j2'') }}' threescale_cicd_apicast_sandbox_endpoint: '{{ lookup(''template'', ''openapi/apicast_sandbox_endpoint.j2'') }}'
threescale_cicd_apicast_production_endpoint: '{{ lookup(''template'', ''openapi/apicast_production_endpoint.j2'') }}' threescale_cicd_apicast_production_endpoint: '{{ lookup(''template'', ''openapi/apicast_production_endpoint.j2'') }}'

23
tasks/api-calls/update_policies.yml

@ -0,0 +1,23 @@
---
- debug:
var: threescale_cicd_policies_to_update
verbosity: 1
- debug:
var: threescale_cicd_update_policies_payload
verbosity: 1
- name: Update the policies chain
uri:
url: https://{{ inventory_hostname }}/admin/api/services/{{ threescale_cicd_api_service_id }}/proxy/policies.json
validate_certs: no
method: PUT
body: '{{ threescale_cicd_update_policies_payload }}'
status_code: 200
register: threescale_cicd_tmpresponse
changed_when: 'threescale_cicd_tmpresponse.status == 200'
- name: Wait for a couple seconds
pause:
seconds: '{{ threescale_cicd_throttling }}'

3
tasks/main.yml

@ -28,6 +28,9 @@
# Update the proxy # Update the proxy
- import_tasks: steps/proxy.yml - import_tasks: steps/proxy.yml
# Create or update policies
- import_tasks: steps/policies.yml
# Create or update application plans # Create or update application plans
- import_tasks: steps/application_plans.yml - import_tasks: steps/application_plans.yml

13
tasks/steps/policies.yml

@ -0,0 +1,13 @@
---
- name: Retrieve existing policies from the 3scale Admin Portal
uri:
url: "https://{{ inventory_hostname }}/admin/api/services/{{ threescale_cicd_api_service_id }}/proxy/policies.json?access_token={{ threescale_cicd_access_token|urlencode }}"
validate_certs: no
register: threescale_cicd_tmpresponse
- set_fact:
threescale_cicd_existing_policies_details: '{{ threescale_cicd_tmpresponse.json|json_query(''policies_config[]'') }}'
- include_tasks: "api-calls/update_policies.yml"
with_items: '{{ threescale_cicd_policies_to_update }}'

7
templates/api-calls/update_policies.j2

@ -0,0 +1,7 @@
{%
set payload = [
'access_token=' ~ threescale_cicd_access_token|urlencode,
'policies_config=' ~ threescale_cicd_policies_to_update|to_json|urlencode
]
%}
{{ payload|join("&") }}

5
templates/existing_policies.j2

@ -0,0 +1,5 @@
{% set policies = [] %}
{% for value in threescale_cicd_existing_policies_details %}
{% do policies.append( value ) %}
{% endfor %}
{{ policies }}

5
templates/wanted_policies.j2

@ -0,0 +1,5 @@
{% set policies = [] %}
{% if threescale_cicd_apicast_policies_cors %}
{% do policies.append( {"name": "cors", "version": "builtin", "configuration": {"allow_credentials": true}, "enabled": true} ) %}
{% endif %}
{{ policies }}

11
vars/main.yml

@ -67,6 +67,16 @@ threescale_cicd_mapping_rules_to_delete: '{{ threescale_cicd_existing_mapping_ru
# update the items that we want and we have # update the items that we want and we have
threescale_cicd_mapping_rules_to_update: '{{ threescale_cicd_existing_mapping_rules.keys()|intersect(threescale_cicd_wanted_mapping_rules.keys()) }}' threescale_cicd_mapping_rules_to_update: '{{ threescale_cicd_existing_mapping_rules.keys()|intersect(threescale_cicd_wanted_mapping_rules.keys()) }}'
##
## Policies computation
##
## what we want
threescale_cicd_wanted_policies: '{{ lookup(''template'', ''wanted_policies.j2'') }}'
## what we have
threescale_cicd_existing_policies: '{{ lookup(''template'', ''existing_policies.j2'') }}'
# update the items that we want and we have
threescale_cicd_policies_to_update: '{{ threescale_cicd_existing_policies|union(threescale_cicd_wanted_policies) }}'
## ##
## 3scale API Payload definition ## 3scale API Payload definition
## ##
@ -77,6 +87,7 @@ threescale_cicd_update_method_payload: '{{ lookup(''template'', ''api-calls/upda
threescale_cicd_create_method_payload: '{{ lookup(''template'', ''api-calls/create_method.j2'') }}' threescale_cicd_create_method_payload: '{{ lookup(''template'', ''api-calls/create_method.j2'') }}'
threescale_cicd_update_mapping_rule_payload: '{{ lookup(''template'', ''api-calls/update_mapping_rule.j2'') }}' threescale_cicd_update_mapping_rule_payload: '{{ lookup(''template'', ''api-calls/update_mapping_rule.j2'') }}'
threescale_cicd_create_mapping_rule_payload: '{{ lookup(''template'', ''api-calls/create_mapping_rule.j2'') }}' threescale_cicd_create_mapping_rule_payload: '{{ lookup(''template'', ''api-calls/create_mapping_rule.j2'') }}'
threescale_cicd_update_policies_payload: '{{ lookup(''template'', ''api-calls/update_policies.j2'') }}'
threescale_cicd_update_application_plan_payload: '{{ lookup(''template'', ''api-calls/update_application_plan.j2'') }}' threescale_cicd_update_application_plan_payload: '{{ lookup(''template'', ''api-calls/update_application_plan.j2'') }}'
threescale_cicd_create_application_plan_payload: '{{ lookup(''template'', ''api-calls/create_application_plan.j2'') }}' threescale_cicd_create_application_plan_payload: '{{ lookup(''template'', ''api-calls/create_application_plan.j2'') }}'
threescale_cicd_find_application_payload: '{{ lookup(''template'', ''api-calls/find_application.j2'') }}' threescale_cicd_find_application_payload: '{{ lookup(''template'', ''api-calls/find_application.j2'') }}'

Loading…
Cancel
Save