Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding CreateVolume functionality #6813

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- redfish_config - add ``CreateVolume`` command to allow creation of volumes on servers (https://github.com/ansible-collections/community.general/pull/6813).
110 changes: 110 additions & 0 deletions plugins/module_utils/redfish_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import random
import string
import re
from ansible.module_utils.urls import open_url
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.common.text.converters import to_text
Expand Down Expand Up @@ -3544,3 +3545,112 @@ def delete_volumes(self, storage_subsystem_id, volume_ids):

return {'ret': True, 'changed': True,
'msg': "The following volumes were deleted: %s" % str(volume_ids)}

def create_volume(self, volume_details, storage_subsystem_id):
# Fail message if run for iLO5 or lower server
vendor = self._get_vendor()['Vendor']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this check up front? Isn't the failure to POST to the VolumeCollection sufficient? Doing version checks for capabilities is not a good practice; the interface itself is self-descriptive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do see your point, but we may benefit from explicitly pointing out that it doesn't work for certain server. Changed the implementation a little, please a have look.

if vendor == 'HPE':
response = self._find_managers_resource()
if response['ret'] is False:
return response

response = self.get_request(self.root_uri + self.manager_uri)
if response['ret'] is False:
return response

resp = response['data']
if 'FirmwareVersion' not in resp.keys():
return {
"ret": False,
"msg": "Key 'FirmwareVersion' not found in response: %s" % (resp)
}

fw_version = resp['FirmwareVersion']
ilo_gen = re.sub(r"\D", "", fw_version).strip()[0]

if int(ilo_gen) < 6:
return {
"ret": False,
"msg": "This operation is not supported for iLO %s servers" % (str(ilo_gen))
}

# Find the Storage resource from the requested ComputerSystem resource
response = self.get_request(self.root_uri + self.systems_uri)
if response['ret'] is False:
return response
data = response['data']
storage_uri = data.get('Storage', {}).get('@odata.id')
if storage_uri is None:
return {'ret': False, 'msg': 'Storage resource not found'}

# Get Storage Collection
response = self.get_request(self.root_uri + storage_uri)
if response['ret'] is False:
return response
data = response['data']

# Collect Storage Subsystems
self.storage_subsystems_uris = [i['@odata.id'] for i in response['data'].get('Members', [])]
if not self.storage_subsystems_uris:
return {
'ret': False,
'msg': "StorageCollection's Members array is either empty or missing"}

# Matching Storage Subsystem ID with user input
self.storage_subsystem_uri = ""
for storage_subsystem_uri in self.storage_subsystems_uris:
if storage_subsystem_uri.split("/")[-2] == storage_subsystem_id:
self.storage_subsystem_uri = storage_subsystem_uri

if not self.storage_subsystem_uri:
return {
'ret': False,
'msg': "Provided Storage Subsystem ID %s does not exist on the server" % storage_subsystem_id}

# Validate input parameters
required_parameters = ['RAIDType', 'Drives', 'CapacityBytes']
allowed_parameters = ['DisplayName', 'InitializeMethod', 'MediaSpanCount',
'Name', 'ReadCachePolicy', 'StripSizeBytes', 'VolumeUsage', 'WriteCachePolicy']

for parameter in required_parameters:
if not volume_details.get(parameter):
return {
'ret': False,
'msg': "%s are required parameter to create a volume" % str(required_parameters)}

# Navigate to the volume uri of the correct storage subsystem
response = self.get_request(self.root_uri + self.storage_subsystem_uri)
if response['ret'] is False:
return response
data = response['data']

# Deleting any volumes of RAIDType None present on the Storage Subsystem
response = self.get_request(self.root_uri + data['Volumes']['@odata.id'])
if response['ret'] is False:
return response
volume_data = response['data']

if "Members" in volume_data:
for member in volume_data["Members"]:
response = self.get_request(self.root_uri + member['@odata.id'])
if response['ret'] is False:
return response
member_data = response['data']

if member_data["RAIDType"] == "None":
response = self.delete_request(self.root_uri + member['@odata.id'])
if response['ret'] is False:
return response

# Construct payload and issue POST command to create volume
volume_details["Links"] = {}
volume_details["Links"]["Drives"] = []
for drive in volume_details["Drives"]:
volume_details["Links"]["Drives"].append({"@odata.id": drive})
del volume_details["Drives"]
payload = volume_details
response = self.post_request(self.root_uri + data['Volumes']['@odata.id'], payload)
TSKushal marked this conversation as resolved.
Show resolved Hide resolved
if response['ret'] is False:
return response
return {'ret': True, 'changed': True,
'msg': "Volume Created"}
32 changes: 30 additions & 2 deletions plugins/modules/redfish_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@
type: bool
default: True
version_added: '7.5.0'
volume_details:
required: false
description:
- Setting dict of volume to be created.
type: dict
default: {}
version_added: '7.4.0'
TSKushal marked this conversation as resolved.
Show resolved Hide resolved
author:
- "Jose Delarosa (@jose-delarosa)"
- "T S Kushal (@TSKushal)"
Expand Down Expand Up @@ -312,6 +319,20 @@
password: "{{ password }}"
storage_subsystem_id: "DExxxxxx"
volume_ids: ["volume1", "volume2"]

- name: Create Volume
community.general.redfish_config:
category: Systems
command: CreateVolume
TSKushal marked this conversation as resolved.
Show resolved Hide resolved
baseuri: "{{ baseuri }}"
username: "{{ username }}"
password: "{{ password }}"
storage_subsystem_id: "DExxxxxx"
volume_details:
Name: "MR Volume"
RAIDType: "RAID0"
Drives:
- "/redfish/v1/Systems/1/Storage/DE00B000/Drives/1"
'''

RETURN = '''
Expand All @@ -330,7 +351,7 @@
# More will be added as module features are expanded
CATEGORY_COMMANDS_ALL = {
"Systems": ["SetBiosDefaultSettings", "SetBiosAttributes", "SetBootOrder",
"SetDefaultBootOrder", "EnableSecureBoot", "SetSecureBoot", "DeleteVolumes"],
"SetDefaultBootOrder", "EnableSecureBoot", "SetSecureBoot", "DeleteVolumes", "CreateVolume"],
"Manager": ["SetNetworkProtocols", "SetManagerNic", "SetHostInterface"],
"Sessions": ["SetSessionService"],
}
Expand Down Expand Up @@ -365,7 +386,8 @@ def main():
sessions_config=dict(type='dict', default={}),
storage_subsystem_id=dict(type='str', default=''),
volume_ids=dict(type='list', default=[], elements='str'),
secure_boot_enable=dict(type='bool', default=True)
secure_boot_enable=dict(type='bool', default=True),
volume_details=dict(type='dict', default={})
),
required_together=[
('username', 'password'),
Expand Down Expand Up @@ -422,6 +444,10 @@ def main():
# Set SecureBoot options
secure_boot_enable = module.params['secure_boot_enable']

# Volume creation options
volume_details = module.params['volume_details']
storage_subsystem_id = module.params['storage_subsystem_id']

# Build root URI
root_uri = "https://" + module.params['baseuri']
rf_utils = RedfishUtils(creds, root_uri, timeout, module,
Expand Down Expand Up @@ -459,6 +485,8 @@ def main():
result = rf_utils.set_secure_boot(secure_boot_enable)
elif command == "DeleteVolumes":
result = rf_utils.delete_volumes(storage_subsystem_id, volume_ids)
elif command == "CreateVolume":
result = rf_utils.create_volume(volume_details, storage_subsystem_id)

elif category == "Manager":
# execute only if we find a Manager service resource
Expand Down