Can I loop over a list in Spinnaker expressions? - spring-el

Let's say I have a spinnaker parameter like this:
parameters.region = "us-east1,us-east2,us-west1"
I want to update a Configmap using that parameter, but I'd like to transform that into a list in my yaml file instead of just the string. Is there a way to do that with SPeL?
For instance, here's what I would like it to look like:
data:
regions.yaml: |
regions:
- us-east1
- us-east2
- us-west1
I see that there is a split(,) method I can use, but I can't figure out how I could loop over that in my output.

Related

Rails update remove number from an array attribute?

Is there a way to remove a number from an attibute array in an update? For example, if I want to update all of an alchy's booze stashes if he runs out of a particular type of booze:
Alchy has_many :stashes
Stash.available_booze_types = [] (filled with booze.ids)
Booze is also a class
#booze.id = 7
if #booze.is_all_gone
#alchy.stashes.update(available_booze_types: "remove #booze.id")
end
update: #booze.id may or may not be present in the available_booze_types array
... so if #booze.id was in any of the Alchy.stash instances (in the available_booze_types attribute array), it would be removed.
I think you can do what you want in the following way:
if #booze.is_all_gone
#alchy.stashes.each do |stash|
stash.available_booze_types.delete(#booze.id)
end
end
However, it looks to me like there are better ways to do what you are trying to do. Rails gives you something like that array by using relations. Also, the data in the array will be lost if you reset the app (if as I understand available_booze_types is an attribute which is not stored in a database). If your application is correctly set up (an stash has many boozes), an scope like the following in Stash class seems to me like the correct approach:
scope :available_boozes, -> { joins(:boozes).where("number > ?", 0) }
You can use it in the following way:
#alchy.stashes.available_boozes
which would only return the ones that are available.

Array from Yaml - Golang

I try to create configuration file for my go app what loops through some jobs.
My .yaml file looks like that (array):
jobToRun:
- name: This is my first job to run
sqlToRun: select 1 from some_table
someVariable: 1
- name: Other job to run
sqlToRun: select 2 from some_table
someVariable: 2
I have successfully imported the YAML file and created also structure.
type Service struct {
JobToRun []struct {
Name string `yaml:"name"`
SQLToRun string `yaml:"SqlToRun"`
SomeVariable int `yaml:"someVariable"`
} `yaml:"jobToRun"`
}
But I have no idea how to assign them to variable.
I tried some stuff what work with Json array-s but without any luck.
So I tried to print it to console without any luck:
println(service.JobToRun.name[0])
before that I tried to assign that SQL to my variable (which works if it is not an array item.
var sqlQuery = service.JobToRun.name[0]
And here is what I try to accomplish:
I take the Job parameters from .yaml array and run it.
I am using that kind of array in YAML because it is easiest way to add new jobs.
ah as soon as i posted it i facepalmed.
println(service.JobToRun[0].Name)
so the reason was that "JobToRun is an array not "Name"

Chef - Ruby - How to extract values from a nested array/list

I'm using this json content (I'm open to suggestions on better formatting here):
{"forwardingZones": [{"name": "corp.mycompany.com","dnsServers": ["192.168.0.1","192.168.0.2"]}]}
Note: we may add more items to this list as we scale out, both more IPs and more names, hence the "join(',')" in the end of the code below.
And I'm trying to loop through it to get this result:
corp.mycompany.com=192.168.0.1;192.168.0.2
Using this code:
forward_zones = node['DNS']['forward_zones'].each do |forwarded_zone|
forwarded_zone_name = forwarded_zone['name']
forwarded_zone_dns_servers = forwarded_zone['dns_servers'].join(';')
"#{forwarded_zone_name}=#{forwarded_zone_dns_servers}"
end.join(',')
This is the result that I get:
{"dnsServers"=>["192.168.0.1", "192.168.0.2"], "name"=>"corp.mycompany.com"}
What am i doing wrong...?
x.each returns x. You want x.map.

Concourse CI - array variable

I'm trying to figure out how to create an array with some CIDR ip address in order to have the same array in my pipeline. So here is an example var file:
whitelist-ip-ranges: |-
- 10.0.0.0/24
- 11.0.0.0/24
- 12.0.0.0/24
My pipeline is like:
....
....
....
params:
variables:
loadBalancerSourceRanges:
{{whitelist-ip-ranges}}
And I want it to be:
....
....
....
params:
variables:
loadBalancerSourceRanges:
- 10.0.0.0/24
- 11.0.0.0/24
- 12.0.0.0/24
or
....
....
....
params:
variables:
loadBalancerSourceRanges: [10.0.0.0/24,11.0.0.0/24,12.0.0.0/24]
Inside my helm template I have my values.yaml file I have of course:
loadBalancerSourceRanges: null
and it will be override by the pipeline.
And finaly, in my service file I'm making a loop:
{{if .Values.loadBalancerSourceRanges}}
loadBalancerSourceRanges:
{{range $rangeList := .Values.loadBalancerSourceRanges}}
- {{ $rangeList }}
{{end}}
{{end}}
Does any of you guys was able to do something like that?
I'm sorry, I cannot speak to anything helm based. I can speak for a concourse pipeline, though.
Concourse does not support providing params to tasks that are an array. params are passed in as environment variables to a running task, so they are transformed from YAML to a simple string key-value pair.
If you want to pass more complex information. There are two options:
encode the param as JSON/YAML so that it can be parsed as string from your tasks environment
provide the task an input from a resource, where a file can be provided -- for example an s3 resource with the contents of the loadBalanceSourceRanges
These programmatic ways are examples I've used before to accomplish passing more complex data (ie arrays) to a task.

Build an array from yaml in rails

I'm working on a simple rails app that does SMS. I am leveraging Twilio for this via the twilio_ruby gem. I have 10 different phone numbers that I want to be able to send SMS from randomly.
I know if I do something like this:
numbers = ["281-555-1212", "821-442-2222", "810-440-2293"]
numbers.sample
281-555-1212
It will randomly pull one of the values from the array, which is exactly what I want. The problem is I don't want to hardcode all 10 of these numbers into the app or commit them to version control.
So I'm listing them in yaml (secrets.yml) along with my Twilio SID/Token. How can I build an array out of the 10 yaml fields i.e. twilio_num_1, twilio_num_2, etc, etc so that I can call numbers.sample?
Or is there a better way to do this?
You can also use
twilio_numbers:
- 281-555-1122
- 817-444-2222
- 802-333-2222
thus you don't have to write the numbers in one line.
Figured this out through trial and error.
In secrets.yml
twilio_numbers: ["281-555-1122","817-444-2222","802-333-2222"]
In my code:
Rails.application.secrets.twilio_numbers.sample
Works like a charm.
create a file: config/twilio_numbers.yml
---
- 281-555-1122
- 817-444-2222
- 802-333-2222
and load it in your config/application.rb like this:
config.twilio_numbers = YAML.load_file 'config/twilio_numbers.yml'
you can then access the array from inside any file like this:
Rails.application.config.twilio_numbers
=> ["281-555-1122", "817-444-2222", "802-333-2222"]

Resources