github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/subnet.go (about)

     1  // Copyright 2021 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  )
    23  
    24  type SubnetGenerator struct {
    25  	AzureService
    26  }
    27  
    28  func (az *SubnetGenerator) lisSubnets() ([]network.Subnet, error) {
    29  	subscriptionID, resourceGroup, authorizer := az.getClientArgs()
    30  	subnetClient := network.NewSubnetsClient(subscriptionID)
    31  	subnetClient.Authorizer = authorizer
    32  	vnetClient := network.NewVirtualNetworksClient(subscriptionID)
    33  	vnetClient.Authorizer = authorizer
    34  	var (
    35  		vnetIter   network.VirtualNetworkListResultIterator
    36  		subnetIter network.SubnetListResultIterator
    37  		err        error
    38  	)
    39  	ctx := context.Background()
    40  	if resourceGroup != "" {
    41  		vnetIter, err = vnetClient.ListComplete(ctx, resourceGroup)
    42  	} else {
    43  		vnetIter, err = vnetClient.ListAllComplete(ctx)
    44  	}
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	var resources []network.Subnet
    49  	for vnetIter.NotDone() {
    50  		vnet := vnetIter.Value()
    51  		vnetID, err := ParseAzureResourceID(*vnet.ID)
    52  		if err != nil {
    53  			return nil, err
    54  		}
    55  		subnetIter, err = subnetClient.ListComplete(ctx, vnetID.ResourceGroup, *vnet.Name)
    56  		if err != nil {
    57  			return nil, err
    58  		}
    59  		for subnetIter.NotDone() {
    60  			item := subnetIter.Value()
    61  			resources = append(resources, item)
    62  			if err := subnetIter.NextWithContext(ctx); err != nil {
    63  				log.Println(err)
    64  				return resources, err
    65  			}
    66  		}
    67  		if err := vnetIter.NextWithContext(ctx); err != nil {
    68  			log.Println(err)
    69  			return resources, err
    70  		}
    71  	}
    72  	return resources, nil
    73  }
    74  
    75  func (az *SubnetGenerator) AppendSubnet(subnet *network.Subnet) {
    76  	az.AppendSimpleResource(*subnet.ID, *subnet.Name, "azurerm_subnet")
    77  }
    78  
    79  func (az *SubnetGenerator) appendRouteTable(subnet *network.Subnet) {
    80  	if props := subnet.SubnetPropertiesFormat; props != nil {
    81  		if prop := props.RouteTable; prop != nil {
    82  			az.appendSimpleAssociation(
    83  				*subnet.ID, *subnet.Name, prop.Name,
    84  				"azurerm_subnet_route_table_association",
    85  				map[string]string{
    86  					"subnet_id":      *subnet.ID,
    87  					"route_table_id": *prop.ID,
    88  				})
    89  		}
    90  	}
    91  }
    92  
    93  func (az *SubnetGenerator) appendNetworkSecurityGroupAssociation(subnet *network.Subnet) {
    94  	if props := subnet.SubnetPropertiesFormat; props != nil {
    95  		if prop := props.NetworkSecurityGroup; prop != nil {
    96  			az.appendSimpleAssociation(
    97  				*subnet.ID, *subnet.Name, prop.Name,
    98  				"azurerm_subnet_network_security_group_association",
    99  				map[string]string{
   100  					"subnet_id":                 *subnet.ID,
   101  					"network_security_group_id": *prop.ID,
   102  				})
   103  		}
   104  	}
   105  }
   106  
   107  func (az *SubnetGenerator) appendNatGateway(subnet *network.Subnet) {
   108  	if props := subnet.SubnetPropertiesFormat; props != nil {
   109  		if prop := props.NatGateway; prop != nil {
   110  			az.appendSimpleAssociation(
   111  				*subnet.ID, *subnet.Name, nil,
   112  				"azurerm_subnet_nat_gateway_association",
   113  				map[string]string{
   114  					"subnet_id":      *subnet.ID,
   115  					"nat_gateway_id": *prop.ID,
   116  				})
   117  		}
   118  	}
   119  }
   120  
   121  func (az *SubnetGenerator) appendServiceEndpointPolicies() error {
   122  	subscriptionID, resourceGroup, authorizer := az.getClientArgs()
   123  	client := network.NewServiceEndpointPoliciesClient(subscriptionID)
   124  	client.Authorizer = authorizer
   125  	var (
   126  		iterator network.ServiceEndpointPolicyListResultIterator
   127  		err      error
   128  	)
   129  	ctx := context.Background()
   130  	if resourceGroup != "" {
   131  		iterator, err = client.ListByResourceGroupComplete(ctx, resourceGroup)
   132  	} else {
   133  		iterator, err = client.ListComplete(ctx)
   134  	}
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	for iterator.NotDone() {
   140  		item := iterator.Value()
   141  		az.AppendSimpleResource(*item.ID, *item.Name, "azurerm_subnet_service_endpoint_storage_policy")
   142  		if err := iterator.NextWithContext(ctx); err != nil {
   143  			log.Println(err)
   144  			return err
   145  		}
   146  	}
   147  	return nil
   148  }
   149  
   150  func (az *SubnetGenerator) InitResources() error {
   151  
   152  	subnets, err := az.lisSubnets()
   153  	if err != nil {
   154  		return err
   155  	}
   156  	for _, subnet := range subnets {
   157  		az.AppendSubnet(&subnet)
   158  		az.appendRouteTable(&subnet)
   159  		az.appendNetworkSecurityGroupAssociation(&subnet)
   160  		az.appendNatGateway(&subnet)
   161  	}
   162  	if err := az.appendServiceEndpointPolicies(); err != nil {
   163  		return err
   164  	}
   165  	return nil
   166  }
   167  
   168  func (az *SubnetGenerator) PostConvertHook() error {
   169  	for _, resource := range az.Resources {
   170  		if resource.InstanceInfo.Type != "azurerm_subnet" {
   171  			continue
   172  		}
   173  		delete(resource.Item, "address_prefix")
   174  	}
   175  	return nil
   176  }