Skip to content
Snippets Groups Projects
Commit 2a2e49f3 authored by Jean-Eric Stranig's avatar Jean-Eric Stranig
Browse files

Merge branch 'BIOMASS-2980-test-nb' into 'master'

Biomass 2980 test nb

See merge request !14
parents 1e18c3cb 1e725efd
No related branches found
No related tags found
1 merge request!14Biomass 2980 test nb
%% Cell type:markdown id:8b30c14c tags:
# Test note book
In this test we're using echo algorythm for it's ease of use and to demonstrate the capabilities and functionality of copa backend API.
Please make sure you have *copa_backend_url* , *client_id* and *url_token* set in /projects/.maap/maap.ini
%% Cell type:markdown id:dd440e2a tags:
### Import and configuration
Let's begin with importing the libraries we need and all the configuration we have, you can find them in /projects/.maap/ folder.
%% Cell type:code id:47236f15 tags:
``` python
import configparser
import os
import requests
import json
import time
```
%% Cell type:code id:544e720f tags:
``` python
#Load the file that contains auth values
config = configparser.ConfigParser()
config.read('/projects/.maap/auth.ini')
#Retrieve auth values
email = config['auth']['email']
password = config['auth']['password']
```
%% Cell type:code id:c25e880c tags:
``` python
#Load the file that contains maap values
config = configparser.ConfigParser()
config.read('/projects/.maap/maap.ini')
#Retrieve maap values
copa_backend_url = config['maap']['copa_backend_url']
CLIENT_ID = config['maap']['client_id']
url_token = config['maap']['url_token']
echo_cwl = 'https://s3public.oss.eu-west-0.prod-cloud-ocb.orange-business.com/cwl/echo-test/workflow.cwl'
#Location for tmp file for created process
JobIdWD = '{}/TestJobID.csv'.format(os.path.abspath(os.getcwd()))
print(JobIdWD)
```
%% Output
/projects/demo-scripts/wps_notebook/wpst_test/TestJobID.csv
%% Cell type:markdown id:144f3655 tags:
### Authentication
Get an authentication token that will allow us to acces copa API service.
%% Cell type:code id:618ebabb tags:
``` python
response = requests.post(url_token, data={'client_id': CLIENT_ID, 'username': email, 'password': password, "grant_type": "password", "scope": "profile"})
data = json.loads(response.text)
oauth_token = data['access_token']
```
%% Cell type:markdown id:ba2317cd tags:
### Deployement
Lets start by getting all deployed processes
%% Cell type:code id:944490ee tags:
``` python
# get deployed cwl workflow
process = requests.get(copa_backend_url+'wpst/processes', headers = {'Authorization': 'Bearer '+ oauth_token})
print(process.content)
```
%% Output
b'{"processes":[]}'
%% Cell type:markdown id:a9a512d7 tags:
In the next step we will check if echo process is already deployed.
%% Cell type:code id:30893bb2 tags:
``` python
# get echo-wf id if exist
process_already_deployed = None
if process.content:
for item in json.loads(process.content).get("processes"):
if item.get('title')== 'wf-echo-process':
process_already_deployed = item.get('id')
print(process_already_deployed)
```
%% Output
None
%% Cell type:markdown id:7af8c2c5 tags:
We cannot have multiple deployement of the same process, and in order to test deploying using the deploy endpoint we have to undeploy the existing one first.
%% Cell type:code id:afe6d7d5 tags:
``` python
if process_already_deployed:
delete_response = requests.delete(copa_backend_url+'wpst/processes/{}'.format(process_already_deployed), headers = {'Authorization': 'Bearer '+ oauth_token})
print(delete_response.content)
```
%% Cell type:markdown id:1edf9f2d tags:
Test deploy endpoint.
%% Cell type:code id:0ab04609 tags:
``` python
#deploy wf-s1-tiling
wfstorage = {"executionUnit": [{"href": "{}".format(echo_cwl)}]}
workflow = requests.post(
copa_backend_url+'wpst/processes/',
headers = {'Authorization': 'Bearer '+ oauth_token},
json=wfstorage)
print(workflow.content)
workflow_id = json.loads(workflow.content).get('processSummary').get('id')
```
%% Output
b'{"processSummary":{"id":"6455224a9f0e9b5a11d2fd82","title":"wf-echo-test","keywords":null,"owsContext":null,"metadata":null,"additionalParameters":null,"links":null,"version":null,"jobControlOptions":null,"outputTransmission":null,"processDescriptionURL":"https://s3public.oss.eu-west-0.prod-cloud-ocb.orange-business.com/cwl/echo-test/workflow.cwl","abstract":"Applies s expressions to EO acquisitions"}}'
%% Cell type:markdown id:5f8751b4 tags:
### workflow description
In the next step we will ask the Api endpoint to give us description of the process we just deployed. we can see many informations and most importantly the inputs to launch a job.
%% Cell type:code id:6b42c492 tags:
``` python
wf_description = requests.get(copa_backend_url+'wpst/processes/{}'.format(workflow_id), headers = {'Authorization': 'Bearer '+ oauth_token})
```
%% Cell type:code id:00385eb4 tags:
``` python
print(json.loads(wf_description.content).get('process').get('inputs')[0])
```
%% Output
{'id': 'input_reference', 'title': 'Input product reference', 'keywords': ['default: https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_36RTT_20191205_0_L2A'], 'owsContext': None, 'metadata': None, 'additionalParameters': None, 'links': None, 'minOccurs': None, 'maxOccurs': None, 'formats': [{'mimeType': 'string[]', 'schema': None, 'encoding': None, 'maximumMegabytes': None, 'default': False}], 'abstract': 'Input product reference'}
%% Cell type:code id:2e7f9e7b tags:
``` python
for item in json.loads(wf_description.content).get('process').get('inputs'):
print("id : {} \t\t||\t title : {}".format(item.get('id'), item.get('title')))
```
%% Output
id : input_reference || title : Input product reference
id : s_expression || title : s expression
id : cbn || title : cbn
id : bucket_name || title : Stage-in source bucket
id : copy_dir_or_file || title : Stage-in mode (dir or file)
id : s3_destination || title : Stage-out target folder
%% Cell type:markdown id:ec731150 tags:
Based on the Description we have above, we can create a payload of input values. this payload will be used to launch a job.
%% Cell type:code id:7f5047e9 tags:
``` python
payload = {
"inputs": [
{
"id": "input_file",
"data": "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_36RTT_20191205_0_L2A",
"href": ""
},
{
"id": "s_message",
"data": "Hello",
"href": ""
},
{
"id": "bucket_name",
"data": "maap-scientific-data",
"href": ""
},
{
"id": "copy_dir_or_file",
"data": "file",
"href": ""
},
{
"id": "s3_destination",
"data": "maap-scientific-data/shared/demo/echo-process-out",
"href": ""
}
],
"outputs": [],
"mode": "ASYNC",
"response": "RAW"
}
```
%% Cell type:markdown id:b0097062 tags:
### Launching a Job
To not cause confusion, we need to define a job in our context. when launching a process, it create an execution instance, and we will call it a job from now on.
The folowing request demonstrate the possibility of launching a process and get the job id.
%% Cell type:code id:97f08280 tags:
``` python
wf_exec = requests.post(copa_backend_url+'wpst/processes/{}/jobs'.format(workflow_id), json=payload, headers = {'Authorization': 'Bearer '+ oauth_token})
job_id = json.loads(wf_exec.content).get('jobId')
print(wf_exec)
```
%% Output
<Response [201]>
%% Cell type:code id:f8f6995a tags:
``` python
# List the executions of the echo workflow
exec_list = requests.get(copa_backend_url+'wpst/processes/{}/jobs'.format(workflow_id), headers = {'Authorization': 'Bearer '+ oauth_token})
print(exec_list.content)
```
%% Output
b'{"jobs":["6455224b9f0e9b5a11d2fd84"]}'
%% Cell type:markdown id:a919ee9a tags:
In the previous step we asked the API to provide us with a List of the executions of the CWL workflow with the given ID.
In the next step we check if the job_id we just created is returned in the list.
%% Cell type:code id:90a62514 tags:
``` python
jobs_list = json.loads(exec_list.content).get("jobs")
assert job_id in jobs_list
```
%% Cell type:markdown id:862e1ee0 tags:
#### Job Status
Get job status.
%% Cell type:code id:addaa77e tags:
``` python
print('please wait...')
job_status=None
while not job_status or json.loads(job_status.content).get('status') == 'RUNNING':
time.sleep(10)
job_status = requests.get(copa_backend_url+'wpst/processes/{}/jobs/{}'.format(workflow_id, job_id), headers = {'Authorization': 'Bearer '+ oauth_token})
print(job_status.content)
```
%% Output
please wait...
b'{"jobId":"6455224b9f0e9b5a11d2fd84","status":"FAILED","message":"https://argo.dev.esa-maap.org/workflows/argo/exec-wf-echo-test-2023-05-05-15-35-39-wpst","progress":null}'
%% Cell type:markdown id:d3b3e0ed tags:
#### Delete Job
%% Cell type:code id:21f4db62 tags:
``` python
deleted_proc = requests.delete('{}wpst/processes/{}/jobs/{}'.format(copa_backend_url, workflow_id, job_id),
headers = {'Authorization': 'Bearer '+ oauth_token})
print(deleted_proc.content)
```
%% Output
b'{"id":"6455224b9f0e9b5a11d2fd84"}'
%% Cell type:markdown id:decb24af tags:
#### Undeploy process.
%% Cell type:code id:3ded80d5 tags:
``` python
requests.delete(copa_backend_url+'wpst/processes/{}'.format(workflow_id), headers = {'Authorization': 'Bearer '+ oauth_token})
```
%% Output
<Response [200]>
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment