HewlettPackard / oneview-python

Python library for HPE OneView
https://github.com/HewlettPackard/oneview-python/wiki
Apache License 2.0
28 stars 27 forks source link

how to detach a profile from a hardware resource? #140

Closed zeshan1977 closed 3 years ago

zeshan1977 commented 3 years ago

I have an existing server hardware and it is assigjned to a server profile. I want to "DETACH" This profile from the server hardware. I cannot find how do I do this via the API

# Add server hardware with scope uri
#server = server_hardwares.get_by_name(server_name)
server = server_hardwares.get_by_name('ILO2M20050BV.XYZFIII.com')
if server: 
   print(server_name+" Already Exist")
   server.remove(force=True)

ERROR

aceback (most recent call last):

  File "/Users/mzeshan/OneDrive - azureford/dev/OMS_ROVReducedOneView_OMS/pythonway/AddRemoveSingleHardware/github_issue_139.py", line 80, in 
    server.remove(force=True)

  File "/Users/mzeshan/opt/anaconda3/lib/python3.7/site-packages/hpeOneView/resources/servers/server_hardware.py", line 99, in remove
    return self.delete(force=force, timeout=timeout)

  File "/Users/mzeshan/opt/anaconda3/lib/python3.7/site-packages/hpeOneView/resources/resource.py", line 63, in __call__
    return self.method(obj, *args, **kwargs)

  File "/Users/mzeshan/opt/anaconda3/lib/python3.7/site-packages/hpeOneView/resources/resource.py", line 198, in delete
    custom_headers=custom_headers, force=force)

  File "/Users/mzeshan/opt/anaconda3/lib/python3.7/site-packages/hpeOneView/resources/resource.py", line 446, in delete
    task, body = self._connection.delete(uri, custom_headers=custom_headers)

  File "/Users/mzeshan/opt/anaconda3/lib/python3.7/site-packages/hpeOneView/connection.py", line 380, in delete
    return self.__do_rest_call('DELETE', uri, {}, custom_headers=custom_headers)

  File "/Users/mzeshan/opt/anaconda3/lib/python3.7/site-packages/hpeOneView/connection.py", line 413, in __do_rest_call
    raise HPEOneViewException(body)

HPEOneViewException: Server /rest/server-hardware/37393150-3032-4D32-3230-303530425633 has an active profile and thus cannot be removed.

SHANDCRUZ commented 3 years ago

Hi @zeshan1977 you need to detach the server hardware from the server profile. The below example will unassign the server hardware from the server profile.

Import the server Profile and Hardware resource.

server_profile = oneview_client.server_profiles server_hardwares = oneview_client.server_hardware

assign the Server Name

server_name = 'ILO2M20050BV.XYZFIII.com'

Get the server Hardware by name

server = server_hardwares.get_by_name(server_name)

Ensure the Server Hardware is in OFF State

value = {"powerState": "Off", "powerControl":"MomentaryPress"} server.patch('replace', '/powerState', value)

Get the Server profile attached to this Hardware

profile = server_profile.get_by_uri(server.data['serverProfileUri'])

detaching the server profile from the hardware

detach_profile = profile.data.copy() detach_profile['serverHardwareUri'] = None detached = profile.update(detach_profile)

Ensure the Server profile is removed from the Hardware

server = server_hardwares.get_by_name(server_name) if server.data['serverProfileUri'] == None: print("Server Profile detached successfully from the Server Hardware")

zeshan1977 commented 3 years ago

Thank You. Intimate knowlwedge of payload is neccessary. Please guide me on online docimentation so that I am able to deduce this for myself. Thanks