New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
azurerm resourceid interpolation function #1466
Comments
@tombuildsstuff thanks for pointing this out to me ... I might try creating a PR for this on the weekend... am I correct in assuming it should be implemented as a data resource? |
Late to the party, but there might be a quick workaround with Might need tweaks for multi-provider setup, but still not too bad. Feel free to improve this one. # assuming there is a list of all_resources
locals {
all_resources = [
{
name = "resource-1"
rg_name = "rg-name-1"
},
{
name = "resource-2"
rg_name = "rg-name-2"
}
]
all_resource_ids = merge(
# forming resource id string by hand, since this is not supported in azurerm_resources data source yet
# "id" = "/subscriptions/xyz/resourceGroups/rgname/providers/Microsoft.Network/virtualNetworks/abc"
{
for k, v in data.azurerm_resources.target_resources : v.name => {
name = v.name,
id = join("/", [
# subscription
"/subscriptions",
data.azurerm_client_config.current.subscription_id,
# resource groups
"resourceGroups",
v.resource_group_name,
# providers (resource type)
"providers",
v.resources[0].type,
// name
v.resources[0].name
]),
}
},
)
}
# get all resources
data "azurerm_resources" "target_resources" {
for_each = { for value in local.all_resources :
value.name => value
}
resource_group_name = each.value.rg_name
name = each.value.name
}
# get current azure client config
data "azurerm_client_config" "current" {
} |
I created a temporary provider while waiting for support from azurerm https://registry.terraform.io/providers/AdamCoulterOz/azurehelpers/latest data "azurehelpers_resource_id" "example" {
resource_id = "resourceId"
} it lets you access the following: data.azurehelpers_resource_id.example.subscription_id
data.azurehelpers_resource_id.example.resource_group_name
data.azurehelpers_resource_id.example.provider_namespace
data.azurehelpers_resource_id.example.resource_type
data.azurehelpers_resource_id.example.name
data.azurehelpers_resource_id.example.parent_resources
data.azurehelpers_resource_id.example.full_resource_type |
@tombuildsstuff, how are those new parsers going? |
Thanks @AdamCoulterOz , After disappointing azurerm_resources, I must admit your provider helped me.
Looks like Azure APIs behave differently for If you could tweak your provider a bit further to fetch version from APIs & append to |
My stop-gap data resource explicitly doesn't try to get the resource, it just parses the ID, because a user won't always have read permission at every scope. Maybe only at the child scope. I can have access to a resource, but not it's resource group for example. I was thinking of cleaning it up a little and submitting it as a PR to the new azapi provider. They have a nice way of passing in the resource type so it could return a cleaner map of fields. |
Exactly! Parsing is all I need in my child module. The prime reason is Before I used the data provider to parse RG, type in the child module which failed randomly due to eventual consistency problems. In my case, child modules refer to private endpoint resource & calling resources are managed resources like storage account, key vault etc., Please don't do the cleanup :) |
Yep, there is no need to read the resourceIds from Azure. This is purely string manipulation which is why I originally suggested a string function as a possibility. Ideally using the azurerm provider model would extend the set of functions with Azure specific functions - e.g. |
yeah, having dug into this we likely won't be adding a |
So, now that Terraform core supports exposing provider functions (hashicorp/terraform#34394) - perhaps time to revisit this one? |
100% agree. Parsing the Azure resource IDs is a perfect use case for a provider function. |
FWIW we've spent some time thinking about this recently, but unfortunately a generic Resource ID parsing function would quickly become problematic/a source of user configuration issues due the random recasings that happen in the Azure API, and whilst we're working to get a handle on those, for that reason we'd be unlikely to ship a generic function. It's worth noting that there's also some of the more problematic scenarios, such as Resource IDs containing multiple segments of the same name (e.g. two That said, I suspect having functions to generate a Resource ID would be useful (e.g. All that to say, we should look into this, but a generic Resource ID interpolation/parsing function is unfortunately more problematic than it first appears, even if it's be useful in the happy path - and so it's more likely this'd ship in the form of more specific Resource ID interpolation/parsing functions, rather than as a generic Resource ID function. Thanks! |
For reference, ARM / Bicep have the following functions that generate resourceIds in this space: These all generate the different scope level resourceIDs. Some use defaults from context for parameters such as subscription when unspecified. |
Community Note
Description
The Azure resource IDs are a fixed format, and it would be useful to have an function to convert from the full string to a map of the constituent parts.
An Azure specific interpolation function (or perhaps data source), which takes an argument of either:
"resourceId"
[ "subscriptionId", "resourceGroupName", "Microsoft.Provider/type", "resourceName" ]
(If possible, make the first two in the list optional. If resource group is not specified then default to the resource group for the current resource stanza (if that context is held). If subscription is not specified then default to the current subscription.)
The function is pure string manipulation (unless subscription / resource group defaults are possible), but it would help massively.
Example resourceids
For a resource group, resource and sub-resource
Output map
Using /subscriptions/2d31be49-d999-4415-bb65-8aec2c90ba62/resourceGroups/rgname/providers/Microsoft.Network/virtualNetworks/resname as the input example, then the output map would look something like:
If there is a subresource (e.g. subnets) then the resource value should be everything after the resource provider namespace, e.g. "resname/subnets/subresname"
New or Affected Resource(s)
Potential Terraform Configuration
References
The text was updated successfully, but these errors were encountered: