Skip to content

Commit 889be58

Browse files
authored
Handle more error codes when an RP isn't registered (#20848)
There's more than one error code returned from unregistered RPs.
1 parent a3b2c13 commit 889be58

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

sdk/azcore/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
### Bugs Fixed
2121
* Retry policy always clones the underlying `*http.Request` before invoking the next policy.
22+
* Added `MissingRegistrationForResourceProvider` to the list of error codes for unregistered resource providers.
2223

2324
### Other Changes
2425

sdk/azcore/arm/runtime/pipeline_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestDisableAutoRPRegistration(t *testing.T) {
104104
srv, close := mock.NewServer()
105105
defer close()
106106
// initial response that RP is unregistered
107-
srv.SetResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp)))
107+
srv.SetResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp1)))
108108
opts := &armpolicy.ClientOptions{DisableRPRegistration: true, ClientOptions: policy.ClientOptions{Transport: srv}}
109109
req, err := azruntime.NewRequest(context.Background(), http.MethodGet, srv.URL())
110110
if err != nil {

sdk/azcore/arm/runtime/policy_register_rp.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func (r *rpRegistrationPolicy) Do(req *azpolicy.Request) (*http.Response, error)
8080
// policy is disabled
8181
return req.Next()
8282
}
83-
const unregisteredRPCode = "MissingSubscriptionRegistration"
8483
const registeredState = "Registered"
8584
var rp string
8685
var resp *http.Response
@@ -101,7 +100,7 @@ func (r *rpRegistrationPolicy) Do(req *azpolicy.Request) (*http.Response, error)
101100
// to the caller so its error unmarshalling will kick in
102101
return resp, err
103102
}
104-
if !strings.EqualFold(reqErr.ServiceError.Code, unregisteredRPCode) {
103+
if !isUnregisteredRPCode(reqErr.ServiceError.Code) {
105104
// not a 409 due to unregistered RP. just return the response
106105
// to the caller so its error unmarshalling will kick in
107106
return resp, err
@@ -173,6 +172,20 @@ func (r *rpRegistrationPolicy) Do(req *azpolicy.Request) (*http.Response, error)
173172
return resp, fmt.Errorf("exceeded attempts to register %s", rp)
174173
}
175174

175+
var unregisteredRPCodes = []string{
176+
"MissingSubscriptionRegistration",
177+
"MissingRegistrationForResourceProvider",
178+
}
179+
180+
func isUnregisteredRPCode(errorCode string) bool {
181+
for _, code := range unregisteredRPCodes {
182+
if strings.EqualFold(errorCode, code) {
183+
return true
184+
}
185+
}
186+
return false
187+
}
188+
176189
func getSubscription(path string) (string, error) {
177190
parts := strings.Split(path, "/")
178191
for i, v := range parts {

sdk/azcore/arm/runtime/policy_register_rp_test.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/stretchr/testify/require"
2525
)
2626

27-
const rpUnregisteredResp = `{
27+
const rpUnregisteredResp1 = `{
2828
"error":{
2929
"code":"MissingSubscriptionRegistration",
3030
"message":"The subscription registration is in 'Unregistered' state. The subscription must be registered to use namespace 'Microsoft.Storage'. See https://aka.ms/rps-not-found for how to register subscriptions.",
@@ -37,6 +37,19 @@ const rpUnregisteredResp = `{
3737
}
3838
}`
3939

40+
const rpUnregisteredResp2 = `{
41+
"error":{
42+
"code":"MissingRegistrationForResourceProvider",
43+
"message":"The subscription registration is in 'Unregistered' state. The subscription must be registered to use namespace 'Microsoft.Storage'. See https://aka.ms/rps-not-found for how to register subscriptions.",
44+
"details":[{
45+
"code":"MissingRegistrationForResourceProvider",
46+
"target":"Microsoft.Storage",
47+
"message":"The subscription registration is in 'Unregistered' state. The subscription must be registered to use namespace 'Microsoft.Storage'. See https://aka.ms/rps-not-found for how to register subscriptions."
48+
}
49+
]
50+
}
51+
}`
52+
4053
// some content was omitted here as it's not relevant
4154
const rpRegisteringResp = `{
4255
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Storage",
@@ -89,7 +102,7 @@ func TestRPRegistrationPolicySuccess(t *testing.T) {
89102
srv, close := mock.NewServer()
90103
defer close()
91104
// initial response that RP is unregistered
92-
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp)))
105+
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp1)))
93106
// polling responses to Register() and Get(), in progress
94107
srv.RepeatResponse(5, mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte(rpRegisteringResp)))
95108
// polling response, successful registration
@@ -180,7 +193,7 @@ func TestRPRegistrationPolicyTimesOut(t *testing.T) {
180193
srv, close := mock.NewServer()
181194
defer close()
182195
// initial response that RP is unregistered
183-
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp)))
196+
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp1)))
184197
// polling responses to Register() and Get(), in progress but slow
185198
// tests registration takes too long, times out
186199
srv.RepeatResponse(10, mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte(rpRegisteringResp)), mock.WithSlowResponse(400*time.Millisecond))
@@ -212,7 +225,7 @@ func TestRPRegistrationPolicyExceedsAttempts(t *testing.T) {
212225
// add a cycle of unregistered->registered so that we keep retrying and hit the cap
213226
for i := 0; i < 4; i++ {
214227
// initial response that RP is unregistered
215-
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp)))
228+
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp1)))
216229
// polling responses to Register() and Get(), in progress
217230
srv.RepeatResponse(2, mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte(rpRegisteringResp)))
218231
// polling response, successful registration
@@ -246,7 +259,7 @@ func TestRPRegistrationPolicyCanCancel(t *testing.T) {
246259
srv, close := mock.NewServer()
247260
defer close()
248261
// initial response that RP is unregistered
249-
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp)))
262+
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp2)))
250263
// polling responses to Register() and Get(), in progress but slow so we have time to cancel
251264
srv.RepeatResponse(10, mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte(rpRegisteringResp)), mock.WithSlowResponse(300*time.Millisecond))
252265
// log only RP registration
@@ -287,7 +300,7 @@ func TestRPRegistrationPolicyDisabled(t *testing.T) {
287300
srv, close := mock.NewServer()
288301
defer close()
289302
// initial response that RP is unregistered
290-
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp)))
303+
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp2)))
291304
ops := testRPRegistrationOptions(srv)
292305
ops.MaxAttempts = -1
293306
client := newFakeClient(t, srv, ops)
@@ -305,7 +318,7 @@ func TestRPRegistrationPolicyDisabled(t *testing.T) {
305318
require.Error(t, err)
306319
var respErr *exported.ResponseError
307320
require.ErrorAs(t, err, &respErr)
308-
require.EqualValues(t, "MissingSubscriptionRegistration", respErr.ErrorCode)
321+
require.EqualValues(t, "MissingRegistrationForResourceProvider", respErr.ErrorCode)
309322
require.Zero(t, resp)
310323
// shouldn't be any log entries
311324
require.Zero(t, logEntries)
@@ -315,7 +328,7 @@ func TestRPRegistrationPolicyAudience(t *testing.T) {
315328
srv, close := mock.NewServer()
316329
defer close()
317330
// initial response that RP is unregistered
318-
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp)))
331+
srv.AppendResponse(mock.WithStatusCode(http.StatusConflict), mock.WithBody([]byte(rpUnregisteredResp2)))
319332
// polling responses to Register() and Get(), in progress
320333
srv.AppendResponse(mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte(rpRegisteringResp)))
321334
// polling response, successful registration

0 commit comments

Comments
 (0)