commit e5f9546f862e7c86c3971b0b4f2820e2d11a3336 Author: Nicolas Massé Date: Thu Feb 1 15:11:50 2018 +0100 initial commit diff --git a/3scale_status.yml b/3scale_status.yml new file mode 100644 index 0000000..04bb404 --- /dev/null +++ b/3scale_status.yml @@ -0,0 +1,29 @@ +--- + + - name: Get a list of available services + uri: + url: "https://{{ threescale_admin_hostname }}/admin/api/services.json?access_token={{ access_token }}" + validate_certs: no + register: response + + - set_fact: + services: '{{ response.json|json_query(''services[*].service.system_name'') }}' + services_details: '{{ response.json|json_query(''services[].{"system_name": service.system_name, "id": service.id}'') }}' + + - name: Get the list of existing applications + uri: + url: https://{{ threescale_admin_hostname }}/admin/api/applications.json?access_token={{ access_token|urlencode }} + validate_certs: no + register: response + + - set_fact: + applications: '{{ response.json|json_query(''applications[*].application.name'') }}' + + - name: Get the default (first) account + uri: + url: https://{{ threescale_admin_hostname }}/admin/api/accounts.json?access_token={{ access_token|urlencode }}&state=approved&page=1&per_page=1 + validate_certs: no + register: response + + - set_fact: + account_id: '{{ response.json.accounts[0].account.id }}' diff --git a/create_api.yml b/create_api.yml new file mode 100644 index 0000000..7dca219 --- /dev/null +++ b/create_api.yml @@ -0,0 +1,66 @@ +--- + + - debug: msg="Working on service {{ item.service.name }}" + + - set_fact: + body_create_svc: '{{ "access_token=" ~ access_token|urlencode }}' + + - set_fact: + body_create_svc: '{{ body_create_svc ~ "&" ~ (param.key|urlencode) ~ "=" ~ (param.value|urlencode) }}' + with_dict: '{{ item.service }}' + loop_control: + loop_var: param + when: 'item.service.system_name not in services' + + - name: Create the service + uri: + url: https://{{ threescale_admin_hostname }}/admin/api/services.json + validate_certs: no + method: POST + body: '{{ body_create_svc }}' + status_code: 201 + register: response + when: 'item.service.system_name not in services' + + - set_fact: + services: '{{ services|union([ item.service.system_name ]) }}' + services_details: '{{ services_details|union([{ "system_name": item.service.system_name, "id": response.json.service.id }]) }}' + when: 'item.service.system_name not in services' + + - set_fact: + service_id: '{{ (services_details|selectattr("system_name", "equalto", item.service.system_name)|first)["id"] }}' + + - set_fact: + body_update_proxy: '{{ "access_token=" ~ access_token|urlencode }}' + + - set_fact: + body_update_proxy: '{{ body_update_proxy ~ "&" ~ (param.key|urlencode) ~ "=" ~ (param.value|urlencode) }}' + with_dict: '{{ item.proxy }}' + loop_control: + loop_var: param + + - name: Set Backend URL + set_fact: + body_update_proxy: '{{ body_update_proxy ~ "&api_backend" ~ "=" ~ (item.backend|urlencode) }}' + + - name: Update the proxy + uri: + url: https://{{ threescale_admin_hostname }}/admin/api/services/{{ service_id }}/proxy.json + validate_certs: no + method: PATCH + body: '{{ body_update_proxy }}' + + - name: Get the list of existing application plans + uri: + url: https://{{ threescale_admin_hostname }}/admin/api/services/{{ service_id }}/application_plans.json?access_token={{ access_token|urlencode }} + validate_certs: no + register: response + + - set_fact: + application_plans: '{{ response.json|json_query(''plans[*].application_plan.system_name'') }}' + application_plans_details: '{{ response.json|json_query(''plans[].{"system_name": application_plan.system_name, "id": application_plan.id}'') }}' + + - include: create_application_plan.yml + with_items: '{{ item.application_plans }}' + loop_control: + loop_var: plan diff --git a/create_application_plan.yml b/create_application_plan.yml new file mode 100644 index 0000000..5bd2b00 --- /dev/null +++ b/create_application_plan.yml @@ -0,0 +1,38 @@ +--- + + - debug: msg="Working on plan {{ plan.system_name }} / service {{ item.service.name }}" + + - set_fact: + body_create_plan: '{{ "access_token=" ~ access_token|urlencode }}' + + - set_fact: + body_create_plan: '{{ body_create_plan ~ "&" ~ (param.key|urlencode) ~ "=" ~ (param.value|urlencode) }}' + with_dict: '{{ plan }}' + loop_control: + loop_var: param + # applications is a nested hash that is used to create client applications later + when: 'param.key != ''applications'' and plan.system_name not in application_plans ' + + - name: Create the application plan + uri: + url: https://{{ threescale_admin_hostname }}/admin/api/services/{{ service_id }}/application_plans.json + validate_certs: no + method: POST + body: '{{ body_create_plan }}' + status_code: 201 + register: response + when: 'plan.system_name not in application_plans' + + - set_fact: + application_plans: '{{ application_plans|union([ plan.system_name ]) }}' + application_plans_details: '{{ application_plans_details|union([{ "system_name": plan.system_name, "id": response.json.application_plan.id }]) }}' + when: 'plan.system_name not in application_plans' + + - set_fact: + plan_id: '{{ (application_plans_details|selectattr("system_name", "equalto", plan.system_name)|first)["id"] }}' + + - include: create_applications.yml + with_items: '{{ plan.applications }}' + loop_control: + loop_var: app + when: 'app.name not in applications' diff --git a/create_applications.yml b/create_applications.yml new file mode 100644 index 0000000..c0ca7c2 --- /dev/null +++ b/create_applications.yml @@ -0,0 +1,21 @@ +--- + + - debug: msg="Working on application {{ app.name }} / plan {{ plan.system_name }} / service {{ item.service.name }}" + + - set_fact: + body_create_app: '{{ "access_token=" ~ (access_token|urlencode) ~ "&plan_id=" ~ (plan_id) }}' + + - set_fact: + body_create_app: '{{ body_create_app ~ "&" ~ (param.key|urlencode) ~ "=" ~ (param.value|urlencode) }}' + with_dict: '{{ app }}' + loop_control: + loop_var: param + + - name: Create the application + uri: + url: https://{{ threescale_admin_hostname }}/admin/api/accounts/{{ account_id }}/applications.json + validate_certs: no + method: POST + body: '{{ body_create_app }}' + status_code: 201 + register: response diff --git a/hosts.example b/hosts.example new file mode 100644 index 0000000..997b65e --- /dev/null +++ b/hosts.example @@ -0,0 +1,2 @@ +[3scale] +localhost ansible_connection=local threescale_admin_hostname=.3scale.net access_token= diff --git a/main.yml b/main.yml new file mode 100644 index 0000000..ff51968 --- /dev/null +++ b/main.yml @@ -0,0 +1,63 @@ +--- + + - name: Provision APIs in 3scale + hosts: 3scale + become: no + vars: + threescale_apis_to_create: + - service: + name: "Hello API" + system_name: hello-api + backend_version: 1 # 1 means "API Key" + proxy: + credentials_location: headers + api_test_path: /rest/Hello%20API%20Mock/0.8/v1/hello?David + backend: http://example:8080 + application_plans: + - name: Default + system_name: default + default: true + state: published + applications: + - name: Hello App + description: "This is my very first application" + - service: + name: "Github API" + system_name: github-api + backend_version: 1 # 1 means "API Key" + proxy: + credentials_location: headers + api_test_path: / + backend: https://api.github.com + application_plans: + - name: Default + system_name: default + default: true + state: published + applications: + - name: GitHub Test App + description: "This is my very first application" + - service: + name: "Hello API OAuth" + system_name: hello-api-oauth + backend_version: oauth + proxy: + credentials_location: headers + api_test_path: /rest/Hello%20API%20Mock/0.8/v1/hello?Gavin + backend: http://example:8080 + application_plans: + - name: Default + system_name: default + default: true + state: published + applications: + - name: Hello App with OAuth + description: "This is my very first application using OAuth" + redirect_url: https://www.getpostman.com/oauth2/callback + application_id: "my-app" + application_key: "{{ 'my-app'|sha1() }}" + + tasks: + - include: "3scale_status.yml" + - include: "create_api.yml" + with_items: '{{ threescale_apis_to_create }}'