This repository has been archived by the owner on Sep 18, 2022. It is now read-only.
forked from GoogleCloudPlatform/terraformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosmosdb.go
174 lines (154 loc) · 6.35 KB
/
cosmosdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2020 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package azure
import (
"context"
"strings"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2020-03-01/documentdb"
"github.com/Azure/go-autorest/autorest"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/hashicorp/go-azure-helpers/authentication"
)
type CosmosDBGenerator struct {
AzureService
}
func (g *CosmosDBGenerator) listSQLDatabasesAndContainersBehind(resourceGroupName string, accountName string) ([]terraformutils.Resource, []terraformutils.Resource, error) {
var resourcesDatabase []terraformutils.Resource
var resourcesContainer []terraformutils.Resource
ctx := context.Background()
subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
SQLResourcesClient := documentdb.NewSQLResourcesClient(subscriptionID, subscriptionID)
SQLResourcesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
sqlDatabases, err := SQLResourcesClient.ListSQLDatabases(ctx, resourceGroupName, accountName)
if err != nil {
return nil, nil, err
}
for _, sqlDatabase := range *sqlDatabases.Value {
// NOTE:
// For a similar reason as
// https://github.com/terraform-providers/terraform-provider-azurerm/issues/7472#issuecomment-650684349
// The cosmosdb resource format change is NOT yet addressed in terraform provider
// This line is a workaround to convert to old format, and might be removed if they deprecate the old format
sqlDatabaseIDInOldFormat := strings.Replace(*sqlDatabase.ID, "sqlDatabases", "databases", 1)
resourcesDatabase = append(resourcesDatabase, terraformutils.NewSimpleResource(
sqlDatabaseIDInOldFormat,
*sqlDatabase.Name,
"azurerm_cosmosdb_sql_database",
g.ProviderName,
[]string{}))
sqlContainers, err := SQLResourcesClient.ListSQLContainers(ctx, resourceGroupName, accountName, *sqlDatabase.Name)
if err != nil {
return nil, nil, err
}
for _, sqlContainer := range *sqlContainers.Value {
// NOTE:
// For a similar reason as
// https://github.com/terraform-providers/terraform-provider-azurerm/issues/7472#issuecomment-650684349
// The cosmosdb resource format change is NOT yet addressed in terraform provider
// This line is a workaround to convert to old format, and might be removed if they deprecate the old format
sqlContainerIDInOldFormat := strings.Replace(*sqlContainer.ID, "sqlDatabases", "databases", 1)
resourcesContainer = append(resourcesContainer, terraformutils.NewSimpleResource(
sqlContainerIDInOldFormat,
*sqlContainer.Name,
"azurerm_cosmosdb_sql_container",
g.ProviderName,
[]string{}))
}
}
return resourcesDatabase, resourcesContainer, nil
}
func (g *CosmosDBGenerator) listTables(resourceGroupName string, accountName string) ([]terraformutils.Resource, error) {
var resources []terraformutils.Resource
ctx := context.Background()
subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
// NOTE:
// there will be a parameter simplification for interface if we update the package
// https://github.com/Azure/azure-sdk-for-go/blob/v42.0.0/services/cosmos-db/mgmt/2020-03-01/documentdb/tableresources.go#L35
// https://github.com/Azure/azure-sdk-for-go/blob/v44.0.0/services/cosmos-db/mgmt/2020-03-01/documentdb/tableresources.go#L35
TableResourcesClient := documentdb.NewTableResourcesClient(subscriptionID, subscriptionID)
TableResourcesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
tables, err := TableResourcesClient.ListTables(ctx, resourceGroupName, accountName)
if err != nil {
return nil, err
}
for _, table := range *tables.Value {
resources = append(resources, terraformutils.NewSimpleResource(
*table.ID,
*table.Name,
"azurerm_cosmosdb_table",
g.ProviderName,
[]string{}))
}
return resources, nil
}
func (g *CosmosDBGenerator) listAndAddForDatabaseAccounts() ([]terraformutils.Resource, error) {
var resources []terraformutils.Resource
ctx := context.Background()
subscriptionID := g.Args["config"].(authentication.Config).SubscriptionID
// NOTE:
// there will be a parameter simplification for interface if we update the package
// https://github.com/Azure/azure-sdk-for-go/blob/v42.0.0/services/cosmos-db/mgmt/2020-03-01/documentdb/databaseaccounts.go#L35
// https://github.com/Azure/azure-sdk-for-go/blob/v44.0.0/services/cosmos-db/mgmt/2020-03-01/documentdb/databaseaccounts.go#L35
DatabaseAccountsClient := documentdb.NewDatabaseAccountsClient(subscriptionID, subscriptionID)
DatabaseAccountsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
var (
accounts documentdb.DatabaseAccountsListResult
err error
)
if rg := g.Args["resource_group"].(string); rg != "" {
accounts, err = DatabaseAccountsClient.ListByResourceGroup(ctx, rg)
} else {
accounts, err = DatabaseAccountsClient.List(ctx)
}
if err != nil {
return nil, err
}
for _, account := range *accounts.Value {
resources = append(resources, terraformutils.NewSimpleResource(
*account.ID,
*account.Name,
"azurerm_cosmosdb_account",
g.ProviderName,
[]string{}))
id, err := ParseAzureResourceID(*account.ID)
if err != nil {
return nil, err
}
tables, err := g.listTables(id.ResourceGroup, *account.Name)
if err != nil {
return nil, err
}
resources = append(resources, tables...)
sqlDatabases, sqlContainers, err := g.listSQLDatabasesAndContainersBehind(id.ResourceGroup, *account.Name)
if err != nil {
return nil, err
}
resources = append(resources, sqlDatabases...)
resources = append(resources, sqlContainers...)
}
return resources, nil
}
func (g *CosmosDBGenerator) InitResources() error {
functions := []func() ([]terraformutils.Resource, error){
g.listAndAddForDatabaseAccounts,
}
for _, f := range functions {
resources, err := f()
if err != nil {
return err
}
g.Resources = append(g.Resources, resources...)
}
return nil
}