Created
July 28, 2022 15:13
-
-
Save lewismarshall/658e6d9256406d3180653447ae2a970b to your computer and use it in GitHub Desktop.
Azure Marketplace Get Managed Resource Group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type getManagedAppIdInputs struct { | |
subscriptionId string | |
nodeResourceGroupName string | |
clientId string | |
} | |
// getManagedAppID will return the managed app ID | |
// will return the managedAppID if it is set | |
// otherwise will return the managed app ID by: | |
// 1. auditing the Metadata API to get the resource group | |
// 2. using managed APIS to get the managedBy property of the relevant resource groups | |
func getManagedAppID(ctx context.Context, i getManagedAppIdInputs) (*string, error) { | |
// now we must fetch the managedBy property of the resource group | |
conf := auth.NewMSIConfig() | |
conf.ClientID = i.clientId | |
auth, err := conf.Authorizer() | |
if err != nil { | |
return nil, fmt.Errorf("failed to get authorizer - %w", err) | |
} | |
rc := resources.NewGroupsClient(i.subscriptionId, auth) | |
grp, err := rc.GetWithTimeOut(ctx, i.nodeResourceGroupName, ResourceGroupTimeout) | |
if err != nil { | |
return nil, fmt.Errorf("failed to get resource group %s - %w", i.nodeResourceGroupName, err) | |
} | |
if grp.ManagedBy == nil { | |
return nil, fmt.Errorf("no managedBy property found on resource group %s", to.String(grp.Name)) | |
} | |
// Is this a managed app OR a managed resource group? | |
// If we are running on a VM then we would expect a managed app | |
// If we are running on AKS, parse the managed resource group name from the managed by property: | |
// Parse the managedBy property to determine if we have a managed app resource ID or an AKS cluster resource ID | |
if hasProvider(to.String(grp.ManagedBy), "Microsoft.ContainerService/managedClusters") { | |
// AKS cluster | |
// get the new resource group property from the tags | |
managedResourceGroup := grp.Tags["aks-managed-cluster-rg"] | |
if to.String(managedResourceGroup) == "" { | |
return nil, fmt.Errorf("no managed resource group found in tags") | |
} | |
grp, err = rc.GetWithTimeOut(ctx, to.String(managedResourceGroup), ResourceGroupTimeout) | |
if err != nil { | |
return nil, fmt.Errorf("failed to get AKS resource group - %w", err) | |
} | |
if grp.ManagedBy == nil { | |
return nil, fmt.Errorf("no managedBy property found on AKS resource group") | |
} | |
} | |
if hasProvider(to.String(grp.ManagedBy), "Microsoft.Solutions/applications") { | |
return grp.ManagedBy, nil | |
} | |
return nil, fmt.Errorf("no managedBy property found on resource group or owner resource group") | |
} | |
func hasProvider(resourceID string, provider string) bool { | |
parts := strings.Split(resourceID, "providers/") | |
if len(parts) < 2 { | |
return false | |
} | |
return strings.HasPrefix(parts[1], provider) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment