in internal/services/parse/resource.go [79:149]
func newResourceID(name, parentId, resourceType string, skipScopeValidation bool) (ResourceId, error) {
azureResourceType, apiVersion, err := utils.GetAzureResourceTypeApiVersion(resourceType)
if err != nil {
return ResourceId{}, err
}
resourceDef, err := azure.GetResourceDefinition(azureResourceType, apiVersion)
if err != nil {
log.Printf("[WARN] load embedded schema: %+v\n", err)
}
azureResourceId := ""
switch {
case !strings.Contains(azureResourceType, "/"):
// case 0: resource type is a provider type
// avoid duplicated `/` if parent_id is tenant scope
scopeId := parentId
if parentId == "/" {
scopeId = ""
}
azureResourceId = fmt.Sprintf("%s/providers/%s", scopeId, name)
case utils.IsTopLevelResourceType(azureResourceType):
// case 1: top level resource, verify parent_id providers correct scope
if !skipScopeValidation {
if err = validateParentIdScope(resourceDef, parentId); err != nil {
return ResourceId{}, fmt.Errorf("`parent_id is invalid`: %+v", err)
}
}
// build azure resource id
switch azureResourceType {
case arm.ResourceGroupResourceType.String():
azureResourceId = fmt.Sprintf("%s/resourceGroups/%s", parentId, name)
case arm.SubscriptionResourceType.String():
azureResourceId = fmt.Sprintf("/subscriptions/%s", name)
case arm.TenantResourceType.String():
azureResourceId = "/"
case arm.ProviderResourceType.String():
// avoid duplicated `/` if parent_id is tenant scope
scopeId := parentId
if parentId == "/" {
scopeId = ""
}
azureResourceId = fmt.Sprintf("%s/providers/%s", scopeId, name)
default:
// avoid duplicated `/` if parent_id is tenant scope
scopeId := parentId
if parentId == "/" {
scopeId = ""
}
azureResourceId = fmt.Sprintf("%s/providers/%s/%s", scopeId, azureResourceType, name)
}
default:
// case 2: child resource, verify parent_id's type matches with resource type's parent type
if err = validateParentIdType(azureResourceType, parentId); err != nil {
return ResourceId{}, fmt.Errorf("`parent_id is invalid`: %+v", err)
}
// build azure resource id
lastType := azureResourceType[strings.LastIndex(azureResourceType, "/")+1:]
azureResourceId = fmt.Sprintf("%s/%s/%s", parentId, lastType, name)
}
return ResourceId{
AzureResourceId: azureResourceId,
ApiVersion: apiVersion,
AzureResourceType: azureResourceType,
Name: name,
ParentId: parentId,
ResourceDef: resourceDef,
}, nil
}