1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5from __future__ import (absolute_import, division, print_function)
6
7__copyright__ = "(c) 2020 Dell Inc. or its subsidiaries. All rights reserved."
8
9__metaclass__ = type
10
11DOCUMENTATION = '''
12module: show_system_network_summary
13author: "Senthil Kumar Ganesan (@skg-net)"
14short_description: Operations for show_system_network output in json/yaml format.
15description:
16
17  - Get the show system inforamtion of a Leaf-Spine.
18
19options:
20    output_type:
21        type: str
22        description:
23            - json or yaml
24            - Default value is json
25        default: json
26        required: False
27    cli_responses:
28        type: list
29        required: True
30        description:
31            - show system command xml output
32'''
33EXAMPLES = '''
34Copy below YAML into a playbook (e.g. play.yml) and run as follows:
35
36#$ ansible-playbook -i inv show.yml
37name: show system Configuration
38hosts: localhost
39connection: local
40gather_facts: False
41vars:
42  cli:
43    username: admin
44    password: admin
45tasks:
46- name: "Get Dell EMC OS10 Show system summary"
47  os10_command:
48    commands: ['show system | display-xml']
49    provider: "{{ hostvars[item].cli }}"
50  with_items: "{{ groups['all'] }}"
51  register: show_system
52- set_fact:
53     output:  "{{ output|default([])+ [{'inv_name': item.item, 'host': item.invocation.module_args.provider.host, 'stdout_show_system': item.stdout}] }}"
54  loop: "{{ show_system.results }}"
55- debug: var=output
56- name: "show system network call to lib "
57  show_system_network_summary:
58    cli_responses: "{{ output}} "
59    output_type: "{{ output_method if output_method is defined else 'json' }}"
60  register: show_system_network_summary
61- debug: var=show_system_network_summary
62'''
63
64import re
65from ansible_collections.dellemc.os10.plugins.module_utils.network.base_network_show import BaseNetworkShow
66
67
68class ShowSystemNetworkSummary(BaseNetworkShow):
69    def __init__(self):
70        BaseNetworkShow.__init__(self)
71        self.cli_responses = self.module.params['cli_responses']
72        self.output_type = self.module.params['output_type']
73        self.changed = False
74
75    def get_fields(self):
76        spec_fields = {
77            'cli_responses': {
78                'type': 'list',
79                'required': True
80            },
81            'output_type': {
82                'type': 'str',
83                'default': "json",
84                'required': False
85            }
86        }
87        return spec_fields
88
89    def perform_action(self):
90        out = list()
91        show_system_summary = self.cli_responses
92        if len(show_system_summary) > 0:
93            for item in show_system_summary:
94                out_dict = {}
95                host = item.get("host")
96                inv_name = item.get("inv_name")
97                show_system_response = item.get("stdout_show_system")
98                if show_system_response is not None:
99                    result = BaseNetworkShow.xml_to_dict(
100                        self, show_system_response[0])
101                    rpc_reply = result.get("rpc-reply")
102                    if rpc_reply is not None:
103                        data = rpc_reply.get("data")
104                        if data is not None:
105                            out_dict["host"] = host
106                            out_dict["inv_name"] = inv_name
107                            system_state = data.get("system-state")
108                            if system_state is not None:
109                                system_status = system_state.get(
110                                    "system-status")
111                                if system_status is not None:
112                                    out_dict["hostname"] = system_status.get(
113                                        "hostname")
114                            system = data.get("system")
115                            if system is not None:
116                                node = system.get("node")
117                                if node is not None:
118                                    out_dict["node-mac"] = node.get("node-mac")
119                                    unit = node.get("unit")
120                                    if unit is not None:
121                                        out_dict["software-version"] = unit.get(
122                                            "software-version")
123                                    mfg_info = node.get("mfg-info")
124                                    if mfg_info is not None:
125                                        out_dict["service-tag"] = mfg_info.get(
126                                            "service-tag")
127                                        out_dict["device type"] = mfg_info.get(
128                                            "product-name")
129                            if bool(out_dict):
130                                out.append(out_dict)
131        if self.output_type != "json":
132            self.exit_msg.update(
133                {"results": (BaseNetworkShow.dict_to_yaml(self, out))})
134        else:
135            self.exit_msg.update({"results": (out)})
136        self.module.exit_json(changed=False, msg=self.exit_msg)
137
138
139def main():
140    module_instance = ShowSystemNetworkSummary()
141    module_instance.perform_action()
142
143
144if __name__ == '__main__':
145    main()
146