Пример #1
0
func NewClient() *AzureClient {
	c, err := LoadAzureCredentials()
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return nil
	}

	spt, err := NewServicePrincipalTokenFromCredentials(c, azure.AzureResourceManagerScope)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return nil
	}

	rmc := resources.NewResourceGroupsClient(c["subscriptionID"])
	rmc.Authorizer = spt
	rmc.PollingMode = autorest.DoNotPoll

	sac := storage.NewStorageAccountsClient(c["subscriptionID"])
	sac.Authorizer = spt
	sac.PollingMode = autorest.DoNotPoll

	return &AzureClient{
		ResourceManagementClient: &rmc,
		StorageAccountsClient:    &sac,
	}
}
Пример #2
0
func createAccount(resourceGroup, name string) {
	c, err := helpers.LoadCredentials()
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	sac := storage.NewStorageAccountsClient(c["subscriptionID"])

	spt, err := helpers.NewServicePrincipalTokenFromCredentials(c, azure.AzureResourceManagerScope)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	sac.Authorizer = spt

	cna, err := sac.CheckNameAvailability(
		storage.StorageAccountCheckNameAvailabilityParameters{
			Name: name,
			Type: "Microsoft.Storage/storageAccounts"})
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	if !cna.NameAvailable {
		fmt.Printf("%s is unavailable -- try again\n", name)
		return
	}
	fmt.Printf("%s is available\n\n", name)

	sac.Sender = autorest.CreateSender(withWatcher())
	sac.PollingMode = autorest.PollUntilAttempts
	sac.PollingAttempts = 5

	cp := storage.StorageAccountCreateParameters{}
	cp.Location = "westus"
	cp.Properties.AccountType = storage.StandardLRS

	sa, err := sac.Create(resourceGroup, name, cp)
	if err != nil {
		if sa.Response.StatusCode != http.StatusAccepted {
			fmt.Printf("Creation of %s.%s failed with err -- %v\n", resourceGroup, name, err)
			return
		} else {
			fmt.Printf("Create initiated for %s.%s -- poll %s to check status\n",
				resourceGroup,
				name,
				sa.GetPollingLocation())
			return
		}
	}

	fmt.Printf("Successfully created %s.%s\n\n", resourceGroup, name)

	sac.Sender = nil
	r, err := sac.Delete(resourceGroup, name)
	if err != nil {
		fmt.Printf("Delete of %s.%s failed with status %s\n...%v\n", resourceGroup, name, r.Status, err)
		return
	}
	fmt.Printf("Deletion of %s.%s succeeded -- %s\n", resourceGroup, name, r.Status)
}
Пример #3
0
func checkName(name string) {
	c, err := helpers.LoadCredentials()
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	sac := storage.NewStorageAccountsClient(c["subscriptionID"])

	spt, err := helpers.NewServicePrincipalTokenFromCredentials(c, azure.AzureResourceManagerScope)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	sac.Authorizer = spt

	sac.Sender = autorest.CreateSender(
		autorest.WithLogging(log.New(os.Stdout, "sdk-example: ", log.LstdFlags)))

	i := inspectors{}
	sac.RequestInspector = i
	sac.ResponseInspector = i

	cna, err := sac.CheckNameAvailability(
		storage.StorageAccountCheckNameAvailabilityParameters{
			Name: name,
			Type: "Microsoft.Storage/storageAccounts"})

	if err != nil {
		log.Fatalf("Error: %v", err)
	} else {
		if cna.NameAvailable {
			fmt.Printf("The name '%s' is available\n", name)
		} else {
			fmt.Printf("The name '%s' is unavailable because %s\n", name, cna.Message)
		}
	}
}