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

Added local accounts workflow sample code for Vcenter Appliance #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
122 changes: 122 additions & 0 deletions samples/vsphere/appliances/local_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env python

"""
* *******************************************************
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
* SPDX-License-Identifier: MIT
* *******************************************************
*
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
"""

from getpass import getpass
from pprint import pprint

from vmware.vapi.vsphere.client import create_vsphere_client
from com.vmware.vapi.std.errors_client import NotFound, InvalidArgument, AlreadyExists

from samples.vsphere.common import sample_cli
from samples.vsphere.common import sample_util
from samples.vsphere.common.ssl_helper import get_unverified_session


class LocalAccounts:

def __init__(self):
parser = sample_cli.build_arg_parser()
args = sample_util.process_cli_args(parser.parse_args())
session = get_unverified_session() if args.skipverification else None
self.client = create_vsphere_client(server=args.server,
Copy link
Contributor

Choose a reason for hiding this comment

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

Either use self.client.appliance.LocalAccounts directly in sample body, or make self.client a method scope variable.

username=args.username,
password=args.password,
session=session)
self.local_accounts = self.client.appliance.LocalAccounts

def run(self):
"""
Running the workflow for Local accounts.
It creates local account and updates its default security settings.
Deletes the created local account at the end.
"""

print("Listing available accounts")
Copy link
Contributor

Choose a reason for hiding this comment

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

Add /n at the beginning of each print? So it will look nicer.

self.list_accounts()

print("Create local account for yourself")
local_user = self.create_local_account()

print("Updating the local accounts security settings")
self.update_local_account_security(local_user)

print("Get information for specific account")
self.get_account_info()

print("Deleting the local account")
self.delete_local_account(local_user)

def get_account_info(self):
try:
username = input("username ::")
pprint(self.local_accounts.get(username))
except NotFound as e:
print("Local Account mentioned is not found")
Copy link
Contributor

Choose a reason for hiding this comment

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

You can raise an ValueError here, instead of just print.


def list_accounts(self):
pprint(self.local_accounts.list())
Copy link
Contributor

Choose a reason for hiding this comment

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

Method is not needed.


def create_local_account(self):
account_created = False
try:
config = self.local_accounts.Config()
print("The following are minimum details required to create local account")
username = input("username of local account ::")
config.password = getpass("password ::")
print("Roles can be operator, admin, superAdmin.")
config.roles = [input("role :: ")]
config.full_name = input("Full name of user ::")

self.local_accounts.create(username=username, config=config)
print("Listing available accounts after creation of " + username)
self.list_accounts()
account_created = True
except AlreadyExists as e:
print("local account is already present")
Copy link
Contributor

Choose a reason for hiding this comment

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

Re-raise exceptions instead of print, so the issues are more obvious.

except InvalidArgument as e:
print(str(e))
return username if account_created else None

def update_local_account_security(self, username):
# update the account security settings with custom default
config = self.local_accounts.UpdateConfig()
config.days_after_password_expiration = 1
config.inactive_after_password_expiration = True
config.warn_days_before_password_expiration = 7
self.list_accounts()
try:
if username is not None:
self.local_accounts.update(username, config)
except NotFound as e:
print("Local Account mentioned is not found")

def delete_local_account(self, username):
if username is not None:
self.local_accounts.delete(username=username)
print("Listing available accounts after deletion of " + username)
self.list_accounts()


def main():
try:
local_accounts = LocalAccounts()
local_accounts.run()
except Exception:
import traceback
traceback.print_exc()
Comment on lines +112 to +118
Copy link
Contributor

Choose a reason for hiding this comment

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

try except doesn't add any value. Remove it.



if __name__ == '__main__':
main()