mirror of
https://github.com/hoshikawa2/OCI_API_Gateway_Automation2.git
synced 2026-07-09 17:34:20 +00:00
Correção dos casos:
- Host não pode ser caminho inválido. Melhor optar por IP caso seja um mock-up ao invés de colocar um endpoint inválido pois o API Gateway testa - schema: Se o host não estiver com http:// ou https:// o serviço não estava pegando de schema. Corrigido
This commit is contained in:
Binary file not shown.
@@ -1,19 +1,19 @@
|
||||
import base64
|
||||
import json
|
||||
import io
|
||||
from fdk import response
|
||||
import oci
|
||||
import requests
|
||||
import time
|
||||
from itertools import groupby
|
||||
import yaml
|
||||
import datetime
|
||||
import base64
|
||||
import json
|
||||
import io
|
||||
from fdk import response
|
||||
import oci
|
||||
import requests
|
||||
import time
|
||||
from itertools import groupby
|
||||
import yaml
|
||||
import datetime
|
||||
|
||||
#### IDCS Routines
|
||||
#### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction
|
||||
#### https://docs.oracle.com/en/learn/migrate-api-to-api-gateway/#introduction
|
||||
#### IDCS Routines
|
||||
#### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#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"
|
||||
|
||||
auth = clientID + ":" + secretID
|
||||
@@ -31,16 +31,16 @@ def auth_idcs(token, url, clientID, secretID):
|
||||
response = requests.request("POST", url, headers=headers, data=payload)
|
||||
return response
|
||||
|
||||
#Function used to load the configurations from the config.json file
|
||||
def getOptions():
|
||||
#Function used to load the configurations from the config.json file
|
||||
def getOptions():
|
||||
fo = open("config.json", "r")
|
||||
config = fo.read()
|
||||
options = json.loads(config)
|
||||
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]
|
||||
if (len(base_path) == 0):
|
||||
base_path = strPath
|
||||
@@ -48,7 +48,7 @@ def find_base_path(strPath):
|
||||
base_path = "/" + base_path
|
||||
return base_path
|
||||
|
||||
def find_path(strPath):
|
||||
def find_path(strPath):
|
||||
base_path = strPath.split('/')
|
||||
if (len(base_path) == 0):
|
||||
return strPath
|
||||
@@ -62,10 +62,10 @@ def find_path(strPath):
|
||||
base_path = auxPath
|
||||
return auxPath
|
||||
|
||||
def removeLastSlash(path):
|
||||
def removeLastSlash(path):
|
||||
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")
|
||||
apigateway_client = oci.apigateway.DeploymentClient(config)
|
||||
listGateway = apigateway_client.list_deployments(compartment_id=compartmendId, display_name=displayName, lifecycle_state="ACTIVE")
|
||||
@@ -91,7 +91,7 @@ def creeateOrUpdateDeployment(compartmendId, displayName, validation_deployment_
|
||||
else:
|
||||
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")
|
||||
logging = oci.loggingingestion.LoggingClient(config)
|
||||
apigateway_client = oci.apigateway.DeploymentClient(config)
|
||||
@@ -289,7 +289,7 @@ def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gate
|
||||
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 = ""
|
||||
for spec in routes:
|
||||
if (find_path(spec["path"]) == path and spec["backend"]["status"] == status):
|
||||
@@ -298,7 +298,7 @@ def accMethods(routes, path, status):
|
||||
METHOD = (METHOD + " " + method).lstrip().upper()
|
||||
return METHOD
|
||||
|
||||
def accMethods_v2(routes, path, status):
|
||||
def accMethods_v2(routes, path, status):
|
||||
METHOD = ""
|
||||
for spec in routes:
|
||||
if (spec["path"] == path and spec["backend"]["status"] == status):
|
||||
@@ -307,15 +307,22 @@ def accMethods_v2(routes, path, status):
|
||||
METHOD = (METHOD + " " + method).lstrip().upper()
|
||||
return METHOD
|
||||
|
||||
def check_endpoint(endpoint):
|
||||
def check_endpoint(schemes, endpoint):
|
||||
if (schemes == ""):
|
||||
if (endpoint.find("http://") == -1 and endpoint.find("https://") == -1):
|
||||
endpoint = "https://" + endpoint
|
||||
else:
|
||||
if (endpoint.find("http://") == -1 and endpoint.find("https://") == -1):
|
||||
if (schemes.find("://") == -1):
|
||||
endpoint = schemes + "://" + endpoint
|
||||
else:
|
||||
endpoint = schemes + endpoint
|
||||
return endpoint
|
||||
|
||||
def key_func(k):
|
||||
def key_func(k):
|
||||
return k['PATH']
|
||||
|
||||
def group_by(payload):
|
||||
def group_by(payload):
|
||||
config = oci.config.from_file("config")
|
||||
logging = oci.loggingingestion.LoggingClient(config)
|
||||
payload = json.loads(payload)
|
||||
@@ -340,7 +347,7 @@ def group_by(payload):
|
||||
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
|
||||
|
||||
def verify_path(json_data_list):
|
||||
def verify_path(json_data_list):
|
||||
list_final = []
|
||||
for item in json_data_list:
|
||||
if (item["PATH"] == ""):
|
||||
@@ -373,7 +380,7 @@ def verify_path(json_data_list):
|
||||
return list_final
|
||||
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"
|
||||
config = oci.config.from_file("config")
|
||||
apigateway_client = oci.apigateway.ApiGatewayClient(config)
|
||||
@@ -412,7 +419,13 @@ def process_api_spec(api_id, compartmentId, environment, swagger, functionId, ho
|
||||
PATH_PREFIX = find_base_path(spec["path"])
|
||||
METHOD = accMethods(api_spec["routes"], PATH, status)
|
||||
else:
|
||||
fullEndpoint = check_endpoint((endPoint + removeLastSlash(fullSpec["basePath"]) + spec["path"]).replace("{", "${request.path[").replace("}", "]}"))
|
||||
schemes = ""
|
||||
try:
|
||||
schemes = fullSpec["schemes"][0]
|
||||
except:
|
||||
schemes = "https"
|
||||
|
||||
fullEndpoint = check_endpoint(schemes, (endPoint + removeLastSlash(fullSpec["basePath"]) + spec["path"]).replace("{", "${request.path[").replace("}", "]}"))
|
||||
FULL_PATH = fullSpec["basePath"] + spec["path"]
|
||||
ENDPOINT = fullEndpoint
|
||||
PATH = spec["path"]
|
||||
@@ -513,11 +526,11 @@ def process_api_spec(api_id, compartmentId, environment, swagger, functionId, ho
|
||||
source="EXAMPLE-source-Value",
|
||||
type="EXAMPLE-type-Value")]))
|
||||
|
||||
def DateEncoder(obj):
|
||||
def DateEncoder(obj):
|
||||
if isinstance(obj, datetime.datetime):
|
||||
return obj.strftime('%Y-%m-%d')
|
||||
|
||||
def is_json(swagger):
|
||||
def is_json(swagger):
|
||||
try:
|
||||
body = json.loads(swagger)
|
||||
return True
|
||||
@@ -529,14 +542,14 @@ def is_json(swagger):
|
||||
except:
|
||||
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
|
||||
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")
|
||||
logging = oci.loggingingestion.LoggingClient(config)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user