Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Do group overrides within a default resource function
Browse files Browse the repository at this point in the history
- Rename certain kinds

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Jan 6, 2022
1 parent 2a6c806 commit 529035d
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 121 deletions.
103 changes: 0 additions & 103 deletions config/apigroup_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ limitations under the License.

package config

import (
"strings"

"github.com/crossplane/terrajet/pkg/config"
tjconfig "github.com/crossplane/terrajet/pkg/config"
"github.com/crossplane/terrajet/pkg/types/name"
)

// This file is generated
// nolint:misspell
var (
Expand Down Expand Up @@ -721,99 +713,4 @@ var (
"azurerm_app_service_slot_virtual_network_swift_connection": "web",
"azurerm_app_service": "web",
}

// mappings from group name to # of '_'-separated tokens to skip
// while constructing Kind names under that group
kindNameRuleMap = map[string]int{
"dbformariadb": 2,
"dbformysql": 2,
"dbforpostgresql": 2,
"devspaces": 2,
"devtestlab": 3,
"logic": 3,
"machinelearningservices": 3,
"operationalinsights": 3,
"security": 3,
"timeseriesinsights": 5,
"maintenance": 3,
}
)

func addDefaultGroupKindConfigurator(pc *config.Provider, tResource string) {
apiGroup, ok := apiGroupMap[tResource]
if !ok {
return
}

pc.AddResourceConfigurator(tResource, func(r *tjconfig.Resource) {
r.ShortGroup = apiGroup
parts := strings.Split(tResource, "_")
i, ok := kindNameRuleMap[apiGroup]
if !ok {
i = 1 // by default we drop only the first token (azurerm)
// check if group name is a prefix for the resource name
for j := 2; j <= len(parts); j++ {
// do not include azurerm in comparison
if strings.Join(parts[1:j], "") == apiGroup {
// if group name is a prefix for resource name,
// we do not include it in Kind name
i = j
}
}
}
if i >= len(parts) {
i = len(parts) - 1
}
r.Kind = name.NewFromSnake(strings.Join(parts[i:], "_")).Camel
})
}

func init() {
name.AddAcronym("iothub", "IOTHub")
name.AddAcronym("iot", "IOT")
name.AddAcronym("servicebus", "ServiceBus")
name.AddAcronym("hbase", "HBase")
name.AddAcronym("rserver", "RServer")
name.AddAcronym("dsc", "DSC")
name.AddAcronym("datetime", "DateTime")
name.AddAcronym("postgresql", "PostgreSQL")
name.AddAcronym("aaaa", "AAAA")
name.AddAcronym("caa", "CAA")
name.AddAcronym("cname", "CNAME")
name.AddAcronym("mx", "MX")
name.AddAcronym("ns", "NS")
name.AddAcronym("ptr", "PTR")
name.AddAcronym("srv", "SRV")
name.AddAcronym("txt", "TXT")
name.AddAcronym("ddos", "DDOS")
name.AddAcronym("elasticpool", "ElasticPool")
name.AddAcronym("eventgrid", "EventGrid")
name.AddAcronym("eventhub", "EventHub")
name.AddAcronym("keyspace", "KeySpace")
name.AddAcronym("aad", "AAD")
name.AddAcronym("hci", "HCI")
name.AddAcronym("udf", "UDF")
name.AddAcronym("mssql", "MSSQL")
name.AddAcronym("aadb2c", "AADB2C")
name.AddAcronym("cosmosdb", "CosmosDB")
name.AddAcronym("nodeconfiguration", "NodeConfiguration")
name.AddAcronym("runbook", "RunBook")
name.AddAcronym("vmware", "VMware")
name.AddAcronym("directline", "DirectLine")
name.AddAcronym("ms", "MS")
name.AddAcronym("dataset", "DataSet")
name.AddAcronym("sqlapi", "SQLAPI")
name.AddAcronym("ssis", "SSIS")
name.AddAcronym("odata", "OData")
name.AddAcronym("sftp", "SFTP")
name.AddAcronym("mariadb", "MariaDB")
name.AddAcronym("dps", "DPS")
name.AddAcronym("sas", "SAS")
name.AddAcronym("powerbi", "PowerBI")
name.AddAcronym("aws", "AWS")
name.AddAcronym("filesystem", "FileSystem")
name.AddAcronym("hpc", "HPC")
name.AddAcronym("hostname", "HostName")
name.AddAcronym("datasource", "DataSource")

}
8 changes: 4 additions & 4 deletions config/eventhub/config.go → config/datashare/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package eventhub
package datashare

import (
"github.com/crossplane/terrajet/pkg/config"
)

// Configure configures eventhub group
// Configure configures datashare group
func Configure(p *config.Provider) {
p.AddResourceConfigurator("azurerm_eventhub", func(r *config.Resource) {
r.Kind = "Hub"
p.AddResourceConfigurator("azurerm_data_share", func(r *config.Resource) {
r.Kind = "DataShare"
})
}
28 changes: 28 additions & 0 deletions config/notificationhubs/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2021 The Crossplane 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 notificationhubs

import (
"github.com/crossplane/terrajet/pkg/config"
)

// Configure configures notificationhubs group
func Configure(p *config.Provider) {
p.AddResourceConfigurator("azurerm_notification_hub", func(r *config.Resource) {
r.Kind = "NotificationHub"
})
}
97 changes: 93 additions & 4 deletions config/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,56 @@ import (
"strings"

tjconfig "github.com/crossplane/terrajet/pkg/config"
"github.com/crossplane/terrajet/pkg/types/name"
)

const (
groupIOTHub = "iothub"
var (
// mappings from group name to # of '_'-separated tokens to skip
// while constructing Kind names under that group
kindNameRuleMap = map[string]int{
"dbformariadb": 2,
"dbformysql": 2,
"dbforpostgresql": 2,
"devspaces": 2,
"devtestlab": 3,
"logic": 2,
"machinelearningservices": 3,
"security": 1,
"timeseriesinsights": 5,
"azurestackhci": 3,
"maintenance": 1,
"cognitiveservices": 2,
"notificationhubs": 3,
}
)

// default api-group & kind configuration for all resources
func groupOverrides() tjconfig.ResourceOption {
return func(r *tjconfig.Resource) {
if strings.HasPrefix(r.Name, "azurerm_iothub") {
r.ShortGroup = groupIOTHub
apiGroup, ok := apiGroupMap[r.Name]
if !ok {
return
}

r.ShortGroup = apiGroup
parts := strings.Split(r.Name, "_")
i, ok := kindNameRuleMap[apiGroup]
if !ok {
i = 1 // by default we drop only the first token (azurerm)
// check if group name is a prefix for the resource name
for j := 2; j <= len(parts); j++ {
// do not include azurerm in comparison
if strings.Join(parts[1:j], "") == apiGroup {
// if group name is a prefix for resource name,
// we do not include it in Kind name
i = j
}
}
}
if i >= len(parts) {
i = len(parts) - 1
}
r.Kind = name.NewFromSnake(strings.Join(parts[i:], "_")).Camel
}
}

Expand All @@ -39,3 +78,53 @@ func externalNameConfig() tjconfig.ResourceOption {
r.ExternalName = tjconfig.IdentifierFromProvider
}
}

func init() {
name.AddAcronym("iothub", "IOTHub")
name.AddAcronym("iot", "IOT")
name.AddAcronym("servicebus", "ServiceBus")
name.AddAcronym("hbase", "HBase")
name.AddAcronym("rserver", "RServer")
name.AddAcronym("dsc", "DSC")
name.AddAcronym("datetime", "DateTime")
name.AddAcronym("postgresql", "PostgreSQL")
name.AddAcronym("aaaa", "AAAA")
name.AddAcronym("caa", "CAA")
name.AddAcronym("cname", "CNAME")
name.AddAcronym("mx", "MX")
name.AddAcronym("ns", "NS")
name.AddAcronym("ptr", "PTR")
name.AddAcronym("srv", "SRV")
name.AddAcronym("txt", "TXT")
name.AddAcronym("ddos", "DDOS")
name.AddAcronym("elasticpool", "ElasticPool")
name.AddAcronym("eventgrid", "EventGrid")
name.AddAcronym("eventhub", "EventHub")
name.AddAcronym("keyspace", "KeySpace")
name.AddAcronym("aad", "AAD")
name.AddAcronym("hci", "HCI")
name.AddAcronym("udf", "UDF")
name.AddAcronym("mssql", "MSSQL")
name.AddAcronym("aadb2c", "AADB2C")
name.AddAcronym("cosmosdb", "CosmosDB")
name.AddAcronym("nodeconfiguration", "NodeConfiguration")
name.AddAcronym("runbook", "RunBook")
name.AddAcronym("vmware", "VMware")
name.AddAcronym("directline", "DirectLine")
name.AddAcronym("ms", "MS")
name.AddAcronym("dataset", "DataSet")
name.AddAcronym("sqlapi", "SQLAPI")
name.AddAcronym("ssis", "SSIS")
name.AddAcronym("odata", "OData")
name.AddAcronym("sftp", "SFTP")
name.AddAcronym("mariadb", "MariaDB")
name.AddAcronym("dps", "DPS")
name.AddAcronym("sas", "SAS")
name.AddAcronym("powerbi", "PowerBI")
name.AddAcronym("aws", "AWS")
name.AddAcronym("filesystem", "FileSystem")
name.AddAcronym("hpc", "HPC")
name.AddAcronym("hostname", "HostName")
name.AddAcronym("datasource", "DataSource")
name.AddAcronym("healthbot", "HealthBot")
}
12 changes: 6 additions & 6 deletions config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
tjconfig "github.com/crossplane/terrajet/pkg/config"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/crossplane-contrib/provider-jet-azure/config/datashare"
"github.com/crossplane-contrib/provider-jet-azure/config/notificationhubs"
"github.com/crossplane-contrib/provider-jet-azure/config/storagesync"
tf "github.com/hashicorp/terraform-provider-azurerm/xpprovider"

"github.com/crossplane-contrib/provider-jet-azure/config/apimanagement"
"github.com/crossplane-contrib/provider-jet-azure/config/base"
"github.com/crossplane-contrib/provider-jet-azure/config/eventhub"
"github.com/crossplane-contrib/provider-jet-azure/config/healthbot"
"github.com/crossplane-contrib/provider-jet-azure/config/logic"
"github.com/crossplane-contrib/provider-jet-azure/config/security"

Expand Down Expand Up @@ -139,8 +140,6 @@ func GetProvider() *tjconfig.Provider {
pc.AddResourceConfigurator(name, func(r *tjconfig.Resource) {
r.ExternalName = tjconfig.IdentifierFromProvider
})
// default api-group & kind configuration for all resources
addDefaultGroupKindConfigurator(pc, name)
}

for _, configure := range []func(provider *tjconfig.Provider){
Expand All @@ -160,11 +159,12 @@ func GetProvider() *tjconfig.Provider {
iothub.Configure,
monitor.Configure,
apimanagement.Configure,
eventhub.Configure,
healthbot.Configure,
logic.Configure,
security.Configure,
base.Configure,
datashare.Configure,
notificationhubs.Configure,
storagesync.Configure,
} {
configure(pc)
}
Expand Down
8 changes: 4 additions & 4 deletions config/healthbot/config.go → config/storagesync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package healthbot
package storagesync

import (
"github.com/crossplane/terrajet/pkg/config"
)

// Configure configures healthbot group
// Configure configures storagesync group
func Configure(p *config.Provider) {
p.AddResourceConfigurator("azurerm_healthbot", func(r *config.Resource) {
r.Kind = "Bot"
p.AddResourceConfigurator("azurerm_storage_sync", func(r *config.Resource) {
r.Kind = "StorageSync"
})
}

0 comments on commit 529035d

Please sign in to comment.