Skip to content

Commit 4413113

Browse files
authored
Refactor exported, internal public surface area (Azure#17595)
* Refactor exported, internal public surface area Internal packages are used to break circular dependencies. Some of this internal content is actually publicly exported via type aliases. To make it clear which internal parts are public, they've been moved into the internal/exported package along with comments indicating from where they're exposed. Moved some shared stuff that was just for pollers into internal/pollers. Added shared.BytesSetter interface to decouple the payload caching from the underlying implementation. Moved some non-shared constants around. Removed unused x-ms-date constant. * directly export AccessToken, TokenCredential, TokenRequestOptions
1 parent 78fbcfb commit 4413113

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+525
-494
lines changed

sdk/azcore/arm/internal/pollers/async/async.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func New(resp *http.Response, finalState pollers.FinalStateVia, pollerID string)
6969
}
7070
// check for provisioning state
7171
state, err := armpollers.GetProvisioningState(resp)
72-
if errors.Is(err, shared.ErrNoBody) || state == "" {
72+
if errors.Is(err, pollers.ErrNoBody) || state == "" {
7373
// NOTE: the ARM RPC spec explicitly states that for async PUT the initial response MUST
7474
// contain a provisioning state. to maintain compat with track 1 and other implementations
7575
// we are explicitly relaxing this requirement.

sdk/azcore/arm/internal/pollers/body/body.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
armpollers "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/internal/pollers"
1414
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/pollers"
15-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
1615
"github.com/Azure/azure-sdk-for-go/sdk/internal/log"
1716
)
1817

@@ -50,7 +49,7 @@ func New(resp *http.Response, pollerID string) (*Poller, error) {
5049
// status code and provisioning state, we might change the value.
5150
curState := pollers.StatusInProgress
5251
provState, err := armpollers.GetProvisioningState(resp)
53-
if err != nil && !errors.Is(err, shared.ErrNoBody) {
52+
if err != nil && !errors.Is(err, pollers.ErrNoBody) {
5453
return nil, err
5554
}
5655
if resp.StatusCode == http.StatusCreated && provState != "" {
@@ -87,7 +86,7 @@ func (p *Poller) Update(resp *http.Response) error {
8786
return nil
8887
}
8988
state, err := armpollers.GetProvisioningState(resp)
90-
if errors.Is(err, shared.ErrNoBody) {
89+
if errors.Is(err, pollers.ErrNoBody) {
9190
// a missing response body in non-204 case is an error
9291
return err
9392
} else if state == "" {

sdk/azcore/arm/internal/pollers/body/body_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515
"testing"
1616

17-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
17+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/pollers"
1818
)
1919

2020
const (
@@ -113,7 +113,7 @@ func TestUpdateNoProvStateFail(t *testing.T) {
113113
if err == nil {
114114
t.Fatal("unexpected nil error")
115115
}
116-
if !errors.Is(err, shared.ErrNoBody) {
116+
if !errors.Is(err, pollers.ErrNoBody) {
117117
t.Fatalf("unexpected error type %T", err)
118118
}
119119
}

sdk/azcore/arm/internal/pollers/loc/loc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (p *Poller) Update(resp *http.Response) error {
7575
if runtime.HasStatusCode(resp, http.StatusOK, http.StatusCreated) {
7676
// if a 200/201 returns a provisioning state, use that instead
7777
state, err := armpollers.GetProvisioningState(resp)
78-
if err != nil && !errors.Is(err, shared.ErrNoBody) {
78+
if err != nil && !errors.Is(err, pollers.ErrNoBody) {
7979
return err
8080
}
8181
if state != "" {

sdk/azcore/arm/internal/pollers/pollers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package pollers
99
import (
1010
"net/http"
1111

12-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
12+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/pollers"
1313
)
1414

1515
// provisioningState returns the provisioning state from the response or the empty string.
@@ -50,7 +50,7 @@ func status(jsonBody map[string]interface{}) string {
5050
// Typically used for Azure-AsyncOperation flows.
5151
// If there is no status in the response body the empty string is returned.
5252
func GetStatus(resp *http.Response) (string, error) {
53-
jsonBody, err := shared.GetJSON(resp)
53+
jsonBody, err := pollers.GetJSON(resp)
5454
if err != nil {
5555
return "", err
5656
}
@@ -60,7 +60,7 @@ func GetStatus(resp *http.Response) (string, error) {
6060
// GetProvisioningState returns the LRO's state from the response body.
6161
// If there is no state in the response body the empty string is returned.
6262
func GetProvisioningState(resp *http.Response) (string, error) {
63-
jsonBody, err := shared.GetJSON(resp)
63+
jsonBody, err := pollers.GetJSON(resp)
6464
if err != nil {
6565
return "", err
6666
}

sdk/azcore/arm/internal/pollers/pollers_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"strings"
1414
"testing"
1515

16-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
16+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/pollers"
1717
)
1818

1919
func TestGetStatusSuccess(t *testing.T) {
@@ -35,14 +35,14 @@ func TestGetNoBody(t *testing.T) {
3535
Body: http.NoBody,
3636
}
3737
status, err := GetStatus(resp)
38-
if !errors.Is(err, shared.ErrNoBody) {
38+
if !errors.Is(err, pollers.ErrNoBody) {
3939
t.Fatalf("unexpected error %T", err)
4040
}
4141
if status != "" {
4242
t.Fatal("expected empty status")
4343
}
4444
status, err = GetProvisioningState(resp)
45-
if !errors.Is(err, shared.ErrNoBody) {
45+
if !errors.Is(err, pollers.ErrNoBody) {
4646
t.Fatalf("unexpected error %T", err)
4747
}
4848
if status != "" {

sdk/azcore/arm/runtime/pipeline.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,38 @@ import (
1010
"errors"
1111
"reflect"
1212

13+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
1314
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
1415
armpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
1516
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
16-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/pipeline"
17-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
1817
azpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1918
azruntime "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
2019
)
2120

2221
// NewPipeline creates a pipeline from connection options.
2322
// The telemetry policy, when enabled, will use the specified module and version info.
24-
func NewPipeline(module, version string, cred shared.TokenCredential, plOpts azruntime.PipelineOptions, options *arm.ClientOptions) (pipeline.Pipeline, error) {
23+
func NewPipeline(module, version string, cred azcore.TokenCredential, plOpts azruntime.PipelineOptions, options *arm.ClientOptions) (azruntime.Pipeline, error) {
2524
if options == nil {
2625
options = &arm.ClientOptions{}
2726
}
2827
conf, err := getConfiguration(&options.ClientOptions)
2928
if err != nil {
30-
return pipeline.Pipeline{}, err
29+
return azruntime.Pipeline{}, err
3130
}
3231
authPolicy := NewBearerTokenPolicy(cred, &armpolicy.BearerTokenOptions{
3332
Scopes: []string{conf.Audience + "/.default"},
3433
AuxiliaryTenants: options.AuxiliaryTenants,
3534
})
36-
perRetry := make([]pipeline.Policy, 0, len(plOpts.PerRetry)+1)
35+
perRetry := make([]azpolicy.Policy, 0, len(plOpts.PerRetry)+1)
3736
copy(perRetry, plOpts.PerRetry)
3837
plOpts.PerRetry = append(perRetry, authPolicy)
3938
if !options.DisableRPRegistration {
4039
regRPOpts := armpolicy.RegistrationOptions{ClientOptions: options.ClientOptions}
4140
regPolicy, err := NewRPRegistrationPolicy(cred, &regRPOpts)
4241
if err != nil {
43-
return pipeline.Pipeline{}, err
42+
return azruntime.Pipeline{}, err
4443
}
45-
perCall := make([]pipeline.Policy, 0, len(plOpts.PerCall)+1)
44+
perCall := make([]azpolicy.Policy, 0, len(plOpts.PerCall)+1)
4645
copy(perCall, plOpts.PerCall)
4746
plOpts.PerCall = append(perCall, regPolicy)
4847
}

sdk/azcore/arm/runtime/pipeline_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"testing"
1414
"time"
1515

16+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
1617
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
1718
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
18-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
1919
"github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
2020
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
2121
azruntime "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
@@ -179,15 +179,15 @@ func TestPipelineAudience(t *testing.T) {
179179
t.Fatal("unexpected audience " + audience)
180180
}
181181
getTokenCalled := false
182-
cred := mockCredential{getTokenImpl: func(ctx context.Context, options shared.TokenRequestOptions) (*shared.AccessToken, error) {
182+
cred := mockCredential{getTokenImpl: func(ctx context.Context, options policy.TokenRequestOptions) (*azcore.AccessToken, error) {
183183
getTokenCalled = true
184184
if n := len(options.Scopes); n != 1 {
185185
t.Fatalf("expected 1 scope, got %d", n)
186186
}
187187
if options.Scopes[0] != audience+"/.default" {
188188
t.Fatalf(`unexpected scope "%s"`, options.Scopes[0])
189189
}
190-
return &shared.AccessToken{Token: "...", ExpiresOn: time.Now().Add(time.Hour)}, nil
190+
return &azcore.AccessToken{Token: "...", ExpiresOn: time.Now().Add(time.Hour)}, nil
191191
}}
192192
req, err := azruntime.NewRequest(context.Background(), http.MethodGet, srv.URL())
193193
if err != nil {

sdk/azcore/arm/runtime/policy_bearer_token.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
1314
armpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
1415
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
1516
azpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
@@ -23,8 +24,8 @@ type acquiringResourceState struct {
2324

2425
// acquire acquires or updates the resource; only one
2526
// thread/goroutine at a time ever calls this function
26-
func acquire(state acquiringResourceState) (newResource *shared.AccessToken, newExpiration time.Time, err error) {
27-
tk, err := state.p.cred.GetToken(state.ctx, shared.TokenRequestOptions{
27+
func acquire(state acquiringResourceState) (newResource *azcore.AccessToken, newExpiration time.Time, err error) {
28+
tk, err := state.p.cred.GetToken(state.ctx, azpolicy.TokenRequestOptions{
2829
Scopes: state.p.options.Scopes,
2930
TenantID: state.tenant,
3031
})
@@ -37,18 +38,18 @@ func acquire(state acquiringResourceState) (newResource *shared.AccessToken, new
3738
// BearerTokenPolicy authorizes requests with bearer tokens acquired from a TokenCredential.
3839
type BearerTokenPolicy struct {
3940
// mainResource is the resource to be retreived using the tenant specified in the credential
40-
mainResource *shared.ExpiringResource[*shared.AccessToken, acquiringResourceState]
41+
mainResource *shared.ExpiringResource[*azcore.AccessToken, acquiringResourceState]
4142
// auxResources are additional resources that are required for cross-tenant applications
42-
auxResources map[string]*shared.ExpiringResource[*shared.AccessToken, acquiringResourceState]
43+
auxResources map[string]*shared.ExpiringResource[*azcore.AccessToken, acquiringResourceState]
4344
// the following fields are read-only
44-
cred shared.TokenCredential
45+
cred azcore.TokenCredential
4546
options armpolicy.BearerTokenOptions
4647
}
4748

4849
// NewBearerTokenPolicy creates a policy object that authorizes requests with bearer tokens.
4950
// cred: an azcore.TokenCredential implementation such as a credential object from azidentity
5051
// opts: optional settings. Pass nil to accept default values; this is the same as passing a zero-value options.
51-
func NewBearerTokenPolicy(cred shared.TokenCredential, opts *armpolicy.BearerTokenOptions) *BearerTokenPolicy {
52+
func NewBearerTokenPolicy(cred azcore.TokenCredential, opts *armpolicy.BearerTokenOptions) *BearerTokenPolicy {
5253
if opts == nil {
5354
opts = &armpolicy.BearerTokenOptions{}
5455
}
@@ -58,7 +59,7 @@ func NewBearerTokenPolicy(cred shared.TokenCredential, opts *armpolicy.BearerTok
5859
mainResource: shared.NewExpiringResource(acquire),
5960
}
6061
if len(opts.AuxiliaryTenants) > 0 {
61-
p.auxResources = map[string]*shared.ExpiringResource[*shared.AccessToken, acquiringResourceState]{}
62+
p.auxResources = map[string]*shared.ExpiringResource[*azcore.AccessToken, acquiringResourceState]{}
6263
}
6364
for _, t := range opts.AuxiliaryTenants {
6465
p.auxResources[t] = shared.NewExpiringResource(acquire)

sdk/azcore/arm/runtime/policy_bearer_token_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
1516
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
1617
armpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
17-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/pipeline"
1818
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
19+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1920
azpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
2021
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
2122
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
@@ -29,14 +30,14 @@ const (
2930
)
3031

3132
type mockCredential struct {
32-
getTokenImpl func(ctx context.Context, options shared.TokenRequestOptions) (*shared.AccessToken, error)
33+
getTokenImpl func(ctx context.Context, options policy.TokenRequestOptions) (*azcore.AccessToken, error)
3334
}
3435

35-
func (mc mockCredential) GetToken(ctx context.Context, options shared.TokenRequestOptions) (*shared.AccessToken, error) {
36+
func (mc mockCredential) GetToken(ctx context.Context, options policy.TokenRequestOptions) (*azcore.AccessToken, error) {
3637
if mc.getTokenImpl != nil {
3738
return mc.getTokenImpl(ctx, options)
3839
}
39-
return &shared.AccessToken{Token: "***", ExpiresOn: time.Now().Add(time.Hour)}, nil
40+
return &azcore.AccessToken{Token: "***", ExpiresOn: time.Now().Add(time.Hour)}, nil
4041
}
4142

4243
func (mc mockCredential) NewAuthenticationPolicy() azpolicy.Policy {
@@ -47,11 +48,11 @@ func (mc mockCredential) Do(req *azpolicy.Request) (*http.Response, error) {
4748
return nil, nil
4849
}
4950

50-
func newTestPipeline(opts *azpolicy.ClientOptions) pipeline.Pipeline {
51+
func newTestPipeline(opts *azpolicy.ClientOptions) runtime.Pipeline {
5152
return runtime.NewPipeline("testmodule", "v0.1.0", runtime.PipelineOptions{}, opts)
5253
}
5354

54-
func defaultTestPipeline(srv azpolicy.Transporter, scope string) (pipeline.Pipeline, error) {
55+
func defaultTestPipeline(srv azpolicy.Transporter, scope string) (runtime.Pipeline, error) {
5556
retryOpts := azpolicy.RetryOptions{
5657
MaxRetryDelay: 500 * time.Millisecond,
5758
RetryDelay: time.Millisecond,
@@ -97,7 +98,7 @@ func TestBearerPolicy_CredentialFailGetToken(t *testing.T) {
9798
defer close()
9899
expectedErr := errors.New("oops")
99100
failCredential := mockCredential{}
100-
failCredential.getTokenImpl = func(ctx context.Context, options shared.TokenRequestOptions) (*shared.AccessToken, error) {
101+
failCredential.getTokenImpl = func(ctx context.Context, options policy.TokenRequestOptions) (*azcore.AccessToken, error) {
101102
return nil, expectedErr
102103
}
103104
b := NewBearerTokenPolicy(failCredential, nil)
@@ -156,7 +157,7 @@ func TestBearerPolicy_GetTokenFailsNoDeadlock(t *testing.T) {
156157
MaxRetries: 3,
157158
}
158159
b := NewBearerTokenPolicy(mockCredential{}, nil)
159-
pipeline := newTestPipeline(&azpolicy.ClientOptions{Transport: srv, Retry: retryOpts, PerRetryPolicies: []pipeline.Policy{b}})
160+
pipeline := newTestPipeline(&azpolicy.ClientOptions{Transport: srv, Retry: retryOpts, PerRetryPolicies: []azpolicy.Policy{b}})
160161
req, err := runtime.NewRequest(context.Background(), http.MethodGet, srv.URL())
161162
if err != nil {
162163
t.Fatal(err)
@@ -189,7 +190,7 @@ func TestBearerTokenWithAuxiliaryTenants(t *testing.T) {
189190
AuxiliaryTenants: []string{"tenant1", "tenant2", "tenant3"},
190191
},
191192
)
192-
pipeline := newTestPipeline(&azpolicy.ClientOptions{Transport: srv, Retry: retryOpts, PerRetryPolicies: []pipeline.Policy{b}})
193+
pipeline := newTestPipeline(&azpolicy.ClientOptions{Transport: srv, Retry: retryOpts, PerRetryPolicies: []azpolicy.Policy{b}})
193194
req, err := runtime.NewRequest(context.Background(), http.MethodGet, srv.URL())
194195
if err != nil {
195196
t.Fatalf("Unexpected error: %v", err)

0 commit comments

Comments
 (0)