Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding Examples for Python SDK to create FCR and FCR based Connections #7

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
19c93c1
Adding Examples for Python SDK to create FCR and FCR based Connections
jkallem-equinix Aug 6, 2024
aac1ad2
docs: provide steps for updating and releasing this SDK (#8)
ctreatma Aug 8, 2024
108f67a
docs: provide steps for updating and releasing this SDK (#8)
ctreatma Aug 8, 2024
980731a
modifying version in Fabricv4 Makefile for sync job
jkallem-equinix Aug 13, 2024
f3d4391
Updating Patch names according to the scripts
jkallem-equinix Aug 14, 2024
a85d5f4
Updated SDK patch Paths for Make File
jkallem-equinix Aug 15, 2024
bce14a5
Updated patch Paths for Make.fabricv4 File
jkallem-equinix Aug 15, 2024
9739900
Updated patch Paths for Make.fabricv4 File
jkallem-equinix Aug 15, 2024
ccd9c07
Updated patch Paths for Make.fabricv4 File
jkallem-equinix Aug 15, 2024
aabd04c
Updated patch Paths for sync-fabricv4 yaml and sync yaml File
jkallem-equinix Aug 16, 2024
83146b1
chore(deps): update dependency urllib3 to >=2.2.2, <3.0.0
renovate[bot] Aug 16, 2024
3753b4d
sync: fetch 2024-08-16 spec and apply patches
github-actions[bot] Aug 16, 2024
a5a07a1
sync: patch spec with 2024-08-16 spec
github-actions[bot] Aug 16, 2024
62cd77f
sync: generate client with 2024-08-16 spec
github-actions[bot] Aug 16, 2024
3dc0e87
sync: uncommitted changes detected when opening PR
github-actions[bot] Aug 16, 2024
3aa543d
ci: regenerate code for version 0.3.0 triggered by @ctreatma
Aug 20, 2024
785c6ed
chore: remove .openapi-generator directory (#20)
ctreatma Aug 20, 2024
9789a31
Resolving the Comments for Example
jkallem-equinix Aug 21, 2024
476aabd
Adding Examples for Python SDK to create FCR and FCR based Connections
jkallem-equinix Aug 6, 2024
164d8db
modifying version in Fabricv4 Makefile for sync job
jkallem-equinix Aug 13, 2024
3ed8ff6
Updating Patch names according to the scripts
jkallem-equinix Aug 14, 2024
f047b8e
Updated SDK patch Paths for Make File
jkallem-equinix Aug 15, 2024
0708794
Updated patch Paths for Make.fabricv4 File
jkallem-equinix Aug 15, 2024
26cad01
Updated patch Paths for Make.fabricv4 File
jkallem-equinix Aug 15, 2024
5f0af84
Updated patch Paths for Make.fabricv4 File
jkallem-equinix Aug 15, 2024
bca5d2d
Updated patch Paths for sync-fabricv4 yaml and sync yaml File
jkallem-equinix Aug 16, 2024
0933662
chore(deps): update dependency urllib3 to >=2.2.2, <3.0.0
renovate[bot] Aug 16, 2024
c73c3fb
sync: fetch 2024-08-16 spec and apply patches
github-actions[bot] Aug 16, 2024
bca1560
sync: patch spec with 2024-08-16 spec
github-actions[bot] Aug 16, 2024
8bff317
sync: generate client with 2024-08-16 spec
github-actions[bot] Aug 16, 2024
88a8c5a
sync: uncommitted changes detected when opening PR
github-actions[bot] Aug 16, 2024
7774182
ci: regenerate code for version 0.3.0 triggered by @ctreatma
Aug 20, 2024
e8c60a3
chore: remove .openapi-generator directory (#20)
ctreatma Aug 20, 2024
153a133
Resolving the Comments for Example
jkallem-equinix Aug 21, 2024
7360ce9
Merge remote-tracking branch 'origin/CXF-96059' into CXF-96059
jkallem-equinix Aug 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/services/fabricv4/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = ['cloud_router','connections','utils','oauth2']
1 change: 1 addition & 0 deletions examples/services/fabricv4/cloud_router/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = ['cloud_router','cloud_router_management']
93 changes: 93 additions & 0 deletions examples/services/fabricv4/cloud_router/cloud_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from examples.services.fabricv4.utils import utils
from equinix.services import fabricv4 as module
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we import just as fabricv4 and not as module? Better for code readability to have a specific name other than an abstract one.

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the notes sections of all comments.

- 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": [
"[email protected]"
]
}
]
)
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the notes section of this comment. It's too much detail.

-----
- 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)

123 changes: 123 additions & 0 deletions examples/services/fabricv4/cloud_router/cloud_router_management.py
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to add steps in comments. They can read the code for this.

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All steps from comments can be removed.

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not going to comment on all steps removals but please go through and remove all references to notes and steps in comment strings.

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')
1 change: 1 addition & 0 deletions examples/services/fabricv4/connections/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = ['connection_management','fcr_2_aws_connection','fcr_2_port_connection','fcr_2_azure_connection']
115 changes: 115 additions & 0 deletions examples/services/fabricv4/connections/connection_management.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an import collision here. Why are both being imported?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that from examples.services import fabricv4 should be removable.

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove example usage from comment strings.

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')
Loading