Skip to content

Commit d89d709

Browse files
committed
Added local accounts workflow sample code for Appliance
Signed-off-by: Mahesh kumar<kumahesh@vmware.com>
1 parent 5402ed5 commit d89d709

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2016. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
17+
from vmware.vapi.vsphere.client import create_vsphere_client
18+
from com.vmware.vapi.std.errors_client import NotFound, InvalidArgument, AlreadyExists
19+
20+
from samples.vsphere.common import sample_cli
21+
from samples.vsphere.common import sample_util
22+
from samples.vsphere.common.ssl_helper import get_unverified_session
23+
24+
from getpass import getpass
25+
from pprint import pprint
26+
27+
28+
class LocalAccounts:
29+
30+
def __init__(self):
31+
parser = sample_cli.build_arg_parser()
32+
args = sample_util.process_cli_args(parser.parse_args())
33+
session = get_unverified_session() if args.skipverification else None
34+
self.client = create_vsphere_client(server=args.server,
35+
username=args.username,
36+
password=args.password,
37+
session=session)
38+
self.local_accounts = self.client.appliance.LocalAccounts
39+
40+
def run(self):
41+
"""
42+
Running the workflow for Local accounts.
43+
It creates local account and updates its default security settings.
44+
Deletes the created local account at the end.
45+
"""
46+
47+
print("Listing available accounts")
48+
self.list_accounts()
49+
50+
print("Create local account for yourself")
51+
local_user = self.create_local_account()
52+
53+
print("Updating the local accounts security settings")
54+
self.update_local_account_security(local_user)
55+
56+
print("Get information for specific account")
57+
self.get_account_info()
58+
59+
print("Deleting the local account")
60+
self.delete_local_account(local_user)
61+
62+
def get_account_info(self):
63+
try:
64+
username = input("username ::")
65+
pprint(self.local_accounts.get(username))
66+
except NotFound as e:
67+
print("Local Account mentioned is not found")
68+
69+
def list_accounts(self):
70+
pprint(self.local_accounts.list())
71+
72+
def create_local_account(self):
73+
account_created = False
74+
try:
75+
config = self.local_accounts.Config()
76+
print("The following are minimum details required to create local account")
77+
username = input("username of local account ::")
78+
config.password = getpass("password ::")
79+
print("Roles can be operator, admin, superAdmin.")
80+
config.roles = [input("role :: ")]
81+
config.full_name = input("Full name of user ::")
82+
83+
self.local_accounts.create(username=username, config=config)
84+
print("Listing available accounts after creation of " + username)
85+
self.list_accounts()
86+
account_created = True
87+
except AlreadyExists as e:
88+
print("local account is already present")
89+
except InvalidArgument as e:
90+
print(str(e))
91+
return username if account_created else None
92+
93+
def update_local_account_security(self, username):
94+
# update the account security settings with custom default
95+
config = self.local_accounts.UpdateConfig()
96+
config.days_after_password_expiration = 1
97+
config.inactive_after_password_expiration = True
98+
config.warn_days_before_password_expiration = 7
99+
self.list_accounts()
100+
try:
101+
if username is not None:
102+
self.local_accounts.update(username, config)
103+
except NotFound as e:
104+
print("Local Account mentioned is not found")
105+
106+
def delete_local_account(self, username):
107+
if username is not None:
108+
self.local_accounts.delete(username=username)
109+
print("Listing available accounts after deletion of " + username)
110+
self.list_accounts()
111+
112+
113+
def main():
114+
try:
115+
local_accounts = LocalAccounts()
116+
local_accounts.run()
117+
except Exception:
118+
import traceback
119+
traceback.print_exc()
120+
121+
122+
if __name__ == '__main__':
123+
main()

0 commit comments

Comments
 (0)