From ca83ad5d0163334eac085378ffb9de9b072ec8a2 Mon Sep 17 00:00:00 2001 From: Aigerim Beishenbekova Date: Mon, 13 Feb 2023 14:39:45 +0100 Subject: [PATCH] added client with managed identity --- .../azcommunicationidentity/client.go | 33 +++++++++++++++---- .../azcommunicationidentity/example_test.go | 26 +++++++++++++++ .../azcommunicationidentity/go.mod | 12 ++++++- .../azcommunicationidentity/go.sum | 18 ++++++++++ 4 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 sdk/communication/azcommunicationidentity/example_test.go diff --git a/sdk/communication/azcommunicationidentity/client.go b/sdk/communication/azcommunicationidentity/client.go index f75032367835..56a2936751d0 100644 --- a/sdk/communication/azcommunicationidentity/client.go +++ b/sdk/communication/azcommunicationidentity/client.go @@ -20,11 +20,14 @@ package azcommunicationidentity import ( "context" "errors" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" "net/http" "net/url" "strings" + + "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" + ) type Client struct { @@ -32,15 +35,31 @@ type Client struct { pl runtime.Pipeline } -// NewClient creates a new instance of Client with the specified values. +// ClientOptions contains the optional parameters when creating a Client. +type ClientOptions struct { + azcore.ClientOptions +} + +// NewClient creates a new instance of Azure Communication Identity Client with the specified values. // - endpoint - The communication resource, for example https://my-resource.communication.azure.com -// - pl - the pipeline used for sending requests and handling responses. -func NewClient(endpoint string, pl runtime.Pipeline) *Client { - client := &Client{ +// - cred - an Azure AD credential, typically obtained via the azidentity module +// - options - client options; pass nil to accept the default values +func NewClient(endpoint string, credential azcore.TokenCredential, options *ClientOptions) *Client { + authPolicy := runtime.NewBearerTokenPolicy(credential, []string{"https://communication.azure.com//.default"}, nil) + var conOptions = getClientOptions(options) + conOptions.PerRetryPolicies = append(conOptions.PerRetryPolicies, authPolicy) + pl := runtime.NewPipeline("azcommunicationidentity", "v1.0.0", runtime.PipelineOptions{}, &conOptions.ClientOptions) + return &Client{ endpoint: endpoint, pl: pl, } - return client +} + +func getClientOptions[T any](o *T) *T { + if o == nil { + return new(T) + } + return o } // Create - Create a new identity, and optionally, an access token. diff --git a/sdk/communication/azcommunicationidentity/example_test.go b/sdk/communication/azcommunicationidentity/example_test.go new file mode 100644 index 000000000000..37c20996cc7b --- /dev/null +++ b/sdk/communication/azcommunicationidentity/example_test.go @@ -0,0 +1,26 @@ + +package azcommunicationidentity_test + +import ( + "fmt" + "context" + "testing" + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" + "github.com/Azure/azure-sdk-for-go/sdk/communication/azcommunicationidentity" +) + +func TestExampleNewServiceClient(t *testing.T) { + endpoint := "" + + cred, err := azidentity.NewDefaultAzureCredential(nil) + if err != nil { + panic(err) + } + client := azcommunicationidentity.NewClient(endpoint, cred, nil) + clientCreateResponse, _ := client.Create(context.TODO(), nil) + fmt.Printf("response>>%#v", clientCreateResponse) + id := *clientCreateResponse.AccessTokenResult.Identity.ID + fmt.Printf("\nID>>%#v", id) + clientDeleteResponse, _ := client.Delete(context.TODO(), id, nil) + fmt.Printf("\ndelete response>>%#v", clientDeleteResponse) +} \ No newline at end of file diff --git a/sdk/communication/azcommunicationidentity/go.mod b/sdk/communication/azcommunicationidentity/go.mod index 9dd0361b9728..bf4581d0bacc 100644 --- a/sdk/communication/azcommunicationidentity/go.mod +++ b/sdk/communication/azcommunicationidentity/go.mod @@ -2,10 +2,20 @@ module github.com/Azure/azure-sdk-for-go/sdk/communication/azcommunicationidenti go 1.18 -require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1 +require ( + github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1 + github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0 +) require ( github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirect + github.com/AzureAD/microsoft-authentication-library-for-go v0.5.1 // indirect + github.com/golang-jwt/jwt v3.2.1+incompatible // indirect + github.com/google/uuid v1.1.1 // indirect + github.com/kylelemons/godebug v1.1.0 // indirect + github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 // indirect + golang.org/x/crypto v0.0.0-20220511200225-c6db032c6c88 // indirect golang.org/x/net v0.5.0 // indirect + golang.org/x/sys v0.4.0 // indirect golang.org/x/text v0.6.0 // indirect ) diff --git a/sdk/communication/azcommunicationidentity/go.sum b/sdk/communication/azcommunicationidentity/go.sum index a113e22ca43b..fecdd60b6ada 100644 --- a/sdk/communication/azcommunicationidentity/go.sum +++ b/sdk/communication/azcommunicationidentity/go.sum @@ -1,12 +1,30 @@ github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1 h1:gVXuXcWd1i4C2Ruxe321aU+IKGaStvGB/S90PUPB/W8= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1/go.mod h1:DffdKW9RFqa5VgmsjUOsS7UE7eiA5iAvYUs63bhKQ0M= +github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0 h1:QkAcEIAKbNL4KoFr4SathZPhDhF4mVwpBMFlYjyAqy8= +github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0/go.mod h1:bhXu1AjYL+wutSL/kpSq6s7733q2Rb0yuot9Zgfqa/0= github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 h1:+5VZ72z0Qan5Bog5C+ZkgSqUbeVUd9wgtHOrIKuc5b8= github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w= +github.com/AzureAD/microsoft-authentication-library-for-go v0.5.1 h1:BWe8a+f/t+7KY7zH2mqygeUD0t8hNFXe08p1Pb3/jKE= +github.com/AzureAD/microsoft-authentication-library-for-go v0.5.1/go.mod h1:Vt9sXTKwMyGcOxSmLDMnGPgqsUg7m8pe215qMLrDXw4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c= +github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= +github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 h1:Qj1ukM4GlMWXNdMBuXcXfz/Kw9s1qm0CLY32QxuSImI= +github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +golang.org/x/crypto v0.0.0-20220511200225-c6db032c6c88 h1:Tgea0cVUD0ivh5ADBX4WwuI12DUd2to3nCYe2eayMIw= +golang.org/x/crypto v0.0.0-20220511200225-c6db032c6c88/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=