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

     1  // Copyright 2020 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/keyvault/mgmt/2018-02-14/keyvault"
    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 KeyVaultGenerator struct {
    28  	AzureService
    29  }
    30  
    31  func (g KeyVaultGenerator) createResources(ctx context.Context, client keyvault.VaultsClient) ([]terraformutils.Resource, error) {
    32  	resourceListResultIterator, err := client.ListComplete(ctx, nil)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	var resources []terraformutils.Resource
    37  	for resourceListResultIterator.NotDone() {
    38  		vault := resourceListResultIterator.Value()
    39  		resources = append(resources, terraformutils.NewSimpleResource(
    40  			*vault.ID,
    41  			*vault.Name,
    42  			"azurerm_key_vault",
    43  			"azurerm",
    44  			[]string{}))
    45  		if err := resourceListResultIterator.NextWithContext(ctx); err != nil {
    46  			log.Println(err)
    47  			return resources, err
    48  		}
    49  	}
    50  	return resources, nil
    51  }
    52  
    53  func (g KeyVaultGenerator) createResourcesByResourceGroup(ctx context.Context, rg string, client keyvault.VaultsClient) ([]terraformutils.Resource, error) {
    54  	iterator, err := client.ListByResourceGroupComplete(ctx, rg, nil)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	var resources []terraformutils.Resource
    59  	for iterator.NotDone() {
    60  		vault := iterator.Value()
    61  		resources = append(resources, terraformutils.NewSimpleResource(
    62  			*vault.ID,
    63  			*vault.Name,
    64  			"azurerm_key_vault",
    65  			"azurerm",
    66  			[]string{}))
    67  		if err := iterator.NextWithContext(ctx); err != nil {
    68  			log.Println(err)
    69  			return resources, err
    70  		}
    71  	}
    72  	return resources, nil
    73  }
    74  
    75  func (g *KeyVaultGenerator) InitResources() error {
    76  	ctx := context.Background()
    77  	vaultsClient := keyvault.NewVaultsClient(g.Args["config"].(authentication.Config).SubscriptionID)
    78  
    79  	vaultsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    80  
    81  	var err error
    82  	if rg := g.Args["resource_group"].(string); rg != "" {
    83  		g.Resources, err = g.createResourcesByResourceGroup(ctx, rg, vaultsClient)
    84  		return err
    85  	}
    86  	g.Resources, err = g.createResources(ctx, vaultsClient)
    87  	return err
    88  }