github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/cosmosdb.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  	"strings"
    20  
    21  	"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
    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 CosmosDBGenerator struct {
    28  	AzureService
    29  }
    30  
    31  func (g *CosmosDBGenerator) listSQLDatabasesAndContainersBehind(resourceGroupName string, accountName string) ([]terraformutils.Resource, []terraformutils.Resource, error) {
    32  	var resourcesDatabase []terraformutils.Resource
    33  	var resourcesContainer []terraformutils.Resource
    34  	ctx := context.Background()
    35  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
    36  	SQLResourcesClient := documentdb.NewSQLResourcesClient(subscriptionID)
    37  	SQLResourcesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    38  
    39  	sqlDatabases, err := SQLResourcesClient.ListSQLDatabases(ctx, resourceGroupName, accountName)
    40  	if err != nil {
    41  		return nil, nil, err
    42  	}
    43  	for _, sqlDatabase := range *sqlDatabases.Value {
    44  		// NOTE:
    45  		// For a similar reason as
    46  		// https://github.com/terraform-providers/terraform-provider-azurerm/issues/7472#issuecomment-650684349
    47  		// The cosmosdb resource format change is NOT yet addressed in terraform provider
    48  		// This line is a workaround to convert to old format, and might be removed if they deprecate the old format
    49  		sqlDatabaseIDInOldFormat := strings.Replace(*sqlDatabase.ID, "sqlDatabases", "databases", 1)
    50  		resourcesDatabase = append(resourcesDatabase, terraformutils.NewSimpleResource(
    51  			sqlDatabaseIDInOldFormat,
    52  			*sqlDatabase.Name,
    53  			"azurerm_cosmosdb_sql_database",
    54  			g.ProviderName,
    55  			[]string{}))
    56  
    57  		sqlContainers, err := SQLResourcesClient.ListSQLContainers(ctx, resourceGroupName, accountName, *sqlDatabase.Name)
    58  		if err != nil {
    59  			return nil, nil, err
    60  		}
    61  		for _, sqlContainer := range *sqlContainers.Value {
    62  			// NOTE:
    63  			// For a similar reason as
    64  			// https://github.com/terraform-providers/terraform-provider-azurerm/issues/7472#issuecomment-650684349
    65  			// The cosmosdb resource format change is NOT yet addressed in terraform provider
    66  			// This line is a workaround to convert to old format, and might be removed if they deprecate the old format
    67  			sqlContainerIDInOldFormat := strings.Replace(*sqlContainer.ID, "sqlDatabases", "databases", 1)
    68  			resourcesContainer = append(resourcesContainer, terraformutils.NewSimpleResource(
    69  				sqlContainerIDInOldFormat,
    70  				*sqlContainer.Name,
    71  				"azurerm_cosmosdb_sql_container",
    72  				g.ProviderName,
    73  				[]string{}))
    74  		}
    75  	}
    76  
    77  	return resourcesDatabase, resourcesContainer, nil
    78  }
    79  
    80  func (g *CosmosDBGenerator) listTables(resourceGroupName string, accountName string) ([]terraformutils.Resource, error) {
    81  	var resources []terraformutils.Resource
    82  	ctx := context.Background()
    83  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
    84  	TableResourcesClient := documentdb.NewTableResourcesClient(subscriptionID)
    85  	TableResourcesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
    86  
    87  	tables, err := TableResourcesClient.ListTables(ctx, resourceGroupName, accountName)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	for _, table := range *tables.Value {
    92  		resources = append(resources, terraformutils.NewSimpleResource(
    93  			*table.ID,
    94  			*table.Name,
    95  			"azurerm_cosmosdb_table",
    96  			g.ProviderName,
    97  			[]string{}))
    98  	}
    99  
   100  	return resources, nil
   101  }
   102  
   103  func (g *CosmosDBGenerator) listAndAddForDatabaseAccounts() ([]terraformutils.Resource, error) {
   104  	var resources []terraformutils.Resource
   105  	ctx := context.Background()
   106  	subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
   107  	DatabaseAccountsClient := documentdb.NewDatabaseAccountsClient(subscriptionID)
   108  	DatabaseAccountsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
   109  
   110  	var (
   111  		accounts documentdb.DatabaseAccountsListResult
   112  		err      error
   113  	)
   114  	if rg := g.Args["resource_group"].(string); rg != "" {
   115  		accounts, err = DatabaseAccountsClient.ListByResourceGroup(ctx, rg)
   116  	} else {
   117  		accounts, err = DatabaseAccountsClient.List(ctx)
   118  	}
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	for _, account := range *accounts.Value {
   123  		resources = append(resources, terraformutils.NewSimpleResource(
   124  			*account.ID,
   125  			*account.Name,
   126  			"azurerm_cosmosdb_account",
   127  			g.ProviderName,
   128  			[]string{}))
   129  
   130  		id, err := ParseAzureResourceID(*account.ID)
   131  		if err != nil {
   132  			return nil, err
   133  		}
   134  
   135  		tables, err := g.listTables(id.ResourceGroup, *account.Name)
   136  		if err != nil {
   137  			return nil, err
   138  		}
   139  		resources = append(resources, tables...)
   140  
   141  		sqlDatabases, sqlContainers, err := g.listSQLDatabasesAndContainersBehind(id.ResourceGroup, *account.Name)
   142  		if err != nil {
   143  			return nil, err
   144  		}
   145  		resources = append(resources, sqlDatabases...)
   146  		resources = append(resources, sqlContainers...)
   147  	}
   148  
   149  	return resources, nil
   150  }
   151  
   152  func (g *CosmosDBGenerator) InitResources() error {
   153  	functions := []func() ([]terraformutils.Resource, error){
   154  		g.listAndAddForDatabaseAccounts,
   155  	}
   156  
   157  	for _, f := range functions {
   158  		resources, err := f()
   159  		if err != nil {
   160  			return err
   161  		}
   162  		g.Resources = append(g.Resources, resources...)
   163  	}
   164  
   165  	return nil
   166  }