Debian Codegraph @

ansible_collections/amazon/aws/plugins/module_utils/direct_connect.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2017 Ansible Project
4 #
5 # This code is part of Ansible, but is an independent component.
6 # This particular file snippet, and this file snippet only, is BSD licensed.
7 # Modules you write using this snippet, which is embedded dynamically by Ansible
8 # still belong to the author of the module, and may assign their own license
9 # to the complete work.
10 #
11 # Redistribution and use in source and binary forms, with or without modification,
12 # are permitted provided that the following conditions are met:
13 #
14 # * Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # * Redistributions in binary form must reproduce the above copyright notice,
17 # this list of conditions and the following disclaimer in the documentation
18 # and/or other materials provided with the distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """
31 This module adds shared support for Direct Connect modules.
32 """
33
34 import traceback
35
36 try:
37 import botocore
38 except ImportError:
39 pass
40
41 from .retries import AWSRetry
42
43
44 class DirectConnectError(Exception):
45 def __init__(self, msg, last_traceback=None, exception=None):
46 self.msg = msg
47 self.last_traceback = last_traceback
48 self.exception = exception
49
50
51 def delete_connection(client, connection_id):
52 try:
53 AWSRetry.jittered_backoff()(client.delete_connection)(connectionId=connection_id)
54 except botocore.exceptions.ClientError as e:
55 raise DirectConnectError(
56 msg=f"Failed to delete DirectConnection {connection_id}.",
57 last_traceback=traceback.format_exc(),
58 exception=e,
59 )
60
61
62 def associate_connection_and_lag(client, connection_id, lag_id):
63 try:
64 AWSRetry.jittered_backoff()(client.associate_connection_with_lag)(connectionId=connection_id, lagId=lag_id)
65 except botocore.exceptions.ClientError as e:
66 raise DirectConnectError(
67 msg=f"Failed to associate Direct Connect connection {connection_id} with link aggregation group {lag_id}.",
68 last_traceback=traceback.format_exc(),
69 exception=e,
70 )
71
72
73 def disassociate_connection_and_lag(client, connection_id, lag_id):
74 try:
75 AWSRetry.jittered_backoff()(client.disassociate_connection_from_lag)(connectionId=connection_id, lagId=lag_id)
76 except botocore.exceptions.ClientError as e:
77 raise DirectConnectError(
78 msg=f"Failed to disassociate Direct Connect connection {connection_id} from link aggregation group {lag_id}.",
79 last_traceback=traceback.format_exc(),
80 exception=e,
81 )
82
83
84 def delete_virtual_interface(client, virtual_interface):
85 try:
86 AWSRetry.jittered_backoff()(client.delete_virtual_interface)(virtualInterfaceId=virtual_interface)
87 except botocore.exceptions.ClientError as e:
88 raise DirectConnectError(
89 msg=f"Could not delete virtual interface {virtual_interface}",
90 last_traceback=traceback.format_exc(),
91 exception=e,
92 )