From 19c93c1a30d56213139f8d908ef37814bb934fcc Mon Sep 17 00:00:00 2001 From: jkallem Date: Tue, 6 Aug 2024 13:06:05 -0700 Subject: [PATCH] Adding Examples for Python SDK to create FCR and FCR based Connections --- examples/services/fabricv4/__init__.py | 1 + .../fabricv4/cloud_router/__init__.py | 1 + .../fabricv4/cloud_router/cloud_router.py | 93 +++++++++ .../cloud_router/cloud_router_management.py | 123 ++++++++++++ .../services/fabricv4/connections/__init__.py | 1 + .../connections/connection_management.py | 115 +++++++++++ .../connections/fcr_2_aws_connection.py | 180 ++++++++++++++++++ .../connections/fcr_2_azure_connection.py | 141 ++++++++++++++ .../connections/fcr_2_port_connection.py | 168 ++++++++++++++++ examples/services/fabricv4/oauth2/__init__.py | 1 + .../oauth2/configure_client_credentials.py | 76 ++++++++ examples/services/fabricv4/utils/utils.py | 35 ++++ 12 files changed, 935 insertions(+) create mode 100644 examples/services/fabricv4/__init__.py create mode 100644 examples/services/fabricv4/cloud_router/__init__.py create mode 100644 examples/services/fabricv4/cloud_router/cloud_router.py create mode 100644 examples/services/fabricv4/cloud_router/cloud_router_management.py create mode 100644 examples/services/fabricv4/connections/__init__.py create mode 100644 examples/services/fabricv4/connections/connection_management.py create mode 100644 examples/services/fabricv4/connections/fcr_2_aws_connection.py create mode 100644 examples/services/fabricv4/connections/fcr_2_azure_connection.py create mode 100644 examples/services/fabricv4/connections/fcr_2_port_connection.py create mode 100644 examples/services/fabricv4/oauth2/__init__.py create mode 100644 examples/services/fabricv4/oauth2/configure_client_credentials.py create mode 100644 examples/services/fabricv4/utils/utils.py diff --git a/examples/services/fabricv4/__init__.py b/examples/services/fabricv4/__init__.py new file mode 100644 index 00000000..b66116e8 --- /dev/null +++ b/examples/services/fabricv4/__init__.py @@ -0,0 +1 @@ +__all__ = ['cloud_router','connections','utils','oauth2'] \ No newline at end of file diff --git a/examples/services/fabricv4/cloud_router/__init__.py b/examples/services/fabricv4/cloud_router/__init__.py new file mode 100644 index 00000000..03ceb237 --- /dev/null +++ b/examples/services/fabricv4/cloud_router/__init__.py @@ -0,0 +1 @@ +__all__ = ['cloud_router','cloud_router_management'] \ No newline at end of file diff --git a/examples/services/fabricv4/cloud_router/cloud_router.py b/examples/services/fabricv4/cloud_router/cloud_router.py new file mode 100644 index 00000000..00855498 --- /dev/null +++ b/examples/services/fabricv4/cloud_router/cloud_router.py @@ -0,0 +1,93 @@ +from examples.services.fabricv4.utils import utils +from equinix.services import fabricv4 as module +import cloud_router_management + + +if __name__ == "__main__": + + """ + Create a Fabric Cloud Router (FCR) using the Equinix Fabric API. + + This method sends a request to create a new Fabric Cloud Router with specified configurations + such as router type, name, location, package, and associated project and account details. + Notifications are also configured to receive updates regarding the router. + + Construct a `CloudRouterPostRequest` object using the `module.CloudRouterPostRequest` class. + This object contains the details required to create the Fabric Cloud Router. + + Notes: + - Ensure that the necessary modules and classes (e.g., `CloudRouterPostRequest`) are imported + and accessible before invoking this method. + - The values provided in the request object are hardcoded for this example; modify them according + to your requirements. + - This method only constructs and logs the request object; it does not send the request to the API. + You may need to call an appropriate method or function to execute the API call. + """ + + utils.pr_yellow('\nCreate Fabric Cloud Router') + fcr_request = module.CloudRouterPostRequest( + type="XF_ROUTER", + name="Python_FCR", + location={"metro_code": "SV"}, + package={"code": "STANDARD"}, + order={"purchase_order_number": "1-14535"}, + project={"project_id": "123456877663"}, + account={"account_number": 123456}, + notifications=[ + { + "type": "ALL", + "emails": [ + "test@test.com" + ] + } + ] + ) + fcr_uuid = cloud_router_management.create_fcr(fcr_request) + + """ + Retrieve the details of a Fabric Cloud Router using its UUID. + + This method fetches and returns the detailed information associated with a specific + Fabric Cloud Router, identified by its UUID. It interacts with the Equinix Fabric API + to retrieve the cloud router's data, such as its configuration, status, and related + connections. + """ + + utils.pr_yellow('\nGet Fabric Cloud Router Details by UUID') + cloud_router_management.get_fcr_uuid(fcr_uuid) + + """ + Updates the name of a Fabric Cloud Router (FCR) using its UUID. + + This method performs an update operation on a specified Fabric Cloud Router by replacing + the current name with a new one. It leverages the Equinix Fabric API to execute this change. + + Notes: + ----- + - Ensure that the provided UUID corresponds to an existing Fabric Cloud Router. + - The `op` field in `CloudRouterChangeOperation` is set to "replace", which indicates that the current name + of the router will be replaced with the new name. + - The `path` field is set to "/name", which specifies that the operation targets the name attribute of the router. + - The `value` field is set to the new name provided by the user. + """ + + update_name = [module.CloudRouterChangeOperation( + op="replace", + path="/name", + value="panthers-test-updated1" + )] + cloud_router_management.update_fcr_by_uuid(fcr_uuid,update_name) + + """ + Deletes a Fabric Cloud Router (FCR) with the specified UUID. + + This method handles the deletion of a Fabric Cloud Router by calling the + relevant function in the `cloud_router_management` module. It first prints + a message to the console indicating the start of the deletion process, + using a yellow color for emphasis. After that, it proceeds to delete the + specified Fabric Cloud Router. + + """ + utils.pr_yellow('\nDelete Fabric Cloud Router') + cloud_router_management.delete_fcr(fcr_uuid) + diff --git a/examples/services/fabricv4/cloud_router/cloud_router_management.py b/examples/services/fabricv4/cloud_router/cloud_router_management.py new file mode 100644 index 00000000..34b92a99 --- /dev/null +++ b/examples/services/fabricv4/cloud_router/cloud_router_management.py @@ -0,0 +1,123 @@ +import json +import time + +from icecream import ic +from examples.services.fabricv4.utils import utils +from examples.services.fabricv4.oauth2 import configure_client_credentials +from examples.services import fabricv4 +from equinix.services import fabricv4 + +""" + Create a Fabric Cloud Router (FCR) using the provided request payload. + + Args: + fcr_request: An instance of the request payload object for creating a Fabric Cloud Router. + + Returns: + str: The UUID of the created Fabric Cloud Router. + + Steps: + 1. Print the FCR request payload. + 2. Initialize the Equinix Fabric client. + 3. Call the create_cloud_router API method with the request payload. + 4. Parse and format the API response into JSON. + 5. Print the creation response and the UUID of the created FCR. + 6. Return the UUID of the created FCR. +""" + + +def create_fcr(fcr_request): + utils.pr_purple('\nCreate FCR request payload:\n') + ic(fcr_request.to_json()) + client = configure_client_credentials.get_equinix_fabric_client() + cloudrouterapicall = fabricv4.CloudRoutersApi(client) + response = cloudrouterapicall.create_cloud_router(fcr_request).to_json() + json_resp = json.loads(response) + json_formatted_str = json.dumps(json_resp, sort_keys=True, indent=4) + time.sleep(15) + utils.pr_cyan('\nCloud router creation response\n') + ic(json_formatted_str) + utils.pr_cyan('\nconnection_uuid = ' + json_resp['uuid']) + return json_resp['uuid'] + + +""" + Retrieve the details of an existing Fabric Cloud Router (FCR) by its UUID. + + Args: + fcr_uuid (str): The UUID of the Fabric Cloud Router to retrieve. + + Returns: + None: This method prints the details of the FCR to the console. + + Steps: + 1. Initialize the Equinix Fabric client. + 2. Call the get_cloud_router_by_uuid API method with the UUID. + 3. Parse and format the API response into JSON. + 4. Print the response with the FCR details. +""" + + +def get_fcr_uuid(fcr_uuid): + client = configure_client_credentials.get_equinix_fabric_client() + cloudrouterapicall = fabricv4.CloudRoutersApi(client) + response = cloudrouterapicall.get_cloud_router_by_uuid(fcr_uuid).to_json() + json_resp = json.loads(response) + json_formatted_str = json.dumps(json_resp, sort_keys=True, indent=4) + time.sleep(15) + ic('\nget FCR response = ', json_formatted_str) + ic('\nFCR Get Successfully\n') + + +""" + Update an existing Fabric Cloud Router (FCR) using the provided UUID and update payload. + + Args: + fcr_uuid (str): The UUID of the Fabric Cloud Router to update. + update_payload: An instance of the update payload object containing the updates for the FCR. + + Returns: + None: This method prints the update response to the console. + + Steps: + 1. Initialize the Equinix Fabric client. + 2. Call the update_cloud_router_by_uuid API method with the UUID and update payload. + 3. Parse and format the API response into JSON. + 4. Print the response with the updated FCR details. +""" + + +def update_fcr_by_uuid(fcr_uuid, update_payload): + client = configure_client_credentials.get_equinix_fabric_client() + cloudrouterapicall = fabricv4.CloudRoutersApi(client) + response = cloudrouterapicall.update_cloud_router_by_uuid(fcr_uuid, update_payload).to_json() + json_resp = json.loads(response) + json_formatted_str = json.dumps(json_resp, sort_keys=True, indent=4) + time.sleep(15) + ic('\nupdate FCR response = ', json_formatted_str) + ic('\nFCR updated Successfully\n') + + +""" + Delete an existing Fabric Cloud Router (FCR) using the provided UUID. + + Args: + fcr_uuid (str): The UUID of the Fabric Cloud Router to delete. + + Returns: + None: This method prints the deletion response to the console. + + Steps: + 1. Initialize the Equinix Fabric client. + 2. Call the delete_cloud_router_by_uuid API method with the UUID. + 3. Print the deletion response. +""" + + +def delete_fcr(fcr_uuid): + client = configure_client_credentials.get_equinix_fabric_client() + cloudrouterapicall = fabricv4.CloudRoutersApi(client) + response = cloudrouterapicall.delete_cloud_router_by_uuid(fcr_uuid) + time.sleep(15) + ic('\ndelete FCR response = ', response) + ic('\nFCR Deleted Successfully\n') diff --git a/examples/services/fabricv4/connections/__init__.py b/examples/services/fabricv4/connections/__init__.py new file mode 100644 index 00000000..c3ea4f3a --- /dev/null +++ b/examples/services/fabricv4/connections/__init__.py @@ -0,0 +1 @@ +__all__ = ['connection_management','fcr_2_aws_connection','fcr_2_port_connection','fcr_2_azure_connection'] \ No newline at end of file diff --git a/examples/services/fabricv4/connections/connection_management.py b/examples/services/fabricv4/connections/connection_management.py new file mode 100644 index 00000000..52393bcf --- /dev/null +++ b/examples/services/fabricv4/connections/connection_management.py @@ -0,0 +1,115 @@ +import json +import time + + +from icecream import ic +from examples.services.fabricv4.utils import utils +from examples.services.fabricv4.oauth2 import configure_client_credentials +from examples.services import fabricv4 +from equinix.services import fabricv4 + + +def create_fcr_connection(fcr_uuid, fcr2colo_request): + """ + Create a connection for a Fabric Cloud Router (FCR) within the Equinix Fabric environment. + + This method handles the process of establishing a connection between a Fabric Cloud Router (FCR) and a + colocation or another FCR within the Equinix Fabric. It generates the connection request payload, + sends the request to the Equinix Fabric API, and returns the unique identifier (UUID) of the created connection. + + Returns: + str: The UUID of the newly created connection, which can be used for further operations like querying the connection + status, updating, or deleting the connection. + + Example Usage: + fcr_uuid = "1234-abcd-5678-efgh" + fcr2colo_request = FCRConnectionRequest(...) # An instance of a request object with necessary attributes. + connection_uuid = create_fcr_connection(fcr_uuid, fcr2colo_request) + print(f"Created FCR Connection UUID: {connection_uuid}") + """ + utils.pr_purple('\nFCR Connection Request Payload:\n') + ic(fcr2colo_request.to_json()) + client = configure_client_credentials.get_equinix_fabric_client() + connections = fabricv4.ConnectionsApi(client) + response = dict(connections.create_connection(fcr2colo_request)) + pretty_json = json.dumps(response, indent=4, default=utils.custom_serializer) + time.sleep(20) + utils.pr_cyan('\nFCR Connection response\n') + ic(pretty_json) + utils.pr_cyan('\nfcr_conn_response = '+response['uuid']) + return response['uuid'] + + +def configure_routing_protocol(connection_uuid,routing_protocol_request): + """ + Configures a routing protocol for a specified connection in the Equinix Fabric. + + This method sets up a routing protocol for the given connection UUID using the + specified routing protocol request. The method leverages the Equinix Fabric API + to create the routing protocol associated with the connection. The response from + the API is formatted and printed for verification. + + This would configure a BGP routing protocol for the specified connection, using + the provided ASNs and authentication key, and then print the response from the + Equinix Fabric API. + """ + rp_type = fabricv4.RoutingProtocolBase(routing_protocol_request) + client = configure_client_credentials.get_equinix_fabric_client() + routing_protocol = fabricv4.RoutingProtocolsApi(client) + response = dict(routing_protocol.create_connection_routing_protocol(connection_uuid, rp_type)) + json_formatted_str = json.dumps(response, indent=4, default=utils.custom_serializer) + time.sleep(15) + ic('\nconfigure routing protocol = ', json_formatted_str) + ic('\nrouting protocol configured successful\n') + + +def get_connection_details_by_uuid(fcr_uuid): + """ + Retrieve connection details using the unique identifier (UUID) of a Fabric Cloud Router (FCR) connection. + + This method interacts with the Equinix Fabric API to fetch detailed information about a specific connection + identified by its UUID. The response is then formatted into a JSON string for easier readability and logged + for debugging purposes. + """ + client = configure_client_credentials.get_equinix_fabric_client() + cloudrouterapicall = fabricv4.ConnectionsApi(client) + response = dict(cloudrouterapicall.get_connection_by_uuid(fcr_uuid)) + json_formatted_str = json.dumps(response, indent=4, default=utils.custom_serializer) + time.sleep(15) + ic('\nget connection details response = ', json_formatted_str) + ic('\ngot connection details successful\n') + + +def update_connection_details_by_uuid(connection_uuid,update_payload): + """ + Updates the details of an existing connection in Equinix Fabric using its UUID. + + This method sends an update request to the Equinix Fabric API to modify the details of a + connection specified by its UUID. The updated details are provided through the update_payload + parameter. After the update, the method formats the response into a JSON string and logs + the updated connection details. + """ + client = configure_client_credentials.get_equinix_fabric_client() + cloudrouterapicall = fabricv4.ConnectionsApi(client) + response = dict(cloudrouterapicall.update_connection_by_uuid(connection_uuid,update_payload)) + json_formatted_str = json.dumps(response, indent=4, default=utils.custom_serializer) + time.sleep(15) + ic('\nupdate connection details = ', json_formatted_str) + ic('\nconnections details updated successful\n') + + +def delete_connection(con_uuid): + """ + Deletes a connection from the Equinix Fabric using the connection UUID. + + This method interacts with the Equinix Fabric API to delete a specified connection. + The method retrieves a configured client for the Equinix Fabric, utilizes the + ConnectionsApi to perform the deletion, and then logs the response. + """ + client = configure_client_credentials.get_equinix_fabric_client() + connections = fabricv4.ConnectionsApi(client) + response = connections.delete_connection_by_uuid_with_http_info(con_uuid).json + pretty_json = json.dumps(response, indent=4, default=utils.custom_serializer) + time.sleep(15) + ic('\ndelete connection response = ', pretty_json) + ic('\nConnection Deleted Successfully\n') diff --git a/examples/services/fabricv4/connections/fcr_2_aws_connection.py b/examples/services/fabricv4/connections/fcr_2_aws_connection.py new file mode 100644 index 00000000..28245ae2 --- /dev/null +++ b/examples/services/fabricv4/connections/fcr_2_aws_connection.py @@ -0,0 +1,180 @@ +import time +import os + +from examples.services.fabricv4.utils import utils +from equinix.services import fabricv4 as module +from examples.services.fabricv4.cloud_router import cloud_router_management +from examples.services.fabricv4.connections import connection_management + +if __name__ == "__main__": + """ + Create a Fabric Cloud Router (FCR) using the Equinix Fabric API. + + This method sends a request to create a new Fabric Cloud Router with specified configurations + such as router type, name, location, package, and associated project and account details. + Notifications are also configured to receive updates regarding the router. + + Construct a `CloudRouterPostRequest` object using the `module.CloudRouterPostRequest` class. + This object contains the details required to create the Fabric Cloud Router. + """ + + utils.pr_yellow('\nCreate Fabric Cloud Router') + fcr_request = module.CloudRouterPostRequest( + type="XF_ROUTER", + name="fcrtoawsfcr_01", + location={"metro_code": "SV"}, + package={"code": "STANDARD"}, + order={"purchase_order_number": "1-1453534"}, + project={"project_id": "xxxx22-xxxx-xxxx-xxxx"}, + account={"account_number": 123456}, + notifications=[ + { + "type": "ALL", + "emails": [ + "test@test.com" + ] + } + ] + ) + fcr_uuid = cloud_router_management.create_fcr(fcr_request) + + """ + Create a Fabric Cloud Router (FCR) to AWS Connection. + + This method creates a connection from an Equinix Fabric Cloud Router (FCR) to an AWS service provider using + the Equinix API. The connection is established by specifying various parameters such as connection type, + bandwidth, redundancy priority, and the details of both the A-side (Cloud Router) and Z-side (AWS service + provider). + """ + utils.pr_yellow('\nCreate Fabric Cloud Router to AWS Connection') + fcr2aws_request = module.ConnectionPostRequest( + type="IP_VC", + name="fcr2aws_conn_python_01", + bandwidth=50, + redundancy={ + "priority": "PRIMARY" + }, + a_side={ + "accessPoint": { + "type": "CLOUD_ROUTER", + "router": { + "uuid": fcr_uuid + } + } + }, + z_side={ + "accessPoint": { + "type": "SP", + "profile": { + "type": "L2_PROFILE", + "uuid": "3435fefrr-4534ffvrt-45345dvff-cvvvv" + }, + "location": { + "metroCode": "SV" + }, + "sellerRegion": "us-west-1", + "authenticationKey": os.getenv("AWS_AUTH_KEY") + } + }, + project={"project_id": "xxxx22-xxxx-xxxx-xxxx"}, + notifications=[ + { + "type": "ALL", + "emails": [ + "test@test.com" + ] + } + ] + ) + fcr2aws = connection_management.create_fcr_connection(fcr_uuid, fcr2aws_request) + + """Configures a routing protocol for a Fabric Cloud Router (FCR) to AWS connection identified by the provided + connection UUID. + + This method is designed to configure the routing protocol details for an existing connection between a Fabric + Cloud Router and AWS. It uses the `connection_uuid` to identify the connection and applies the specified + routing protocol settings. + + The routing protocol configuration includes the following details: + - Protocol Type: DIRECT + - Direct IPv4 Configuration: Specifies the Equinix Interface IP (equinixIfaceIp) with the value '99.65.179.45/30'. + """ + utils.pr_yellow('\nConfigure Routing Protocol Detail by UUID') + routing_protocol_request = module.RoutingProtocolDirectType( + type="DIRECT", + directIpv4={ + "equinixIfaceIp": 'ip' + } + ) + connection_management.configure_routing_protocol(fcr2aws, routing_protocol_request) + + """ + Accepts an AWS connection from the Equinix Fabric using the AWS_ACCESS_KEY and AWS_SECRET_KEY. + + This method updates the connection details of an existing AWS connection in the Equinix Fabric by using the + provided AWS credentials. It performs the following steps: + + 1. Displays a message indicating that the name details will be updated using the Connection UUID. + 2. Constructs a `ConnectionChangeOperation` object that specifies the operation to add AWS credentials to the + connection. + 3. The `ConnectionChangeOperation` object is populated with AWS_ACCESS_KEY and AWS_SECRET_KEY fetched from the + environment variables. + + Environment Variables: + - AWS_ACCESS_KEY: The AWS access key required to authenticate the connection. + - AWS_SECRET_KEY: The AWS secret key required to authenticate the connection. + """ + utils.pr_yellow('\nAccepting AWS Connection using AWS_ACCESS_KEY and AWS_SECRET_KEY') + aws_accept_connection = [module.ConnectionChangeOperation( + op="add", + path="", + value={ + "additionalInfo": [ + { + "key": "accessKey", + "value": os.getenv("AWS_ACCESS_KEY") + }, + { + "key": "secretKey", + "value": os.getenv("AWS_SECRET_KEY") + } + ] + } + ) + ] + connection_management.update_connection_details_by_uuid(fcr2aws, aws_accept_connection) + + """ + Retrieves the connection details for a Fabric Cloud Router (FCR) to AWS connection by its UUID. + This method is primarily used to fetch detailed information about a specific connection between + a Fabric Cloud Router and AWS, using the connection's unique identifier (UUID). + """ + utils.pr_yellow('\nGet FCR2AWS Connection Details by UUID') + time.sleep(120) + connection_management.get_connection_details_by_uuid(fcr2aws) + + """ + Deletes a Fabric Cloud Router (FCR) to AWS connection. + + This function performs the following steps: + 1. Prints a message indicating that the deletion of the Fabric Cloud Router + to AWS connection is about to begin. The message is printed in yellow for visibility. + 2. Calls the `delete_connection` method from the `connection_management` module + to delete the specified Fabric Cloud Router to AWS connection. + """ + utils.pr_yellow('\nDelete Fabric Cloud Router to AWS Connection') + time.sleep(20) + connection_management.delete_connection(fcr2aws) + + """ + Deletes a Fabric Cloud Router (FCR) with the specified UUID. + + This method handles the deletion of a Fabric Cloud Router by calling the + relevant function in the `cloud_router_management` module. It first prints + a message to the console indicating the start of the deletion process, + using a yellow color for emphasis. After that, it proceeds to delete the + specified Fabric Cloud Router. + """ + utils.pr_yellow('\nDelete Fabric Cloud Router') + time.sleep(20) + cloud_router_management.delete_fcr(fcr_uuid) diff --git a/examples/services/fabricv4/connections/fcr_2_azure_connection.py b/examples/services/fabricv4/connections/fcr_2_azure_connection.py new file mode 100644 index 00000000..ff0d74bc --- /dev/null +++ b/examples/services/fabricv4/connections/fcr_2_azure_connection.py @@ -0,0 +1,141 @@ +import time +import os + +from examples.services.fabricv4.utils import utils +from equinix.services import fabricv4 as module +from examples.services.fabricv4.cloud_router import cloud_router_management +from examples.services.fabricv4.connections import connection_management + +if __name__ == "__main__": + """ + Create a Fabric Cloud Router (FCR) using the Equinix Fabric API. + + This method sends a request to create a new Fabric Cloud Router with specified configurations + such as router type, name, location, package, and associated project and account details. + Notifications are also configured to receive updates regarding the router. + + Construct a `CloudRouterPostRequest` object using the `module.CloudRouterPostRequest` class. + This object contains the details required to create the Fabric Cloud Router. + """ + utils.pr_yellow('\nCreate Fabric Cloud Router') + fcr_request = module.CloudRouterPostRequest( + type="XF_ROUTER", + name="fcrtoazurefcr_01", + location={"metro_code": "DC"}, + package={"code": "STANDARD"}, + order={"purchase_order_number": "1-1453534"}, + project={"project_id": "12354863434345"}, + account={"account_number": 123456}, + notifications=[ + { + "type": "ALL", + "emails": [ + "test@test.com" + ] + } + ] + ) + fcr_uuid = cloud_router_management.create_fcr(fcr_request) + + """ + Create a Fabric Cloud Router (FCR) to Azure Connection. + + This method creates a connection from an Equinix Fabric Cloud Router (FCR) to an Azure service provider using + the Equinix API. The connection is established by specifying various parameters such as connection type, + bandwidth, redundancy priority, and the details of both the A-side (Cloud Router) and Z-side (Azure service + provider). + """ + utils.pr_yellow('\nCreate Fabric Cloud Router to Azure Connection') + fcr2azure_request = module.ConnectionPostRequest( + type="IP_VC", + name="fcr2azure_conn_python_01", + bandwidth=50, + aSide={ + "accessPoint": { + "type": "CLOUD_ROUTER", + "router": { + "uuid": fcr_uuid + } + } + }, + zSide={ + "accessPoint": { + "type": "SP", + "profile": { + "type": "L2_PROFILE", + "uuid": "xxxx22-xxxx-xxxx-xxxx" + }, + "authenticationKey": os.getenv("AZURE_AUTH_KEY"), + "location": { + "metroCode": "DC" + }, + "peeringType": "PRIVATE" + } + }, + order={"purchaseOrderNumber": "1-129105284100"}, + project={"projectId": "124345345345345"}, + notifications=[ + { + "type": "ALL", + "emails": [ + "panthersfcr@test.com" + ] + } + ] + ) + fcr2azure = connection_management.create_fcr_connection(fcr_uuid, fcr2azure_request) + + """Configures a routing protocol for a Fabric Cloud Router (FCR) to Azure connection identified by the provided + connection UUID. + + This method is designed to configure the routing protocol details for an existing connection between a Fabric + Cloud Router and Azure. It uses the `connection_uuid` to identify the connection and applies the specified + routing protocol settings. + + The routing protocol configuration includes the following details: + - Protocol Type: DIRECT + - Direct IPv4 Configuration: Specifies the Equinix Interface IP (equinixIfaceIp) with the value '99.65.179.45/30'. + """ + utils.pr_yellow('\nConfigure Routing Protocol Detail by UUID') + routing_protocol_request = module.RoutingProtocolDirectType( + type="DIRECT", + directIpv4={ + "equinixIfaceIp": 'ip' + } + ) + connection_management.configure_routing_protocol(fcr2azure, routing_protocol_request) + + """ + Retrieves the connection details for a Fabric Cloud Router (FCR) to Azure connection by its UUID. + This method is primarily used to fetch detailed information about a specific connection between + a Fabric Cloud Router and Azure, using the connection's unique identifier (UUID). + """ + utils.pr_yellow('\nGet FCR2Azure Connection Details by UUID') + time.sleep(120) + connection_management.get_connection_details_by_uuid(fcr2azure) + + """ + Deletes a Fabric Cloud Router (FCR) to Azure connection. + + This function performs the following steps: + 1. Prints a message indicating that the deletion of the Fabric Cloud Router + to Azure connection is about to begin. The message is printed in yellow for visibility. + 2. Calls the `delete_connection` method from the `connection_management` module + to delete the specified Fabric Cloud Router to Azure connection. + """ + utils.pr_yellow('\nDelete Fabric Cloud Router to Azure Connection') + time.sleep(20) + connection_management.delete_connection(fcr2azure) + + """ + Deletes a Fabric Cloud Router (FCR) with the specified UUID. + + This method handles the deletion of a Fabric Cloud Router by calling the + relevant function in the `cloud_router_management` module. It first prints + a message to the console indicating the start of the deletion process, + using a yellow color for emphasis. After that, it proceeds to delete the + specified Fabric Cloud Router. + """ + utils.pr_yellow('\nDelete Fabric Cloud Router') + time.sleep(20) + cloud_router_management.delete_fcr(fcr_uuid) diff --git a/examples/services/fabricv4/connections/fcr_2_port_connection.py b/examples/services/fabricv4/connections/fcr_2_port_connection.py new file mode 100644 index 00000000..08071d2e --- /dev/null +++ b/examples/services/fabricv4/connections/fcr_2_port_connection.py @@ -0,0 +1,168 @@ +import time + +from examples.services.fabricv4.utils import utils +from equinix.services import fabricv4 as module +from examples.services.fabricv4.cloud_router import cloud_router_management +from examples.services.fabricv4.connections import connection_management + +if __name__ == "__main__": + """ + Create a Fabric Cloud Router (FCR) using the Equinix Fabric API. + + This method sends a request to create a new Fabric Cloud Router with specified configurations + such as router type, name, location, package, and associated project and account details. + Notifications are also configured to receive updates regarding the router. + + Construct a `CloudRouterPostRequest` object using the `module.CloudRouterPostRequest` class. + This object contains the details required to create the Fabric Cloud Router. + """ + utils.pr_yellow('\nCreate Fabric Cloud Router') + fcr_request = module.CloudRouterPostRequest( + type="XF_ROUTER", + name="fcrtoportfcr_01", + location={"metro_code": "SV"}, + package={"code": "STANDARD"}, + order={"purchase_order_number": "1-1453534"}, + project={"project_id": "1232432542545435"}, + account={"account_number": 123456}, + notifications=[ + { + "type": "ALL", + "emails": [ + "test@test.com" + ] + } + ] + ) + fcr_uuid = cloud_router_management.create_fcr(fcr_request) + + """ + Create a Fabric Cloud Router (FCR) to Colo Port Connection. + + This method creates a connection from an Equinix Fabric Cloud Router (FCR) to an Colo Equinix Port using + the Equinix API. The connection is established by specifying various parameters such as connection type, + bandwidth, redundancy priority, and the details of both the A-side (Cloud Router) and Z-side (Equinix Port). + """ + utils.pr_yellow('\nCreate Fabric Cloud Router to Colo Connection') + fcr2colo_request = module.ConnectionPostRequest( + type="IP_VC", + name="fcr2port_conn_python_01", + bandwidth=10, + a_side={ + "accessPoint": { + "type": "CLOUD_ROUTER", + "router": { + "uuid": fcr_uuid + } + } + }, + z_side={ + "accessPoint": { + "type": "COLO", + "port": { + "uuid": "xxxx22-xxxx-xxxx-xxxx" + }, + "linkProtocol": { + "type": "DOT1Q", + "vlanTag": 3200 + } + } + }, + project={"project_id": "1232432542545435"}, + notifications=[ + { + "type": "ALL", + "emails": [ + "test@test.com" + ] + } + ] + ) + fcr2colo = connection_management.create_fcr_connection(fcr_uuid, fcr2colo_request) + + """ + Configures a routing protocol for a Fabric Cloud Router (FCR) to Port connection identified by the provided + connection UUID. + + This method is designed to configure the routing protocol details for an existing connection between a Fabric + Cloud Router and Port. It uses the `connection_uuid` to identify the connection and applies the specified + routing protocol settings. + + The routing protocol configuration includes the following details: + - Protocol Type: DIRECT + - Direct IPv4 Configuration: Specifies the Equinix Interface IP (equinixIfaceIp) with the value + '99.65.179.9/30'. + """ + utils.pr_yellow('\nConfigure Routing Protocol Detail by UUID') + routing_protocol_request = module.RoutingProtocolDirectType( + type="DIRECT", + directIpv4={ + "equinixIfaceIp": 'ip' + } + ) + connection_management.configure_routing_protocol(fcr2colo, routing_protocol_request) + + """ + Retrieves the connection details for a Fabric Cloud Router (FCR) to Port connection by its UUID. + This method is primarily used to fetch detailed information about a specific connection between + a Fabric Cloud Router and Port, using the connection's unique identifier (UUID). + """ + utils.pr_yellow('\nGet FCR2Port Connection Details by UUID') + connection_management.get_connection_details_by_uuid(fcr2colo) + + """ + Updates the name details of a connection using the provided Connection UUID. + + This function changes the name attribute of an existing connection name to "fcr2PortUpdate" + by performing a "replace" operation on the connection's name field. The connection is + identified by its UUID, passed as the `fcr2colo` parameter. + """ + utils.pr_yellow('\nUpdate Name Details Using Connection_UUID') + port_conn_name_update = [module.ConnectionChangeOperation( + op="replace", + path="/name", + value="fcr2PortUpdate" + ) + ] + connection_management.update_connection_details_by_uuid(fcr2colo,port_conn_name_update) + + """ + Updates the bandwidth details of a connection using the provided Connection UUID. + + This function changes the name attribute of an existing connection name to 50 Mbps + by performing a "replace" operation on the connection's bandwidth field. The connection is + identified by its UUID, passed as the `fcr2colo` parameter. + """ + utils.pr_yellow('\nUpdate Bandwidth Details Using Connection_UUID') + port_conn_bandwidth_update = [module.ConnectionChangeOperation( + op="replace", + path="/bandwidth", + value=50 + ) + ] + connection_management.update_connection_details_by_uuid(fcr2colo,port_conn_bandwidth_update) + + """ + Deletes a Fabric Cloud Router (FCR) to Port Colo connection. + + This function performs the following steps: + 1. Prints a message indicating that the deletion of the Fabric Cloud Router + to Port connection is about to begin. The message is printed in yellow for visibility. + 2. Calls the `delete_connection` method from the `connection_management` module + to delete the specified Fabric Cloud Router to Port connection. + """ + utils.pr_yellow('\nDelete Fabric Cloud Router to Colo Connection') + time.sleep(60) + connection_management.delete_connection(fcr2colo) + + """ + Deletes a Fabric Cloud Router (FCR) with the specified UUID. + + This method handles the deletion of a Fabric Cloud Router by calling the + relevant function in the `cloud_router_management` module. It first prints + a message to the console indicating the start of the deletion process, + using a yellow color for emphasis. After that, it proceeds to delete the + specified Fabric Cloud Router. + """ + utils.pr_yellow('\nDelete Fabric Cloud Router') + cloud_router_management.delete_fcr(fcr_uuid) \ No newline at end of file diff --git a/examples/services/fabricv4/oauth2/__init__.py b/examples/services/fabricv4/oauth2/__init__.py new file mode 100644 index 00000000..36a7b69a --- /dev/null +++ b/examples/services/fabricv4/oauth2/__init__.py @@ -0,0 +1 @@ +__all__ = ['configure_client_credentials'] \ No newline at end of file diff --git a/examples/services/fabricv4/oauth2/configure_client_credentials.py b/examples/services/fabricv4/oauth2/configure_client_credentials.py new file mode 100644 index 00000000..057bfef3 --- /dev/null +++ b/examples/services/fabricv4/oauth2/configure_client_credentials.py @@ -0,0 +1,76 @@ + +import os +import json + +from rauth import OAuth2Service +from equinix.services import fabricv4 + + +def get_equinix_fabric_client(): + """ + Initializes and returns an Equinix Fabric API client using OAuth2 authentication. + + This method is designed to authenticate with the Equinix Fabric API using OAuth2 + client credentials flow. It retrieves the client ID and client secret from the + environment variables `EQUINIX_API_CLIENTID` and `EQUINIX_API_CLIENTSECRET`. These + credentials are then used to obtain an OAuth2 access token, which is subsequently + used to configure the Equinix Fabric API client. + + The method performs the following steps: + 1. Retrieves the `client_id` and `client_secret` from environment variables. + 2. Creates an `OAuth2Service` instance using the client credentials. + 3. Requests an access token using the `client_credentials` grant type. + 4. Configures the Equinix Fabric API client with the obtained access token. + 5. Returns the initialized `fabricv4.ApiClient` instance. + + Returns: + fabricv4.ApiClient: An instance of `fabricv4.ApiClient` that is authenticated + and ready to interact with the Equinix Fabric API. + + Raises: + EnvironmentError: If the `EQUINIX_API_CLIENT_ID` or `EQUINIX_API_CLIENT_SECRET` + environment variables are not set. + OAuth2Error: If there is an error obtaining the access token from the OAuth2 + service. + + Example: + To use this function, ensure that the environment variables are set and call + it as follows: + + ```python + client = get_equinix_fabric_client() + # Now you can use `client` to interact with the Equinix Fabric API + ``` + + Note: + This method is configured to work with the PROD (PRODUCTION) + environment of the Equinix API. If you need to connect to a different + environment, adjust the `access_token_url`, `authorize_url`, and `base_url` + accordingly. + + """ + + client_id = os.getenv("EQUINIX_API_CLIENT_ID") + client_secret = os.getenv("EQUINIX_API_CLIENT_SECRET") + + oauth2_service = OAuth2Service( + name="Fabricv4", + client_id=client_id, + client_secret=client_secret, + access_token_url="https://api.equinix.com/oauth2/v1/token", + authorize_url="https://api.equinix.com/oauth2/v1/token", + base_url="https://api.equinix.com/", + ) + + data = { + 'code': 'Fabricv4', # specific to my app + 'grant_type': 'client_credentials', # generally required! + } + + oauth2_session = oauth2_service.get_auth_session(data=data, decoder=json.loads) + conf = fabricv4.Configuration( + host="https://api.equinix.com/", + ) + + conf.access_token = oauth2_session.access_token + return fabricv4.ApiClient(conf) \ No newline at end of file diff --git a/examples/services/fabricv4/utils/utils.py b/examples/services/fabricv4/utils/utils.py new file mode 100644 index 00000000..9acb34a8 --- /dev/null +++ b/examples/services/fabricv4/utils/utils.py @@ -0,0 +1,35 @@ +# Utility functions. + +from datetime import datetime +from enum import Enum + + +def pr_cyan(skk): + """Prints the given string in cyan color.""" + print("\033[96m {}\033[00m".format(skk)) + + +def pr_yellow(skk): + """Prints the given string in yellow color.""" + print("\033[93m {}\033[00m".format(skk)) + + +def pr_purple(skk): + """Prints the given string in purple color.""" + print("\033[95m {}\033[00m".format(skk)) + + +def custom_serializer(obj): + """ + Custom JSON serializer to handle various object types. + - Handles Enums by returning their value. + - Converts datetime objects to ISO format. + - Converts objects with `__dict__` attribute to dictionary representation. + """ + if isinstance(obj, Enum): + return obj.value + if isinstance(obj, datetime): + return obj.isoformat() + if hasattr(obj, '__dict__'): + return obj.__dict__ + return str(obj) \ No newline at end of file