I want to create a resource for many regions, environments, apps, etc at once.
I'd like to do something like this:
param apps array = [
'app1'
'app2'
'app3'
]
param environments array = [
'alpha'
'beta'
]
param regions array = [
'ne'
'we'
'uks'
]
resource origin_group 'Microsoft.Cdn/profiles/origingroups#2021-06-01' = [ for region in regions: {
[ for env in environments: {
[ for app in apps: {
parent: profiles_global_fd_name_resource
name: '${env}-${region}-${app}-origin-group'
properties: {
loadBalancingSettings: {
sampleSize: 4
successfulSamplesRequired: 3
additionalLatencyInMilliseconds: 50
}
healthProbeSettings: {
probePath: '/'
probeRequestType: 'HEAD'
probeProtocol: 'Http'
probeIntervalInSeconds: 100
}
sessionAffinityState: 'Disabled'
}
}]
}]
}]
All docs mentioning nested loops talk about looping inside a resource to create many sub-resources. Not what I'm after. Perhaps another way would be to somehow merge all these arrays into a single array of objects of every possible iteration. Not sure where to start with that either.
Any help much appreciated.
This is not supported for the moment but it will (see Is there plans to support nested loop on resources?).
Using a little bit of math, you could achieve what you'd like (Not sure if you should):
param environments array = [ 'alpha', 'beta' ]
param regions array = [ 'ne', 'we', 'uks' ]
param apps array = [ 'app1', 'app2', 'app3' ]
// Setting some variables for clarity
var envCount = length(environments)
var regionCount = length(regions)
var appCount = length(apps)
// Setting the total number of combination
var originGroupCount = envCount * regionCount * appCount
// Iterate all possible combinations
output originGroupNames array = [for i in range(0, originGroupCount): {
name: '${environments[i / (regionCount * appCount) % envCount]}-${regions[i / appCount % regionCount]}-${apps[i % appCount]}-origin-group'
}]
this will output all possible combinations (I think) for origin group name.
Related
I'm trying to figure out how to access the last index of a nested array. The array I am working with is provided by an API call and is VERY LARGE so I'll shorten it below for convenience.
The array:
{
"result": {
"86400": [
[
1381190400,
123.610000,
123.610000,
123.610000,
123.610000,
0.100000,
0.0
],
[
1381276800,
123.610000,
124.190000,
123.900000,
124.180000,
3.991600,
0.0
],
...
[
1600646400,
11078,
11078,
10906.9,
10950.4,
623.00835437,
6841501.73480653
]
]
},
"allowance": {
"cost": 0.015,
"remaining": 9.985,
"upgrade": "For unlimited API access, create an account at ________"
}
}
I would like to access the last index of ['result']['86400'] , which contains:
[
1600646400,
11078,
11078,
10906.9,
10950.4,
623.00835437,
6841501.73480653
]
I am using Flutter in my code, so here is what I have tried:
http.Response historicalResponse = await http.get(historicalRequestURL);
var decodedHistorical = jsonDecode(historicalResponse.body);
var historicalPrice = decodedHistorical['result']['86400'.length - 1][0];
print(historicalPrice);
This causes a few errors stating:
"NoSuchMethodError: The method '[]' was called on null."
"Receiver: null"
"Tried calling: [](0)"
I believe ['86400'.length - 1] is causing the error.
I have also tried using
var historicalPrice = decodedHistorical['result']['86400'][decodedHistorical.length - 1][0];
instead. This doesn't cause an error but it gives me the length of the outer array, which is 2 but I need the length of the inner array named '86400'.
You should implement below way
void parseJson() async {
http.Response historicalResponse = await http.get(historicalRequestURL);
Map decodedHistorical = jsonDecode(historicalResponse.body);
List selectedMap = decodedHistorical['result']['86400'];
var finalValue = selectedMap.last;
print(finalValue);
}
Instead of using var, use List so that you can access its last element by historicalPrice.last getter.
List historicalPrice = decodedHistorical['result']['86400'];
List lastElement=historicalPrice.last;
i would like to build a list of maps in terraform. Is there an operation that would let me do that.
eg: i would like to build the structure (from aws_instance.test.*.private_ip)
addresses = [
{
address = private_ip-1
},
{
address = private_ip-2
}
...
]
Apologies if this has already been resolved, I see that it's an older question.
I'm not sure if this is best practice, but this is the way I would attempt to reach the desired state.
Using the null_resource you'd be able to do the following:
variable "addresses" {
type = "list"
default = [
"private_ip-1",
"private_ip-2",
"private_ip-3"
]
}
resource "null_resource" "test" {
count = "${length(var.addresses)}"
triggers {
address = "${element(var.addresses, count.index)}"
}
}
output "addresses" {
value = "${null_resource.test.*.triggers}"
}
And have this output:
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
addresses = [
{
address = private_ip-1
},
{
address = private_ip-2
},
{
address = private_ip-3
}
]
I'm currently on terraform 0.11.5, null_resource appears to have been added in 0.6.7
There are limitations to the null_resource triggers though. interpolated variables can only result in strings. So, unfortunately you won't be able to interpolate a value that would result in a list or a map; for example:
resource "null_resource" "test" {
triggers {
mylist = "${var.addresses}"
}
}
will result in an error
Error: null_resource.test: triggers (address): '' expected type 'string', got unconvertible type '[]interface {}'
With terraform 10.8 you can
output "private_subnets" {
value = [
"${aws_subnet.private.*.id}"
]
}
https://github.com/hashicorp/terraform/issues/7430
I have the following code where I design the format of a JSON-array dynamically from variables ==>
var Title = '"text_title"'
var Topic2 = '"text_topic2"'
var jsonData = [ ];
createWantedJSON("title", data[i].topic, jsonData)
function createWantedJSON(title, Topic2, arr) {
arr.push({
"Topic": title,
[Topic2] : {
"Topic3": [{
"Subitem1": "Text1",
"Subitem2": "Text2",
"Subitem3": "Text3"
}]
}
}
This goes fine for the key and value which are not nested. But I don't know how to make it dynamic for the content of Topic3. I tried making a variable with the following content =>
'"Subitem1": "Text1", "Subitem2": "Text2", "Subitem3": "Text3"' but then of course it sees the content as one string and not as elements of the nested Topic3 array. I guess this isn't that hard but I can't seem to fix it.
I have this javascript array:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
]);
Im sending a request to my server using ajax and Im reciving as response this json objects:
[{"PRICE":"1.10662","TIME":"1467923352"},{"PRICE":"1.10663","TIME":"1467923353"}]
What im trying to do is to parse the response and push the objects inside of my array so the final result should be this:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
['1.10662', 1467923352, ],
['1.10663', 1467923353, ],
]);
Please keep in mind the number of objets I receive as response changes in every request. In the example Im giving is received just two objects but this changes all the time.
Thank you in advance
I guess you can break your problem down into 2 parts. First think about how you can make the response data to look the same as data, in terms of data format. Then merge the 2 arrays into one.
Here is my approach. First use the map api to go through every object in resp and extract the property value of PRICE and TIME and store it into a new array. This should leave you with an array of array with position 0 as price and position 1 as time.
[ [ '1.10662', '1467923352' ], [ '1.10663', '1467923353' ] ]
Then use the concat api to combine the 2 arrays into one.
Example:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
]);
var resp = [{"PRICE":"1.10662","TIME":"1467923352"},{"PRICE":"1.10663","TIME":"1467923353"}];
data = data.concat(
resp.map(entity => { return [ entity.PRICE, entity.TIME ]; })
);
Output:
[ [ 'Price', 'Time' ],
[ '1.10661', 1467923348 ],
[ '1.10675', 1467923349 ],
[ '1.10681', 1467923350 ],
[ '1.10690', 1467923351 ],
[ '1.10662', '1467923352' ],
[ '1.10663', '1467923353' ] ]
You might want to read more about the map and concat api here;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
Thank you very much for the answers! I found the solution, let me share it with you.
(The example is a bit different but the idea is exactly the same)
<html>
<body>
<script>
var myArray = [{"ASK":"1.10662","TIME":"1467923348"},{"ASK":"1.10663","TIME":"1467923346"},{"ASK":"1.10661","TIME":"1467923340"},{"ASK":"1.10663","TIME":"1467923346"},
{"ASK":"1.10661","TIME":"1467923340"}];
var data = ([
['PRICE', 'TIME'],
]);
var actualrows = data.length
var finalrows = data.length + myArray.length;
for( var i=actualrows; i<finalrows; i++ ) {
data.push( [] );
}
while(actualrows < finalrows){
var column = 0;
while(column <2){
data[actualrows][0] = myArray[actualrows-1]['ASK'];
data[actualrows][1] = myArray[actualrows-1]['TIME'];;
column++;
}
actualrows++ ;
}
</script>
</body>
</html>
How do I get a more complex sort on a query, I have this query currently:
var store = Ext.create('Rally.data.custom.Store',{
data: changes,
limit: 'Infinity',
pageSize: 5000,
sorters: [
{
property: 'ReleaseScope',
direction: 'ASC'
},
{
property: 'ScheduleState',
direction: 'DESC'
}
]
});
Because the ScheduleState is hydrated I can't sort by the normal numerics, can I define the order using some kind of matcher?
i.e. say I want to show in order [Accepted, Completed, In-Progress, Defined, Backlog]
And, if I wanted to complicate this further and show stories with story points first, something like
All stories with a story point value != 0
Sorted by schedulestate [accepted, completed, in-progress, defined etc..]
stories with no story point value
some other sort here possibly
You can pass a sorterFn rather than a property/direction combo to implement custom sort logic:
sorters: [
{
sorterFn: function(a, b) {
var scheduleStates = ['Accepted', 'Completed', 'In-Progress', 'Defined'],
aState = a.get('ScheduleState'),
aIndex = _.indexOf(scheduleStates, aState),
bState = b.get('ScheduleState'),
bIndex = _.indexOf(scheduleStates, bState);
return a - b;
}
}
]
The above function should sort them based on schedule state descending I think.