func DataPlaneResourceIDWithResourceType()

in internal/services/parse/data_plane_resource_id.go [56:94]


func DataPlaneResourceIDWithResourceType(azureResourceId, resourceType string) (DataPlaneResourceId, error) {
	azureResourceType, _, err := utils.GetAzureResourceTypeApiVersion(resourceType)
	if err != nil {
		return DataPlaneResourceId{}, err
	}

	name := ""
	parentId := ""
	if apiPath := findApiPathByResourceType(azureResourceType); apiPath != nil {
		urlFormatParts := strings.Split(apiPath.UrlFormat, "/")
		azureResourceIdParts := strings.Split(azureResourceId, "/")
		j := len(azureResourceIdParts) - 1
		for i := len(urlFormatParts) - 1; i >= 0; i-- {
			if j < 0 {
				return DataPlaneResourceId{}, fmt.Errorf("index %d is less than 0", j)
			}
			switch {
			case strings.HasPrefix(urlFormatParts[i], "{name"):
				name = azureResourceIdParts[j]
				j--
			case urlFormatParts[i] == "{parentId}":
				for j >= 0 {
					if j > 0 && i > 0 && azureResourceIdParts[j-1] == urlFormatParts[i-1] {
						break
					}
					parentId = azureResourceIdParts[j] + "/" + parentId
					j--
				}
			case urlFormatParts[i] == "{apiVersion}":
				j--
			case strings.EqualFold(azureResourceIdParts[j], urlFormatParts[i]):
				j--
			}
		}
	}
	parentId = strings.TrimSuffix(parentId, "/")

	return NewDataPlaneResourceId(name, parentId, resourceType)
}