VMware {code} Community
brianez21
Contributor
Contributor

Newbie question Regarding Opts::validate()

Hey,

I have just started using the SDK and have a query regarding the opts::validate().

I have implemented perl a script which will loop through an array of vCenters calling a connect subroutine for each vcenter and connect into each one using predefined usernames & passwords.

This works great, however, only if the username & password are correct, if not, the script quits..

I understand that opts::validate, checks all credentials are valid and if not will display an error message & quit.

In a situation where the credentials for a given vcenter may be incorect is there a way to avoid the script quiting and just move on to the next vcenter?

eg : if(opts::validate == true){login}else{next vcenter}

See below for my requirement :

#####

foreach vcenter{

    connect(vcenter, vcenterCredentials)

}

#####

sub connect{

    Opts::set_option('server',      <passed_vcenter>);

    Opts::set_option('username',    <passed_username>);

    Opts::set_option('password',    <passed_password>);

    #~#

    Opts::parse();

    Opts::validate(); <------------- if valid:

                                                  Util::connect()

                                                      ### Do stuff ###

                                                  Util:: disconnect()

                                             else:

                                                     ### Break out of sub connect ###

}

Any help would be appreciated.

Tags (1)
Reply
0 Kudos
1 Reply
MR-Z
VMware Employee
VMware Employee

I believe the authentication process does not occur until connect() time. Wrong credential should not have caused your connection to fail at the validation time. 

If you are managing sessions to multiple servers, you could use a different SDK routine to do that. For example, assume your VCs are in a list of @VC and you can keep all the connections via a hash %VIM, like this:

        foreach my $vc (@VC) {
                $VIM{$vc} = Vim->new(service_url => "https://$vc/sdk");
                $VIM{$vc}->login(user_name => $username, password => $password);
                print color('green') . $vc . color('reset') . " ";
        }
        foreach my $vc (@VC) {
                $VIM{$vc} = Vim->new(service_url => "https://$vc/sdk");
                $VIM{$vc}->login(user_name => $username, password => $password);
        }

 

You can also monitor the VIM connection and handle potential failure there. To do anything with that connection, such as find_entity_view():

my $vm = $VIM{$vc}->find_entity_view(view_type=>'VirtualMachine');

Reply
0 Kudos