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