Perl API - environments variables
11 answers
Hi,
I have a perl script that works with the Build Forge through the API.
I want to check if a value exist in one of my environment variable, and if not to add it.
I tried to look in the documentation, but i can't find it.
Please help,
Thanks
shiran
Hi shiran,
I have pasted some code that may help you. It looks in environment for a variable and changes the value of the variable if it doesn't contain the expected value.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
if ($envEntry->getValue() ne 'EXPECTED_VALUE') {
my $oldValue = $envEntry->getValue();
$envEntry->setValue('NEW_VALUE');
$envEntry = $envEntry->update();
print "Changed the value of " . $varName . " from " . $oldValue . " to " . $envEntry->getValue() . ".\n";
} else {
print "Variable " . $varName . " already contains the expected value.\n";
}
$conn->logout();
$conn->close();
bju
Hi,
I have a perl script that works with the Build Forge through the API.
I want to check if a value exist in one of my environment variable, and if not to add it.
I tried to look in the documentation, but i can't find it.
Please help,
Thanks
shiran
Hi shiran,
I have pasted some code that may help you. It looks in environment for a variable and changes the value of the variable if it doesn't contain the expected value.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
if ($envEntry->getValue() ne 'EXPECTED_VALUE') {
my $oldValue = $envEntry->getValue();
$envEntry->setValue('NEW_VALUE');
$envEntry = $envEntry->update();
print "Changed the value of " . $varName . " from " . $oldValue . " to " . $envEntry->getValue() . ".\n";
} else {
print "Variable " . $varName . " already contains the expected value.\n";
}
$conn->logout();
$conn->close();
bju
Hi bju,
I am sorry, i forgot to mention it is a variable with pull down list so i need to check if the option exist in the list and of not add it.
Thanks a lot
shiran
Hi,
I have a perl script that works with the Build Forge through the API.
I want to check if a value exist in one of my environment variable, and if not to add it.
I tried to look in the documentation, but i can't find it.
Please help,
Thanks
shiran
Hi shiran,
I have pasted some code that may help you. It looks in environment for a variable and changes the value of the variable if it doesn't contain the expected value.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
if ($envEntry->getValue() ne 'EXPECTED_VALUE') {
my $oldValue = $envEntry->getValue();
$envEntry->setValue('NEW_VALUE');
$envEntry = $envEntry->update();
print "Changed the value of " . $varName . " from " . $oldValue . " to " . $envEntry->getValue() . ".\n";
} else {
print "Variable " . $varName . " already contains the expected value.\n";
}
$conn->logout();
$conn->close();
bju
Hi bju,
I am sorry, i forgot to mention it is a variable with pull down list so i need to check if the option exist in the list and of not add it.
Thanks a lot
shiran
Hi,
I've pasted some code for environment entry option. I have to warn you that there appears to be a bug in the Perl client because the getOption/getOptions functions on the EnvironmentEntry don't seem to return any of the options.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $optionName = 'Model Office';
my $optionValue = 'MO';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
die('Env entry is not a pull down variable type entry') unless ($envEntry->getVariableType() eq 'PULLDOWN');
my $envEntryOption = $envEntry->getOption($optionName);
if (!defined($envEntryOption)) {
$envEntryOption = new BuildForge::Services::DBO::EnvironmentEntryOption($conn);
$envEntryOption->setName($optionName);
$envEntryOption->setValue($optionValue);
$envEntryOption = $envEntryOption->update();
$envEntry->addOption($envEntryOption);
$envEntry = $envEntry->update();
print "Added env entry option named " . $optionName . " with value of " . $optionValue . " to env entry named " . $envEntry->getName() . ".\n";
} else {
print "The option " . $optionName . " already exists for variable " . $varName . " and contains value of " . $envEntryOption->getValue() . ".\n";
}
$conn->logout();
$conn->close();
bju
Hi,
I have a perl script that works with the Build Forge through the API.
I want to check if a value exist in one of my environment variable, and if not to add it.
I tried to look in the documentation, but i can't find it.
Please help,
Thanks
shiran
Thasnk you,
So because i don't get ane information on envEntryOption veriable it will always create the option even if it exist.
There is a bug open on this issue? do you know?
Hi shiran,
I have pasted some code that may help you. It looks in environment for a variable and changes the value of the variable if it doesn't contain the expected value.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
if ($envEntry->getValue() ne 'EXPECTED_VALUE') {
my $oldValue = $envEntry->getValue();
$envEntry->setValue('NEW_VALUE');
$envEntry = $envEntry->update();
print "Changed the value of " . $varName . " from " . $oldValue . " to " . $envEntry->getValue() . ".\n";
} else {
print "Variable " . $varName . " already contains the expected value.\n";
}
$conn->logout();
$conn->close();
bju
Hi bju,
I am sorry, i forgot to mention it is a variable with pull down list so i need to check if the option exist in the list and of not add it.
Thanks a lot
shiran
Hi,
I've pasted some code for environment entry option. I have to warn you that there appears to be a bug in the Perl client because the getOption/getOptions functions on the EnvironmentEntry don't seem to return any of the options.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $optionName = 'Model Office';
my $optionValue = 'MO';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
die('Env entry is not a pull down variable type entry') unless ($envEntry->getVariableType() eq 'PULLDOWN');
my $envEntryOption = $envEntry->getOption($optionName);
if (!defined($envEntryOption)) {
$envEntryOption = new BuildForge::Services::DBO::EnvironmentEntryOption($conn);
$envEntryOption->setName($optionName);
$envEntryOption->setValue($optionValue);
$envEntryOption = $envEntryOption->update();
$envEntry->addOption($envEntryOption);
$envEntry = $envEntry->update();
print "Added env entry option named " . $optionName . " with value of " . $optionValue . " to env entry named " . $envEntry->getName() . ".\n";
} else {
print "The option " . $optionName . " already exists for variable " . $varName . " and contains value of " . $envEntryOption->getValue() . ".\n";
}
$conn->logout();
$conn->close();
bju
Hi,
Yes, when I ran the last script that I pasted - it did create the option even if it already existed because each time getOption or getOptions was called, it always returned the value as undefined or containing zero options. I'm not aware of a defect for this yet, so if you wanted to open a defect through the jazz.net site that is an option.
I believe the Java client does work for these methods, so that could be another option although I realize the rest of your script is in Perl.
bju
Yes, when I ran the last script that I pasted - it did create the option even if it already existed because each time getOption or getOptions was called, it always returned the value as undefined or containing zero options. I'm not aware of a defect for this yet, so if you wanted to open a defect through the jazz.net site that is an option.
I believe the Java client does work for these methods, so that could be another option although I realize the rest of your script is in Perl.
bju
Hi,
Yes, when I ran the last script that I pasted - it did create the option even if it already existed because each time getOption or getOptions was called, it always returned the value as undefined or containing zero options. I'm not aware of a defect for this yet, so if you wanted to open a defect through the jazz.net site that is an option.
I believe the Java client does work for these methods, so that could be another option although I realize the rest of your script is in Perl.
bju
OK, thanks for your help, i opened a bug.
Hi,
Yes, when I ran the last script that I pasted - it did create the option even if it already existed because each time getOption or getOptions was called, it always returned the value as undefined or containing zero options. I'm not aware of a defect for this yet, so if you wanted to open a defect through the jazz.net site that is an option.
I believe the Java client does work for these methods, so that could be another option although I realize the rest of your script is in Perl.
bju
OK, thanks for your help, i opened a bug.
IBM answer:
Resolution: Unresolved --> Works as Designed
Comments:
added: In order to get the environment entry options, the environment entry must be gotten directly, via the BuildForge::Services::DBO::EnvironmentEntry::findByUuid($connection, $environmentEntryUuid) command. (This is documented under this function.) If the environment entry is gotten from the parent environment group's $environment->getEntry() or $environment->getEntries() functions, getOption() and getOptions() will return nothing. A snippet of code to get an environment entry's options:
$var = $environment->getEntry('VARNAME');
$var = BuildForge::Services::DBO::EnvironmentEntry::findByUuid($conn, $var->getUuid());
$opts = $var->getOptions();
Hi,
With your information, I've modified the script. It worked for me.
With your information, I've modified the script. It worked for me.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $optionName = 'Model Office';
my $optionValue = 'MO';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
die('Env entry is not a pull down variable type entry') unless ($envEntry->getVariableType() eq 'PULLDOWN');
$envEntry = BuildForge::Services::DBO::EnvironmentEntry->findByUuid($conn, $envEntry->getUuid());
die('Could not find env entry by UUID') unless defined($envEntry);
my $envEntryOption = $envEntry->getOption($optionName);
if (!defined($envEntryOption)) {
$envEntryOption = new BuildForge::Services::DBO::EnvironmentEntryOption($conn);
$envEntryOption->setName($optionName);
$envEntryOption->setValue($optionValue);
$envEntryOption = $envEntryOption->update();
$envEntry->addOption($envEntryOption);
$envEntry = $envEntry->update();
print "Added env entry option named " . $optionName . " with value of " . $optionValue . " to env entry named " . $envEntry->getName() . ".\n";
} else {
print "The option " . $optionName . " already exists for variable " . $varName . " and contains value of " . $envEntryOption->getValue() . ".\n";
}
$conn->logout();
$conn->close();
Hi,
With your information, I've modified the script. It worked for me.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $optionName = 'Model Office';
my $optionValue = 'MO';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
die('Env entry is not a pull down variable type entry') unless ($envEntry->getVariableType() eq 'PULLDOWN');
$envEntry = BuildForge::Services::DBO::EnvironmentEntry->findByUuid($conn, $envEntry->getUuid());
die('Could not find env entry by UUID') unless defined($envEntry);
my $envEntryOption = $envEntry->getOption($optionName);
if (!defined($envEntryOption)) {
$envEntryOption = new BuildForge::Services::DBO::EnvironmentEntryOption($conn);
$envEntryOption->setName($optionName);
$envEntryOption->setValue($optionValue);
$envEntryOption = $envEntryOption->update();
$envEntry->addOption($envEntryOption);
$envEntry = $envEntry->update();
print "Added env entry option named " . $optionName . " with value of " . $optionValue . " to env entry named " . $envEntry->getName() . ".\n";
} else {
print "The option " . $optionName . " already exists for variable " . $varName . " and contains value of " . $envEntryOption->getValue() . ".\n";
}
$conn->logout();
$conn->close();
Hi,
every time i run this line:
my $env = BuildForge::Services::DBO::Environment->findByName
($conn, $envName);
I get Could not find env message, i tried with few of my environment but it happened with all of them, do you know what can be the problem?
I have installed a new BF server so the environment were imported to the new server.
Thanks,
shiran
Hi,
With your information, I've modified the script. It worked for me.
use strict;
use BuildForge::Services;
my $host = 'localhost';
my $user = 'root';
my $password = 'root';
my $envName = 'TEST_ENV_1';
my $varName = 'TEST_VAR_1';
my $optionName = 'Model Office';
my $optionValue = 'MO';
my $conn = new BuildForge::Services::Connection($host);
my $token = $conn->authUser($user, $password);
my $env = BuildForge::Services::DBO::Environment->findByName($conn, $envName);
die('Could not find env ' . $envName) unless defined($env);
my $envEntry = $env->getEntry($varName);
die('Could not find env entry with name ' . $varName) unless defined($envEntry);
die('Env entry is not a pull down variable type entry') unless ($envEntry->getVariableType() eq 'PULLDOWN');
$envEntry = BuildForge::Services::DBO::EnvironmentEntry->findByUuid($conn, $envEntry->getUuid());
die('Could not find env entry by UUID') unless defined($envEntry);
my $envEntryOption = $envEntry->getOption($optionName);
if (!defined($envEntryOption)) {
$envEntryOption = new BuildForge::Services::DBO::EnvironmentEntryOption($conn);
$envEntryOption->setName($optionName);
$envEntryOption->setValue($optionValue);
$envEntryOption = $envEntryOption->update();
$envEntry->addOption($envEntryOption);
$envEntry = $envEntry->update();
print "Added env entry option named " . $optionName . " with value of " . $optionValue . " to env entry named " . $envEntry->getName() . ".\n";
} else {
print "The option " . $optionName . " already exists for variable " . $varName . " and contains value of " . $envEntryOption->getValue() . ".\n";
}
$conn->logout();
$conn->close();
Hi,
every time i run this line:
my $env = BuildForge::Services::DBO::Environment->findByName
($conn, $envName);
I get Could not find env message, i tried with few of my environment but it happened with all of them, do you know what can be the problem?
I have installed a new BF server so the environment were imported to the new server.
Thanks,
shiran
Check to see if you can manually view the contents of the environment from the console. The relocation process may have missed the environments or the names may have changed slightly.
page 1of 1 pagesof 2 pages