Terraform variable user inputs long key value pair - terraform

I am using a 3rd party app called Morpheus (not a fan of it but its enforced)
I built some modules which are easy enough to request the end user selecting a "Catalog item" to be provisioned and asked for name, and drop down list with ex: instance size, etc.
My issue is thou, I need the user to enter in 1 long string of key value pair to be added to an attribute.
for example
parameters = [var.params]
and the end user inputs in the text field
"example1": "value1", "example2": "value2"
but of course this does not work. I tried {"example1": "value1", "example2": "value2"} and
parameters = [jsondecode(var.params)]
also does not work. Stuck on what / how to request end user to enter in the values in a specific format. The txt box is 1 liner btw :(, hence why im not a fan of morpheus but whatev's

Related

How to check if input is a key in dictionary even partially?

Lets say you have a dictionary that has a grocery item (key) and its price(value). We ask the user for input (grocery item) and they type in whatever. They dont know what item is in the dictionary or not. Im trying to specify that even if the input contains 1 word of the item in the dictionary, to use that as the key and get the price.
So if the official key in the dictionary was "Milk Gallon", and the user types in "milk", how can i make them reference the same thing? If they type milk i want the output to be the value of "Milk gallon"?
you can use if in
you can try this:-
store = {"milk gallon":100,"tomato":50,"fish":190}
user = input("Whatever you want-->")
for i in store:
if user.lower() in i:
user = i
print(user)

Insert values into API request dynamically?

I have an API request I'm writing to query OpenWeatherMap's API to get weather data. I am using a city_id number to submit a request for a unique place in the world. A successful API query looks like this:
r = requests.get('http://api.openweathermap.org/data/2.5/group?APPID=333de4e909a5ffe9bfa46f0f89cad105&id=4456703&units=imperial')
The key part of this is 4456703, which is a unique city_ID
I want the user to choose a few cities, which then I'll look through a JSON file for the city_ID, then supply the city_ID to the API request.
I can add multiple city_ID's by hard coding. I can also add city_IDs as variables. But what I can't figure out is if users choose a random number of cities (could be up to 20), how can I insert this into the API request. I've tried adding lists and tuples via several iterations of something like...
#assume the user already chose 3 cities, city_ids are below
city_ids = [763942, 539671, 334596]
r = requests.get(f'http://api.openweathermap.org/data/2.5/groupAPPID=333de4e909a5ffe9bfa46f0f89cad105&id={city_ids}&units=imperial')
Maybe a list is not the right data type to use?
Successful code would look something like...
r = requests.get(f'http://api.openweathermap.org/data/2.5/group?APPID=333de4e909a5ffe9bfa46f0f89cad105&id={city_id1},{city_id2},{city_id3}&units=imperial')
Except, as I stated previously, the user could choose 3 cities or 10 so that part would have to be updated dynamically.
you can use some string methods and list comprehensions to append all the variables of a list to single string and format that to the API string as following:
city_ids_list = [763942, 539671, 334596]
city_ids_string = ','.join([str(city) for city in city_ids_list]) # Would output "763942,539671,334596"
r = requests.get('http://api.openweathermap.org/data/2.5/group?APPID=333de4e909a5ffe9bfa46f0f89cad105&id={city_ids}&units=imperial'.format(city_ids=city_ids_string))
hope it helps,
good luck

Converting nested attributes from string to numeric

I have a GALLERY model containing nested SLIDE attributes. Some of my attributes are enum values. Without intervention my params hash receives the enum values as "0" or "1". This results in an error e.g. '0' is not a valid gallery type.
Here is an example of my params hash:
{"name"=>"Video quis terrax", "gallery_type"=>"0",
"slides_attributes"=>{"0"=>{"id"=>"2", "order"=>"0",
"slide_type"=>"0", "image_cache"=>"", "embedded_url"=>"",
"_destroy"=>"0"},
"1441761800650"=>{"order"=>"0", "slide_type"=>"0",
"image"=>#,
#original_filename="large_veh_photo.jpg",
#content_type="image/jpeg", #headers="Content-Disposition: form-data;
name=\"gallery[slides_attributes][1441761800650][image]\";
filename=\"large_veh2.jpg\"\r\nContent-Type: image/jpeg\r\n">,
"image_cache"=>"", "embedded_url"=>"", "thumb_text"=>"2nd Image",
"_destroy"=>"0"},
"1441761824429"=>{"order"=>"0", "slide_type"=>"0",
"image_cache"=>"", "embedded_url"=>"", "_destroy"=>"0"}}}
You will notice that there are 3 slides in this example. The first one is identified by "0", the 2nd one by "1441761800650" and the 3rd one by "1441761824429". The first slide has already been saved. The 2nd one is a new slide and the 3rd one has no details filled in. These long number are automatically allocated by Rails in the view.
How can I change the "order" and "slide_type" attributes to numeric values before the controller attempts to do
#gallery.update(gallery_params)
Do I have any control over the random numbers assigned to my slides in the view?
You need to clean up the params in the controller. You should be able to do something like this:
Class SomeController < ActionController
before_filter: clean_gallery_params
def clean_gallery_params
params[:slides_attributes].each do |slide_attribute|
params[:slides_attributes][slide_attribute][:order] = params[:slides_attributes][slide_attribute][:order].to_i
params[:slides_attributes][slide_attribute][:slide_type] = params[:slides_attributes][slide_attribute][:slide_type].to_i
end
end
...

Can I set the default value of a custom list column to be a new Guid?

I tried setting the defaultvalue property of the field to Guid.NewGuid() but every item created has the same guid so I guess the Guid.NewGuid() is being stored as the default rather than being run each time.
Is the only way to achieve this to add an event handler to the list for OnAdded?
I'm assuming you're using a Single Line of Text field for this. The standard default for such a field is always a constant, you can't assign a variable or function via the object model. All that would do is assign the static result of that particular call of the function.
While text fields can support a calculated default value, it uses the same functions that are in Calculated columns, which do not support random numbers.
Your best bet is to use an Event Handler, I would recommend ItemAdding over ItemAdded as well. You'd be assigning to properties.AfterProperties["fieldname"] instead of field.DefaultValue, of course.
If you are creating the field through code and setting the field.DefaultValue = Guid.NewGuid(), this will run the Guid.NewGuid() and store the returned Guid as the default
It is the equlivant of running the folowing code:
Guid newGuid = Guid.NewGuid();
string newGuidString = newGuid.ToString();
field.DefaultValue = newGuidString;
I dont know of any method you can use to set the field to generate a new Guid on item creation other than using an Event handler.
It should be posable to genrate a random number using the field.DefaultValue = "RANDBETWEEN(10,20)"; but i have not tested this

UserControl that embeds another user control, how to access embedded form fields?

I have a long form that the user has to fill out.
So I broke the form into logical units and created another user control for some elements (they will be reused elsewhere).
So say the form has these fields:
UserControl3.ascx
Username
password
email -- usercontrol2.ascx
address -- usercontrol2.ascx
city -- usercontrol2.ascx
state -- usercontrol2.ascx
So now in the codebehidn of usercontrol3.ascx, how will I access the usercontrol2.ascx's fields so I can write to the db?
Something like this works, but it is just not elegant:
Dim txtBox as TextBox = Ctype(parentControl.Controls(Index), System.Web.UI.Controls.TextBox)
stringVariable = txtBox.Text
The correct way to do it is to implement properties for your parentControl that access child's controls properties.
Public Property AddressField() as string
Set(byval value as string)
txtAddressField.Text = value
End Set
Get
Return txtAddressField.Text
End Get
End Property
if user control 3 contains user control 2, I would modify the code for user control 2 to expose public properties for the information that you need to retreive.
edit
There are other ways of doing it, but the property route is the safest route, and avoids strong dependencies between the two controls.

Resources