mirror of
https://github.com/hoshikawa2/OCI_API_Gateway_Automation2.git
synced 2026-03-06 10:11:01 +00:00
New Release. This version can import JSON or YAML API format (OpenAPI or Swagger)
This commit is contained in:
Binary file not shown.
@@ -6,6 +6,7 @@ import oci
|
||||
import requests
|
||||
import time
|
||||
from itertools import groupby
|
||||
import yaml
|
||||
|
||||
#### IDCS Routines
|
||||
#### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction
|
||||
@@ -475,6 +476,23 @@ def process_api_spec(api_id, compartmentId, environment, swagger, functionId, ho
|
||||
source="EXAMPLE-source-Value",
|
||||
type="EXAMPLE-type-Value")]))
|
||||
|
||||
def is_json(swagger):
|
||||
try:
|
||||
body = json.loads(swagger)
|
||||
return True
|
||||
except:
|
||||
try:
|
||||
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
|
||||
s = json.dumps(yaml_object, indent=2)
|
||||
return False
|
||||
except:
|
||||
return False
|
||||
|
||||
def convert_json(swagger):
|
||||
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
|
||||
return json.dumps(yaml_object, indent=2)
|
||||
|
||||
|
||||
###
|
||||
|
||||
def handler(ctx, data: io.BytesIO = None):
|
||||
@@ -494,13 +512,13 @@ def handler(ctx, data: io.BytesIO = None):
|
||||
body = dict(json.loads(data.getvalue().decode('utf-8')).get("data"))["body"]
|
||||
# body content
|
||||
swagger = str(body)
|
||||
body = json.loads(body)
|
||||
if (is_json(swagger)):
|
||||
body = json.loads(body)
|
||||
else:
|
||||
body = json.loads(convert_json(swagger))
|
||||
swagger = convert_json(swagger)
|
||||
|
||||
# functions context variables
|
||||
# fn config app ociapigw-app oci_region us-ashburn-1
|
||||
# fn config app ociapigw-app environment QA
|
||||
oci_region = app_context['oci_region']
|
||||
environment = app_context['environment']
|
||||
environment = "DEV"
|
||||
|
||||
# header values
|
||||
access_token = header["token"]
|
||||
@@ -547,7 +565,6 @@ def handler(ctx, data: io.BytesIO = None):
|
||||
"active": True,
|
||||
"context": {
|
||||
"environment": environment,
|
||||
"oci_region": oci_region,
|
||||
"api_id": api_id
|
||||
}})
|
||||
|
||||
|
||||
@@ -6,4 +6,4 @@ six
|
||||
PyJWT
|
||||
py3_lru_cache
|
||||
simplejson
|
||||
|
||||
PyYAML
|
||||
|
||||
@@ -4,6 +4,7 @@ import io
|
||||
from fdk import response
|
||||
import oci
|
||||
import requests
|
||||
import yaml
|
||||
|
||||
#### IDCS Routines
|
||||
#### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction
|
||||
@@ -123,7 +124,7 @@ def process_api_spec(displayName, compartmentId, environment, swagger):
|
||||
except(Exception) as ex:
|
||||
jsonData = 'error parsing json payload: ' + str(ex)
|
||||
put_logs_response = logging.put_logs(
|
||||
log_id="ocid1.log.oc1.iad.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
log_id="ocid1.log.oc1.iad.amaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaan",
|
||||
put_logs_details=oci.loggingingestion.models.PutLogsDetails(
|
||||
specversion="EXAMPLE-specversion-Value",
|
||||
log_entry_batches=[
|
||||
@@ -135,6 +136,22 @@ def process_api_spec(displayName, compartmentId, environment, swagger):
|
||||
source="EXAMPLE-source-Value",
|
||||
type="EXAMPLE-type-Value")]))
|
||||
|
||||
def is_json(swagger):
|
||||
try:
|
||||
body = json.loads(swagger)
|
||||
return True
|
||||
except:
|
||||
try:
|
||||
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
|
||||
s = json.dumps(yaml_object, indent=2)
|
||||
return False
|
||||
except:
|
||||
return False
|
||||
|
||||
def convert_json(swagger):
|
||||
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
|
||||
return json.dumps(yaml_object, indent=2)
|
||||
|
||||
###
|
||||
|
||||
def handler(ctx, data: io.BytesIO = None):
|
||||
@@ -154,9 +171,13 @@ def handler(ctx, data: io.BytesIO = None):
|
||||
body = dict(json.loads(data.getvalue().decode('utf-8')).get("data"))["body"]
|
||||
# body content
|
||||
swagger = str(body)
|
||||
body = json.loads(body)
|
||||
if (is_json(swagger)):
|
||||
body = json.loads(body)
|
||||
else:
|
||||
body = json.loads(convert_json(swagger))
|
||||
swagger = convert_json(swagger)
|
||||
|
||||
environment = "DEV" #for future development
|
||||
environment = "DEV"
|
||||
|
||||
# header values
|
||||
access_token = header["token"]
|
||||
@@ -174,17 +195,17 @@ def handler(ctx, data: io.BytesIO = None):
|
||||
except(Exception) as ex1:
|
||||
jsonData = 'error parsing json payload: ' + str(ex1)
|
||||
put_logs_response = logging.put_logs(
|
||||
log_id="ocid1.log.oc1.iad.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
put_logs_details=oci.loggingingestion.models.PutLogsDetails(
|
||||
specversion="EXAMPLE-specversion-Value",
|
||||
log_entry_batches=[
|
||||
oci.loggingingestion.models.LogEntryBatch(
|
||||
entries=[
|
||||
oci.loggingingestion.models.LogEntry(
|
||||
data="error: " + jsonData,
|
||||
id="ocid1.test.oc1..00000001.EXAMPLE-id-Value")],
|
||||
source="EXAMPLE-source-Value",
|
||||
type="EXAMPLE-type-Value")]))
|
||||
log_id="ocid1.log.oc1.iad.amaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaan",
|
||||
put_logs_details=oci.loggingingestion.models.PutLogsDetails(
|
||||
specversion="EXAMPLE-specversion-Value",
|
||||
log_entry_batches=[
|
||||
oci.loggingingestion.models.LogEntryBatch(
|
||||
entries=[
|
||||
oci.loggingingestion.models.LogEntry(
|
||||
data="error: " + jsonData,
|
||||
id="ocid1.test.oc1..00000001.EXAMPLE-id-Value")],
|
||||
source="EXAMPLE-source-Value",
|
||||
type="EXAMPLE-type-Value")]))
|
||||
|
||||
return response.Response(
|
||||
ctx,
|
||||
@@ -196,23 +217,37 @@ def handler(ctx, data: io.BytesIO = None):
|
||||
api_id = process_api_spec(displayName, compartmentId, environment, swagger)
|
||||
|
||||
rdata = json.dumps({
|
||||
"active": True,
|
||||
"context": {
|
||||
"environment": environment,
|
||||
"display_name": displayName,
|
||||
"api_id": json.dumps(api_id)
|
||||
}})
|
||||
"active": True,
|
||||
"context": {
|
||||
"environment": environment,
|
||||
"display_name": displayName,
|
||||
"api_id": json.dumps(api_id)
|
||||
}})
|
||||
|
||||
# put_logs_response = logging.put_logs(
|
||||
# log_id="ocid1.log.oc1.iad.amaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaan",
|
||||
# put_logs_details=oci.loggingingestion.models.PutLogsDetails(
|
||||
# specversion="EXAMPLE-specversion-Value",
|
||||
# log_entry_batches=[
|
||||
# oci.loggingingestion.models.LogEntryBatch(
|
||||
# entries=[
|
||||
# oci.loggingingestion.models.LogEntry(
|
||||
# data="request payload: " + json.dumps(header),
|
||||
# id="ocid1.test.oc1..00000001.EXAMPLE-id-Value-1")],
|
||||
# source="EXAMPLE-source-Value",
|
||||
# type="EXAMPLE-type-Value")]))
|
||||
|
||||
|
||||
return response.Response(
|
||||
ctx, response_data=rdata,
|
||||
status_code=200,
|
||||
headers={"Content-Type": "application/json", "data": rdata}
|
||||
ctx, response_data=rdata,
|
||||
status_code=200,
|
||||
headers={"Content-Type": "application/json", "data": rdata}
|
||||
)
|
||||
|
||||
except(Exception) as ex:
|
||||
jsonData = 'error parsing json payload: ' + str(ex)
|
||||
put_logs_response = logging.put_logs(
|
||||
log_id="ocid1.log.oc1.iad.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
log_id="ocid1.log.oc1.iad.amaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaan",
|
||||
put_logs_details=oci.loggingingestion.models.PutLogsDetails(
|
||||
specversion="EXAMPLE-specversion-Value",
|
||||
log_entry_batches=[
|
||||
|
||||
@@ -6,4 +6,4 @@ six
|
||||
PyJWT
|
||||
py3_lru_cache
|
||||
simplejson
|
||||
|
||||
PyYAML
|
||||
|
||||
Reference in New Issue
Block a user