Skip to content

Commit

Permalink
fix: fixed java , python lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
anaik91 committed Aug 23, 2023
1 parent 6d17716 commit 541877d
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 248 deletions.
128 changes: 85 additions & 43 deletions tools/target-server-validator/apigee.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@


class Apigee:

def __init__(
self,
apigee_type="x",
base_url="https://apigee.googleapis.com/v1",
auth_type="oauth",
org="validate"):
self,
apigee_type="x",
base_url="https://apigee.googleapis.com/v1",
auth_type="oauth",
org="validate",
):
self.org = org
self.baseurl = f"{base_url}/organizations/{org}"
self.apigee_type = apigee_type
self.auth_type = auth_type
access_token = self.get_access_token()
self.auth_header = {
'Authorization': 'Bearer {}'.format(access_token) if self.auth_type == 'oauth' else 'Basic {}'.format(access_token) # noqa
"Authorization": "Bearer {}".format(access_token)
if self.auth_type == "oauth"
else "Basic {}".format(access_token) # noqa
}

def is_token_valid(self, token):
Expand All @@ -48,27 +50,37 @@ def is_token_valid(self, token):
return False

def get_access_token(self):
token = os.getenv('APIGEE_ACCESS_TOKEN' if self.apigee_type == 'x' else 'APIGEE_OPDK_ACCESS_TOKEN') # noqa
token = os.getenv(
"APIGEE_ACCESS_TOKEN"
if self.apigee_type == "x"
else "APIGEE_OPDK_ACCESS_TOKEN"
)
if token is not None:
if self.apigee_type == 'x':
if self.apigee_type == "x":
if self.is_token_valid(token):
return token
else:
print('please run "export APIGEE_ACCESS_TOKEN=$(gcloud auth print-access-token)" first !! ') # noqa
print(
'please run "export APIGEE_ACCESS_TOKEN=$(gcloud auth print-access-token)" first !! ' # noqa type: ignore
)
sys.exit(1)
else:
return token
else:
if self.apigee_type == 'x':
print('please run "export APIGEE_ACCESS_TOKEN=$(gcloud auth print-access-token)" first !! ') # noqa
if self.apigee_type == "x":
print(
'please run "export APIGEE_ACCESS_TOKEN=$(gcloud auth print-access-token)" first !! ' # noqa
)
else:
print('please export APIGEE_OPDK_ACCESS_TOKEN')
print("please export APIGEE_OPDK_ACCESS_TOKEN")
sys.exit(1)

def set_auth_header(self):
access_token = self.get_access_token()
self.auth_header = {
'Authorization': 'Bearer {}'.format(access_token) if self.auth_type == 'oauth' else 'Basic {}'.format(access_token) # noqa
"Authorization": "Bearer {}".format(access_token)
if self.auth_type == "oauth"
else "Basic {}".format(access_token)
}

def list_environments(self):
Expand Down Expand Up @@ -111,44 +123,53 @@ def create_api(self, api_name, proxy_bundle_path):
url = f"{self.baseurl}/apis?action=import&name={api_name}&validate=true" # noqa
proxy_bundle_name = os.path.basename(proxy_bundle_path)
files = [
('data',(proxy_bundle_name,open(proxy_bundle_path,'rb'),'application/zip')) # noqa
(
"data",
(proxy_bundle_name, open(proxy_bundle_path, "rb"), "application/zip"), # noqa
)
]
headers = self.auth_header.copy()
response = requests.request("POST", url, headers=headers, data={}, files=files) # noqa
response = requests.request(
"POST", url, headers=headers, data={}, files=files
)
if response.status_code == 200:
return True
else:
print(response.json())
return False

def get_api_revisions_deployment(self, env, api_name, api_rev): # noqa
url = url = f"{self.baseurl}/environments/{env}/apis/{api_name}/revisions/{api_rev}/deployments" # noqa
url = (
url
) = f"{self.baseurl}/environments/{env}/apis/{api_name}/revisions/{api_rev}/deployments" # noqa
headers = self.auth_header.copy()
response = requests.request("GET", url, headers=headers, data={})
if response.status_code == 200:
resp = response.json()
api_deployment_status = resp.get('state', '')
if self.apigee_type == 'x':
if api_deployment_status == 'READY':
api_deployment_status = resp.get("state", "")
if self.apigee_type == "x":
if api_deployment_status == "READY":
return True
if self.apigee_type == 'opdk':
if api_deployment_status == 'deployed':
if self.apigee_type == "opdk":
if api_deployment_status == "deployed":
return True
print(f"API {api_name} is in Status: {api_deployment_status} !") # noqa
return False
else:
return False

def deploy_api(self, env, api_name, api_rev):
url = url = f"{self.baseurl}/environments/{env}/apis/{api_name}/revisions/{api_rev}/deployments?override=true" # noqa
url = (
url
) = f"{self.baseurl}/environments/{env}/apis/{api_name}/revisions/{api_rev}/deployments?override=true" # noqa
headers = self.auth_header.copy()
response = requests.request("POST", url, headers=headers, data={})
if response.status_code == 200:
return True
else:
resp = response.json()
if 'already deployed' in resp['error']['message']:
print('Proxy {} is already Deployed'.format(api_name))
if "already deployed" in resp["error"]["message"]:
print("Proxy {} is already Deployed".format(api_name))
return True
return False

Expand All @@ -158,59 +179,80 @@ def deploy_api_bundle(self, env, api_name, proxy_bundle_path, api_rev=1): # noq
api_deployment_retry_count = 0
api_exists = False
if self.get_api(api_name):
print(f'Proxy with name {api_name} already exists in Apigee Org {self.org}') # noqa
api_exists = True
print(
f"Proxy with name {api_name} already exists in Apigee Org {self.org}" # noqa
)
api_exists = True
else:
if self.create_api(api_name, proxy_bundle_path):
print(f'Proxy has been imported with name {api_name} in Apigee Org {self.org}') # noqa
print(
f"Proxy has been imported with name {api_name} in Apigee Org {self.org}" # noqa
)
api_exists = True
else:
print(f'ERROR : Proxy {api_name} import failed !!! ')
print(f"ERROR : Proxy {api_name} import failed !!! ")
return False
if api_exists:
if self.deploy_api(env, api_name, api_rev):
print(f'Proxy with name {api_name} has been deployed to {env} in Apigee Org {self.org}') # noqa
print(
f"Proxy with name {api_name} has been deployed to {env} in Apigee Org {self.org}" # noqa
)
while api_deployment_retry_count < api_deployment_retry:
if self.get_api_revisions_deployment(env, api_name, api_rev): # noqa
print(f'Proxy {api_name} active in runtime after {api_deployment_retry_count*api_deployment_sleep} seconds ') # noqa
if self.get_api_revisions_deployment(
env, api_name, api_rev
):
print(
f"Proxy {api_name} active in runtime after {api_deployment_retry_count*api_deployment_sleep} seconds " # noqa
)
return True
else:
print(f"Checking API deployment status in {api_deployment_sleep} seconds") # noqa
print(
f"Checking API deployment status in {api_deployment_sleep} seconds" # noqa
)
sleep(api_deployment_sleep)
api_deployment_retry_count += 1
else:
print(f'ERROR : Proxy deployment to {env} in Apigee Org {self.org} Failed !!') # noqa
print(
f"ERROR : Proxy deployment to {env} in Apigee Org {self.org} Failed !!" # noqa
)
return False

def get_api_vhost(self, vhost_name, env):
if self.apigee_type == 'opdk':
if self.apigee_type == "opdk":
url = f"{self.baseurl}/environments/{env}/virtualhosts/{vhost_name}" # noqa
else:
url = f"{self.baseurl}/envgroups/{vhost_name}"
headers = self.auth_header.copy()
response = requests.request("GET", url, headers=headers)
if response.status_code == 200:
if self.apigee_type == 'opdk':
hosts = response.json()['hostAliases']
if self.apigee_type == "opdk":
hosts = response.json()["hostAliases"]
else:
hosts = response.json()['hostnames']
hosts = response.json()["hostnames"]
if len(hosts) == 0:
print(f'ERROR: Vhost/Env Group {vhost_name} contains no domains') # noqa
print(
f"ERROR: Vhost/Env Group {vhost_name} contains no domains" # noqa
)
return None
return hosts
else:
print(f'ERROR: Vhost/Env Group {vhost_name} contains no domains') # noqa
print(f"ERROR: Vhost/Env Group {vhost_name} contains no domains") # noqa
return None

def list_apis(self, api_type):
url = f"{self.baseurl}/{api_type}"
headers = self.auth_header.copy()
response = requests.get(url, headers=headers)
if response.status_code == 200:
if self.apigee_type == 'x':
if self.apigee_type == "x":
if len(response.json()) == 0:
return []
return [ p['name'] for p in response.json()['proxies' if api_type == 'apis' else 'sharedFlows']] # noqa
return [
p["name"]
for p in response.json()[
"proxies" if api_type == "apis" else "sharedFlows"
]
] # noqa
return response.json()
else:
return []
Expand All @@ -235,5 +277,5 @@ def fetch_api_revision(self, api_type, api_name, revision, export_dir): # noqa

def write_proxy_bundle(self, export_dir, file_name, data):
file_path = f"./{export_dir}/{file_name}.zip"
with open(file_path, 'wb') as fl:
with open(file_path, "wb") as fl:
shutil.copyfileobj(data, fl)
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,66 @@
import com.apigee.flow.execution.ExecutionResult;
import com.apigee.flow.execution.spi.Execution;
import com.apigee.flow.message.MessageContext;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;


/**
* A callout that checks if a particular port is open on a specified host.
*/
public class PortOpenCheck implements Execution {

private static String available(String host,int port) {
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(host, port), 1000);
return "REACHABLE";
} catch (SocketTimeoutException e) {
return "NOT_REACHABLE";
} catch (UnknownHostException e) {
return "UNKNOWN_HOST";
}
catch (IOException e) {
return "NOT_REACHABLE";
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException("You should handle this error.", e);
}
}
}
}

public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) {

try {
String host_name = messageContext.getMessage().getHeader("host_name");
String port = messageContext.getMessage().getHeader("port_number");
int port_number = Integer.parseInt(port);
String Status = available(host_name,port_number);
// messageContext.getMessage().setContent(Status);
messageContext.setVariable("REACHABLE_STATUS", Status);
return ExecutionResult.SUCCESS;

} catch (Exception e) {
return ExecutionResult.ABORT;
}
/**
* Checks if the specified host and port are available.
*
* @param host The host name or IP address to check.
* @param port The port number to check.
* @return A string indicating whether the host and port are available
*/
private static String available(final String host, final int port) {
Socket socket = new Socket();
final int sockettimeout = 1000;
try {
socket.connect(new InetSocketAddress(host, port), sockettimeout);
return "REACHABLE";
} catch (SocketTimeoutException e) {
return "NOT_REACHABLE";
} catch (UnknownHostException e) {
return "UNKNOWN_HOST";
} catch (IOException e) {
return "NOT_REACHABLE";
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException("You should handle this error.", e);
}
}
}
}

/**
* Executes the callout.
*
* @param messageContext The message context.
* @param executionContext The execution context.
* @return The execution result.
*/
public ExecutionResult execute(final MessageContext messageContext,
final ExecutionContext executionContext) {
try {
String hostname = messageContext.getMessage().getHeader("host_name");
String port = messageContext.getMessage().getHeader("port_number");
int portnumber = Integer.parseInt(port);
String status = available(hostname, portnumber);
// messageContext.getMessage().setContent(Status);
messageContext.setVariable("REACHABLE_STATUS", status);
return ExecutionResult.SUCCESS;
} catch (Exception e) {
return ExecutionResult.ABORT;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 Google LLC

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
* A callout that checks if a particular port is open on a specified host.
*
* @author anaik91
* @version .01
*/
package com.apigeesample;

0 comments on commit 541877d

Please sign in to comment.