|
| 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 | +} |
0 commit comments