Browse Source

initial commit

master
Nicolas Massé 8 years ago
commit
e5f9546f86
  1. 29
      3scale_status.yml
  2. 66
      create_api.yml
  3. 38
      create_application_plan.yml
  4. 21
      create_applications.yml
  5. 2
      hosts.example
  6. 63
      main.yml

29
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 }}'

66
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

38
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'

21
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

2
hosts.example

@ -0,0 +1,2 @@
[3scale]
localhost ansible_connection=local threescale_admin_hostname=<TENANT>.3scale.net access_token=<ACCESS_TOKEN>

63
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 }}'
Loading…
Cancel
Save