github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/private_endpoint.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 PrivateEndpointGenerator struct {
    25  	AzureService
    26  }
    27  
    28  func (az *PrivateEndpointGenerator) listServices() ([]network.PrivateLinkService, error) {
    29  	subscriptionID, resourceGroup, authorizer := az.getClientArgs()
    30  	client := network.NewPrivateLinkServicesClient(subscriptionID)
    31  	client.Authorizer = authorizer
    32  	var (
    33  		iterator network.PrivateLinkServiceListResultIterator
    34  		err      error
    35  	)
    36  	ctx := context.Background()
    37  	if resourceGroup != "" {
    38  		iterator, err = client.ListComplete(ctx, resourceGroup)
    39  	} else {
    40  		iterator, err = client.ListBySubscriptionComplete(ctx)
    41  	}
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	var resources []network.PrivateLinkService
    46  	for iterator.NotDone() {
    47  		item := iterator.Value()
    48  		resources = append(resources, item)
    49  		if err := iterator.NextWithContext(ctx); err != nil {
    50  			log.Println(err)
    51  			return resources, err
    52  		}
    53  	}
    54  	return resources, nil
    55  }
    56  
    57  func (az *PrivateEndpointGenerator) AppendServices(link *network.PrivateLinkService) {
    58  	az.AppendSimpleResource(*link.ID, *link.Name, "azurerm_private_link_service")
    59  }
    60  
    61  func (az *PrivateEndpointGenerator) listEndpoints() ([]network.PrivateEndpoint, error) {
    62  	subscriptionID, resourceGroup, authorizer := az.getClientArgs()
    63  	client := network.NewPrivateEndpointsClient(subscriptionID)
    64  	client.Authorizer = authorizer
    65  	var (
    66  		iterator network.PrivateEndpointListResultIterator
    67  		err      error
    68  	)
    69  	ctx := context.Background()
    70  	if resourceGroup != "" {
    71  		iterator, err = client.ListComplete(ctx, resourceGroup)
    72  	} else {
    73  		iterator, err = client.ListBySubscriptionComplete(ctx)
    74  	}
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	var resources []network.PrivateEndpoint
    79  	for iterator.NotDone() {
    80  		item := iterator.Value()
    81  		resources = append(resources, item)
    82  		if err := iterator.NextWithContext(ctx); err != nil {
    83  			log.Println(err)
    84  			return resources, err
    85  		}
    86  	}
    87  	return resources, nil
    88  }
    89  
    90  func (az *PrivateEndpointGenerator) AppendEndpoint(link *network.PrivateEndpoint) {
    91  	az.AppendSimpleResource(*link.ID, *link.Name, "azurerm_private_endpoint")
    92  }
    93  
    94  func (az *PrivateEndpointGenerator) InitResources() error {
    95  
    96  	services, err := az.listServices()
    97  	if err != nil {
    98  		return err
    99  	}
   100  	for _, link := range services {
   101  		az.AppendServices(&link)
   102  	}
   103  	endpoints, err := az.listEndpoints()
   104  	if err != nil {
   105  		return err
   106  	}
   107  	for _, endpoint := range endpoints {
   108  		az.AppendEndpoint(&endpoint)
   109  	}
   110  	return nil
   111  }