github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/load_balancer.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  	"regexp"
    21  
    22  	"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-03-01/network"
    23  	"github.com/Azure/go-autorest/autorest"
    24  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    25  	"github.com/hashicorp/go-azure-helpers/authentication"
    26  )
    27  
    28  type LoadBalancerGenerator struct {
    29  	AzureService
    30  }
    31  
    32  func (g *LoadBalancerGenerator) listLoadBalancerProbes(resourceGroupName string, loadBalancerName string) ([]terraformutils.Resource, error) {
    33  	var resources []terraformutils.Resource
    34  	ctx := context.Background()
    35  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
    36  
    37  	LoadBalancerProbesClient := network.NewLoadBalancerProbesClient(subscriptionID)
    38  	LoadBalancerProbesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    39  	loadBalancerProbeIterator, err := LoadBalancerProbesClient.ListComplete(ctx, resourceGroupName, loadBalancerName)
    40  
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	for loadBalancerProbeIterator.NotDone() {
    45  		loadBalancerProbe := loadBalancerProbeIterator.Value()
    46  		// NOTE:
    47  		// This works out the loadBalancer resource id from current probe
    48  		// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/loadBalancers/lb1
    49  		// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/loadBalancers/lb1/probes/probe1
    50  		//
    51  		// As the related data_source in azurerm provider works by starting to look up with loadbalancer_id
    52  		// https://github.com/terraform-providers/terraform-provider-azurerm/blob/v2.18.0/azurerm/internal/services/network/lb_probe_resource.go#L186
    53  		re := regexp.MustCompile(`/probes/.*$`)
    54  		loadBalancerID := re.ReplaceAllLiteralString(*loadBalancerProbe.ID, "")
    55  		resources = append(resources, terraformutils.NewResource(
    56  			*loadBalancerProbe.ID,
    57  			*loadBalancerProbe.Name,
    58  			"azurerm_lb_probe",
    59  			g.ProviderName,
    60  			map[string]string{
    61  				"loadbalancer_id": loadBalancerID,
    62  			},
    63  			[]string{},
    64  			map[string]interface{}{},
    65  		))
    66  
    67  		if err := loadBalancerProbeIterator.Next(); err != nil {
    68  			log.Println(err)
    69  			break
    70  		}
    71  	}
    72  
    73  	return resources, nil
    74  }
    75  
    76  func (g *LoadBalancerGenerator) listInboundNatRules(resourceGroupName string, loadBalancerName string) ([]terraformutils.Resource, error) {
    77  	var resources []terraformutils.Resource
    78  	ctx := context.Background()
    79  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
    80  
    81  	InboundNatRulesClient := network.NewInboundNatRulesClient(subscriptionID)
    82  	InboundNatRulesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    83  	InboundNatRuleIterator, err := InboundNatRulesClient.ListComplete(ctx, resourceGroupName, loadBalancerName)
    84  
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	for InboundNatRuleIterator.NotDone() {
    89  		InboundNatRule := InboundNatRuleIterator.Value()
    90  		// NOTE:
    91  		// Similar to above explanation, work out loadbalancer_id for azurerm datasource impl
    92  		re := regexp.MustCompile(`/inboundNatRules/.*$`)
    93  		loadBalancerID := re.ReplaceAllLiteralString(*InboundNatRule.ID, "")
    94  		resources = append(resources, terraformutils.NewResource(
    95  			*InboundNatRule.ID,
    96  			*InboundNatRule.Name,
    97  			"azurerm_lb_nat_rule",
    98  			g.ProviderName,
    99  			map[string]string{
   100  				"loadbalancer_id": loadBalancerID,
   101  			},
   102  			[]string{},
   103  			map[string]interface{}{},
   104  		))
   105  
   106  		if err := InboundNatRuleIterator.Next(); err != nil {
   107  			log.Println(err)
   108  			break
   109  		}
   110  	}
   111  
   112  	return resources, nil
   113  }
   114  
   115  func (g *LoadBalancerGenerator) listLoadBalancerBackendAddressPools(resourceGroupName string, loadBalancerName string) ([]terraformutils.Resource, error) {
   116  	var resources []terraformutils.Resource
   117  	ctx := context.Background()
   118  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
   119  
   120  	LoadBalancerBackendAddressPoolsClient := network.NewLoadBalancerBackendAddressPoolsClient(subscriptionID)
   121  	LoadBalancerBackendAddressPoolsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
   122  	loadBalancerBackendAddressPoolIterator, err := LoadBalancerBackendAddressPoolsClient.ListComplete(ctx, resourceGroupName, loadBalancerName)
   123  
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	for loadBalancerBackendAddressPoolIterator.NotDone() {
   128  		loadBalancerBackendAddressPool := loadBalancerBackendAddressPoolIterator.Value()
   129  		// NOTE:
   130  		// Similar to above explanation, work out loadbalancer_id for azurerm datasource impl
   131  		re := regexp.MustCompile(`/backendAddressPools/.*$`)
   132  		loadBalancerID := re.ReplaceAllLiteralString(*loadBalancerBackendAddressPool.ID, "")
   133  		resources = append(resources, terraformutils.NewResource(
   134  			*loadBalancerBackendAddressPool.ID,
   135  			*loadBalancerBackendAddressPool.Name,
   136  			"azurerm_lb_backend_address_pool",
   137  			g.ProviderName,
   138  			map[string]string{
   139  				"loadbalancer_id": loadBalancerID,
   140  			},
   141  			[]string{},
   142  			map[string]interface{}{},
   143  		))
   144  		if err := loadBalancerBackendAddressPoolIterator.Next(); err != nil {
   145  			log.Println(err)
   146  			break
   147  		}
   148  	}
   149  
   150  	return resources, nil
   151  }
   152  
   153  func (g *LoadBalancerGenerator) listAndAddForLoadBalancers() ([]terraformutils.Resource, error) {
   154  	var resources []terraformutils.Resource
   155  	ctx := context.Background()
   156  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
   157  
   158  	LoadBalancersClient := network.NewLoadBalancersClient(subscriptionID)
   159  	LoadBalancersClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
   160  
   161  	var (
   162  		loadBalancerIterator network.LoadBalancerListResultIterator
   163  		err                  error
   164  	)
   165  
   166  	if rg := g.Args["resource_group"].(string); rg != "" {
   167  		loadBalancerIterator, err = LoadBalancersClient.ListComplete(ctx, rg)
   168  	} else {
   169  		loadBalancerIterator, err = LoadBalancersClient.ListAllComplete(ctx)
   170  	}
   171  
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  	for loadBalancerIterator.NotDone() {
   176  		loadBalancer := loadBalancerIterator.Value()
   177  		resources = append(resources, terraformutils.NewSimpleResource(
   178  			*loadBalancer.ID,
   179  			*loadBalancer.Name,
   180  			"azurerm_lb",
   181  			g.ProviderName,
   182  			[]string{}))
   183  
   184  		id, err := ParseAzureResourceID(*loadBalancer.ID)
   185  		if err != nil {
   186  			return nil, err
   187  		}
   188  
   189  		probes, err := g.listLoadBalancerProbes(id.ResourceGroup, *loadBalancer.Name)
   190  		if err != nil {
   191  			return nil, err
   192  		}
   193  		resources = append(resources, probes...)
   194  
   195  		inboundNatRules, err := g.listInboundNatRules(id.ResourceGroup, *loadBalancer.Name)
   196  		if err != nil {
   197  			return nil, err
   198  		}
   199  		resources = append(resources, inboundNatRules...)
   200  
   201  		backendAddressPools, err := g.listLoadBalancerBackendAddressPools(id.ResourceGroup, *loadBalancer.Name)
   202  		if err != nil {
   203  			return nil, err
   204  		}
   205  		resources = append(resources, backendAddressPools...)
   206  
   207  		if err := loadBalancerIterator.Next(); err != nil {
   208  			log.Println(err)
   209  			return resources, err
   210  		}
   211  	}
   212  
   213  	return resources, nil
   214  }
   215  
   216  func (g *LoadBalancerGenerator) InitResources() error {
   217  	functions := []func() ([]terraformutils.Resource, error){
   218  		g.listAndAddForLoadBalancers,
   219  	}
   220  
   221  	for _, f := range functions {
   222  		resources, err := f()
   223  		if err != nil {
   224  			return err
   225  		}
   226  		g.Resources = append(g.Resources, resources...)
   227  	}
   228  
   229  	return nil
   230  }