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

Python SDK:Scopes URI Issue #114

Closed zeshan1977 closed 3 years ago

zeshan1977 commented 3 years ago

How do I get scopes assigned to Hardware enrolled in oneview

Assume that I have Four machines enrolled in one view.. Assume that I have two scopes. Machine _1 -> Scope 1 Machine_2 ->Scope 2 Machine_3 ->NO SCOPE Machine_4-> Scope 2

How can I access the above relationship,see the example code. The ScopesURI field is differnet between the Server_Hardware and the scopes object, WHAT is the link betweeen them.see below print out code

def usage(): if len(sys.argv) < 2: print("You have specified too few arguments","Usage >>"+ sys.argv[0]+ " oneviewIP [sample 19.14.3.210]") print("FYI: This works on the basis of account readonly/readonly existing with the local domain of the appliance") sys.exit() try: socket.inet_aton(sys.argv[1]) except socket.error: print("You have an invalid IP Parameter","Usage >>"+ sys.argv[0]+ " oneviewIP [sample 19.14.3.210]") sys.exit()

def parseandSetLoginParams():

This is config json sample with all the attribs

#https://github.com/HewlettPackard/oneview-python/blob/master/examples/config-rename.json
config = {
     "ip": sys.argv[1],
     "credentials": {
         "userName":"readonly",
         "password":"readonly",
         "authLoginDomain":"local"
     }
}
return config

################################################################

Create Session

################################################################

def OneViewAttribs(lconfig): try: oneview_client = OneViewClient(lconfig) except HPOneViewException: print("ERROR:Cannot Connect OV: >>"+ sys.argv[1]+ " oneviewIP [sample: 19.14.3.210]") sys.exit() return oneview_client

#################################################################

Off of the oneView object return , ov_scopes, server_hardwares and the dict pf scope->resource

################################################################

def returnobjectree(object): oneview_client2=object server_hardwares = oneview_client2.server_hardware server_hardware_all = server_hardwares.get_all() print("server_hardware_all ") pprint(server_hardware_all) #################################################################

Get scopes

################################################################
#Step A
ov_scopes=oneview_client2.scopes
retlistdict=ov_scopes.get_all()
print ("retlistdict .ovscopes ---> ")
pprint(retlistdict)
return (retlistdict,server_hardware_all)

if name == "main": usage() returnobjectree(OneViewAttribs(parseandSetLoginParams()))

chebroluharika commented 3 years ago

Hi @zeshan1977, Firstly thanks for pointing out this. And coming to the reason for having different scopesUri's in scopes object and serverhardwares object is because of naming convention. Basically scopesUri represent the URI for the resource scope assignments i.e., uri of the resource having the prefix as "/rest/scopes/resources/" And since the resources (i.e, scopes and serverhardwares) are different, their uris are also different and hence the scopes uris are different. So, to link between them we need to refer uri property in GET:/rest/scopes/resources/** to get scopes associated with a resource. Please find attached code. scopes.txt

zeshan1977 commented 3 years ago

-- I do not understand what you mean by GET:/rest/scopes/resources/** to get scopes associated with a resource, like how do i this? -- When I run what you have attached. I get below error

Find resource assignments to scope when resource unassigned

ERROR:hpOneView.exceptions:Uncaught Exception: AttributeError with message: 'Scopes' object has no attribute 'get_scope_resource' Traceback (most recent call last): File "OV_serverhardware_scope_test_github.zeshan1977.reply.py", line 50, in scope_assigned_resource = scopes.get_scope_resource(server_name.data['uri']) AttributeError: 'Scopes' object has no attribute 'get_scope_resource' Tue Dec 15:10:14:23 517 mzeshan:TA2920387_x86_AUT_1_Sub1

pip freeze |grep hpOneView hpOneView==5.2.0

zeshan1977 commented 3 years ago

Also when I substitute get_Scope_resource by get_by_uri method. I get ERROR : scope_assigned_resource = scopes.get_by_uri(server_name.data['uri'])

HPOneViewUnknownType: Unrecognized URI for this resource File "/Users/mzeshan/opt/anaconda3/lib/python3.7/site-packages/hpOneView/resources/resource.py", line 278, in get_by_uri self._helper.validate_resource_uri(uri)

File "/Users/mzeshan/opt/anaconda3/lib/python3.7/site-packages/hpOneView/resources/resource.py", line 670, in validate_resource_uri raise exceptions.HPOneViewUnknownType(UNRECOGNIZED_URI)

HPOneViewUnknownType: Unrecognized URI for this resource

nabhajit-ray commented 3 years ago

We were able to reproduce the issue and the same was fixed in our oneview-python master branch(not released) .

For the fix you need to install the latest python sdk from the master branch which has the fix for this issue The steps to install are below: $ git clone https://github.com/HewlettPackard/oneview-python.git $ cd oneview-python $ pip install .

I would like to mention that we have changed our package name from hpOneView to hpeOneView , so an usual git pull won't get the latest changes. Instead, you must clone the new repository as mentioned in the steps.

Detailed explanation of the issue: Since the user wants to get the scope uri assigned to a server hardware, so he used _get_byname method and captured the value of scopesUri key.

But instead, user can use _get_scoperesource method which is implemented recently in scopes library and available in master (unreleased).

This new method expects resource uri as input (for e.g. /rest/server-hardware/30373737-3237-4D32-3230-333030314752') and returns the list of scope uris assigned to that resource. Please check the sample code snippet and its output below.

CODE

****Get the scopes assigned to a resource

print("\n## Get the scopes assigned to a resource")

scope_assigned_resource = scopes.get_scope_resource('/rest/server-hardware/30373737-3237-4D32-3230-333030314752')

pprint(scope_assigned_resource.data)

**RESPONSE ***

*** Get the scopes assigned to a resource

{'eTag': '"2020-12-15T05:25:42.107Z/2020-12-15T05:25:42.107Z"',

'resourceUri': '/rest/server-hardware/30373737-3237-4D32-3230-333030314752',

'scopeUris': ['/rest/scopes/93f17ccd-65ee-4721-9b68-0342f38921c5', '/rest/scopes/a40680e9-f9b7-41b3-8506-7e2679ed11f2', '/rest/scopes/8a681342-faf5-46ab-bac1-9954b36276e6'],

'type': 'ScopedResource',

'uri': '/rest/scopes/resources/rest/server-hardware/30373737-3237-4D32-3230-333030314752'}

I hope this resolves the issue you are facing.

zeshan1977 commented 3 years ago

I will test it out and let you know. Thanks

zeshan1977 commented 3 years ago

THis does'nt seem to work.

Assume that I have Four machines enrolled in one view.. Assume that I have two scopes. named Scope_1 and Scope_2

Machine _1.resource_uri -> Scope_1 Machine_2.resource_uri ->Scope_2 Machine_3.resource_uri ->NO SCOPE Machine_4.resource_uri -> Scope_2

How can I access the above relationship,

zeshan1977 commented 3 years ago

rom hpeOneView.oneview_client import OneViewClient

config = { "ip": "19.XX.XXX.XXX", "credentials": { "userName": "readonly", "password": "readonly", "authLoginDomain":"local" } }

server_name = "0000A66101, bay 3"

oneview_client = OneViewClient(config) server_hardwares = oneview_client.server_hardware scopes = oneview_client.scopes

Get list of all server hardware resources

print("Get list of all server hardware resources") server_hardware_all = server_hardwares.get_all() server_names = [] server_names_with_scopeUris = [] for serv in server_hardware_all: server_names.append(serv['name']) server_names_with_scopeUris.append(serv['scopesUri']) print("{} has {} as scopesUri".format(str(serv['name']), str(serv['scopesUri'])))

Get the resource assignments of scope when unassigned

print("\n## Find resource assignments to scope when resource unassigned") for server in server_hardware_all: server_name = server_hardwares.get_by_name(server['name']) scope_assigned_resource = scopes.get_scope_resource(server_name.data['uri']) print("{} has {} as scopesUri".format(str(server_name.data['name']), str(scope_assigned_resource.data['uri'])))

zeshan1977 commented 3 years ago

both loops are printing the same information

VenkateshRavula commented 3 years ago

@zeshan1977 , When you use "get_scope_resource" method for scopes object, you should access "scopeUris" key in the response. Instead you have used "uri" key in the response. Please change and retry. The "scopeUris" key contains list of all the scopes assigned to the server hardware.

# Get the resource assignments of scope when unassigned
print("\n## Find resource assignments to scope when resource unassigned")
for server in server_hardware_all:
    server_name = server_hardwares.get_by_name(server['name'])
    scope_assigned_resource = scopes.get_scope_resource(server_name.data['uri'])
    print("{} has {} as scopesUri".format(str(server_name.data['name']), 
    scope_assigned_resource.data['scopeUris']))
zeshan1977 commented 3 years ago

ok thanks ,I am closing this issue

zeshan1977 commented 3 years ago

It works