Skip to content

Commit ac877b9

Browse files
authored
doc: update quickstart and migration guide (Azure#17612)
* doc: update quickstart and migration guide * fix: wrong usage of log.Fatal * fix: tab problem * fix: add version prerequisites * fix: tab problem, context change and some minor change
1 parent 8d5dc19 commit ac877b9

File tree

2 files changed

+85
-71
lines changed

2 files changed

+85
-71
lines changed

documentation/MIGRATION_GUIDE.md

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ This document is intended for users that are familiar with the previous version
1818

1919
## Prerequisites
2020

21-
The last 2 released stable versions of Go are supported.
21+
- Go 1.18
22+
- Latest version of resource management modules
2223

2324
## General Changes
2425

@@ -46,7 +47,7 @@ client.Authorizer = authorizer
4647

4748
```go
4849
credential, err := azidentity.NewClientSecretCredential("<TenantId>", "<ClientId>", "<ClientSecret>", nil)
49-
client := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, nil)
50+
client, err := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, nil)
5051
```
5152

5253
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)
@@ -55,42 +56,32 @@ For detailed information on the benefits of using the new authentication types,
5556

5657
There are some minor changes in the error handling.
5758

58-
- When there is an error in the SDK request, in the previous version (`services/**/mgmt/**`), the return value will all be non-nil, and you can get the raw HTTP response from the response value. In the latest version (`sdk/resourcemanager/**/arm**`), the first return value will be empty and you need to convert the error to the `azcore.HTTPResponse` interface to get the raw HTTP response. When the request is successful and there is no error returned, you can get the raw HTTP response in `RawResponse` property of the first return value.
59+
- When there is an error in the SDK request, in the previous version (`services/**/mgmt/**`), the return value will all be non-nil, and you can get the raw HTTP response from the response value. In the latest version (`sdk/resourcemanager/**/arm**`), the first return value will be empty and you need to convert the error to the `azcore.ResponseError` interface to get the raw HTTP response. When the request is successful and there is no error returned, you can get the raw HTTP response from request context.
5960

6061
**Previous version (`services/**/mgmt/**`)**
6162

6263
```go
63-
resp, err := resourceGroupsClient.CreateOrUpdate(context.Background(), resourceGroupName, resourceGroupParameters)
64+
resp, err := resourceGroupsClient.CreateOrUpdate(context.TODO(), resourceGroupName, resourceGroupParameters)
6465
if err != nil {
65-
log.Fatalf("Status code: %d", resp.Response.Response.StatusCode)
66+
log.Fatalf("Status code: %d", resp.Response().StatusCode)
6667
}
6768
```
6869

6970
**Latest version (`sdk/resourcemanager/**/arm**`)**
7071

7172
```go
72-
resp, err := resourceGroupsClient.CreateOrUpdate(context.Background(), resourceGroupName, resourceGroupParameters, nil)
73+
var rawResponse *http.Response
74+
ctxWithResp := runtime.WithCaptureResponse(context.TODO(), &rawResponse)
75+
resp, err := resourceGroupsClient.CreateOrUpdate(ctxWithResp, resourceGroupName, resourceGroupParameters, nil)
7376
if err != nil {
74-
var respErr azcore.HTTPResponse
77+
var respErr *azcore.ResponseError
7578
if errors.As(err, &respErr) {
76-
log.Fatalf("Status code: %d", respErr.RawResponse().StatusCode)
79+
log.Fatalf("Status code: %d", respErr.RawResponse.StatusCode)
80+
} else {
81+
log.Fatalf("Other error: %+v", err)
7782
}
78-
log.Fatalf("Other error: %+v", err)
7983
}
80-
```
81-
82-
**When there is no error in latest version (`sdk/resourcemanager/**/arm**`)**
83-
84-
```go
85-
resp, err := resourceGroupsClient.CreateOrUpdate(context.Background(), resourceGroupName, resourceGroupParameters, nil)
86-
if err != nil {
87-
var respErr azcore.HTTPResponse
88-
if errors.As(err, &respErr) {
89-
log.Fatalf("Status code: %d", respErr.RawResponse().StatusCode)
90-
}
91-
log.Fatalf("Other error: %+v", err)
92-
}
93-
log.Printf("Status code: %d", resp.RawResponse.StatusCode)
84+
log.Printf("Status code: %d", rawResponse.StatusCode)
9485
```
9586

9687
### Long Running Operations
@@ -102,28 +93,28 @@ In the latest version, if a request is a long-running operation, the function na
10293
**Previous version (`services/**/mgmt/**`)**
10394

10495
```go
105-
future, err := virtualMachinesClient.CreateOrUpdate(context.Background(), "<resource group name>", "<virtual machine name>", param)
96+
future, err := virtualMachinesClient.CreateOrUpdate(context.TODO(), "<resource group name>", "<virtual machine name>", param)
10697
if err != nil {
107-
log.Fatal(err)
98+
log.Fatal(err)
10899
}
109-
if err := future.WaitForCompletionRef(context.Background(), virtualMachinesClient.Client); err != nil {
110-
log.Fatal(err)
100+
if err := future.WaitForCompletionRef(context.TODO(), virtualMachinesClient.Client); err != nil {
101+
log.Fatal(err)
111102
}
112103
vm, err := future.Result(virtualMachinesClient)
113104
if err != nil {
114-
log.Fatal(err)
105+
log.Fatal(err)
115106
}
116107
log.Printf("virtual machine ID: %v", *vm.ID)
117108
```
118109

119110
**Latest version (`sdk/resourcemanager/**/arm**`)**
120111

121112
```go
122-
poller, err := client.BeginCreateOrUpdate(context.Background(), "<resource group name>", "<virtual machine name>", param, nil)
113+
poller, err := client.BeginCreateOrUpdate(context.TODO(), "<resource group name>", "<virtual machine name>", param, nil)
123114
if err != nil {
124-
log.Fatal(err)
115+
log.Fatal(err)
125116
}
126-
resp, err := poller.PollUntilDone(context.Background(), 30*time.Second)
117+
resp, err := poller.PollUntilDone(context.TODO(), 30*time.Second)
127118
if err != nil {
128119
log.Fatal(err)
129120
}
@@ -139,15 +130,15 @@ In the latest version, if a request is a paginated operation, a struct `**Pager`
139130
**Previous version (`services/**/mgmt/**`)**
140131

141132
```go
142-
pager, err := resourceGroupsClient.List(context.Background(), "", nil)
133+
pager, err := resourceGroupsClient.List(context.TODO(), "", nil)
143134
if err != nil {
144135
log.Fatal(err)
145136
}
146137
for p.NotDone() {
147138
for _, v := range pager.Values() {
148139
log.Printf("resource group ID: %s\n", *rg.ID)
149140
}
150-
if err := pager.NextWithContext(context.Background()); err != nil {
141+
if err := pager.NextWithContext(context.TODO()); err != nil {
151142
log.Fatal(err)
152143
}
153144
}
@@ -156,15 +147,16 @@ for p.NotDone() {
156147
**Latest version (`sdk/resourcemanager/**/arm**`)**
157148

158149
```go
159-
pager := resourceGroupsClient.List(nil)
160-
for pager.NextPage(context.Background()) {
161-
for _, rg := range pager.PageResponse().ResourceGroupListResult.Value {
150+
pager := resourceGroupsClient.NewListPager(nil)
151+
for pager.More() {
152+
nextResult, err := pager.NextPage(ctx)
153+
if err != nil {
154+
log.Fatalf("failed to advance page: %v", err)
155+
}
156+
for _, rg := range nextResult.Value {
162157
log.Printf("resource group ID: %s\n", *rg.ID)
163158
}
164159
}
165-
if err := pager.Err(); err != nil {
166-
log.Fatalf("failed to advance page: %v", err)
167-
}
168160
```
169161

170162
### Customized Policy
@@ -195,7 +187,7 @@ options := &arm.ClientOptions{
195187
Transport: &httpClient,
196188
},
197189
}
198-
client := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, options)
190+
client, err := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, options)
199191
```
200192

201193
## Need help?

documentation/new-version-quickstart.md

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ If you are an existing user of the older version of Azure management library for
2323
Prerequisites
2424
-------------
2525

26+
You will need Go 1.18 and latest version of resource management modules.
27+
2628
You will need the following values to authenticate to Azure
2729

2830
- **Subscription ID**
@@ -112,12 +114,12 @@ Once you have a credential, you will need to decide what service to use and crea
112114
To show an example, we will create a client to manage Virtual Machines. The code to achieve this task would be:
113115

114116
```go
115-
client := armcompute.NewVirtualMachinesClient("<subscription ID>", credential, nil)
117+
client, err := armcompute.NewVirtualMachinesClient("<subscription ID>", credential, nil)
116118
```
117119
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:
118120

119121
```go
120-
client := armnetwork.NewVirtualNetworksClient("<subscription ID>", credential, nil)
122+
client, err := armnetwork.NewVirtualNetworksClient("<subscription ID>", credential, nil)
121123
```
122124

123125
Interacting with Azure Resources
@@ -151,8 +153,6 @@ import (
151153
"time"
152154

153155
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
154-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
155-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
156156
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
157157
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
158158
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
@@ -162,7 +162,7 @@ import (
162162
***Define some global variables***
163163
```go
164164
var (
165-
ctx = context.Background()
165+
ctx = context.TODO()
166166
subscriptionId = os.Getenv("AZURE_SUBSCRIPTION_ID")
167167
location = "westus2"
168168
resourceGroupName = "resourceGroupName"
@@ -172,14 +172,19 @@ var (
172172

173173
***Write a function to create a resource group***
174174
```go
175-
func createResourceGroup(ctx context.Context, credential azcore.TokenCredential) (armresources.ResourceGroupsCreateOrUpdateResponse, error) {
176-
rgClient := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
175+
func createResourceGroup(ctx context.Context, credential azcore.TokenCredential) (*armresources.ResourceGroupsClientCreateOrUpdateResponse, error) {
176+
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
177+
if err != nil {
178+
return nil, err
179+
}
177180

178181
param := armresources.ResourceGroup{
179-
Location: to.StringPtr(location),
182+
Location: to.Ptr(location),
180183
}
181184

182-
return rgClient.CreateOrUpdate(context.Background(), resourceGroupName, param, nil)
185+
resp, err := rgClient.CreateOrUpdate(ctx, resourceGroupName, param, nil)
186+
187+
return &resp, err
183188
}
184189
```
185190

@@ -207,43 +212,59 @@ Example: Managing Resource Groups
207212
***Update a resource group***
208213

209214
```go
210-
func updateResourceGroup(ctx context.Context, credential azcore.TokenCredential) (armresources.ResourceGroupsUpdateResponse, error) {
211-
rgClient := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
215+
func updateResourceGroup(ctx context.Context, credential azcore.TokenCredential) (*armresources.ResourceGroupsClientUpdateResponse, error) {
216+
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
217+
if err != nil {
218+
return nil, err
219+
}
212220

213221
update := armresources.ResourceGroupPatchable{
214222
Tags: map[string]*string{
215-
"new": to.StringPtr("tag"),
223+
"new": to.Ptr("tag"),
216224
},
217225
}
218-
return rgClient.Update(ctx, resourceGroupName, update, nil)
226+
227+
resp,err :=rgClient.Update(ctx, resourceGroupName, update, nil)
228+
229+
return &resp, err
219230
}
220231
```
221232

222233
***List all resource groups***
223234

224235
```go
225236
func listResourceGroups(ctx context.Context, credential azcore.TokenCredential) ([]*armresources.ResourceGroup, error) {
226-
rgClient := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
227-
228-
pager := rgClient.List(nil)
229-
237+
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
238+
if err != nil {
239+
return nil, err
240+
}
241+
242+
pager := rgClient.NewListPager(nil)
243+
230244
var resourceGroups []*armresources.ResourceGroup
231-
for pager.NextPage(ctx) {
232-
resp := pager.PageResponse()
233-
if resp.ResourceGroupListResult.Value != nil {
234-
resourceGroups = append(resourceGroups, resp.ResourceGroupListResult.Value...)
245+
for pager.More() {
246+
nextResult, err := pager.NextPage(ctx)
247+
if err != nil {
248+
return nil, err
249+
}
250+
if nextResult.ResourceGroupListResult.Value != nil {
251+
resourceGroups = append(resourceGroups, nextResult.ResourceGroupListResult.Value...)
235252
}
236253
}
237-
return resourceGroups, pager.Err()
254+
255+
return resourceGroups, nil
238256
}
239257
```
240258

241259
***Delete a resource group***
242260

243261
```go
244262
func deleteResourceGroup(ctx context.Context, credential azcore.TokenCredential) error {
245-
rgClient := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
246-
263+
rgClient, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
264+
if err != nil {
265+
return err
266+
}
267+
247268
poller, err := rgClient.BeginDelete(ctx, resourceGroupName, nil)
248269
if err != nil {
249270
return err
@@ -256,10 +277,10 @@ func deleteResourceGroup(ctx context.Context, credential azcore.TokenCredential)
256277
***Invoking the update, list and delete of resource group in the main function***
257278
```go
258279
func main() {
259-
cred, err := azidentity.NewDefaultAzureCredential(nil)
260-
if err != nil {
261-
log.Fatalf("authentication failure: %+v", err)
262-
}
280+
cred, err := azidentity.NewDefaultAzureCredential(nil)
281+
if err != nil {
282+
log.Fatalf("authentication failure: %+v", err)
283+
}
263284

264285
resourceGroup, err := createResourceGroup(ctx, cred)
265286
if err != nil {
@@ -283,7 +304,7 @@ func main() {
283304
log.Fatalf("cannot delete resource group: %+v", err)
284305
}
285306
log.Printf("Resource Group deleted")
286-
})
307+
}
287308
```
288309

289310
Example: Managing Virtual Machines
@@ -297,11 +318,12 @@ Long Running Operations
297318
In the samples above, you might notice that some operations have a ``Begin`` prefix (for example, ``BeginDelete``). This indicates the operation is a Long-Running Operation (LRO). For resource management libraries, this kind of operation is quite common since certain resource operations may take a while to finish. When you need to use those LROs, you will need to use a poller and keep polling for the result until it is done. To illustrate this pattern, here is an example
298319

299320
```go
300-
poller, err := client.BeginCreate(context.Background(), "resource_identifier", "additonal_parameter")
321+
ctx := context.TODO()
322+
poller, err := client.BeginCreate(ctx, "resource_identifier", "additonal_parameter")
301323
if err != nil {
302324
// handle error...
303325
}
304-
resp, err = poller.PollUntilDone(context.Background(), 5 * time.Second)
326+
resp, err = poller.PollUntilDone(ctx, 5 * time.Second)
305327
if err != nil {
306328
// handle error...
307329
}

0 commit comments

Comments
 (0)