-
Notifications
You must be signed in to change notification settings - Fork 177
/
resource_ids.go
113 lines (96 loc) · 3.24 KB
/
resource_ids.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azure
import (
"fmt"
"regexp"
"strings"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
)
// SubscriptionFromRID returns the subscription id component of a resource or panics if the resource id does not
// contain a subscription.
func SubscriptionFromRID(rid string) string {
parts := strings.Split(rid, "/")
for idx, part := range parts {
if part == "subscriptions" && idx+1 < len(parts) {
return parts[idx+1]
}
}
panic(fmt.Sprintf("no subscription id component in in %s", rid))
}
// Creates Azure subscription resource ID
func SubscriptionRID(subscriptionId string) string {
returnValue := fmt.Sprintf("/subscriptions/%s", subscriptionId)
return returnValue
}
// Creates subscription-level deployment resource ID
func SubscriptionDeploymentRID(subscriptionId, deploymentId string) string {
returnValue := fmt.Sprintf(
"%s/providers/Microsoft.Resources/deployments/%s",
SubscriptionRID(subscriptionId),
deploymentId,
)
return returnValue
}
// Creates resource group level deployment resource ID
func ResourceGroupDeploymentRID(subscriptionId string, resourceGroupName string, deploymentId string) string {
returnValue := fmt.Sprintf(
"%s/providers/Microsoft.Resources/deployments/%s",
ResourceGroupRID(subscriptionId, resourceGroupName),
deploymentId,
)
return returnValue
}
// Creates resource ID for an Azure resource group
func ResourceGroupRID(subscriptionId, resourceGroupName string) string {
returnValue := fmt.Sprintf("%s/resourceGroups/%s", SubscriptionRID(subscriptionId), resourceGroupName)
return returnValue
}
func WebsiteRID(subscriptionId, resourceGroupName, websiteName string) string {
returnValue := fmt.Sprintf(
"%s/providers/Microsoft.Web/sites/%s",
ResourceGroupRID(subscriptionId, resourceGroupName),
websiteName,
)
return returnValue
}
func ContainerAppRID(subscriptionId, resourceGroupName, containerAppName string) string {
returnValue := fmt.Sprintf(
"%s/providers/Microsoft.App/containerApps/%s",
ResourceGroupRID(subscriptionId, resourceGroupName),
containerAppName,
)
return returnValue
}
func SpringAppRID(subscriptionId, resourceGroupName, springAppName string) string {
returnValue := fmt.Sprintf(
"%s/providers/Microsoft.AppPlatform/Spring/%s",
ResourceGroupRID(subscriptionId, resourceGroupName),
springAppName,
)
return returnValue
}
func KubernetesServiceRID(subscriptionId, resourceGroupName, clusterName string) string {
return fmt.Sprintf(
"%s/providers/Microsoft.ContainerService/managedClusters/%s",
ResourceGroupRID(subscriptionId, resourceGroupName),
clusterName,
)
}
func StaticWebAppRID(subscriptionId, resourceGroupName, staticSiteName string) string {
returnValue := fmt.Sprintf(
"%s/providers/Microsoft.Web/staticSites/%s",
ResourceGroupRID(subscriptionId, resourceGroupName),
staticSiteName,
)
return returnValue
}
var resourceIdRegex = regexp.MustCompile("/.+/(?i)resourceGroups/(.+?)/.+")
// Find the resource group name from the resource id
func GetResourceGroupName(resourceId string) *string {
matches := resourceIdRegex.FindSubmatch([]byte(resourceId))
if matches == nil || len(matches) < 2 {
return nil
}
return convert.RefOf(string(matches[1]))
}