Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_san_connectivity_policy_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_san_connectivity_policy_info
16 short_description: Gather information about SAN Connectivity Policies and their vHBAs in Cisco Intersight
17 description:
18 - Retrieve information about SAN Connectivity Policies and their associated vHBAs from L(Cisco Intersight,https://intersight.com).
19 - Query policies by organization, policy name, or filter vHBAs by name patterns.
20 - Returns structured data combining policy metadata with detailed vHBA configurations.
21 - Supports filtering by organization, policy name, and vHBA name.
22 - If no filters are provided, all SAN Connectivity Policies and their vHBAs will be returned.
23 - Returns structured data with both policy information and associated vHBA details.
24 extends_documentation_fragment: intersight
25 options:
26 organization:
27 description:
28 - The name of the organization to filter SAN Connectivity Policies by.
29 - Use 'default' for the default organization.
30 - When specified, only policies from this organization will be returned.
31 type: str
32 name:
33 description:
34 - The exact name of the SAN Connectivity Policy to retrieve information from.
35 - When specified, only the matching policy and its vHBAs will be returned.
36 - Can be combined with vhba_name filter to get specific vHBAs from a specific policy.
37 type: str
38 vhba_name:
39 description:
40 - Filter vHBAs by name within the policies.
41 - Can be used with or without policy name filtering.
42 - Supports exact matching (e.g., "vhba-a" will match only "vhba-a").
43 - When combined with policy name, filters vHBAs within that specific policy.
44 type: str
45 author:
46 - Ron Gershburg (@rgershbu)
47 '''
48
49 EXAMPLES = r'''
50 # Basic Usage Examples
51 - name: Fetch all SAN Connectivity Policies and their vHBAs from all organizations
52 cisco.intersight.intersight_san_connectivity_policy_info:
53 api_private_key: "{{ api_private_key }}"
54 api_key_id: "{{ api_key_id }}"
55 register: all_san_policies
56
57 # Organization-specific Examples
58 - name: Fetch all SAN Connectivity Policies from the default organization
59 cisco.intersight.intersight_san_connectivity_policy_info:
60 api_private_key: "{{ api_private_key }}"
61 api_key_id: "{{ api_key_id }}"
62 organization: "default"
63 register: default_org_policies
64
65 - name: Fetch all SAN Connectivity Policies from a custom organization
66 cisco.intersight.intersight_san_connectivity_policy_info:
67 api_private_key: "{{ api_private_key }}"
68 api_key_id: "{{ api_key_id }}"
69 organization: "Engineering"
70 register: engineering_policies
71
72 # Specific Policy Examples
73 - name: Fetch a specific SAN Connectivity Policy by name with all its vHBAs
74 cisco.intersight.intersight_san_connectivity_policy_info:
75 api_private_key: "{{ api_private_key }}"
76 api_key_id: "{{ api_key_id }}"
77 name: "fi-attached-san-policy"
78 register: specific_policy
79
80 # vHBA Filtering Examples
81 - name: Find all policies containing a specific vHBA
82 cisco.intersight.intersight_san_connectivity_policy_info:
83 api_private_key: "{{ api_private_key }}"
84 api_key_id: "{{ api_key_id }}"
85 vhba_name: "vhba-a"
86 register: policies_with_vhba_a
87
88 - name: Find specific vHBA in a specific policy
89 cisco.intersight.intersight_san_connectivity_policy_info:
90 api_private_key: "{{ api_private_key }}"
91 api_key_id: "{{ api_key_id }}"
92 name: "fi-attached-san-policy"
93 vhba_name: "vhba-b"
94 register: specific_vhba_in_policy
95 '''
96
97 RETURN = r'''
98 api_response:
99 description:
100 - The API response containing policy and vHBA information.
101 - Returns a dictionary when querying a single policy or no policies found.
102 - Returns a list when multiple policies are found.
103 returned: always
104 type: dict
105 sample:
106 # Single policy response (when name parameter is used or only one policy found)
107 Name: "fi-attached-san-policy"
108 ObjectType: "vnic.SanConnectivityPolicy"
109 Moid: "12345678901234567890abcd"
110 Description: "SAN connectivity policy for FI-attached servers"
111 TargetPlatform: "FIAttached"
112 WwnnAddressType: "POOL"
113 PlacementMode: "custom"
114 Organization:
115 Name: "default"
116 ObjectType: "organization.Organization"
117 Moid: "abcdef1234567890abcdef12"
118 Tags:
119 - Key: "Environment"
120 Value: "Production"
121 WwnnPool:
122 Moid: "fedcba0987654321fedcba09"
123 ObjectType: "fcpool.Pool"
124 vhbas:
125 - Name: "vhba-a"
126 ObjectType: "vnic.FcIf"
127 Moid: "vhba12345678901234567890"
128 Order: 0
129 PersistentBindings: true
130 Type: "fc-initiator"
131 Placement:
132 SwitchId: "A"
133 PciLink: 0
134 WwpnAddressType: "POOL"
135 SanConnectivityPolicy:
136 Moid: "12345678901234567890abcd"
137 ObjectType: "vnic.SanConnectivityPolicy"
138 - Name: "vhba-b"
139 ObjectType: "vnic.FcIf"
140 Moid: "vhba09876543210987654321"
141 Order: 1
142 PersistentBindings: true
143 Type: "fc-initiator"
144 Placement:
145 SwitchId: "B"
146 PciLink: 0
147 WwpnAddressType: "POOL"
148 SanConnectivityPolicy:
149 Moid: "12345678901234567890abcd"
150 ObjectType: "vnic.SanConnectivityPolicy"
151 '''
152
153
154 from ansible.module_utils.basic import AnsibleModule
155 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
156
157
158 def get_vhbas_for_policy(intersight, policy_moid, vhba_name_filter=None):
159 """
160 Get all vHBAs associated with a specific policy.
161 """
162
163 # Build filter for vHBAs associated with this policy
164 filter_conditions = [f"SanConnectivityPolicy.Moid eq '{policy_moid}'"]
165
166 # Add vHBA name filter if provided (exact match only)
167 if vhba_name_filter:
168 filter_conditions.append(f"Name eq '{vhba_name_filter}'")
169
170 query_params = {
171 '$filter': ' and '.join(filter_conditions)
172 }
173
174 # Reset api_response before the API call to avoid previous responses
175 intersight.result['api_response'] = {}
176
177 # Get vHBAs for this policy
178 intersight.get_resource(
179 resource_path='/vnic/FcIfs',
180 query_params=query_params,
181 return_list=True
182 )
183
184 # Capture the response immediately before it gets overwritten
185 vhbas_response = intersight.result.get('api_response', [])
186
187 # Ensure we return a list
188 if isinstance(vhbas_response, list):
189 return vhbas_response
190 elif isinstance(vhbas_response, dict):
191 return [vhbas_response]
192 else:
193 return []
194
195
196 def main():
197 argument_spec = intersight_argument_spec.copy()
198 argument_spec.update(
199 organization=dict(type='str'),
200 name=dict(type='str'),
201 vhba_name=dict(type='str')
202 )
203 module = AnsibleModule(
204 argument_spec,
205 supports_check_mode=True,
206 )
207
208 intersight = IntersightModule(module)
209
210 # Resource path used to fetch policy info
211 resource_path = '/vnic/SanConnectivityPolicies'
212
213 # Get query parameters for policies
214 query_params = intersight.set_query_params()
215
216 # Reset api_response before the API call to avoid previous responses
217 intersight.result['api_response'] = {}
218
219 # Get SAN Connectivity policies
220 intersight.get_resource(
221 resource_path=resource_path,
222 query_params=query_params,
223 return_list=True
224 )
225
226 policies = intersight.result.get('api_response', [])
227 vhba_name_filter = module.params.get('vhba_name')
228
229 # Build structured response
230 structured_results = []
231
232 # Ensure policies is always a list for iteration, even if a single dict is returned
233 if isinstance(policies, dict):
234 policies = [policies]
235 elif not isinstance(policies, list):
236 policies = []
237
238 for policy in policies:
239 policy_moid = policy.get('Moid')
240 if not policy_moid:
241 continue
242
243 # Get vHBAs for this policy
244 vhbas = get_vhbas_for_policy(intersight, policy_moid, vhba_name_filter)
245
246 # Embed vHBAs in the policy response
247 if vhbas:
248 policy['vhbas'] = vhbas
249
250 structured_results.append(policy)
251
252 # Create final response structure
253 final_api_response = None
254
255 # Set final response based on number of policies found
256 if len(structured_results) == 1:
257 # Single policy - return as dict
258 final_api_response = structured_results[0]
259 elif len(structured_results) > 1:
260 # Multiple policies - return as list
261 final_api_response = structured_results
262 else:
263 # No policies found - return empty structure
264 final_api_response = {}
265
266 # Use intersight.result and update api_response directly
267 intersight.result['api_response'] = final_api_response
268 final_result = intersight.result
269
270 module.exit_json(**final_result)
271
272
273 if __name__ == '__main__':
274 main()