github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/public_ip.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/network/mgmt/2020-03-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 PublicIPGenerator struct {
    28  	AzureService
    29  }
    30  
    31  func (g *PublicIPGenerator) listAndAddForPublicIPAddress() ([]terraformutils.Resource, error) {
    32  	var resources []terraformutils.Resource
    33  	ctx := context.Background()
    34  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
    35  	PublicIPAddressesClient := network.NewPublicIPAddressesClient(subscriptionID)
    36  	PublicIPAddressesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    37  
    38  	var (
    39  		publicIPAddressIterator network.PublicIPAddressListResultIterator
    40  		err                     error
    41  	)
    42  	if rg := g.Args["resource_group"].(string); rg != "" {
    43  		publicIPAddressIterator, err = PublicIPAddressesClient.ListComplete(ctx, rg)
    44  	} else {
    45  		publicIPAddressIterator, err = PublicIPAddressesClient.ListAllComplete(ctx)
    46  	}
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	for publicIPAddressIterator.NotDone() {
    51  		publicIP := publicIPAddressIterator.Value()
    52  		resources = append(resources, terraformutils.NewSimpleResource(
    53  			*publicIP.ID,
    54  			*publicIP.Name,
    55  			"azurerm_public_ip",
    56  			g.ProviderName,
    57  			[]string{}))
    58  
    59  		if err := publicIPAddressIterator.Next(); err != nil {
    60  			log.Println(err)
    61  			return resources, err
    62  		}
    63  	}
    64  
    65  	return resources, nil
    66  }
    67  
    68  func (g *PublicIPGenerator) listAndAddForPublicIPPrefix() ([]terraformutils.Resource, error) {
    69  	var resources []terraformutils.Resource
    70  	ctx := context.Background()
    71  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
    72  	PublicIPPrefixesClient := network.NewPublicIPPrefixesClient(subscriptionID)
    73  	PublicIPPrefixesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    74  
    75  	var (
    76  		publicIPPrefixIterator network.PublicIPPrefixListResultIterator
    77  		err                    error
    78  	)
    79  
    80  	if rg := g.Args["resource_group"].(string); rg != "" {
    81  		publicIPPrefixIterator, err = PublicIPPrefixesClient.ListComplete(ctx, rg)
    82  	} else {
    83  		publicIPPrefixIterator, err = PublicIPPrefixesClient.ListAllComplete(ctx)
    84  	}
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	for publicIPPrefixIterator.NotDone() {
    89  		publicIPPrefix := publicIPPrefixIterator.Value()
    90  		resources = append(resources, terraformutils.NewSimpleResource(
    91  			*publicIPPrefix.ID,
    92  			*publicIPPrefix.Name,
    93  			"azurerm_public_ip_prefix",
    94  			g.ProviderName,
    95  			[]string{}))
    96  
    97  		if err := publicIPPrefixIterator.Next(); err != nil {
    98  			log.Println(err)
    99  			return resources, err
   100  		}
   101  	}
   102  
   103  	return resources, nil
   104  }
   105  
   106  func (g *PublicIPGenerator) InitResources() error {
   107  	functions := []func() ([]terraformutils.Resource, error){
   108  		g.listAndAddForPublicIPAddress,
   109  		g.listAndAddForPublicIPPrefix,
   110  	}
   111  
   112  	for _, f := range functions {
   113  		resources, err := f()
   114  		if err != nil {
   115  			return err
   116  		}
   117  		g.Resources = append(g.Resources, resources...)
   118  	}
   119  
   120  	return nil
   121  }