|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +* ******************************************************* |
| 5 | +* Copyright (c) VMware, Inc. 2019. 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 | +from getpass import getpass |
| 17 | +from pprint import pprint |
| 18 | + |
| 19 | +from vmware.vapi.vsphere.client import create_vsphere_client |
| 20 | +from com.vmware.vapi.std.errors_client import NotFound, InvalidArgument, AlreadyExists |
| 21 | + |
| 22 | +from samples.vsphere.common import sample_cli |
| 23 | +from samples.vsphere.common import sample_util |
| 24 | +from samples.vsphere.common.ssl_helper import get_unverified_session |
| 25 | + |
| 26 | +class LocalAccounts: |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + parser = sample_cli.build_arg_parser() |
| 30 | + args = sample_util.process_cli_args(parser.parse_args()) |
| 31 | + session = get_unverified_session() if args.skipverification else None |
| 32 | + self.client = create_vsphere_client(server=args.server, |
| 33 | + username=args.username, |
| 34 | + password=args.password, |
| 35 | + session=session) |
| 36 | + self.local_accounts = self.client.appliance.LocalAccounts |
| 37 | + |
| 38 | + def run(self): |
| 39 | + """ |
| 40 | + Running the workflow for Local accounts. |
| 41 | + It creates local account and updates its default security settings. |
| 42 | + Deletes the created local account at the end. |
| 43 | + """ |
| 44 | + |
| 45 | + print("Listing available accounts") |
| 46 | + self.list_accounts() |
| 47 | + |
| 48 | + print("Create local account for yourself") |
| 49 | + local_user = self.create_local_account() |
| 50 | + |
| 51 | + print("Updating the local accounts security settings") |
| 52 | + self.update_local_account_security(local_user) |
| 53 | + |
| 54 | + print("Get information for specific account") |
| 55 | + self.get_account_info() |
| 56 | + |
| 57 | + print("Deleting the local account") |
| 58 | + self.delete_local_account(local_user) |
| 59 | + |
| 60 | + def get_account_info(self): |
| 61 | + try: |
| 62 | + username = input("username ::") |
| 63 | + pprint(self.local_accounts.get(username)) |
| 64 | + except NotFound as e: |
| 65 | + print("Local Account mentioned is not found") |
| 66 | + |
| 67 | + def list_accounts(self): |
| 68 | + pprint(self.local_accounts.list()) |
| 69 | + |
| 70 | + def create_local_account(self): |
| 71 | + account_created = False |
| 72 | + try: |
| 73 | + config = self.local_accounts.Config() |
| 74 | + print("The following are minimum details required to create local account") |
| 75 | + username = input("username of local account ::") |
| 76 | + config.password = getpass("password ::") |
| 77 | + print("Roles can be operator, admin, superAdmin.") |
| 78 | + config.roles = [input("role :: ")] |
| 79 | + config.full_name = input("Full name of user ::") |
| 80 | + |
| 81 | + self.local_accounts.create(username=username, config=config) |
| 82 | + print("Listing available accounts after creation of " + username) |
| 83 | + self.list_accounts() |
| 84 | + account_created = True |
| 85 | + except AlreadyExists as e: |
| 86 | + print("local account is already present") |
| 87 | + except InvalidArgument as e: |
| 88 | + print(str(e)) |
| 89 | + return username if account_created else None |
| 90 | + |
| 91 | + def update_local_account_security(self, username): |
| 92 | + # update the account security settings with custom default |
| 93 | + config = self.local_accounts.UpdateConfig() |
| 94 | + config.days_after_password_expiration = 1 |
| 95 | + config.inactive_after_password_expiration = True |
| 96 | + config.warn_days_before_password_expiration = 7 |
| 97 | + self.list_accounts() |
| 98 | + try: |
| 99 | + if username is not None: |
| 100 | + self.local_accounts.update(username, config) |
| 101 | + except NotFound as e: |
| 102 | + print("Local Account mentioned is not found") |
| 103 | + |
| 104 | + def delete_local_account(self, username): |
| 105 | + if username is not None: |
| 106 | + self.local_accounts.delete(username=username) |
| 107 | + print("Listing available accounts after deletion of " + username) |
| 108 | + self.list_accounts() |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + try: |
| 113 | + local_accounts = LocalAccounts() |
| 114 | + local_accounts.run() |
| 115 | + except Exception: |
| 116 | + import traceback |
| 117 | + traceback.print_exc() |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + main() |
0 commit comments