mirror of
https://github.com/hoshikawa2/OCI_API_Gateway_Automation2.git
synced 2026-07-10 01:44:19 +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 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 @@ def auth_idcs(token, url, clientID, secretID):
|
|||||||
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 @@ def find_base_path(strPath):
|
|||||||
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 @@ def find_path(strPath):
|
|||||||
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 @@ def creeateOrUpdateDeployment(compartmendId, displayName, validation_deployment_
|
|||||||
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 @@ 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)
|
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 @@ def accMethods(routes, path, status):
|
|||||||
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,15 +307,22 @@ def accMethods_v2(routes, path, status):
|
|||||||
METHOD = (METHOD + " " + method).lstrip().upper()
|
METHOD = (METHOD + " " + method).lstrip().upper()
|
||||||
return METHOD
|
return METHOD
|
||||||
|
|
||||||
def check_endpoint(endpoint):
|
def check_endpoint(schemes, endpoint):
|
||||||
|
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
|
||||||
|
else:
|
||||||
|
if (endpoint.find("http://") == -1 and endpoint.find("https://") == -1):
|
||||||
|
if (schemes.find("://") == -1):
|
||||||
|
endpoint = schemes + "://" + endpoint
|
||||||
|
else:
|
||||||
|
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)
|
||||||
@@ -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})
|
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"] == ""):
|
||||||
@@ -373,7 +380,7 @@ def verify_path(json_data_list):
|
|||||||
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)
|
||||||
@@ -412,7 +419,13 @@ def process_api_spec(api_id, compartmentId, environment, swagger, functionId, ho
|
|||||||
PATH_PREFIX = find_base_path(spec["path"])
|
PATH_PREFIX = find_base_path(spec["path"])
|
||||||
METHOD = accMethods(api_spec["routes"], PATH, status)
|
METHOD = accMethods(api_spec["routes"], PATH, status)
|
||||||
else:
|
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"]
|
FULL_PATH = fullSpec["basePath"] + spec["path"]
|
||||||
ENDPOINT = fullEndpoint
|
ENDPOINT = fullEndpoint
|
||||||
PATH = spec["path"]
|
PATH = spec["path"]
|
||||||
@@ -513,11 +526,11 @@ def process_api_spec(api_id, compartmentId, environment, swagger, functionId, ho
|
|||||||
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
|
||||||
@@ -529,14 +542,14 @@ def is_json(swagger):
|
|||||||
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)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user