github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/storage_account.go (about)

     1  // Copyright 2019 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package azure
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
    22  	"github.com/Azure/go-autorest/autorest"
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  	"github.com/hashicorp/go-azure-helpers/authentication"
    25  )
    26  
    27  type StorageAccountGenerator struct {
    28  	AzureService
    29  }
    30  
    31  func (g StorageAccountGenerator) createResourcesByResourceGroup(ctx context.Context, client storage.AccountsClient, rg string) ([]terraformutils.Resource, error) {
    32  	accountListResult, err := client.ListByResourceGroup(ctx, rg)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	var resources []terraformutils.Resource
    37  	if accounts := accountListResult.Value; accounts != nil {
    38  		for _, account := range *accounts {
    39  			resources = append(resources, terraformutils.NewSimpleResource(
    40  				*account.ID,
    41  				*account.Name,
    42  				"azurerm_storage_account",
    43  				"azurerm",
    44  				[]string{}))
    45  		}
    46  	}
    47  	return resources, nil
    48  }
    49  func (g StorageAccountGenerator) createResources(ctx context.Context, client storage.AccountsClient) ([]terraformutils.Resource, error) {
    50  	accountListResultIterator, err := client.ListComplete(ctx)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	var resources []terraformutils.Resource
    55  	for accountListResultIterator.NotDone() {
    56  		account := accountListResultIterator.Value()
    57  		resources = append(resources, terraformutils.NewSimpleResource(
    58  			*account.ID,
    59  			*account.Name,
    60  			"azurerm_storage_account",
    61  			"azurerm",
    62  			[]string{}))
    63  		if err := accountListResultIterator.Next(); err != nil {
    64  			log.Println(err)
    65  			return resources, err
    66  		}
    67  	}
    68  	return resources, nil
    69  }
    70  
    71  func (g *StorageAccountGenerator) InitResources() error {
    72  	ctx := context.Background()
    73  	accountsClient := storage.NewAccountsClient(g.Args["config"].(authentication.Config).SubscriptionID)
    74  	accountsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    75  	if rg := g.Args["resource_group"].(string); rg != "" {
    76  		output, err := g.createResourcesByResourceGroup(ctx, accountsClient, rg)
    77  		g.Resources = output
    78  		return err
    79  	}
    80  	output, err := g.createResources(ctx, accountsClient)
    81  	g.Resources = output
    82  	return err
    83  }