Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_bgp_config.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: NetApp, Inc
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = """
11 module: na_ontap_bgp_config
12 short_description: NetApp ONTAP network BGP configuration
13 extends_documentation_fragment:
14 - netapp.ontap.netapp.na_ontap_rest
15 version_added: '22.13.0'
16 author: NetApp Ansible Team (@carchi8py) <[email protected]>
17 description:
18 - Create/ modify/ delete BGP configuration for a node.
19 options:
20 state:
21 description:
22 - Specifies whether to create/ update or delete the border gateway protocol (BGP) configuration for a node.
23 choices: ['present', 'absent']
24 type: str
25 default: present
26
27 node:
28 description:
29 - Specifies the node on which configuration details will be managed.
30 required: true
31 type: str
32
33 asn:
34 description:
35 - Specifies the autonomous system number (ASN). The ASN attribute is a positive integer of the range from 1 to 4,294,967,295.
36 - It should typically be chosen from RFC6996 "Autonomous System (AS) Reservation for Private Use" or
37 the AS number assigned to the operator's organization.
38 type: int
39
40 hold_time:
41 description:
42 - Specifies the hold time in seconds. The default value is 180.
43 type: int
44 default: 180
45
46 router_id:
47 description:
48 - Specifies the local router ID. The router-id value takes the form of an IPv4 address.
49 - The default router-id will be initialized using a local IPv4 address in admin vserver if not given for create operation.
50 type: str
51
52 notes:
53 - Only supported with REST and requires ONTAP 9.6 or later.
54 """
55
56 EXAMPLES = """
57 - name: Create BGP configuration for a node
58 netapp.ontap.na_ontap_bgp_config:
59 state: present
60 node: "csahu-node1"
61 asn: 10
62 hold_time: 180
63 router_id: "10.0.1.112"
64 hostname: "{{ netapp_hostname }}"
65 username: "{{ netapp_username }}"
66 password: "{{ netapp_password }}"
67 https: true
68 validate_certs: "{{ validate_certs }}"
69 use_rest: always
70
71 - name: Modify BGP configuration for a node
72 netapp.ontap.na_ontap_bgp_config:
73 state: present
74 node: "csahu-node1"
75 hold_time: 360
76 hostname: "{{ netapp_hostname }}"
77 username: "{{ netapp_username }}"
78 password: "{{ netapp_password }}"
79 https: true
80 validate_certs: "{{ validate_certs }}"
81 use_rest: always
82
83 - name: Delete BGP configuration for a node
84 netapp.ontap.na_ontap_bgp_config:
85 state: absent
86 node: "csahu-node1"
87 hostname: "{{ netapp_hostname }}"
88 username: "{{ netapp_username }}"
89 password: "{{ netapp_password }}"
90 https: true
91 validate_certs: "{{ validate_certs }}"
92 use_rest: always
93 """
94
95 RETURN = """
96 """
97
98 import traceback
99 from ansible.module_utils.basic import AnsibleModule
100 from ansible.module_utils._text import to_native
101 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
102 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
103 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
104
105
106 class NetAppOntapBgpConfiguration:
107 def __init__(self):
108 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
109 self.argument_spec.update(dict(
110 state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
111 node=dict(required=True, type='str'),
112 asn=dict(required=False, type='int'),
113 hold_time=dict(required=False, type='int', default=180),
114 router_id=dict(required=False, type='str'),
115 ))
116
117 self.module = AnsibleModule(
118 argument_spec=self.argument_spec,
119 supports_check_mode=True
120 )
121
122 self.na_helper = NetAppModule(self.module)
123 self.parameters = self.na_helper.check_and_set_parameters(self.module)
124
125 self.rest_api = netapp_utils.OntapRestAPI(self.module)
126 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_bgp_config:', 9, 6)
127
128 def get_bgp_config(self):
129 """ Retrieves border gateway protocol (BGP) configuration for the given node """
130 api = 'private/cli/network/bgp/config'
131 fields = 'node,asn,hold-time,router-id'
132 params = {
133 'node': self.parameters['node'],
134 'fields': fields
135 }
136 record, error = rest_generic.get_one_record(self.rest_api, api, params)
137 if error:
138 self.module.fail_json(msg="Error fetching BGP configuration for %s: %s" % (self.parameters['node'], to_native(error)),
139 exception=traceback.format_exc())
140 if record:
141 return {
142 'node': record.get('node'),
143 'asn': record.get('asn'),
144 'hold_time': record.get('hold_time'),
145 'router_id': record.get('router_id')
146 }
147 return None
148
149 def create_bgp_config(self):
150 """ Creates border gateway protocol (BGP) configuration for the given node """
151 api = 'private/cli/network/bgp/config'
152 body = {
153 'node': self.parameters['node']
154 }
155 options = ('asn', 'hold_time', 'router_id')
156 for option in options:
157 if option in self.parameters:
158 body[option] = self.parameters[option]
159 dummy, error = rest_generic.post_async(self.rest_api, api, body)
160 if error:
161 self.module.fail_json(msg="Error creating BGP configuration for %s: %s" % (self.parameters['node'], to_native(error)),
162 exception=traceback.format_exc())
163
164 def modify_bgp_config(self, modify):
165 """ Modifues border gateway protocol (BGP) configuration for the given node """
166 api = 'private/cli/network/bgp/config'
167 params = {
168 'node': self.parameters['node']
169 }
170 dummy, error = rest_generic.patch_async(self.rest_api, api, uuid_or_name=None, body=modify, query=params)
171 if error:
172 self.module.fail_json(msg='Error modifying BGP configuration for %s: %s.' % (self.parameters['node'], to_native(error)),
173 exception=traceback.format_exc())
174
175 def delete_bgp_config(self):
176 """ Deletes border gateway protocol (BGP) configuration for the given node """
177 api = 'private/cli/network/bgp/config'
178 params = {
179 'node': self.parameters['node']
180 }
181 dummy, error = rest_generic.delete_async(self.rest_api, api, uuid=None, query=params)
182 if error:
183 self.module.fail_json(msg='Error deleting BGP configuration for %s: %s.' % (self.parameters['node'], to_native(error)),
184 exception=traceback.format_exc())
185
186 def apply(self):
187 current = self.get_bgp_config()
188 modify = None
189 cd_action = self.na_helper.get_cd_action(current, self.parameters)
190 if cd_action is None and self.parameters['state'] == 'present':
191 modify = self.na_helper.get_modified_attributes(current, self.parameters)
192 if self.na_helper.changed and not self.module.check_mode:
193 if cd_action == 'create':
194 self.create_bgp_config()
195 elif cd_action == 'delete':
196 self.delete_bgp_config()
197 elif modify:
198 self.modify_bgp_config(modify)
199 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
200 self.module.exit_json(**result)
201
202
203 def main():
204 bgp_config = NetAppOntapBgpConfiguration()
205 bgp_config.apply()
206
207
208 if __name__ == '__main__':
209 main()