github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/scaleset.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/compute/mgmt/2019-12-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 ScaleSetGenerator struct {
    28  	AzureService
    29  }
    30  
    31  func (g ScaleSetGenerator) createResourcesByResourceGroup(ctx context.Context, client compute.VirtualMachineScaleSetsClient, rg string) ([]terraformutils.Resource, error) {
    32  	scaleSetIterator, err := client.ListComplete(ctx, rg)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	var resources []terraformutils.Resource
    37  	for scaleSetIterator.NotDone() {
    38  		scaleSet := scaleSetIterator.Value()
    39  		newResource := terraformutils.NewSimpleResource(
    40  			*scaleSet.ID,
    41  			*scaleSet.Name,
    42  			"azurerm_virtual_machine_scale_set",
    43  			"azurerm",
    44  			[]string{})
    45  		resources = append(resources, newResource)
    46  		if err := scaleSetIterator.Next(); err != nil {
    47  			log.Println(err)
    48  			return resources, err
    49  		}
    50  	}
    51  	return resources, nil
    52  }
    53  
    54  func (g ScaleSetGenerator) createResources(ctx context.Context, client compute.VirtualMachineScaleSetsClient) ([]terraformutils.Resource, error) {
    55  	scaleSetIterator, err := client.ListAllComplete(ctx)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	var resources []terraformutils.Resource
    60  	for scaleSetIterator.NotDone() {
    61  		scaleSet := scaleSetIterator.Value()
    62  		newResource := terraformutils.NewSimpleResource(
    63  			*scaleSet.ID,
    64  			*scaleSet.Name,
    65  			"azurerm_virtual_machine_scale_set",
    66  			"azurerm",
    67  			[]string{})
    68  		resources = append(resources, newResource)
    69  		if err := scaleSetIterator.Next(); err != nil {
    70  			log.Println(err)
    71  			return resources, err
    72  		}
    73  	}
    74  	return resources, nil
    75  }
    76  
    77  func (g *ScaleSetGenerator) InitResources() error {
    78  	ctx := context.Background()
    79  	ScaleSetClient := compute.NewVirtualMachineScaleSetsClient(g.Args["config"].(authentication.Config).SubscriptionID)
    80  
    81  	ScaleSetClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    82  
    83  	if rg := g.Args["resource_group"].(string); rg != "" {
    84  		var err error
    85  		g.Resources, err = g.createResourcesByResourceGroup(ctx, ScaleSetClient, rg)
    86  		return err
    87  	}
    88  	var err error
    89  	g.Resources, err = g.createResources(ctx, ScaleSetClient)
    90  	return err
    91  }