Exemplo n.º 1
0
func (c *AzureClient) createStorageAccount(resourceGroupName, storageAccountName, location string, accountType storage.AccountType) error {
	cna, err := c.StorageAccountsClient.CheckNameAvailability(
		storage.StorageAccountCheckNameAvailabilityParameters{
			Name: storageAccountName,
			Type: "Microsoft.Storage/storageAccounts"})
	if err != nil {
		fmt.Printf("Error: %v", err)
		return err
	}
	if !cna.NameAvailable {
		fmt.Printf("%s is unavailable -- try again\n", storageAccountName)
		return errors.New(storageAccountName + " is unavailable")
	}
	fmt.Printf("Storage account name %s is available\n", storageAccountName)

	cp := storage.StorageAccountCreateParameters{}
	cp.Location = location
	cp.Properties.AccountType = accountType

	sa, err := c.StorageAccountsClient.Create(resourceGroupName, storageAccountName, cp)
	if err != nil {
		if sa.Response.StatusCode != http.StatusAccepted {
			fmt.Printf("Creation of %s.%s failed", resourceGroupName, storageAccountName)
			return err
		}
	}

	fmt.Printf("Creation initiated %s.%s\n", resourceGroupName, storageAccountName)
	return nil
}
Exemplo n.º 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)
}