1#!/usr/local/bin/python3.8
2# -*- coding:utf-8 -*-
3
4# Copyright (C) 2020 Inspur Inc. All Rights Reserved.
5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7from __future__ import (absolute_import, division, print_function)
8
9__metaclass__ = type
10
11DOCUMENTATION = '''
12---
13module: edit_ipv6
14version_added: "0.1.0"
15author:
16    - WangBaoshan (@ISIB-group)
17short_description: Set ipv6 information.
18description:
19   - Set ipv6 information on Inspur server.
20options:
21    interface_name:
22        description:
23            - Set interface_name.
24        choices: ['eth0', 'eth1', 'bond0']
25        required: True
26        type: str
27    ipv6_status:
28        description:
29            - Enable or disable IPV6.
30        choices: ['enable', 'disable']
31        type: str
32    ipv6_dhcp_enable:
33        description:
34            - Enable 'Enable DHCP' to dynamically configure IPv6 address using Dynamic Host Configuration Protocol (DHCP).
35        choices: ['dhcp', 'static']
36        type: str
37    ipv6_address:
38        description:
39            - If DHCP is disabled, specify a static IPv6 address to be configured for the selected interface.
40            - Required when I(ipv6_dhcp_enable=static).
41        type: str
42    ipv6_index:
43        description:
44            - Ipv6 index(0-15).
45            - Required when I(ipv6_dhcp_enable=static).
46        type: int
47    ipv6_prefix:
48        description:
49            - The subnet prefix length for the IPv6 settings(0-128).
50            - Required when I(ipv6_dhcp_enable=static).
51        type: int
52    ipv6_gateway:
53        description:
54            - If DHCP is disabled, specify a static Default Gateway to be configured for the selected interface.
55            - Required when I(ipv6_dhcp_enable=static).
56        type: str
57extends_documentation_fragment:
58    - inspur.sm.ism
59'''
60
61EXAMPLES = '''
62- name: Ipv6 test
63  hosts: ism
64  connection: local
65  gather_facts: no
66  vars:
67    ism:
68      host: "{{ ansible_ssh_host }}"
69      username: "{{ username }}"
70      password: "{{ password }}"
71
72  tasks:
73
74  - name: "Set ipv6 information"
75    inspur.sm.edit_ipv6:
76      interface_name: "eth0"
77      ipv6_status: "disable"
78      provider: "{{ ism }}"
79
80  - name: "Set ipv6 information"
81    inspur.sm.edit_ipv6:
82      interface_name: "eth0"
83      ipv6_status: "enable"
84      ipv6_dhcp_enable: "dhcp"
85      provider: "{{ ism }}"
86
87  - name: "Set ipv6 information"
88    inspur.sm.edit_ipv6:
89      interface_name: "eth0"
90      ipv6_status: "enable"
91      ipv6_dhcp_enable: "static"
92      ipv6_address: "::ffff:100:2:36:10"
93      ipv6_index: 12
94      ipv6_prefix: 16
95      ipv6_gateway: "::"
96      provider: "{{ ism }}"
97'''
98
99RETURN = '''
100message:
101    description: Messages returned after module execution.
102    returned: always
103    type: str
104state:
105    description: Status after module execution.
106    returned: always
107    type: str
108changed:
109    description: Check to see if a change was made on the device.
110    returned: always
111    type: bool
112'''
113
114from ansible.module_utils.basic import AnsibleModule
115from ansible_collections.inspur.sm.plugins.module_utils.ism import (ism_argument_spec, get_connection)
116
117
118class Network(object):
119    def __init__(self, argument_spec):
120        self.spec = argument_spec
121        self.module = None
122        self.init_module()
123        self.results = dict()
124
125    def init_module(self):
126        """Init module object"""
127
128        self.module = AnsibleModule(
129            argument_spec=self.spec, supports_check_mode=False)
130
131    def run_command(self):
132        self.module.params['subcommand'] = 'setipv6'
133        self.results = get_connection(self.module)
134        if self.results['State'] == 'Success':
135            self.results['changed'] = True
136
137    def show_result(self):
138        """Show result"""
139        self.module.exit_json(**self.results)
140
141    def work(self):
142        """Worker"""
143        self.run_command()
144        self.show_result()
145
146
147def main():
148    argument_spec = dict(
149        interface_name=dict(type='str', required=True, choices=['eth0', 'eth1', 'bond0']),
150        ipv6_status=dict(type='str', required=False, choices=['enable', 'disable']),
151        ipv6_dhcp_enable=dict(type='str', required=False, choices=['dhcp', 'static']),
152        ipv6_address=dict(type='str', required=False),
153        ipv6_index=dict(type='int', required=False),
154        ipv6_prefix=dict(type='int', required=False),
155        ipv6_gateway=dict(type='str', required=False),
156
157    )
158    argument_spec.update(ism_argument_spec)
159    net_obj = Network(argument_spec)
160    net_obj.work()
161
162
163if __name__ == '__main__':
164    main()
165