Skip to content

Commit 33b488d

Browse files
authored
Update get started and migration guide to use new client factory (Azure#20604)
* Update get started and migration guide to use new client factory * fix * fix * fi * fix * fix * fix
1 parent 77eb24d commit 33b488d

File tree

3 files changed

+60
-35
lines changed

3 files changed

+60
-35
lines changed

documentation/MIGRATION_GUIDE.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ import "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresou
5858

5959
```go
6060
credential, err := azidentity.NewClientSecretCredential("<TenantId>", "<ClientId>", "<ClientSecret>", nil)
61-
client, err := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, nil)
61+
clientFactory, err := armresources.NewClientFactory(<subscription ID>, credential, &options)
62+
if err != nil {
63+
log.Fatal(err)
64+
}
65+
client := clientFactory.NewResourceGroupsClient()
6266
```
6367

6468
For detailed information on the benefits of using the new authentication types, please refer to [this page](https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/azidentity/README.md)
@@ -111,7 +115,11 @@ options := arm.ClientOptions {
111115
Cloud: cloud.AzureChina,
112116
},
113117
}
114-
client, err := armcompute.NewVirtualMachinesClient("<SubscriptionId>", credential, &options)
118+
clientFactory, err := armcompute.NewClientFactory(<subscription ID>, credential, &options)
119+
if err != nil {
120+
log.Fatal(err)
121+
}
122+
client := clientFactory.NewVirtualMachinesClient()
115123
```
116124

117125
For detailed information on the cloud configuration, please refer to [this page](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud)
@@ -275,7 +283,11 @@ options := &arm.ClientOptions{
275283
Transport: &httpClient,
276284
},
277285
}
278-
client, err := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, options)
286+
clientFactory, err := armresources.NewClientFactory(<subscription ID>, credential, &options)
287+
if err != nil {
288+
log.Fatal(err)
289+
}
290+
client := clientFactory.NewResourceGroupsClient()
279291
```
280292

281293
## Need help?

documentation/new-version-guideline.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ import "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresou
152152
```
153153

154154
```go
155-
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential,
155+
clientFactory, err := armresources.NewClientFactory(subscriptionId, credential,
156156
&arm.ClientOptions{
157157
ClientOptions: policy.ClientOptions{
158158
Retry: policy.RetryOptions{
@@ -162,6 +162,10 @@ rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential
162162
},
163163
},
164164
)
165+
if err != nil {
166+
log.Fatal(err)
167+
}
168+
rgClient := clientFactory.NewResourceGroupsClient()
165169
```
166170

167171
### Customized Policy
@@ -181,13 +185,17 @@ import "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresou
181185
```go
182186
// your own implementation of HTTP client
183187
httpClient := NewYourOwnHTTPClient{}
184-
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential,
188+
clientFactory, err := armresources.NewClientFactory(subscriptionId, credential,
185189
&arm.ClientOptions{
186190
ClientOptions: policy.ClientOptions{
187191
Transport: &httpClient,
188192
},
189193
},
190194
)
195+
if err != nil {
196+
log.Fatal(err)
197+
}
198+
rgClient := clientFactory.NewResourceGroupsClient()
191199
```
192200

193201
### Reference
@@ -210,7 +218,7 @@ import "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresou
210218
```
211219

212220
```go
213-
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential,
221+
clientFactory, err := armresources.NewClientFactory(subscriptionId, credential,
214222
&arm.ClientOptions{
215223
ClientOptions: policy.ClientOptions{
216224
Logging: policy.LogOptions{
@@ -220,6 +228,10 @@ rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential
220228
},
221229
},
222230
)
231+
if err != nil {
232+
log.Fatal(err)
233+
}
234+
rgClient := clientFactory.NewResourceGroupsClient()
223235
```
224236

225237
You could use the `azcore/log` package to control log event and redirect log to the desired location. For example:

documentation/new-version-quickstart.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ cred, err := azidentity.NewDefaultAzureCredential(nil)
107107

108108
For more details on how authentication works in `azidentity`, please see the documentation for `azidentity` at [pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity).
109109

110+
## Client Factory
111+
110112

111113
## Creating a Resource Management Client
112114

113-
Once you have a credential, you will need to decide what service to use and create a client to connect to that service. In this section, we will use `Compute` as our target service. The Compute modules consist of one or more clients. A client groups a set of related APIs, providing access to its functionality within the specified subscription. You will need to create one or more clients to access the APIs you require using your `azcore.TokenCredential`.
115+
Once you have a credential, you will need to decide what service to use and create a client to connect to that service. We provide a client factory which could be used to create any client in one service module. In this section, we will use `Compute` as our target service. The Compute modules consist of one or more clients. A client groups a set of related APIs, providing access to its functionality within the specified subscription. You will need to create one or more clients through the client factory to access the APIs you require using your `azcore.TokenCredential`.
114116

115117
To show an example, we will create a client to manage Virtual Machines. The code to achieve this task would be:
116118

@@ -119,7 +121,8 @@ import "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute
119121
```
120122

121123
```go
122-
client, err := armcompute.NewVirtualMachinesClient("<subscription ID>", credential, nil)
124+
computeClientFactory,err := armcompute.NewClientFactory("<subscription ID>", credential, nil)
125+
client := computeClientFactory.NewVirtualMachinesClient()
123126
```
124127
You can use the same pattern to connect with other Azure services that you are using. For example, in order to manage Virtual Network resources, you would install the Network package and create a `VirtualNetwork` Client:
125128

@@ -129,7 +132,8 @@ import "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork
129132
```
130133

131134
```go
132-
client, err := armnetwork.NewVirtualNetworksClient("<subscription ID>", credential, nil)
135+
networkClientFactory,err := armnetwork.NewClientFactory("<subscription ID>", credential, nil)
136+
client := networkClientFactory.NewVirtualNetworksClient()
133137
```
134138

135139
## Interacting with Azure Resources
@@ -175,16 +179,13 @@ var (
175179
location = "westus2"
176180
resourceGroupName = "resourceGroupName"
177181
interval = 5 * time.Second
182+
rgClient *armresources.ResourceGroupsClient
178183
)
179184
```
180185

181186
***Write a function to create a resource group***
182187
```go
183-
func createResourceGroup(ctx context.Context, credential azcore.TokenCredential) (*armresources.ResourceGroupsClientCreateOrUpdateResponse, error) {
184-
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
185-
if err != nil {
186-
return nil, err
187-
}
188+
func createResourceGroup(ctx context.Context) (*armresources.ResourceGroupsClientCreateOrUpdateResponse, error) {
188189

189190
param := armresources.ResourceGroup{
190191
Location: to.Ptr(location),
@@ -203,8 +204,14 @@ func main() {
203204
if err != nil {
204205
log.Fatalf("authentication failure: %+v", err)
205206
}
206-
207-
resourceGroup, err := createResourceGroup(ctx, cred)
207+
208+
clientFactory, err := armresources.NewClientFactory(subscriptionId, cred, nil)
209+
if err != nil {
210+
log.Fatalf("cannot create client factory: %+v", err)
211+
}
212+
rgClient = clientFactory.NewResourceGroupsClient()
213+
214+
resourceGroup, err := createResourceGroup(ctx)
208215
if err != nil {
209216
log.Fatalf("cannot create resource group: %+v", err)
210217
}
@@ -219,11 +226,7 @@ Let's demonstrate management client's usage by showing additional samples.
219226
***Update a resource group***
220227

221228
```go
222-
func updateResourceGroup(ctx context.Context, credential azcore.TokenCredential) (*armresources.ResourceGroupsClientUpdateResponse, error) {
223-
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
224-
if err != nil {
225-
return nil, err
226-
}
229+
func updateResourceGroup(ctx context.Context) (*armresources.ResourceGroupsClientUpdateResponse, error) {
227230

228231
update := armresources.ResourceGroupPatchable{
229232
Tags: map[string]*string{
@@ -240,11 +243,7 @@ func updateResourceGroup(ctx context.Context, credential azcore.TokenCredential)
240243
***List all resource groups***
241244

242245
```go
243-
func listResourceGroups(ctx context.Context, credential azcore.TokenCredential) ([]*armresources.ResourceGroup, error) {
244-
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
245-
if err != nil {
246-
return nil, err
247-
}
246+
func listResourceGroups(ctx context.Context) ([]*armresources.ResourceGroup, error) {
248247

249248
pager := rgClient.NewListPager(nil)
250249

@@ -267,11 +266,7 @@ You could see there is a pattern for pageable operation here. With `NewListPager
267266
***Delete a resource group***
268267

269268
```go
270-
func deleteResourceGroup(ctx context.Context, credential azcore.TokenCredential) error {
271-
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
272-
if err != nil {
273-
return err
274-
}
269+
func deleteResourceGroup(ctx context.Context) error {
275270

276271
poller, err := rgClient.BeginDelete(ctx, resourceGroupName, nil)
277272
if err != nil {
@@ -291,25 +286,31 @@ func main() {
291286
log.Fatalf("authentication failure: %+v", err)
292287
}
293288

294-
resourceGroup, err := createResourceGroup(ctx, cred)
289+
clientFactory, err := armresources.NewClientFactory(subscriptionId, cred, nil)
290+
if err != nil {
291+
log.Fatalf("cannot create client factory: %+v", err)
292+
}
293+
rgClient = clientFactory.NewResourceGroupsClient()
294+
295+
resourceGroup, err := createResourceGroup(ctx)
295296
if err != nil {
296297
log.Fatalf("cannot create resource group: %+v", err)
297298
}
298299
log.Printf("Resource Group %s created", *resourceGroup.ResourceGroup.ID)
299300

300-
updatedRG, err := updateResourceGroup(ctx, cred)
301+
updatedRG, err := updateResourceGroup(ctx)
301302
if err != nil {
302303
log.Fatalf("cannot update resource group: %+v", err)
303304
}
304305
log.Printf("Resource Group %s updated", *updatedRG.ResourceGroup.ID)
305306

306-
rgList, err := listResourceGroups(ctx, cred)
307+
rgList, err := listResourceGroups(ctx)
307308
if err != nil {
308309
log.Fatalf("cannot list resource group: %+v", err)
309310
}
310311
log.Printf("We totally have %d resource groups", len(rgList))
311312

312-
if err := deleteResourceGroup(ctx, cred); err != nil {
313+
if err := deleteResourceGroup(ctx); err != nil {
313314
log.Fatalf("cannot delete resource group: %+v", err)
314315
}
315316
log.Printf("Resource Group deleted")

0 commit comments

Comments
 (0)