Skip to content
Merged
Prev Previous commit
Next Next commit
modified_example
  • Loading branch information
SHANDCRUZ committed Apr 27, 2021
commit df06a7c9b4bf3be2801022c39374c305087b3799
49 changes: 32 additions & 17 deletions examples/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
}
}

# Try load config from a file (if there is a config file)
config = try_load_from_file(config)

oneview_client = OneViewClient(config)
users = oneview_client.users
scopes = oneview_client.scopes

# Get the scope Uri
scope_options = {
"name": "SampleScopeForTest",
"description": "Sample Scope description"
}
scope = scopes.get_by_name(scope_options['name'])
if not scope:
scope = scopes.create(scope_options)
scope_uri = scope.data['uri']

options = {
'emailAddress': 'testUser@example.com',
'enabled': 'true',
Expand All @@ -38,7 +55,7 @@
'permissions': [
{
'roleName': 'Infrastructure administrator',
'scopeUri': '/rest/scopes/6cf6d4da-1b5e-4322-9dff-6ef545ad700f'
'scopeUri': scope_uri
}
],
'type': 'UserAndPermissions',
Expand Down Expand Up @@ -77,11 +94,6 @@
'userName': 'testUser2'
}
]
# Try load config from a file (if there is a config file)
config = try_load_from_file(config)

oneview_client = OneViewClient(config)
users = oneview_client.users

# Create a User
user = users.create(options)
Expand All @@ -90,14 +102,14 @@

# Create a Multiple Users
multi_user = users.create_multiple_user(multi_users)
print("Created multiple users successfully.\n")
print("\nCreated multiple users successfully.\n")
print(multi_user)

# Updata the user
data = user.data.copy()
data["password"] = "change1234"
updated_user = user.update(data)
print("The users is updated successfully....\n")
print("\nThe users is updated successfully....\n")
print(updated_user.data)

# Add role to userName
Expand All @@ -107,7 +119,7 @@
}
]
role = users.add_role_to_userName("testUser1", role_options)
print("Successfully added new role to existing one....\n")
print("\nSuccessfully added new role to existing one....\n")
print(role)

# Update role to userName (it will replace entrie role with specified role)
Expand All @@ -121,20 +133,20 @@
]

role = users.update_role_to_userName("testUser1", role_options)
print("Successfully updated the role to the username....\n")
print("\nSuccessfully updated the role to the username....\n")
print(role)

# Remove a role from the user
role = users.remove_role_from_username("testUser1", "Scope administrator")
print("Removed role from the user successfully...\n")
print("\nRemoved role from the user successfully...\n")
print(role)

# Get user by name
user = users.get_by_userName(options['userName'])
print("Found user by uri = '%s'\n" % user.data['uri'])
print("\nFound user by uri = '%s'\n" % user.data['uri'])

# Get all users
print("Get all users")
print("\nGet all users")
all_users = users.get_all()
pprint(all_users)

Expand All @@ -148,24 +160,26 @@

# Get the user's role list
rolelist = users.get_role_by_userName("testUser")
print(">> Got all the roles for the users")
print("\n>> Got all the roles for the users\n")
print(rolelist)

# Get by role
role = users.get_user_by_role("Infrastructure administrator")
print(">> Got the users by role name\n")
print("\n>> Got the users by role name\n")
print(role)

# Remove single user
user_to_delete = users.get_by_userName("testUser")
user_to_delete.delete()
print("Successfully deleted the testuser2 user.....\n")
print("\nSuccessfully deleted the testuser2 user.....\n")

# Remove Multiple users
user_name = ["testUser1", "testUser2"]
users.delete_multiple_user(user_name)
print("Deleted multiple users successfully...\n")
print("\nDeleted multiple users successfully...\n")

# NOTE: The below script changes the default administrator's password during first-time appliance setup only.
'''
# Change Password only during the initial setup of the appliance.
change_password_request = {
"oldPassword": "mypass1234",
Expand All @@ -175,3 +189,4 @@
changePasswordResponse = users.change_password(change_password_request)
print("Changed Password successfully")
print(changePasswordResponse)
'''