Skip to content

Commit 8f90251

Browse files
author
Aiden Keating
committed
Initial setup of 3scale services
1 parent 5228943 commit 8f90251

File tree

10 files changed

+372
-0
lines changed

10 files changed

+372
-0
lines changed

ansible/playbooks/3scale.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
3+
- name: Configure 3scale Service
4+
hosts: localhost
5+
connection: local
6+
gather_facts: false
7+
run_once: true
8+
vars_files:
9+
- group_vars/services.yml
10+
- group_vars/3scale.yml
11+
tasks:
12+
- set_fact:
13+
namespace: "{{ threescale_namespace }}"
14+
config_deployment_name: "{{ threescale_config_deployment }}"
15+
config_deployment_namespace: "{{ project_name }}"
16+
resources_dir: "{{ resources_base_dir }}/3scale"
17+
work_dir_name: 3scale
18+
- include_role:
19+
name: ../roles/3scale
20+
when: >
21+
ACTION is not defined or
22+
ACTION is none or
23+
ACTION|trim() == "" or
24+
ACTION|trim() == "install"
25+
- include_role:
26+
name: ../roles/3scale
27+
tasks_from: uninstall
28+
when: >
29+
ACTION is defined and
30+
ACTION|trim() == "uninstall"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
threescale_enable: true
3+
threescale_namespace: 3scale
4+
5+
threescale_apicast_staging_svc: apicast-staging
6+
threescale_apicase_prod_svc: apicast-production
7+
8+
threescale_config_deployment: emergency-console

ansible/playbooks/install.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- group_vars/postgresql.yml
1414
- group_vars/sso.yml
1515
- group_vars/monitoring.yml
16+
- group_vars/3scale.yml
1617

1718
tasks:
1819
- include_role:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[
2+
{
3+
"name": "cors",
4+
"version": "builtin",
5+
"configuration": {
6+
"allow_headers": [
7+
"Authorization"
8+
],
9+
"allow_credentials": true,
10+
"allow_methods": [
11+
"GET",
12+
"OPTIONS",
13+
"POST"
14+
],
15+
"allow_origin": "*"
16+
},
17+
"enabled": true
18+
},
19+
{
20+
"name": "apicast",
21+
"version": "builtin",
22+
"configuration": {},
23+
"enabled": true
24+
}
25+
]

ansible/resources/3scale/route.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: route.openshift.io/v1
2+
kind: Route
3+
metadata:
4+
name: {{ name }}
5+
spec:
6+
port:
7+
targetPort: gateway
8+
tls:
9+
termination: edge
10+
to:
11+
kind: Service
12+
name: {{ apicast_service }}
13+
weight: 100
14+
wildcardPolicy: None
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
threescale_namespace: 3scale
3+
threescale_admin_route: system-provider-admin
4+
threescale_admin_secret: system-seed
5+
6+
emergency_response_namespace: emergency-response-demo

ansible/roles/3scale/meta/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
3+
dependencies:
4+
- role: work_dir
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
---
2+
- name: Copy staging route template
3+
template:
4+
src: "{{ resources_dir }}/route.yml"
5+
dest: "{{ work_dir }}/route.yml"
6+
vars:
7+
name: "{{ service_name }}-staging"
8+
apicast_service: "{{ threescale_apicast_staging_svc }}"
9+
10+
- name: Create staging route
11+
oc_obj:
12+
state: present
13+
oc_binary: "{{ openshift_cli }}"
14+
name: "{{ service_name }}-staging"
15+
kind: route
16+
namespace: "{{ namespace }}"
17+
files:
18+
- "{{ work_dir }}/route.yml"
19+
20+
- name: Get staging route
21+
shell: oc get route {{ service_name }}-staging -n {{ namespace }} -o jsonpath='{.spec.host}'
22+
register: staging_route_result
23+
24+
- set_fact:
25+
staging_route: "https://{{ staging_route_result.stdout }}"
26+
27+
- name: Copy production route template
28+
template:
29+
src: "{{ resources_dir }}/route.yml"
30+
dest: "{{ work_dir }}/route.yml"
31+
vars:
32+
name: "{{ service_name }}-production"
33+
apicast_service: "{{ threescale_apicase_prod_svc }}"
34+
35+
- name: Create production route
36+
oc_obj:
37+
state: present
38+
oc_binary: "{{ openshift_cli }}"
39+
name: "{{ service_name }}-production"
40+
kind: route
41+
namespace: "{{ namespace }}"
42+
files:
43+
- "{{ work_dir }}/route.yml"
44+
45+
- name: Get production route
46+
shell: oc get route {{ service_name }}-production -n {{ namespace }} -o jsonpath='{.spec.host}'
47+
register: production_route_result
48+
49+
- set_fact:
50+
production_route: "https://{{ production_route_result.stdout }}"
51+
52+
- name: Create service
53+
uri:
54+
url: "{{ admin_route }}/admin/api/services.xml"
55+
method: POST
56+
return_content: yes
57+
headers:
58+
Content-Type: "application/x-www-form-urlencoded"
59+
body: "access_token={{ admintoken }}&name={{ service_name | urlencode }}&deployment_option=hosted&backend_version={{ '1' if backend_version == 'oidc' else backend_version }}&system_name={{ system_name }}"
60+
validate_certs: no
61+
status_code: [201, 422]
62+
register: create_svc_result
63+
64+
- name: Get all services
65+
uri:
66+
url: "{{ admin_route }}/admin/api/services.xml?access_token={{ admintoken }}"
67+
method: GET
68+
return_content: yes
69+
validate_certs: no
70+
status_code: 200
71+
register: list_svc_result
72+
73+
- name: Get service Id
74+
xml:
75+
xmlstring: '{{ list_svc_result.content }}'
76+
xpath: //services//service//system_name[.='{{ system_name }}']/parent::service/id
77+
content: text
78+
register: xmlstring
79+
80+
- debug: var=xmlstring.matches[0].id
81+
82+
- set_fact: id={{ xmlstring.matches[0].id }}
83+
84+
- set_fact: oidc_issuer={{ ( backend_version == 'oidc' and proxy_config.sso_endpoint is defined ) | ternary('&oidc_issuer_endpoint=' + ( proxy_config.sso_endpoint | default('') | urlencode ), '') }}
85+
86+
- name: Config backend service proxy
87+
uri:
88+
url: "{{ admin_route }}/admin/api/services/{{ id }}/proxy.xml"
89+
method: PATCH
90+
return_content: yes
91+
headers:
92+
Content-Type: "application/x-www-form-urlencoded"
93+
body: "access_token={{ admintoken }}&endpoint={{ production_route | urlencode }}\
94+
&sandbox_endpoint={{ staging_route | urlencode }}\
95+
&api_backend={{ proxy_config.backend_endpoint | urlencode }}\
96+
{{ oidc_issuer }}"
97+
validate_certs: no
98+
when: proxy_config is defined
99+
100+
- name: Create application plan
101+
uri:
102+
url: "{{ admin_route }}/admin/api/services/{{ id }}/application_plans.xml"
103+
method: POST
104+
return_content: yes
105+
headers:
106+
Content-Type: "application/x-www-form-urlencoded"
107+
body: "access_token={{ admintoken }}&name={{ application_plan.name | urlencode }}&system_name={{ application_plan.system_name }}&state=published"
108+
validate_certs: no
109+
status_code: [201, 422]
110+
register: create_plan_result
111+
when: application_plan is defined
112+
113+
- name: Get all application plans
114+
uri:
115+
url: "{{ admin_route }}/admin/api/services/{{ id }}/application_plans.xml?access_token={{ admintoken }}"
116+
method: GET
117+
return_content: yes
118+
validate_certs: no
119+
status_code: 200
120+
register: list_app_plans_result
121+
122+
- name: Get plan Id
123+
xml:
124+
xmlstring: '{{ list_app_plans_result.content }}'
125+
xpath: //plans//plan//service_id[.='{{ id }}']/parent::plan/id
126+
content: text
127+
register: xmlstring
128+
when: application_plan is defined
129+
130+
- debug: var=xmlstring.matches[0].id
131+
when: application_plan is defined
132+
133+
- set_fact: plan_id={{ xmlstring.matches[0].id }}
134+
when: application_plan is defined
135+
136+
- set_fact: redirect_uri={{ ( app_config.redirect_uri is defined ) | ternary('&redirect_url=' + ( app_config.redirect_uri | default('') | urlencode ), '') }}
137+
when: app_config is defined
138+
139+
- debug: var=redirect_uri
140+
141+
- name: Create application
142+
uri:
143+
url: "{{ admin_route }}/admin/api/accounts/{{ account_id }}/applications.xml"
144+
method: POST
145+
return_content: yes
146+
headers:
147+
Content-Type: "application/x-www-form-urlencoded"
148+
body: "access_token={{ admintoken }}&plan_id={{ plan_id }}&name={{ app_config.name }}\
149+
&description={{ app_config.description }}&application_id={{ app_config.id }}\
150+
&user_key=12345{{ redirect_uri }}"
151+
validate_certs: no
152+
status_code: [201, 422]
153+
register: create_app_result
154+
when: app_config is defined
155+
156+
- name: Get policy chain template
157+
shell: 'cat {{ resources_dir }}/policy-config.json'
158+
register: get_policies
159+
when: proxy_config is defined
160+
161+
- debug: var=get_policies.stdout
162+
when: proxy_config is defined
163+
164+
- name: Enable CORS
165+
uri:
166+
url: "{{ admin_route }}/admin/api/services/{{ id }}/proxy/policies.json"
167+
method: PUT
168+
return_content: yes
169+
headers:
170+
Content-Type: "application/x-www-form-urlencoded"
171+
body: "access_token={{ admintoken }}&policies_config={{ get_policies.stdout | urlencode }}"
172+
validate_certs: no
173+
status_code: 200
174+
register: update_policies_result
175+
when: proxy_config is defined
176+
177+
- set_fact:
178+
config_key: "APICAST_{{ service_name | replace('-', '_') | upper }}"
179+
180+
- name: Set config
181+
shell: oc set env dc/{{ config_deployment_name }} -n {{ config_deployment_namespace }} {{ config_key }}={{ production_route }}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
- name: Get 3scale admin token
3+
shell: oc get secret {{ threescale_admin_secret }} -n {{ threescale_namespace }} -o jsonpath='{.data.ADMIN_ACCESS_TOKEN}' | base64 --decode
4+
register: admin_token_cmd
5+
6+
- name: Get 3scale admin route
7+
shell: oc get route {{ threescale_admin_route }} -n {{ threescale_namespace }} -o jsonpath="{.spec.host}"
8+
register: admin_route_cmd
9+
10+
- set_fact:
11+
threescale_admin_route: 'https://{{ admin_route_cmd.stdout }}'
12+
threescale_admin_token: "{{ admin_token_cmd.stdout }}"
13+
ocp_apps_domain: "{{ admin_route_cmd.stdout | replace('3scale-admin.', '') }}"
14+
15+
- name: Create responder simulator 3scale service
16+
include_tasks: ./create-service.yml
17+
vars:
18+
admin_route: '{{ threescale_admin_route }}'
19+
admintoken: '{{ threescale_admin_token }}'
20+
account_id: '3'
21+
service_name: responder-simulator
22+
backend_version: '1'
23+
system_name: responder-simulator
24+
proxy_config:
25+
backend_endpoint: http://responder-simulator.{{ emergency_response_namespace }}.svc:8080
26+
application_plan:
27+
name: Responder Simulator
28+
system_name: responder-simulator
29+
app_config:
30+
id: www-responder-simulator
31+
name: responder-simulator
32+
description: Responder Simulator
33+
34+
- name: Create responder service 3scale service
35+
include_tasks: ./create-service.yml
36+
vars:
37+
admin_route: '{{ threescale_admin_route }}'
38+
admintoken: '{{ threescale_admin_token }}'
39+
account_id: '3'
40+
service_name: responder-service
41+
backend_version: '1'
42+
system_name: responder-service
43+
proxy_config:
44+
backend_endpoint: http://responder-service.{{ emergency_response_namespace }}.svc:8080
45+
application_plan:
46+
name: Responder Service
47+
system_name: responder-service
48+
app_config:
49+
id: www-responder-service
50+
name: responder-service
51+
description: Responder Service
52+
53+
- name: Create incident service 3scale service
54+
include_tasks: ./create-service.yml
55+
vars:
56+
admin_route: '{{ threescale_admin_route }}'
57+
admintoken: '{{ threescale_admin_token }}'
58+
account_id: '3'
59+
service_name: incident-service
60+
backend_version: '1'
61+
system_name: incident-service
62+
proxy_config:
63+
backend_endpoint: http://incident-service.{{ emergency_response_namespace }}.svc:8080
64+
application_plan:
65+
name: Incident Service
66+
system_name: incident-service
67+
app_config:
68+
id: www-incident-service
69+
name: incident-service
70+
description: Incident Service
71+
72+
- name: Create mission service 3scale service
73+
include_tasks: ./create-service.yml
74+
vars:
75+
admin_route: '{{ threescale_admin_route }}'
76+
admintoken: '{{ threescale_admin_token }}'
77+
account_id: '3'
78+
service_name: mission-service
79+
backend_version: '1'
80+
system_name: mission-service
81+
proxy_config:
82+
backend_endpoint: http://mission-service.{{ emergency_response_namespace }}.svc:8080
83+
application_plan:
84+
name: Mission Service
85+
system_name: mission-service
86+
app_config:
87+
id: www-mission-service
88+
name: mission-service
89+
description: Mission Service
90+
91+
- name: Set config
92+
shell: oc set env dc/{{ config_deployment_name }} -n {{ config_deployment_namespace }} APICAST_ENABLED=true

ansible/roles/openshift_install/tasks/main.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,14 @@
305305
monitor_namespace: "{{ namespace_services }}"
306306
resources_dir: "{{ resources_base_dir }}/monitoring"
307307
work_dir_name: monitoring
308+
309+
- name: setup 3scale with demo services
310+
import_role:
311+
name: 3scale
312+
vars:
313+
namespace: "{{ threescale_namespace }}"
314+
config_deployment_name: "{{ threescale_config_deployment }}"
315+
config_deployment_namespace: "{{ project_name }}"
316+
resources_dir: "{{ resources_base_dir }}/3scale"
317+
work_dir_name: 3scale
318+
when: threescale_enable | default(true) | bool

0 commit comments

Comments
 (0)