Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_timezone.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': 'certified'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: ucs_timezone
16 short_description: Configures timezone on Cisco UCS Manager
17 description:
18 - Configures timezone on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(absent), will unset timezone.
24 - If C(present), will set or update timezone.
25 choices: [absent, present]
26 default: present
27 type: str
28
29 admin_state:
30 description:
31 - The admin_state setting
32 - The enabled admin_state indicates the timezone configuration is utilized by UCS Manager.
33 - The disabled admin_state indicates the timezone configuration is ignored by UCS Manager.
34 choices: [disabled, enabled]
35 default: enabled
36 type: str
37
38 description:
39 description:
40 - A user-defined description of the timezone.
41 - Enter up to 256 characters.
42 - "You can use any characters or spaces except the following:"
43 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
44 aliases: [ descr ]
45 type: str
46
47 timezone:
48 description:
49 - The timezone name.
50 - Time zone names are from the L(tz database,https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
51 - The timezone name is case sensitive.
52 - The timezone name can be between 0 and 510 alphanumeric characters.
53 - You cannot use spaces or any special characters other than
54 - "\"-\" (hyphen), \"_\" (underscore), \"/\" (backslash)."
55 type: str
56
57 requirements:
58 - ucsmsdk
59 author:
60 - David Soper (@dsoper2)
61 - John McDonough (@movinalot)
62 - CiscoUcs (@CiscoUcs)
63 '''
64
65 EXAMPLES = r'''
66 - name: Configure Time Zone
67 cisco.ucs.ucs_timezone:
68 hostname: 172.16.143.150
69 username: admin
70 password: password
71 state: present
72 admin_state: enabled
73 timezone: America/Los_Angeles
74 description: 'Time Zone for Los Angeles'
75
76 - name: Unconfigure Time Zone
77 cisco.ucs.ucs_timezone:
78 hostname: 172.16.143.150
79 username: admin
80 password: password
81 state: absent
82 admin_state: disabled
83 '''
84
85 RETURN = r'''
86 #
87 '''
88
89 from ansible.module_utils.basic import AnsibleModule
90 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
91
92
93 def run_module():
94 argument_spec = ucs_argument_spec.copy()
95 argument_spec.update(
96 timezone=dict(type='str'),
97 description=dict(type='str', aliases=['descr']),
98 admin_state=dict(type='str', default='enabled', choices=['disabled', 'enabled']),
99 state=dict(type='str', default='present', choices=['present', 'absent']),
100 )
101
102 module = AnsibleModule(
103 argument_spec,
104 supports_check_mode=True,
105 required_if=[
106 ['state', 'present', ['timezone']],
107 ],
108 )
109 ucs = UCSModule(module)
110
111 err = False
112
113 changed = False
114 try:
115 mo_exists = False
116 props_match = False
117
118 dn = 'sys/svc-ext/datetime-svc'
119
120 mo = ucs.login_handle.query_dn(dn)
121 if mo:
122 mo_exists = True
123
124 if module.params['state'] == 'absent':
125 # mo must exist but all properties do not have to match
126 if mo_exists:
127 if not module.check_mode:
128 mo.timezone = ""
129 mo.descr = ""
130 ucs.login_handle.add_mo(mo, modify_present=True)
131 ucs.login_handle.commit()
132 changed = True
133 else:
134 if mo_exists:
135 # check top-level mo props
136 kwargs = dict(descr=module.params['description'])
137 kwargs['timezone'] = module.params['timezone']
138 kwargs['admin_state'] = module.params['admin_state']
139 if mo.check_prop_match(**kwargs):
140 props_match = True
141
142 if not props_match:
143 if not module.check_mode:
144 # update mo, timezone mo always exists
145 mo.timezone = module.params['timezone']
146 mo.descr = module.params['description']
147 mo.admin_state = module.params['admin_state']
148 ucs.login_handle.add_mo(mo, modify_present=True)
149 ucs.login_handle.commit()
150 changed = True
151
152 except Exception as e:
153 err = True
154 ucs.result['msg'] = "setup error: %s " % str(e)
155
156 ucs.result['changed'] = changed
157 if err:
158 module.fail_json(**ucs.result)
159 module.exit_json(**ucs.result)
160
161
162 def main():
163 run_module()
164
165
166 if __name__ == '__main__':
167 main()