github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/disk.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/compute/mgmt/2019-07-01/compute"
    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 DiskGenerator struct {
    28  	AzureService
    29  }
    30  
    31  func (g DiskGenerator) createResources(diskListIterator compute.DiskListIterator) ([]terraformutils.Resource, error) {
    32  	var resources []terraformutils.Resource
    33  	for diskListIterator.NotDone() {
    34  		disk := diskListIterator.Value()
    35  		resources = append(resources, terraformutils.NewSimpleResource(
    36  			*disk.ID,
    37  			*disk.Name,
    38  			"azurerm_managed_disk",
    39  			"azurerm",
    40  			[]string{}))
    41  		if err := diskListIterator.Next(); err != nil {
    42  			log.Println(err)
    43  			return resources, err
    44  		}
    45  	}
    46  	return resources, nil
    47  }
    48  
    49  func (g *DiskGenerator) InitResources() error {
    50  	ctx := context.Background()
    51  	disksClient := compute.NewDisksClient(g.Args["config"].(authentication.Config).SubscriptionID)
    52  
    53  	disksClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    54  
    55  	var (
    56  		output compute.DiskListIterator
    57  		err    error
    58  	)
    59  
    60  	if rg := g.Args["resource_group"].(string); rg != "" {
    61  		output, err = disksClient.ListByResourceGroupComplete(ctx, rg)
    62  	} else {
    63  		output, err = disksClient.ListComplete(ctx)
    64  	}
    65  	if err != nil {
    66  		return err
    67  	}
    68  	g.Resources, err = g.createResources(output)
    69  	return err
    70  }