Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_datastore.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Tim Rightnour <[email protected]>
5 # Copyright: (c) 2018, Ansible Project
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_datastore
16 short_description: Configure Datastores
17 version_added: '3.0.0'
18 description:
19 - Configure Storage I/O Control Settings of a Datastore.
20 author:
21 - Nina Loser (@Nina2244)
22 options:
23 name:
24 description: Name of the datastore.
25 required: true
26 type: str
27 datacenter:
28 description:
29 - Datacenter to search for the datastores.
30 aliases: ['datacenter_name']
31 type: str
32 storage_io_control:
33 description:
34 - Specify datastore typ.
35 type: str
36 choices: ['enable_io_statistics', 'enable_statistics', 'disable']
37 required: true
38 congestion_threshold_percentage:
39 description:
40 - Storage I/O congestion threshold in percentage of peak throughput.
41 - "A value between 50% and 100%."
42 - "Recommended: 90%"
43 - Only use O(congestion_threshold_percentage) or O(congestion_threshold_manual).
44 - Only valid when O(storage_io_control=enable_io_statistics).
45 type: int
46 default: 90
47 congestion_threshold_manual:
48 description:
49 - Storage I/O congestion threshold in ms.
50 - Only use O(congestion_threshold_percentage) or O(congestion_threshold_manual).
51 - Only valid when O(storage_io_control=enable_io_statistics).
52 type: int
53 statistic_collection:
54 description:
55 - Include I/O statistics for SDRS.
56 - Only valid when O(storage_io_control=enable_io_statistics) or O(storage_io_control=enable_statistics).
57 type: bool
58 default: true
59 extends_documentation_fragment:
60 - vmware.vmware.base_options
61
62 '''
63
64 EXAMPLES = r'''
65 - name: Configure Storage I/O Control of an mounted datastore
66 community.vmware.vmware_datastore_info:
67 hostname: '{{ vcenter_hostname }}'
68 username: '{{ vcenter_username }}'
69 password: '{{ vcenter_password }}'
70 datacenter_name: '{{ datacenter_name }}'
71 name: datastore1
72 storage_io_control: 'enable_io_statistics'
73 congestion_threshold_manual: 30
74 statistic_collection: true
75 delegate_to: localhost
76 register: info
77
78 '''
79
80 RETURN = r'''
81 result:
82 description: Information about datastore operation.
83 returned: always
84 type: str
85 sample: "Datastore configured successfully."
86 '''
87
88 try:
89 from pyVmomi import vim
90 except ImportError:
91 pass
92
93 from ansible.module_utils.basic import AnsibleModule
94 from ansible.module_utils.common.text.converters import to_native
95 from ansible_collections.community.vmware.plugins.module_utils.vmware import (
96 PyVmomi,
97 wait_for_task,
98 TaskError)
99 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
100
101
102 class VMwareDatastore(PyVmomi):
103 def __init__(self, module):
104 super(VMwareDatastore, self).__init__(module)
105
106 if self.module.params.get('congestion_threshold_percentage') not in range(50, 101):
107 self.module.fail_json(msg="Congestion Threshold should be between 50% and 100%.")
108
109 self.datacenter_name = self.module.params.get('datacenter')
110 if self.datacenter_name:
111 self.datacenter = self.find_datacenter_by_name(self.datacenter_name)
112 if self.datacenter is None:
113 self.module.fail_json(msg="Datacenter %s does not exist." % self.datacenter_name)
114 else:
115 self.datacenter = None
116
117 self.datastore_name = self.module.params.get('name')
118 self.datastore = self.find_datastore_by_name(self.datastore_name, self.datacenter)
119 if self.datastore is None:
120 self.module.fail_json(msg="Datastore %s does not exist." % self.name)
121
122 self.storageResourceManager = self.content.storageResourceManager
123
124 def check_config_diff(self):
125 """
126 Check configuration diff
127 Returns: True if there is diff, else False
128 """
129 iormConfiguration = self.datastore.iormConfiguration
130
131 conf_statsAggregationDisabled = not self.module.params.get('statistic_collection')
132
133 if self.module.params.get('storage_io_control') == "enable_io_statistics":
134 if self.module.params.get('congestion_threshold_manual') is not None:
135 conf_congestionThresholdMode = 'manual'
136 conf_congestionThreshold = self.module.params.get('congestion_threshold_manual')
137 conf_percentOfPeakThroughput = iormConfiguration.percentOfPeakThroughput
138
139 else:
140 conf_congestionThresholdMode = 'automatic'
141 conf_percentOfPeakThroughput = self.module.params.get('congestion_threshold_percentage')
142 conf_congestionThreshold = iormConfiguration.congestionThreshold
143
144 if iormConfiguration.enabled and \
145 iormConfiguration.statsCollectionEnabled and \
146 iormConfiguration.statsAggregationDisabled == conf_statsAggregationDisabled and \
147 iormConfiguration.congestionThresholdMode == conf_congestionThresholdMode and \
148 iormConfiguration.congestionThreshold == conf_congestionThreshold and \
149 iormConfiguration.percentOfPeakThroughput == conf_percentOfPeakThroughput:
150 return False
151 else:
152 return True
153
154 elif self.module.params.get('storage_io_control') == "enable_statistics":
155 if not iormConfiguration.enabled and \
156 iormConfiguration.statsCollectionEnabled and \
157 iormConfiguration.statsAggregationDisabled == conf_statsAggregationDisabled:
158 return False
159 else:
160 return True
161
162 elif self.module.params.get('storage_io_control') == "disable":
163 if not iormConfiguration.enabled and \
164 not iormConfiguration.statsCollectionEnabled:
165 return False
166 else:
167 return True
168
169 def configure(self):
170 """
171 Manage configuration
172 """
173 changed = self.check_config_diff()
174
175 if changed:
176 if not self.module.check_mode:
177 config_spec = vim.StorageResourceManager.IORMConfigSpec()
178
179 iormConfiguration = self.datastore.iormConfiguration
180
181 conf_statsAggregationDisabled = not self.module.params.get('statistic_collection')
182
183 if self.module.params.get('storage_io_control') == "enable_io_statistics":
184 if self.module.params.get('congestion_threshold_manual') is not None:
185 config_spec.congestionThresholdMode = 'manual'
186 config_spec.congestionThreshold = self.module.params.get('congestion_threshold_manual')
187 config_spec.percentOfPeakThroughput = iormConfiguration.percentOfPeakThroughput
188
189 else:
190 config_spec.congestionThresholdMode = 'automatic'
191 config_spec.percentOfPeakThroughput = self.module.params.get('congestion_threshold_percentage')
192 config_spec.congestionThreshold = iormConfiguration.congestionThreshold
193
194 config_spec.enabled = True
195 config_spec.statsCollectionEnabled = True
196 config_spec.statsAggregationDisabled = conf_statsAggregationDisabled
197
198 elif self.module.params.get('storage_io_control') == "enable_statistics":
199 config_spec.enabled = False
200 config_spec.statsCollectionEnabled = True
201 config_spec.statsAggregationDisabled = conf_statsAggregationDisabled
202
203 elif self.module.params.get('storage_io_control') == "disable":
204 config_spec.enabled = False
205 config_spec.statsCollectionEnabled = False
206
207 try:
208 task = self.storageResourceManager.ConfigureDatastoreIORM_Task(self.datastore, config_spec)
209 changed, result = wait_for_task(task)
210 except TaskError as generic_exc:
211 self.module.fail_json(msg=to_native(generic_exc))
212 except Exception as task_e:
213 self.module.fail_json(msg=to_native(task_e))
214 else:
215 changed = True
216
217 results = dict(changed=changed)
218 self.module.exit_json(**results)
219
220
221 def main():
222 argument_spec = base_argument_spec()
223 argument_spec.update(
224 name=dict(type='str', required=True),
225 datacenter=dict(type='str', aliases=['datacenter_name']),
226 storage_io_control=dict(type='str', choices=['enable_io_statistics', 'enable_statistics', 'disable'], required=True),
227 congestion_threshold_percentage=dict(type='int', default=90, required=False),
228 congestion_threshold_manual=dict(type='int', required=False),
229 statistic_collection=dict(type='bool', default=True, required=False)
230 )
231 module = AnsibleModule(
232 argument_spec=argument_spec,
233 supports_check_mode=True,
234 mutually_exclusive=[
235 ['congestion_threshold_percentage', 'congestion_threshold_manual'],
236 ]
237 )
238
239 vmware_datastore = VMwareDatastore(module)
240 vmware_datastore.configure()
241
242
243 if __name__ == '__main__':
244 main()