Skip to content

Commit 297948b

Browse files
committed
FIX: restore apis with global scope.
1 parent 600cbe7 commit 297948b

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

.github/workflows/orchestrator.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ on:
55
branches:
66
- main
77
- test
8-
paths:
9-
- 'test/tf/**'
10-
- '.github/workflows/**'
118
workflow_dispatch:
129

1310
concurrency:

cmd/restore.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,43 @@ func init() {
4848
restoreCmd.MarkFlagRequired("input")
4949
}
5050

51-
// extractProductID extracts the product ID from an APIM scope string.
52-
// Scope format: /subscriptions/.../providers/Microsoft.ApiManagement/service/<apim>/products/<productID>
53-
func extractProductID(scope string) string {
54-
const marker = "/products/"
51+
// extractScopeSuffix extracts the scope suffix after the APIM service name.
52+
// For example, given a scope like:
53+
//
54+
// /subscriptions/.../service/<apim>/products/<productID>
55+
//
56+
// it returns "products/<productID>".
57+
// For instance-level scopes (ending with /service/<apim> or /service/<apim>/)
58+
// it returns an empty string.
59+
func extractScopeSuffix(scope string) string {
60+
const marker = "/service/"
5561
idx := strings.LastIndex(scope, marker)
5662
if idx == -1 {
5763
return ""
5864
}
59-
return scope[idx+len(marker):]
65+
// Skip past "/service/<apim-name>"
66+
rest := scope[idx+len(marker):]
67+
slashIdx := strings.Index(rest, "/")
68+
if slashIdx == -1 {
69+
return ""
70+
}
71+
suffix := rest[slashIdx+1:]
72+
// Trim trailing slash
73+
suffix = strings.TrimRight(suffix, "/")
74+
return suffix
6075
}
6176

62-
// buildScope constructs a full APIM scope resource ID for a product.
63-
func buildScope(azureSubscriptionID, resourceGroup, apimName, productID string) string {
64-
return fmt.Sprintf(
65-
"/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ApiManagement/service/%s/products/%s",
66-
azureSubscriptionID, resourceGroup, apimName, productID,
77+
// buildScopeFromSuffix constructs a full APIM scope resource ID from a suffix.
78+
// If suffix is empty, the scope is the APIM instance itself.
79+
func buildScopeFromSuffix(azureSubscriptionID, resourceGroup, apimName, suffix string) string {
80+
base := fmt.Sprintf(
81+
"/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ApiManagement/service/%s",
82+
azureSubscriptionID, resourceGroup, apimName,
6783
)
84+
if suffix == "" {
85+
return base
86+
}
87+
return base + "/" + suffix
6888
}
6989

7090
func runRestore(cmd *cobra.Command, args []string) error {
@@ -117,14 +137,9 @@ func runRestore(cmd *cobra.Command, args []string) error {
117137
displayName := sub.Properties.DisplayName
118138

119139
// Determine the target scope.
120-
// Extract the product ID from the backup scope and rebuild for the target environment.
121-
productID := extractProductID(sub.Properties.Scope)
122-
if productID == "" {
123-
fmt.Printf(" [SKIP] %s (%s) — could not extract product ID from scope\n", displayName, sid)
124-
failed++
125-
continue
126-
}
127-
scope := buildScope(azureSubID, restoreResourceGroup, restoreAPIMName, productID)
140+
// Extract the scope suffix from the backup and rebuild for the target environment.
141+
scopeSuffix := extractScopeSuffix(sub.Properties.Scope)
142+
scope := buildScopeFromSuffix(azureSubID, restoreResourceGroup, restoreAPIMName, scopeSuffix)
128143

129144
opts := &azure.CreateSubscriptionOptions{
130145
PrimaryKey: sub.Properties.PrimaryKey,
@@ -137,13 +152,18 @@ func runRestore(cmd *cobra.Command, args []string) error {
137152
allowTracing := sub.Properties.AllowTracing
138153
opts.AllowTracing = &allowTracing
139154

155+
scopeLabel := scopeSuffix
156+
if scopeLabel == "" {
157+
scopeLabel = "(instance)"
158+
}
159+
140160
if restoreDryRun {
141-
fmt.Printf(" [DRY-RUN] Would restore: %s (sid=%s, product=%s)\n", displayName, sid, productID)
161+
fmt.Printf(" [DRY-RUN] Would restore: %s (sid=%s, scope=%s)\n", displayName, sid, scopeLabel)
142162
restored++
143163
continue
144164
}
145165

146-
fmt.Printf(" Restoring: %s (sid=%s, product=%s)...\n", displayName, sid, productID)
166+
fmt.Printf(" Restoring: %s (sid=%s, scope=%s)...\n", displayName, sid, scopeLabel)
147167
_, err := client.CreateSubscription(ctx, sid, scope, displayName, opts)
148168
if err != nil {
149169
fmt.Printf(" [FAIL] %s: %v\n", displayName, err)

0 commit comments

Comments
 (0)