github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/virtual_network.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/network/mgmt/2021-02-01/network"
    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 VirtualNetworkGenerator struct {
    28  	AzureService
    29  }
    30  
    31  func (g VirtualNetworkGenerator) createResources(ctx context.Context, iterator network.VirtualNetworkListResultIterator) ([]terraformutils.Resource, error) {
    32  	var resources []terraformutils.Resource
    33  	for iterator.NotDone() {
    34  		virtualNetwork := iterator.Value()
    35  		resources = append(resources, terraformutils.NewSimpleResource(
    36  			*virtualNetwork.ID,
    37  			*virtualNetwork.Name,
    38  			"azurerm_virtual_network",
    39  			g.ProviderName,
    40  			[]string{}))
    41  		if err := iterator.NextWithContext(ctx); err != nil {
    42  			log.Println(err)
    43  			return resources, err
    44  		}
    45  	}
    46  	return resources, nil
    47  }
    48  
    49  func (g *VirtualNetworkGenerator) InitResources() error {
    50  	ctx := context.Background()
    51  	virtualNetworkClient := network.NewVirtualNetworksClient(g.Args["config"].(authentication.Config).SubscriptionID)
    52  
    53  	virtualNetworkClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    54  
    55  	var (
    56  		output network.VirtualNetworkListResultIterator
    57  		err    error
    58  	)
    59  
    60  	if rg := g.Args["resource_group"].(string); rg != "" {
    61  		output, err = virtualNetworkClient.ListComplete(ctx, rg)
    62  	} else {
    63  		output, err = virtualNetworkClient.ListAllComplete(ctx)
    64  	}
    65  	if err != nil {
    66  		return err
    67  	}
    68  	g.Resources, err = g.createResources(ctx, output)
    69  	return err
    70  }
    71  
    72  // NOTE on Virtual Networks and Subnet's:
    73  // Terraform currently provides both a standalone Subnet resource, and allows for Subnets to be defined in-line within the Virtual Network
    74  // resource. At this time you cannot use a Virtual Network with in-line Subnets in conjunction with any Subnet resources.
    75  // Doing so will cause a conflict of Subnet configurations and will overwrite Subnet's.
    76  func (g *VirtualNetworkGenerator) PostConvertHook() error {
    77  	for _, resource := range g.Resources {
    78  		if resource.InstanceInfo.Type != "azurerm_virtual_network" {
    79  			continue
    80  		}
    81  		delete(resource.Item, "subnet")
    82  	}
    83  	return nil
    84  }