I am creating an Azure deployment that will be used across the various regions. I am trying to keep the code agnostic to region and provide region specific info via a JSON file.
The JSON file will have the format of
{
"region1":
{
"key" : "value"
},
"region2":
{
"key" : "value"
}
}
I would like to import the JSON at deployment time and use the values for a specific region by taking an input of the target region to be deployed and storing it in the parameter called region. I then want to use the region parameter to index into the JSON object like the example below:
param _regions object = json(loadTextContent('<json_file_name>'))
param region string
var regionProperty = _regions.${region}.key
Using the syntax above for indexing into the JSON does not work, does anyone have ideas on how I can make this work?
Looking at the documentation:
You can use the [] syntax to access a property.
var regionProperty = _regions[region].key
Related
What is the query format for Terraform New Relic Workload resource? I have the below resource specified in my main.tf file and I want to be able to get all the entities that have a specific label.
...
resource "newrelic_workload" "prod-app" {
name = "Prod App"
account_id = <account id>
entity_search_query {
query = "label.STAGE = prod"
}
}
However, it seems to throw this error:
Error: A downstream error occurred., [{"extensions":{},"message":"Search query \u003clabel.STAGE = 'prod'\u003e is invalid, response message is \u003cProvided filter: label.STAGE = 'prod' is not a valid NRQL query (You can't use field: label.STAGE, that is not defined in schema.)\u003e"}]
There doesn't seem to be much info on the documentation other that it needs to be a valid NRQL query. The example seems to use "name like 'Example application'"
https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/workload
Ok, I figured it out. Turns out that I was just referring to a key that doesn't exist.
It should be tags.<tag key> so tags.label.STAGE.
I noticed my mistake when I looked at the API behind the scenes. It uses the NerdGraph API and the Query entities section describes how to structure your query.
I am working with Cosmos DB and I want to write a SQL query that returns different name of an key in document object.
To elaborate, imagine you have the following document in one container having "makeName" key in "make" object.
{
"vehicleDetailId":"38CBEAF7-5858-4EED-8978-E220D2BA745E",
"type":"Vehicle",
"vehicleDetail":{
"make":{
"Id":"B57ADAAD-C16E-44F9-A05B-AAB3BF7068B9",
"makeName":"BMW"
}
}
}
I want to write a query to display "vehicleMake" key in place of "makeName".
How to give alias name in the nested object property.
Output should be like below
{
"vehicleDetailId":"38CBEAF7-5858-4EED-8978-E220D2BA745E",
"type":"Vehicle",
"vehicleDetail":{
"make":{
"Id":"B57ADAAD-C16E-44F9-A05B-AAB3BF7068B9",
"vehicleMake":"BMW"
}
}
}
I have no idea how to query in Cosmosdb to get the above result.
Aliases for properties are similar to the way you'd create a column alias in SQL Server, with the as keyword. In your example, it would be:
SELECT c.vehicleDetail.make.makeName as vehicleMake
FROM c
This would return:
[
{
"vehicleMake": "BMW"
}
]
Try this:
SELECT c.vehicleDetailId, c.type,
{"make":{"Id":c.vehicleDetail.make.Id, "vehicleMake":c.vehicleDetail.make.makeName}} as vehicleDetail
FROM c
It uses the aliasing described in the following documentation. All of the aliasing examples I could find in the documentation or blog posts only show a single level of json output, but it happens that you can nest an object (make) within an object (vehichleDetail) to get the behavior you want.
https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-aliasing
Thanks in advance.
I'm working on Azure PowerShell Script which will get all the NSG and finds whether it is attached to NIC or Subnet and give the name of the same in CSV.
I'm stuck in parsing the NSG as it has property SubnetsText which outputs the data in below format. I'm trying to parse it using substring method but it did not work. Anybody tried this before?
[
{
"TapConfigurations": [],
"HostedWorkloads": [],
"Id": "/subscriptions/xxxx-xxxx-xxx-xxx-xxxxxx/resourceGroups/vm-test-group/providers/Microsoft.Network/networkInterfaces/testvm1VMNic"
}
]
Below is the cmdlet
$nsg = Get-AzureRmNetworkSecurityGroup -Name testvm1NSG -ResourceGroupName vm-test-group
$nsg.SubnetsText.ToString().Substring(lastindexof('/')+1)
you could just do this:
$nsg.Subnets.Id.Split('/')[-1]
this would split the string on / and get the last item from that operation, which would be the NIC name.
You should be able to do this with Newtonsoft JSON (In Theory you should be able to do this for the whole output from Get-AzureRmNetworkSecurityGroup)
To try this I first took your SubnetsText into a string.
string nsg =
"[{\"TapConfigurations\":[],\"HostedWorkloads\":[],\"Id\":\"/subscriptions/xxxx-xxxx-xxx-xxx-xxxxxx/resourceGroups/vm-test-group/providers/Microsoft.Network/networkInterfaces/testvm1VMNic\"}]";
Next, I Created a new dynamic called X and parsed the JSON into it as a JArray.
dynamic x = JArray.Parse(nsg);
I created a new string called id and took the value of Id. I also created a new list called idList
string id = x[0].Id.ToString();
var idList = new List<string>();
Lastly, I populated the idList with the values of id using .Split() and .ToList()
idList = id.Split('/').ToList();
When writing out x[0].Id to console I get:
/subscriptions/xxxx-xxxx-xxx-xxx-xxxxxx/resourceGroups/vm-test-group/providers/Microsoft.Network/networkInterfaces/testvm1VMNic
And when I get the specific value I want from the list (In this case I want the Interface name, which is the 8th item) I write idList[8] to console and get:
testvm1VMNic
Console.WriteLine(x[0].Id);
Console.WriteLine(idList[1]); // subscriptions
Console.WriteLine(idList[2]); // xxxx-xxxx-xxx-xxx-xxxxxx
Console.WriteLine(idList[3]); // resourceGroups
Console.WriteLine(idList[4]); // vm-test-group
Console.WriteLine(idList[5]); // providers
Console.WriteLine(idList[6]); // Microsoft.Network
Console.WriteLine(idList[7]); // networkInterfaces
Console.WriteLine(idList[8]); // testvm1VMNic
Note: This is in c# (As this is where I was working on a similar tool) but you should be able to do it in a similar way in Powershell if you can access the Powershell Gallery
Im trying to concatenate a data with variable (Terraform v0.12.0):
variable "my_var" {
default = "secret_string"
}
auth_token = data.external.get_secret.result.var.my_var
It's work in case of:
auth_token = data.external.get_secret.result.secret_string
As I can see I can't to add variable to data. Do we have any workaround for this case? Thanks
Not very clear for what you need. Let me guess.
you can have different endpoints to save your secrets, such as hashicorp vault, aws ssm, aws secret managers, with these ways, you can avoid to save secrets directly in source code.
Here I use aws ssm for an example on how to reference a secret via variable.
Suppose you have set SSM key my_var in aws.
variable "my_var" {
default = "my_var"
}
data "aws_ssm_parameter" "read" {
name = "${var.my_var}"
}
So now you can easily reference it
auth_token = "${aws_ssm_parameter.read.value}"
Note: The unencrypted value of a SecureString will be stored in the raw state as plain-text.
Note: The data source is currently following the behavior of the SSM API to return a string value, regardless of parameter type. For type StringList, we can use the built-in split() function to get values in a list. Example: split(",", data.aws_ssm_parameter.subnets.value)
I try to transform flat JSON data from an Event Hub into a DocumentDB. The target structure should look like:
{
"id" : 1
"field_1" : "value_1",
"details" : {
"detail_field_1":"abc",
"detail_field_2":"def"
}
}
Created from source:
{
"id":1,
"field_1" : "value_1",
"detail_field_1":"abc",
"detail_field_2":"def"
}
I checked the documentation of Azure Stream Analytics but there ist no clear description how to create a proper Query.
Who one can help me?
You can leverage the new JavaScript UDF feature to write nested JSON objects to output.
Register a user-defined function, "UDF.getDetails()" as below:
function main(obj) {
//get details object from input payload
var details_obj = {};
details_obj.detail_field_1 = obj.detail_field_1;
details_obj.detail_field_2 = obj.detail_field_2;
return JSON.stringify(details_obj);
}
Then call the UDF in your query to get a string of the nested JSON object.
SELECT
id,
field_1,
UDF.getDetails(input) As details
INTO output
FROM input
Using JavaScript UDF feature, you can return complex JSON.
Example write function.
function main(obj) {
//get details object from input payload
var details_obj = {};
details_obj.detail_field_1 = obj.detail_field_1;
details_obj.detail_field_2 = obj.detail_field_2;
return details_obj;
}
You should not use JSON.stringify since it will make it a string instead of JSON object.
Use it like.
SELECT id, field_1, UDF.getDetails(input) As details
INTO output
FROM input