Skip to content

Commit fb2b81c

Browse files
sandeep-senSandeep SenSandeep SentadeleshJeffreyRichter
authored
azstart example for getting started guide on ms learn (#360)
* adding azstart example * adding copyright header * downgrading to go1.18 * update to arm client factory * Accepting Jeff's suggestion - 1 Co-authored-by: Jeffrey Richter <[email protected]> * Accepting Jeff's suggestion - 2 Co-authored-by: Jeffrey Richter <[email protected]> * Accepting Jeff's suggestion - 3 Co-authored-by: Jeffrey Richter <[email protected]> * Accepting Jeff's suggestion - 4 Co-authored-by: Jeffrey Richter <[email protected]> --------- Co-authored-by: Sandeep Sen <[email protected]> Co-authored-by: Sandeep Sen <[email protected]> Co-authored-by: tadelesh <[email protected]> Co-authored-by: Jeffrey Richter <[email protected]>
1 parent 17a337a commit fb2b81c

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

sdk/azstart/LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Microsoft Corporation.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

sdk/azstart/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- golang
5+
products:
6+
- azure
7+
description: "These code samples will show you how to get started using Azure SDK for Golang."
8+
urlFragment: go
9+
---
10+
11+
# Getting started - Azure SDK for Go
12+
13+
These code sample will show you the basic usage pattern for Azure SDK for Go.
14+
15+
## Features
16+
17+
This project framework provides examples for the following usage pattern:
18+
19+
- How to create management plane clients - [`ExampleUsingARMClients`](azstart.go?plain=1#L14)
20+
- How to create data plane clients - [`ExampleUsingDataPlaneClients`](azstart.go?plain=1#L44)
21+
- How to page over responses - [`ExamplePagingOverACollection`](azstart.go?plain=1#L70)
22+
- How to use long running operations - [`ExampleLongRunningOperation`](azstart.go?plain=1#L103)
23+
24+
### Prerequisites
25+
* An [Azure subscription](https://azure.microsoft.com)
26+
* Go 1.18 or above
27+
28+
### Quickstart
29+
30+
1. Clone the repository.
31+
32+
```bash
33+
git clone https://github.com/Azure-Samples/azure-sdk-for-go-samples.git --branch new-version
34+
```
35+
36+
1. Run `azstart` sample.
37+
38+
```bash
39+
cd azure-sdk-for-go-samples/sdk/azstart
40+
go run azstart.go
41+
```
42+
43+
## Resources
44+
45+
- https://github.com/Azure/azure-sdk-for-go
46+
- https://docs.microsoft.com/en-us/azure/developer/go/
47+
- https://docs.microsoft.com/en-us/rest/api/
48+
- https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk
49+
50+
## Need help?
51+
52+
Post issue on Github (https://github.com/Azure/azure-sdk-for-go/issues)

sdk/azstart/azstart.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
package azstart
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
11+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
12+
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets"
13+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
14+
)
15+
16+
// ExampleUsingARMClients shows how to construct & use an ARM Client to invoke service methods
17+
func ExampleUsingARMClients() {
18+
// Construct a credential type from the azidentity package
19+
// or the module defining the client you wish to use
20+
credential, err := azidentity.NewDefaultAzureCredential(nil)
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
// Construct an ARM client factory passing subscription ID, credential, & optional options
26+
// which could be used to create any client in one ARM module
27+
clientFactory, err := armresources.NewClientFactory("<SubscriptionId>", credential, nil)
28+
29+
// This example creates a ResourceGroupsClient, but you can create any ARM client
30+
client := clientFactory.NewResourceGroupsClient()
31+
32+
// You can now call client methods to invoke service operations
33+
// This example calls CreateOrUpdate, but you can call any client method
34+
response, err := client.CreateOrUpdate(context.TODO(), "<ResourceGroupName>",
35+
armresources.ResourceGroup{
36+
Location: to.Ptr("<ResouceGroupLocation>"), // to.Ptr converts this string to a *string
37+
}, nil)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
// Use the service's response as your application desires
43+
fmt.Printf("Resource group ID: %s\n", *response.ResourceGroup.ID)
44+
}
45+
46+
// ExampleUsingDPClients shows how to construct & use a data-plane Client to invoke service methods
47+
func ExampleUsingDataPlaneClients() {
48+
// Construct a credential type from the azidentity package
49+
// or the module defining the client you wish to use
50+
credential, err := azidentity.NewDefaultAzureCredential(nil)
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
// Construct a DP client passing endpointURL, credential, & optional options
56+
client, err := azsecrets.NewClient("https://<KeyVaultName>.vault.azure.net/", credential, nil)
57+
if err != nil {
58+
panic(err)
59+
}
60+
61+
// You can now call client methods to invoke service operations
62+
response, err := client.SetSecret(context.TODO(), "<SecretName>",
63+
azsecrets.SetSecretParameters{Value: to.Ptr("<SecretValue>")}, nil)
64+
if err != nil {
65+
panic(err)
66+
}
67+
68+
// Use the service's response as your application desires
69+
fmt.Printf("Name: %s, Value: %s\n", *response.ID, *response.Value)
70+
}
71+
72+
// ExamplePagingOverACollection shows how to page over a collection's items
73+
func ExamplePagingOverACollection() {
74+
// Construct a credential type from the azidentity
75+
credential, err := azidentity.NewDefaultAzureCredential(nil)
76+
if err != nil {
77+
panic(err)
78+
}
79+
80+
client, err := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, nil)
81+
if err != nil {
82+
panic(err)
83+
}
84+
85+
// Call a client method that creates a XxxPager; this does NOT invoke a service operation
86+
for pager := client.NewListPager(nil); pager.More(); {
87+
// While pages are getable, request a page of items from the service
88+
page, err := pager.NextPage(context.TODO())
89+
if err != nil {
90+
panic(err)
91+
}
92+
93+
// Process the page's items.
94+
// NOTE: The service desides how many items to return on a page.
95+
// If a page has 0 items, go get the next page.
96+
// Other clients may be adding/deleting items from the collection while
97+
// this code is paging; some items may be skipped or returned multiple times.
98+
for _, item := range page.Value {
99+
_ = item // Here's where your code processes the item as you desire
100+
}
101+
// Looping around will request the next page of items from the service
102+
}
103+
}
104+
105+
// ExampleLongRunningOperation shows how to invoke a long-running operation and poll for its completion
106+
func ExampleLongRunningOperation() {
107+
// Construct a credential type from the azidentity
108+
credential, err := azidentity.NewDefaultAzureCredential(nil)
109+
if err != nil {
110+
panic(err)
111+
}
112+
113+
client, err := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, nil)
114+
if err != nil {
115+
panic(err)
116+
}
117+
118+
// Initiating a long-Running Operation causes the method to return a Poller[T]
119+
poller, err := client.BeginDelete(context.TODO(), "<ResourceGroupName>", nil)
120+
if err != nil {
121+
panic(err)
122+
}
123+
124+
// PollUntilDone causes your goroutine to periodically ask the service the status of the LRO
125+
// It ultimately returns when the operation succeeds, fails, or was canceled.
126+
// If the operation succeeds, err == nil and lroResult has result (if any); else err != nil
127+
lroResult, err := poller.PollUntilDone(context.TODO(), nil)
128+
if err != nil {
129+
panic(err)
130+
}
131+
_ = lroResult // Examine sucessful result (if any)
132+
}

sdk/azstart/go.mod

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module azstart
2+
3+
go 1.18
4+
5+
require (
6+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0
7+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2
8+
github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets v0.11.0
9+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.0
10+
)
11+
12+
require (
13+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0 // indirect
14+
github.com/Azure/azure-sdk-for-go/sdk/keyvault/internal v0.7.1 // indirect
15+
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0 // indirect
16+
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
17+
github.com/google/uuid v1.3.0 // indirect
18+
github.com/kylelemons/godebug v1.1.0 // indirect
19+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
20+
golang.org/x/crypto v0.7.0 // indirect
21+
golang.org/x/net v0.8.0 // indirect
22+
golang.org/x/sys v0.6.0 // indirect
23+
golang.org/x/text v0.8.0 // indirect
24+
)

sdk/azstart/go.sum

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 h1:rTnT/Jrcm+figWlYz4Ixzt0SJVR2cMC8lvZcimipiEY=
2+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0/go.mod h1:ON4tFdPTwRcgWEaVDrN3584Ef+b7GgSJaXxe5fW9t4M=
3+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2 h1:uqM+VoHjVH6zdlkLF2b6O0ZANcHoj3rO0PoQ3jglUJA=
4+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2/go.mod h1:twTKAa1E6hLmSDjLhaCkbTMQKc7p/rNLU40rLxGEOCI=
5+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0 h1:leh5DwKv6Ihwi+h60uHtn6UWAxBbZ0q8DwQVMzf61zw=
6+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
7+
github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets v0.11.0 h1:82w8tzLcOwDP/Q35j/wEBPt0n0kVC3cjtPdD62G8UAk=
8+
github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets v0.11.0/go.mod h1:S78i9yTr4o/nXlH76bKjGUye9Z2wSxO5Tz7GoDr4vfI=
9+
github.com/Azure/azure-sdk-for-go/sdk/keyvault/internal v0.7.1 h1:FbH3BbSb4bvGluTesZZ+ttN/MDsnMmQP36OSnDuSXqw=
10+
github.com/Azure/azure-sdk-for-go/sdk/keyvault/internal v0.7.1/go.mod h1:9V2j0jn9jDEkCkv8w/bKTNppX/d0FVA1ud77xCIP4KA=
11+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal v1.1.2 h1:mLY+pNLjCUeKhgnAJWAKhEUQM+RJQo2H1fuGSw1Ky1E=
12+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups v1.0.0 h1:pPvTJ1dY0sA35JOeFq6TsY2xj6Z85Yo23Pj4wCCvu4o=
13+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.0 h1:yV3wcPPLQ+SLqJmgCs/wXKLxZkswMV4wCdNlG5XY4bQ=
14+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.0/go.mod h1:c/wcGeGx5FUPbM/JltUYHZcKmigwyVLJlDq+4HdtXaw=
15+
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0 h1:UE9n9rkJF62ArLb1F3DEjRt8O3jLwMWdSoypKV4f3MU=
16+
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o=
17+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
18+
github.com/dnaeon/go-vcr v1.1.0 h1:ReYa/UBrRyQdant9B4fNHGoCNKw6qh6P0fsdGmZpR7c=
19+
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
20+
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
21+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
22+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
23+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
24+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
25+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
26+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
27+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
28+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
29+
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
30+
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
31+
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
32+
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
33+
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34+
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
35+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
36+
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
37+
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
38+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
39+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=

0 commit comments

Comments
 (0)