Groovy - Access variable which has value of another variable - groovy

I've been trying to use a common property file in Jenkins which will have details of multiple servers. Based on the selection in Jenkins(By selecting "Build with parameters"), corresponding server details need to be obtained from the property file. For this, I need to access a value of variable created by value of another variable. Is this supported in groovy?
I have defined the properties in a property file and the sample values are like
PROD_SERVERNAME = sampleprodserver;
DEV_SERVERNAME = sampledevserver;
def environment = "PROD"; // this will be given as a parameter
def servername = environment + "_SERVERNAME";
def Propertyfile = readProperties file:propertyfile;
def server = Propertyfile.servername
I expect the value of server should be sampleprodserver but the value i'm getting is null.
Any help would be highly appreciated.

the code
Propertyfile.servername
tries to get property with name servername from Propertyfile variable
and to get the property value by variable value use one of:
Propertyfile.getProperty(servername)
//or
Propertyfile[servername]

Related

"Dereference" a sub-resource in the Azure Python SDK return value

I would like to retrieve the public IP address associated with a given network interface. I need to do something like
client = NetworkManagementClient(...)
interface = client.network_interfaces.get('rg', 'nic-name')
ip_config_id = interface[0].public_ip_address.id
ip_config = some_magic(ip_config_id) # What goes here?
return ip_config.ip_address
This question suggests that in order to implement the some_magic routine, I should parse the ID (by splitting on slashes) and call client.public_ip_addresses.get(). This question indicates that I can call resource_client.resources.get_by_uid, but that doesn't return a PublicIPAddress object (I know I can call as_dict on it and get the data that way).
Is there a way to get an object of the appropriate type (in this case PublicIPAddress) from an object's ID in Azure (without manually parsing the ID)?
Update:
Due to this issue: public_ip_address method within NetworkManagementClient will not return values, we cannot fetch the ip address from PublicIPAddress.
So currently, you can use any other workaround, For example:
myip = client.public_ip_addresses.get(" resource_group_name","public_ip_address_name")
print(myip.ip_address)
You can change this line of code ip_config_id = interface[0].public_ip_address.id to something like my_public_ip_address = interface.ip_configurations[0].public_ip_address, then the return type is PublicIPAddress.
For example:

How to reference a class from a string which is part of a variable 3.7

I have just read a text file and extracted a string and stored it as a variable. This string also happens to be the name of a class I want to reference in order to use in a function for example. The "which_class" variable is the whichever class was stored in the file
I tried passing the which_class variable in as a parameter to the function. Removing the quotations seems to make it work but I am unsure how to do this.
class needed_for_func_one():
multiplier = 1.23
class needed_for_func_two():
multiplier = 1.15
def random_function(which_class):
print(123 * which_class.multiplier)
PSEUDO CODE
READ FROM FILE STORE STRING AS "which_class"
which_class = "needed_for_func_two"
random_function(which_class)
But this didn't work it just gave me an attribute error
the eval function could help you here.
random_function(eval(whichClass))
However, you should probably rethink whether you really want to it that way or if there is a much cleaner solution.
I think your question is related to this one
How you call the function depends if it is a global function or if it is inside an object.
globals()['call_this_function']() # this is probably what you need
or
getattr(from_this_object, 'call_this_function')()
first, to use a class you need an object of a class.
so if what you read is a name of the class or any other thing it does not matter, just use an if statement to decide what is inside that variable so-called "which_class".
then create an object like :
if which_class=="needed_for_func_one":
newObject = needed_for_func_one()
elseif which_class=="needed_for_func_two":
newObject = needed_for_func_two()
then use the print like :
print(123 * newObject.multiplier )

Get/Update "REST Request Properties" value in SOAPUI is coming NULL

I am trying to get the Property value and Set it with different value in SoapUI's "REST Request Properties" (NOT the custom properties). It just gives me NULL value
Here is what I did:
1. Get test step object
2. get the property value with name of the property => It is giving me null value.
I know I am getting the correct object as I was able to rename the same Test Step name with following code
def restRequest = testRunner.testCase.getTestStepByName("Test");
def a = restRequest.getPropertyValue("Method")
log.info(a) // this gives null
restRequest.setName("Test1") // This works
In the step object, there is another object called testRequest from which you can get all those required properties.
For example if you want to get all the properties
log.info step.testRequest.metaClass.methods*.name
For example if you want to know the get methods
log.info step.testRequest.metaClass.methods*.name.findAll {it.startsWith('get')}
Similarly you can get the methods to set the value as well.
For instance, you want to modify Pretty Print from true to false:
step.testRequest.setPrettyPrint(false)
log.info step.testRequest.properties['prettyPrint']
Similarly, you can find the required property name, find the right method to modify the value as per your needs.

Trying to point a puppet variable with the content of others variables

I'm writing a puppet code and need to point to a variable with the information of others.... to make it clear here is the example:
These are the variables with the information:
Array $users_ap1_dev = ['userdev1,userdev2'],
Array $users_ap2_prd = ['userprd1,userprd2'],
but ap1 and ap2 values are store in a fact calles main_app and dev and prd values are store in a fact called env.
I want to retrieve the info and create the user based on the fact information, something like
$dmz_users.each | String $user |{
user { $user:
ensure => 'present',
}
So, how can i put the content of $users_ap1_dev into dmz_users, replacing ap1 and dev with the one store in the fact?
something like?:
Array $dmz_user = "${users_${main_app}_${env}}"
Thanks a lot in advance for the help!
I found the answer, the function: getvar()
It allows you to create a variable and put his content into another...
How to use it:
$dmz_users = getvar("users_${main_app}_${env}")
So let's says $main_app = web and '$env = prod', it will take this two values and will make '$dmz_users = $users_web_prod`

Initialize class reference

I get null value of instrumentRented
when I run
public class RentalAgreement
{
private MusicalInstrument instrumentRented;
public RentalAgreement(Customer renter,
RentalDate dateRented,
MusicalInstrument instrumentRented){
customer = renter;
rentalDate = dateRented;
instrumentRented = instrumentRented;
How to initialize MusicalInstrument reference in RentalAgreement?
Use this.instrumentRented = instrumentRented;
Since the parameter has the same name as the field attribute, you need to prefix with an explicit this to specify the scope.
You must instantiate a class with the new operator.
So somehwere in your code must do
instrumentRented = new MusicalInstrument();
before you access it. After you have done this you can execute functions from that class.
instrumentRented.doSomething();
In the above code you seem to pass it in in the constructor, so this means the caller has to instantiate it.
However I would advise to adopt a naming convention where you can see if a variable is a class member or a local variable. In your above code the local variable has the same name as the parameter, so it will not be set as a member variable, instead you assign it to itself. You might get a warning about this, depending on the evironment (Not sure about this but Eclipse definitely warns somethign like this). This is called shadowing, so what you need to do is:
this.instrumentRented = instrumentRented;

Resources