summaryrefslogtreecommitdiffstats
path: root/ansible_collections/kaytus/ksmanage/plugins/modules/edit_power_budget.py
blob: b36e979e9cffe36500061a22ccf4b3a30c5b2827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/python
# -*- coding:utf-8 -*-

# Copyright(C) 2023 Kaytus Inc. All Rights Reserved.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

DOCUMENTATION = '''
---
module: edit_power_budget
version_added: "1.0.0"
author:
    - WangBaoshan (@ieisystem)
short_description: Set power budget information
description:
   - Set power budget information on kaytus Server.
notes:
   - Does not support C(check_mode).
options:
    range:
        description:
            - Range of power budget watts.
        choices: ['True', 'False']
        default: False
        type: bool
    domain:
        description:
            - Domain id.
            - Required when I(range=False).
        choices: ['system', 'cpu']
        type: str
    action:
        description:
            - Type to action.
            - Required when I(range=False).
        choices: ['add', 'delete', 'open', 'close']
        type: str
    id:
        description:
            - Policy id.
            - Required when I(range=False).
        choices: [1, 2, 3, 4]
        type: int
    watts:
        description:
            - Power budget watts of add.
            - Required when I(action=add).
        type: int
    except_action:
        description:
            - Except action, 0 is do nothing, 1 is send alert, 2 is shutdown system, 3 is shutdown system and send alert.
            - Only the M7 model supports this parameter.
        choices: [0, 1, 2, 3]
        type: int
    start1:
        description:
            - Pause period of add, start time, from 0 to 24.
        type: int
    end1:
        description:
            - Pause period of add, end time, must be greater than start time, from 0 to 24.
        type: int
    week1:
        description:
            - Pause period of add, repetition period.
            - The input parameters are 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun', separated by commas, such as Mon, Wed, Fri.
        type: list
        elements: str
    start2:
        description:
            - Pause period of add, start time, from 0 to 24.
        type: int
    end2:
        description:
            - Pause period of add, end time, must be greater than start time, from 0 to 24.
        type: int
    week2:
        description:
            - Pause period of add, repetition period.
            - The input parameters are 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun', separated by commas, such as Mon, Wed, Fri.
        type: list
        elements: str
    start3:
        description:
            - Pause period of add, start time, from 0 to 24.
        type: int
    end3:
        description:
            - Pause period of add, end time, must be greater than start time, from 0 to 24.
        type: int
    week3:
        description:
            - Pause period of add, repetition period.
            - The input parameters are 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun', separated by commas, such as Mon, Wed, Fri.
        type: list
        elements: str
    start4:
        description:
            - Pause period of add, start time, from 0 to 24.
        type: int
    end4:
        description:
            - Pause period of add, end time, must be greater than start time, from 0 to 24.
        type: int
    week4:
        description:
            - Pause period of add, repetition period.
            - The input parameters are 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun', separated by commas, such as Mon, Wed, Fri.
        type: list
        elements: str
    start5:
        description:
            - Period of add, start time, from 0 to 24.
        type: int
    end5:
        description:
            - Pause period of add, end time, must be greater than start time, from 0 to 24.
        type: int
    week5:
        description:
            - Pause period of add, repetition period.
            - The input parameters are 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun', separated by commas, such as Mon, Wed, Fri.
        type: list
        elements: str
extends_documentation_fragment:
    - kaytus.ksmanage.ksmanage
'''

EXAMPLES = '''
- name: Power budget test
  hosts: ksmanage
  connection: local
  gather_facts: false
  vars:
    ksmanage:
      host: "{{ ansible_ssh_host }}"
      username: "{{ username }}"
      password: "{{ password }}"

  tasks:

  - name: "Get power budget range"
    kaytus.ksmanage.edit_power_budget:
      range: True
      provider: "{{ ksmanage }}"

  - name: "add power budget"
    kaytus.ksmanage.edit_power_budget:
      action: "add"
      id: 1
      watts: 1500
      start1: 2
      end1: 5
      week1:
        - Mon
        - Wed
        - Fri
      provider: "{{ ksmanage }}"

  - name: "Set power budget status to open"
    kaytus.ksmanage.edit_power_budget:
      action: "open"
      id: 1
      provider: "{{ ksmanage }}"

  - name: "Set power budget status to close"
    kaytus.ksmanage.edit_power_budget:
      action: "close"
      id: 1
      provider: "{{ ksmanage }}"

  - name: "Delete power budget"
    kaytus.ksmanage.edit_power_budget:
      action: "delete"
      id: 1
      provider: "{{ ksmanage }}"
'''

RETURN = '''
message:
    description: Messages returned after module execution.
    returned: always
    type: str
state:
    description: Status after module execution.
    returned: always
    type: str
changed:
    description: Check to see if a change was made on the device.
    returned: always
    type: bool
'''

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.kaytus.ksmanage.plugins.module_utils.ksmanage import (ksmanage_argument_spec, get_connection)


class Power(object):
    def __init__(self, argument_spec):
        self.spec = argument_spec
        self.module = None
        self.init_module()
        self.results = dict()

    def init_module(self):
        """Init module object"""

        self.module = AnsibleModule(
            argument_spec=self.spec, supports_check_mode=False)

    def run_command(self):
        self.module.params['subcommand'] = 'setpowerbudget'
        self.results = get_connection(self.module)
        if self.results['State'] == 'Success':
            self.results['changed'] = True

    def show_result(self):
        """Show result"""
        self.module.exit_json(**self.results)

    def work(self):
        """Worker"""
        self.run_command()
        self.show_result()


def main():
    argument_spec = dict(
        range=dict(type='bool', default=False, choices=[True, False]),
        domain=dict(type='str', required=False, choices=['system', 'cpu']),
        action=dict(type='str', required=False, choices=['add', 'delete', 'open', 'close']),
        id=dict(type='int', required=False, choices=[1, 2, 3, 4]),
        watts=dict(type='int', required=False),
        except_action=dict(type='int', required=False, choices=[0, 1, 2, 3]),
        start1=dict(type='int', required=False),
        end1=dict(type='int', required=False),
        week1=dict(type='list', elements='str', required=False),
        start2=dict(type='int', required=False),
        end2=dict(type='int', required=False),
        week2=dict(type='list', elements='str', required=False),
        start3=dict(type='int', required=False),
        end3=dict(type='int', required=False),
        week3=dict(type='list', elements='str', required=False),
        start4=dict(type='int', required=False),
        end4=dict(type='int', required=False),
        week4=dict(type='list', elements='str', required=False),
        start5=dict(type='int', required=False),
        end5=dict(type='int', required=False),
        week5=dict(type='list', elements='str', required=False),
    )
    argument_spec.update(ksmanage_argument_spec)
    power_obj = Power(argument_spec)
    power_obj.work()


if __name__ == '__main__':
    main()