-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user-assigned identity sample. (#213)
* Add user-assigned identity sample. Sample includes creating a user-assigned identity, then adding and removing one from a VM. * add errors package to toml file
- Loading branch information
1 parent
3e573f9
commit 11cbf88
Showing
5 changed files
with
253 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft and contributors. All rights reserved. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
package msi | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/config" | ||
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/iam" | ||
"github.com/Azure/azure-sdk-for-go/services/preview/msi/mgmt/2015-08-31-preview/msi" | ||
"github.com/Azure/go-autorest/autorest/to" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func getMSIUserAssignedIDClient() (*msi.UserAssignedIdentitiesClient, error) { | ||
a, err := iam.GetResourceManagementAuthorizer() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get authorizer") | ||
} | ||
msiClient := msi.NewUserAssignedIdentitiesClient(config.SubscriptionID()) | ||
msiClient.Authorizer = a | ||
msiClient.AddToUserAgent(config.UserAgent()) | ||
return &msiClient, nil | ||
} | ||
|
||
// CreateUserAssignedIdentity creates a user-assigned identity in the specified resource group. | ||
func CreateUserAssignedIdentity(resourceGroup, identity string) (*msi.Identity, error) { | ||
msiClient, err := getMSIUserAssignedIDClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
id, err := msiClient.CreateOrUpdate(context.Background(), resourceGroup, identity, msi.Identity{ | ||
Location: to.StringPtr(config.Location()), | ||
}) | ||
return &id, err | ||
} |