Skip to content

Commit

Permalink
azstart example for getting started guide on ms learn (#360)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
5 people authored May 1, 2023
1 parent 17a337a commit fb2b81c
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 0 deletions.
21 changes: 21 additions & 0 deletions sdk/azstart/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Microsoft Corporation.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
52 changes: 52 additions & 0 deletions sdk/azstart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
page_type: sample
languages:
- golang
products:
- azure
description: "These code samples will show you how to get started using Azure SDK for Golang."
urlFragment: go
---

# Getting started - Azure SDK for Go

These code sample will show you the basic usage pattern for Azure SDK for Go.

## Features

This project framework provides examples for the following usage pattern:

- How to create management plane clients - [`ExampleUsingARMClients`](azstart.go?plain=1#L14)
- How to create data plane clients - [`ExampleUsingDataPlaneClients`](azstart.go?plain=1#L44)
- How to page over responses - [`ExamplePagingOverACollection`](azstart.go?plain=1#L70)
- How to use long running operations - [`ExampleLongRunningOperation`](azstart.go?plain=1#L103)

### Prerequisites
* An [Azure subscription](https://azure.microsoft.com)
* Go 1.18 or above

### Quickstart

1. Clone the repository.

```bash
git clone https://github.com/Azure-Samples/azure-sdk-for-go-samples.git --branch new-version
```

1. Run `azstart` sample.

```bash
cd azure-sdk-for-go-samples/sdk/azstart
go run azstart.go
```

## Resources

- https://github.com/Azure/azure-sdk-for-go
- https://docs.microsoft.com/en-us/azure/developer/go/
- https://docs.microsoft.com/en-us/rest/api/
- https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk

## Need help?

Post issue on Github (https://github.com/Azure/azure-sdk-for-go/issues)
132 changes: 132 additions & 0 deletions sdk/azstart/azstart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package azstart

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
)

// ExampleUsingARMClients shows how to construct & use an ARM Client to invoke service methods
func ExampleUsingARMClients() {
// Construct a credential type from the azidentity package
// or the module defining the client you wish to use
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}

// Construct an ARM client factory passing subscription ID, credential, & optional options
// which could be used to create any client in one ARM module
clientFactory, err := armresources.NewClientFactory("<SubscriptionId>", credential, nil)

// This example creates a ResourceGroupsClient, but you can create any ARM client
client := clientFactory.NewResourceGroupsClient()

// You can now call client methods to invoke service operations
// This example calls CreateOrUpdate, but you can call any client method
response, err := client.CreateOrUpdate(context.TODO(), "<ResourceGroupName>",
armresources.ResourceGroup{
Location: to.Ptr("<ResouceGroupLocation>"), // to.Ptr converts this string to a *string
}, nil)
if err != nil {
panic(err)
}

// Use the service's response as your application desires
fmt.Printf("Resource group ID: %s\n", *response.ResourceGroup.ID)
}

// ExampleUsingDPClients shows how to construct & use a data-plane Client to invoke service methods
func ExampleUsingDataPlaneClients() {
// Construct a credential type from the azidentity package
// or the module defining the client you wish to use
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}

// Construct a DP client passing endpointURL, credential, & optional options
client, err := azsecrets.NewClient("https://<KeyVaultName>.vault.azure.net/", credential, nil)
if err != nil {
panic(err)
}

// You can now call client methods to invoke service operations
response, err := client.SetSecret(context.TODO(), "<SecretName>",
azsecrets.SetSecretParameters{Value: to.Ptr("<SecretValue>")}, nil)
if err != nil {
panic(err)
}

// Use the service's response as your application desires
fmt.Printf("Name: %s, Value: %s\n", *response.ID, *response.Value)
}

// ExamplePagingOverACollection shows how to page over a collection's items
func ExamplePagingOverACollection() {
// Construct a credential type from the azidentity
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}

client, err := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, nil)
if err != nil {
panic(err)
}

// Call a client method that creates a XxxPager; this does NOT invoke a service operation
for pager := client.NewListPager(nil); pager.More(); {
// While pages are getable, request a page of items from the service
page, err := pager.NextPage(context.TODO())
if err != nil {
panic(err)
}

// Process the page's items.
// NOTE: The service desides how many items to return on a page.
// If a page has 0 items, go get the next page.
// Other clients may be adding/deleting items from the collection while
// this code is paging; some items may be skipped or returned multiple times.
for _, item := range page.Value {
_ = item // Here's where your code processes the item as you desire
}
// Looping around will request the next page of items from the service
}
}

// ExampleLongRunningOperation shows how to invoke a long-running operation and poll for its completion
func ExampleLongRunningOperation() {
// Construct a credential type from the azidentity
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}

client, err := armresources.NewResourceGroupsClient("<SubscriptionId>", credential, nil)
if err != nil {
panic(err)
}

// Initiating a long-Running Operation causes the method to return a Poller[T]
poller, err := client.BeginDelete(context.TODO(), "<ResourceGroupName>", nil)
if err != nil {
panic(err)
}

// PollUntilDone causes your goroutine to periodically ask the service the status of the LRO
// It ultimately returns when the operation succeeds, fails, or was canceled.
// If the operation succeeds, err == nil and lroResult has result (if any); else err != nil
lroResult, err := poller.PollUntilDone(context.TODO(), nil)
if err != nil {
panic(err)
}
_ = lroResult // Examine sucessful result (if any)
}
24 changes: 24 additions & 0 deletions sdk/azstart/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module azstart

go 1.18

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2
github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets v0.11.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.0
)

require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/keyvault/internal v0.7.1 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
)
39 changes: 39 additions & 0 deletions sdk/azstart/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 h1:rTnT/Jrcm+figWlYz4Ixzt0SJVR2cMC8lvZcimipiEY=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0/go.mod h1:ON4tFdPTwRcgWEaVDrN3584Ef+b7GgSJaXxe5fW9t4M=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2 h1:uqM+VoHjVH6zdlkLF2b6O0ZANcHoj3rO0PoQ3jglUJA=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2/go.mod h1:twTKAa1E6hLmSDjLhaCkbTMQKc7p/rNLU40rLxGEOCI=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0 h1:leh5DwKv6Ihwi+h60uHtn6UWAxBbZ0q8DwQVMzf61zw=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets v0.11.0 h1:82w8tzLcOwDP/Q35j/wEBPt0n0kVC3cjtPdD62G8UAk=
github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets v0.11.0/go.mod h1:S78i9yTr4o/nXlH76bKjGUye9Z2wSxO5Tz7GoDr4vfI=
github.com/Azure/azure-sdk-for-go/sdk/keyvault/internal v0.7.1 h1:FbH3BbSb4bvGluTesZZ+ttN/MDsnMmQP36OSnDuSXqw=
github.com/Azure/azure-sdk-for-go/sdk/keyvault/internal v0.7.1/go.mod h1:9V2j0jn9jDEkCkv8w/bKTNppX/d0FVA1ud77xCIP4KA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal v1.1.2 h1:mLY+pNLjCUeKhgnAJWAKhEUQM+RJQo2H1fuGSw1Ky1E=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups v1.0.0 h1:pPvTJ1dY0sA35JOeFq6TsY2xj6Z85Yo23Pj4wCCvu4o=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.0 h1:yV3wcPPLQ+SLqJmgCs/wXKLxZkswMV4wCdNlG5XY4bQ=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.0/go.mod h1:c/wcGeGx5FUPbM/JltUYHZcKmigwyVLJlDq+4HdtXaw=
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0 h1:UE9n9rkJF62ArLb1F3DEjRt8O3jLwMWdSoypKV4f3MU=
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dnaeon/go-vcr v1.1.0 h1:ReYa/UBrRyQdant9B4fNHGoCNKw6qh6P0fsdGmZpR7c=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/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/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
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.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=

0 comments on commit fb2b81c

Please sign in to comment.