Skip to content

Commit

Permalink
config: use a more generic GetFullyQualifierIDFn and add it to all re…
Browse files Browse the repository at this point in the history
…sources

Signed-off-by: Muvaffak Onus <me@muvaf.com>
  • Loading branch information
muvaf committed Nov 12, 2021
1 parent 5a4434f commit 9fed46b
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 220 deletions.
22 changes: 20 additions & 2 deletions config/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package common

import (
"fmt"
"path/filepath"
"strings"

tjconfig "github.com/crossplane-contrib/terrajet/pkg/config"
Expand All @@ -36,7 +37,16 @@ func GetNameFromFullyQualifiedID(tfstate map[string]interface{}) string {
return words[len(words)-1]
}

func GetFullyQualifiedIDFn(servicePath string) tjconfig.GetIDFn {
// GetFullyQualifiedIDFn returns a GetIDFn that can parse any Azure fully qualifier.
// An example identifier is as follows:
// /subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/sqlDatabases/%s/containers/%s
// An input to this function to parse it would be:
// serviceProvider: "Microsoft.DocumentDB"
// keyPairs: []string{"databaseAccounts", "account_name", "sqlDatabases", "database_name", "containers", "name"}
func GetFullyQualifiedIDFn(serviceProvider string, keyPairs ...string) tjconfig.GetIDFn {
if len(keyPairs)%2 != 0 {
panic("each service name has to have a key")
}
return func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
Expand All @@ -46,6 +56,14 @@ func GetFullyQualifiedIDFn(servicePath string) tjconfig.GetIDFn {
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers%s/%s", subID, rg, servicePath, name)
path := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s", subID, rg, serviceProvider)
for i := 0; i < len(keyPairs); i += 2 {
val, ok := parameters[keyPairs[i+1]].(string)
if !ok {
return ""
}
path = filepath.Join(path, keyPairs[i], val)
}
return path
}
}
242 changes: 51 additions & 191 deletions config/cosmosdb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,11 @@ func Configure(p *config.Provider) {
r.ExternalName = config.NameAsIdentifier
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID
// /subscriptions/000-000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/acc1/sqlDatabases/db1/containers/container1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
dbName, ok := parameters["database_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/sqlDatabases/%s/containers/%s", subID, rg, account, dbName, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"sqlDatabases", "database_name",
"containers", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_mongo_collection", func(r *config.Resource) {
Expand All @@ -86,25 +72,11 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/mongodbDatabases/db1/collections/collection1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
dbName, ok := parameters["database_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/mongodbDatabases/%s/collections/%s", subID, rg, account, dbName, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"mongodbDatabases", "database_name",
"collections", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_cassandra_keyspace", func(r *config.Resource) {
Expand All @@ -123,21 +95,10 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/cassandraKeyspaces/ks1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/cassandraKeyspaces/%s", subID, rg, account, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"cassandraKeyspaces", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_cassandra_table", func(r *config.Resource) {
Expand All @@ -151,25 +112,11 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/cassandraKeyspaces/ks1/tables/table1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
ksID, ok := parameters["cassandra_keyspace_id"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/cassandraKeyspaces/%s/tables/%s", subID, rg, account, ksID, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"cassandraKeyspaces", "cassandra_keyspace_id",
"tables", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_gremlin_graph", func(r *config.Resource) {
Expand All @@ -192,25 +139,11 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/gremlinDatabases/db1/graphs/graphs1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
dbName, ok := parameters["database_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/gremlinDatabases/%s/graphs/%s", subID, rg, account, dbName, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"gremlinDatabases", "database_name",
"graphs", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_sql_function", func(r *config.Resource) {
Expand Down Expand Up @@ -257,29 +190,12 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/sqlDatabases/db1/containers/c1/storedProcedures/sp1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
dbName, ok := parameters["database_name"].(string)
if !ok {
return ""
}
containerName, ok := parameters["container_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/sqlDatabases/%s/containers/%s/storedProcedures/%s", subID, rg, account, dbName, containerName, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"sqlDatabases", "database_name",
"containers", "container_name",
"storedProcedures", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_gremlin_database", func(r *config.Resource) {
Expand All @@ -299,21 +215,10 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/gremlinDatabases/db1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/gremlinDatabases/%s", subID, rg, account, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"gremlinDatabases", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_mongo_database", func(r *config.Resource) {
Expand All @@ -332,21 +237,10 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/mongodbDatabases/db1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/mongodbDatabases/%s", subID, rg, account, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"mongodbDatabases", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_sql_database", func(r *config.Resource) {
Expand All @@ -365,21 +259,10 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/sqlDatabases/db1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/sqlDatabases/%s", subID, rg, account, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"sqlDatabases", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_table", func(r *config.Resource) {
Expand All @@ -396,23 +279,11 @@ func Configure(p *config.Provider) {
r.UseAsync = true
r.ExternalName = config.NameAsIdentifier
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1/tables/table1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/tables/%s", subID, rg, account, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"tables", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_account", func(r *config.Resource) {
Expand All @@ -426,7 +297,7 @@ func Configure(p *config.Provider) {
r.ExternalName = config.NameAsIdentifier
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID
// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.DocumentDB/databaseAccounts/account1
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("/Microsoft.DocumentDB/databaseAccounts")
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB", "databaseAccounts")
})

p.AddResourceConfigurator("azurerm_cosmosdb_notebook_workspace", func(r *config.Resource) {
Expand All @@ -445,21 +316,10 @@ func Configure(p *config.Provider) {
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID

// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.DocumentDB/databaseAccounts/account1/notebookWorkspaces/notebookWorkspace1
r.ExternalName.GetIDFn = func(name string, parameters map[string]interface{}, providerConfig map[string]interface{}) string {
subID, ok := providerConfig["subscriptionId"].(string)
if !ok {
return ""
}
rg, ok := parameters["resource_group_name"].(string)
if !ok {
return ""
}
account, ok := parameters["account_name"].(string)
if !ok {
return ""
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DocumentDB/databaseAccounts/%s/notebookWorkspaces/%s", subID, rg, account, name)
}
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.DocumentDB",
"databaseAccounts", "account_name",
"notebookWorkspaces", "name",
)
})

p.AddResourceConfigurator("azurerm_cosmosdb_sql_trigger", func(r *config.Resource) {
Expand Down
2 changes: 1 addition & 1 deletion config/ip/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func Configure(p *config.Provider) {
r.ExternalName = config.NameAsIdentifier
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID
// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Network/ipGroups/myIpGroup
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("/Microsoft.Network/ipGroups")
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.Network", "ipGroups", "name")
})
}
2 changes: 1 addition & 1 deletion config/kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Configure(p *config.Provider) {
r.ExternalName = config.NameAsIdentifier
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID
// /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/group1/providers/Microsoft.ContainerService/managedClusters/cluster1
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("/Microsoft.ContainerService/managedClusters")
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.ContainerService", "managedClusters", "name")
})

p.AddResourceConfigurator("azurerm_kubernetes_cluster_node_pool", func(r *config.Resource) {
Expand Down
2 changes: 1 addition & 1 deletion config/network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func Configure(p *config.Provider) {
r.UseAsync = true
r.ExternalName = config.NameAsIdentifier
r.ExternalName.GetNameFn = common.GetNameFromFullyQualifiedID
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("/Microsoft.Network/loadBalancers")
r.ExternalName.GetIDFn = common.GetFullyQualifiedIDFn("Microsoft.Network", "loadBalancers", "name")
})
}
Loading

0 comments on commit 9fed46b

Please sign in to comment.