github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/resource_group.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/resources/mgmt/2019-05-01/resources"
    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 ResourceGroupGenerator struct {
    28  	AzureService
    29  }
    30  
    31  func (g ResourceGroupGenerator) createResources(groupListResultIterator resources.GroupListResultIterator) []terraformutils.Resource {
    32  	var resources []terraformutils.Resource
    33  	for groupListResultIterator.NotDone() {
    34  		group := groupListResultIterator.Value()
    35  		resources = append(resources, terraformutils.NewSimpleResource(
    36  			*group.ID,
    37  			*group.Name,
    38  			"azurerm_resource_group",
    39  			"azurerm",
    40  			[]string{}))
    41  		if err := groupListResultIterator.Next(); err != nil {
    42  			log.Println(err)
    43  			break
    44  		}
    45  	}
    46  	return resources
    47  }
    48  
    49  func (g *ResourceGroupGenerator) InitResources() error {
    50  	ctx := context.Background()
    51  	groupsClient := resources.NewGroupsClient(g.Args["config"].(authentication.Config).SubscriptionID)
    52  
    53  	groupsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    54  
    55  	if rg := g.Args["resource_group"].(string); rg != "" {
    56  		group, err := groupsClient.Get(ctx, rg)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		g.Resources = []terraformutils.Resource{
    61  			terraformutils.NewSimpleResource(
    62  				*group.ID,
    63  				*group.Name,
    64  				"azurerm_resource_group",
    65  				"azurerm",
    66  				[]string{}),
    67  		}
    68  		return nil
    69  	}
    70  	output, err := groupsClient.ListComplete(ctx, "", nil)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	g.Resources = g.createResources(output)
    75  	return nil
    76  }