Correção dos casos:

- Setando como path_prefix o titulo da API ocasiona erro pois o titulo contem espacos em branco
This commit is contained in:
2024-03-18 16:54:58 -03:00
parent 2349b97f77
commit 65812cf3da

View File

@@ -1,19 +1,19 @@
import base64 import base64
import json import json
import io import io
from fdk import response from fdk import response
import oci import oci
import requests import requests
import time import time
from itertools import groupby from itertools import groupby
import yaml import yaml
import datetime import datetime
#### IDCS Routines #### IDCS Routines
#### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction #### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction
#### https://docs.oracle.com/en/learn/migrate-api-to-api-gateway/#introduction #### https://docs.oracle.com/en/learn/migrate-api-to-api-gateway/#introduction
def auth_idcs(token, url, clientID, secretID): def auth_idcs(token, url, clientID, secretID):
url = url + "/oauth2/v1/introspect" url = url + "/oauth2/v1/introspect"
auth = clientID + ":" + secretID auth = clientID + ":" + secretID
@@ -31,16 +31,16 @@
response = requests.request("POST", url, headers=headers, data=payload) response = requests.request("POST", url, headers=headers, data=payload)
return response return response
#Function used to load the configurations from the config.json file #Function used to load the configurations from the config.json file
def getOptions(): def getOptions():
fo = open("config.json", "r") fo = open("config.json", "r")
config = fo.read() config = fo.read()
options = json.loads(config) options = json.loads(config)
return options return options
### OCI API Gateway Migration Routines ### OCI API Gateway Migration Routines
def find_base_path(strPath): def find_base_path(strPath):
base_path = strPath.split('/')[1] base_path = strPath.split('/')[1]
if (len(base_path) == 0): if (len(base_path) == 0):
base_path = strPath base_path = strPath
@@ -48,7 +48,7 @@
base_path = "/" + base_path base_path = "/" + base_path
return base_path return base_path
def find_path(strPath): def find_path(strPath):
base_path = strPath.split('/') base_path = strPath.split('/')
if (len(base_path) == 0): if (len(base_path) == 0):
return strPath return strPath
@@ -62,10 +62,10 @@
base_path = auxPath base_path = auxPath
return auxPath return auxPath
def removeLastSlash(path): def removeLastSlash(path):
return path.rstrip("/") return path.rstrip("/")
def creeateOrUpdateDeployment(compartmendId, displayName, validation_deployment_details, create_deployment_details, api_gateway_id): def creeateOrUpdateDeployment(compartmendId, displayName, validation_deployment_details, create_deployment_details, api_gateway_id):
config = oci.config.from_file("config") config = oci.config.from_file("config")
apigateway_client = oci.apigateway.DeploymentClient(config) apigateway_client = oci.apigateway.DeploymentClient(config)
listGateway = apigateway_client.list_deployments(compartment_id=compartmendId, display_name=displayName, lifecycle_state="ACTIVE") listGateway = apigateway_client.list_deployments(compartment_id=compartmendId, display_name=displayName, lifecycle_state="ACTIVE")
@@ -91,7 +91,7 @@
else: else:
apigateway_client.create_deployment(create_deployment_details=create_deployment_details) apigateway_client.create_deployment(create_deployment_details=create_deployment_details)
def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gateway_id, rate_limit): def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gateway_id, rate_limit):
config = oci.config.from_file("config") config = oci.config.from_file("config")
logging = oci.loggingingestion.LoggingClient(config) logging = oci.loggingingestion.LoggingClient(config)
apigateway_client = oci.apigateway.DeploymentClient(config) apigateway_client = oci.apigateway.DeploymentClient(config)
@@ -289,7 +289,7 @@
creeateOrUpdateDeployment(compartmendId=compartmentId, displayName=displayName, validation_deployment_details=validation_deployment_details, create_deployment_details=create_deployment_details, api_gateway_id=api_gateway_id) creeateOrUpdateDeployment(compartmendId=compartmentId, displayName=displayName, validation_deployment_details=validation_deployment_details, create_deployment_details=create_deployment_details, api_gateway_id=api_gateway_id)
def accMethods(routes, path, status): def accMethods(routes, path, status):
METHOD = "" METHOD = ""
for spec in routes: for spec in routes:
if (find_path(spec["path"]) == path and spec["backend"]["status"] == status): if (find_path(spec["path"]) == path and spec["backend"]["status"] == status):
@@ -298,7 +298,7 @@
METHOD = (METHOD + " " + method).lstrip().upper() METHOD = (METHOD + " " + method).lstrip().upper()
return METHOD return METHOD
def accMethods_v2(routes, path, status): def accMethods_v2(routes, path, status):
METHOD = "" METHOD = ""
for spec in routes: for spec in routes:
if (spec["path"] == path and spec["backend"]["status"] == status): if (spec["path"] == path and spec["backend"]["status"] == status):
@@ -307,7 +307,7 @@
METHOD = (METHOD + " " + method).lstrip().upper() METHOD = (METHOD + " " + method).lstrip().upper()
return METHOD return METHOD
def check_endpoint(schemes, endpoint): def check_endpoint(schemes, endpoint):
if (schemes == ""): if (schemes == ""):
if (endpoint.find("http://") == -1 and endpoint.find("https://") == -1): if (endpoint.find("http://") == -1 and endpoint.find("https://") == -1):
endpoint = "https://" + endpoint endpoint = "https://" + endpoint
@@ -319,10 +319,10 @@
endpoint = schemes + endpoint endpoint = schemes + endpoint
return endpoint return endpoint
def key_func(k): def key_func(k):
return k['PATH'] return k['PATH']
def group_by(payload): def group_by(payload):
config = oci.config.from_file("config") config = oci.config.from_file("config")
logging = oci.loggingingestion.LoggingClient(config) logging = oci.loggingingestion.LoggingClient(config)
payload = json.loads(payload) payload = json.loads(payload)
@@ -347,7 +347,7 @@
result_payload.append({"API_NAME": API_NAME, "TYPE": TYPE, "ENVIRONMENT": ENVIRONMENT, "PATH_PREFIX": PATH_PREFIX, "PATH": PATH, "ENDPOINT": ENDPOINT, "METHOD": method_list, "SCHEMA_BODY_VALIDATION": SCHEMA_BODY_VALIDATION}) result_payload.append({"API_NAME": API_NAME, "TYPE": TYPE, "ENVIRONMENT": ENVIRONMENT, "PATH_PREFIX": PATH_PREFIX, "PATH": PATH, "ENDPOINT": ENDPOINT, "METHOD": method_list, "SCHEMA_BODY_VALIDATION": SCHEMA_BODY_VALIDATION})
return result_payload return result_payload
def verify_path(json_data_list): def verify_path(json_data_list):
list_final = [] list_final = []
for item in json_data_list: for item in json_data_list:
if (item["PATH"] == ""): if (item["PATH"] == ""):
@@ -380,7 +380,7 @@
return list_final return list_final
return json_data_list return json_data_list
def process_api_spec(api_id, compartmentId, environment, swagger, functionId, host, api_gateway_id, rate_limit): def process_api_spec(api_id, compartmentId, environment, swagger, functionId, host, api_gateway_id, rate_limit):
type = "REST" type = "REST"
config = oci.config.from_file("config") config = oci.config.from_file("config")
apigateway_client = oci.apigateway.ApiGatewayClient(config) apigateway_client = oci.apigateway.ApiGatewayClient(config)
@@ -526,11 +526,11 @@
source="EXAMPLE-source-Value", source="EXAMPLE-source-Value",
type="EXAMPLE-type-Value")])) type="EXAMPLE-type-Value")]))
def DateEncoder(obj): def DateEncoder(obj):
if isinstance(obj, datetime.datetime): if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%d') return obj.strftime('%Y-%m-%d')
def is_json(swagger): def is_json(swagger):
try: try:
body = json.loads(swagger) body = json.loads(swagger)
return True return True
@@ -542,14 +542,14 @@
except: except:
return False return False
def convert_json(swagger): def convert_json(swagger):
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
return json.dumps(yaml_object, indent=2, default=DateEncoder) return json.dumps(yaml_object, indent=2, default=DateEncoder)
### ###
def handler(ctx, data: io.BytesIO = None): def handler(ctx, data: io.BytesIO = None):
config = oci.config.from_file("config") config = oci.config.from_file("config")
logging = oci.loggingingestion.LoggingClient(config) logging = oci.loggingingestion.LoggingClient(config)