Azure Go Management SDK 中国版使用示例

简介


刚学习go几天,尝试调用Azure的SDK进行管理API的操作,基本思路是基于注册的AD Application信息生成token,然后再使用Token生成serviceClient,然后再进行资源的创建等操作。因为对go并不是特别熟悉,如有使用不当之处还请批评指正。


环境搭建

  • go 安装(版本:1.10.2)
  • IDE:LiteIDE
  • 依赖安装:
go get -u github.com/Azure/azure-sdk-for-go/...

go get -u github.com/Azure-Samples/azure-sdk-for-go-samples/...

注意:在安装包的过程中会因为网络原因某些包总是无法安装,这时需要等位到GOPATH中的对应安装路径,采用git Clone的方式直接下载包即可。

示例:go get golang.org/x 包失败解决方法

示例程序:

主程序:

// test1 project main.go
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/Azure-Samples/azure-sdk-for-go-samples/iam"
	"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2017-06-01/storage"
	"github.com/Azure/go-autorest/autorest"
	"github.com/Azure/go-autorest/autorest/to"
)

const (
	errorPrefix = "Cannot create storage account, reason: %v"
)

func getStorageAccountsClient(activeDirectoryEndpoint string, baseURL string, subscriptionID string) storage.AccountsClient {

	iam.ParseArgs()

	token, err := iam.GetResourceManagementTokenHybrid(activeDirectoryEndpoint, baseURL)

	if err != nil {
		log.Fatal(fmt.Sprintf(errorPrefix, fmt.Sprintf("Cannot generate token. Error details: %v.", err)))
	}
	storageAccountsClient := storage.NewAccountsClientWithBaseURI(baseURL, subscriptionID)

	storageAccountsClient.Authorizer = autorest.NewBearerAuthorizer(token)

	return storageAccountsClient
}

func main() {
	fmt.Println("Hello World!")
	//初始化参数定义,已经失效,仅供格式参考,根据情况修改为自己的测试参数
	os.Setenv("AZURE_TENANT_ID", "b388b808-0ec9-4a09-a414-a7cbbd8b7e22")
	os.Setenv("AZURE_CLIENT_ID", "0675a148-8425-4cc4-8747-18683cc70422")
	os.Setenv("AZURE_CLIENT_SECRET", "VLMnO1T9zA/D9tgfwve3TO+Pq3w3UzY3lGIi+X7kbK22")
	var subscriptionID string = "e0fbea86-6cf2-4b2d-81e2-9c59f4f96b22"
	var resourceGroupName string = "yuvmtest"
	var testStrorageAccountName string = "yugodemostoragetest191"
	var location string = "chinanorth"

	//注意如下两个参数对于中国区Azure固定,不需要修改
	var baseURL string = "https://management.chinacloudapi.cn/"
	var activeDirectoryEndpoint string = "https://login.chinacloudapi.cn/"

	ctx := context.Background()

	storageAccountsClient := getStorageAccountsClient(activeDirectoryEndpoint, baseURL, subscriptionID)
	fmt.Println(storageAccountsClient)

	future, err := storageAccountsClient.Create(
		ctx,
		resourceGroupName,
		testStrorageAccountName,
		storage.AccountCreateParameters{
			Sku: &storage.Sku{
				Name: storage.StandardLRS},
			Kind:     storage.Storage,
			Location: to.StringPtr(location),
			AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{},
		})

	fmt.Println(future)
	fmt.Println(err)
}


代码调整:

github.com/Azure-Samples/azure-sdk-for-go-samples/iam.ParseArgs 调整为如下形式:

// ParseArgs picks up shared env vars
// Other packages should use this func after helpers.ParseArgs()
func ParseArgs() {

	tenantID = os.Getenv("AZURE_TENANT_ID")
	clientID = os.Getenv("AZURE_CLIENT_ID")
	clientSecret = os.Getenv("AZURE_CLIENT_SECRET")

}

测试结果:

image

参考链接

posted @ 2018-05-18 15:54  taro_秋刀鱼  阅读(915)  评论(0编辑  收藏  举报