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

Commit

Permalink
Merge pull request #25 from turkenh/finalize-kinds-and-groups
Browse files Browse the repository at this point in the history
Finalize config for API group/kind calculation
  • Loading branch information
turkenh authored Jan 4, 2022
2 parents 0bb54f0 + 07c8435 commit 9cfad6c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 202 deletions.
9 changes: 0 additions & 9 deletions config/accessapproval/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,3 @@ func Configure(p *config.Provider) {
r.Kind = "ProjectSettings"
})
}

// Resources in "Access Approval" group.
// Note(turkenh): The following resources are listed under "Access Approval"
// section in Terraform Documentation.
var Resources = map[string]struct{}{
"google_folder_access_approval_settings": {},
"google_organization_access_approval_settings": {},
"google_project_access_approval_settings": {},
}
11 changes: 11 additions & 0 deletions config/bigtable/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bigtable

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

// Configure configures individual resources by adding custom
// ResourceConfigurators.
func Configure(p *config.Provider) {
p.AddResourceConfigurator("google_bigtable_gc_policy", func(r *config.Resource) {
r.Kind = "GarbageCollectionPolicy"
})
}
146 changes: 0 additions & 146 deletions config/cloudplatform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,160 +8,14 @@ import (
// Configure configures individual resources by adding custom
// ResourceConfigurators.
func Configure(p *config.Provider) {
p.AddResourceConfigurator("google_billing_subaccount", func(r *config.Resource) {
r.Kind = "BillingSubaccount"
})
p.AddResourceConfigurator("google_folder", func(r *config.Resource) {
r.Kind = "Folder"
})
p.AddResourceConfigurator("google_folder_iam", func(r *config.Resource) {
r.Kind = "FolderIAM"
})
p.AddResourceConfigurator("google_folder_organization_policy", func(r *config.Resource) {
r.Kind = "FolderOrganizationPolicy"
})
p.AddResourceConfigurator("google_organization_iam", func(r *config.Resource) {
r.Kind = "OrganizationIAM"
})
p.AddResourceConfigurator("google_organization_iam_custom_role", func(r *config.Resource) {
r.Kind = "OrganizationIAMCustomRole"
})
p.AddResourceConfigurator("google_organization_policy", func(r *config.Resource) {
r.Kind = "OrganizationPolicy"
})
p.AddResourceConfigurator("google_project", func(r *config.Resource) {
r.Kind = "Project"
r.TerraformResource.Schema["org_id"].Description =
"The numeric ID of the organization this project belongs to."
})
p.AddResourceConfigurator("google_project_default_service_accounts", func(r *config.Resource) {
r.Kind = "ProjectDefaultServiceAccounts"
})
p.AddResourceConfigurator("google_project_iam", func(r *config.Resource) {
r.Kind = "ProjectIAM"
})
p.AddResourceConfigurator("google_project_iam_custom_role", func(r *config.Resource) {
r.Kind = "ProjectIAMCustomRole"
})
p.AddResourceConfigurator("google_project_organization_policy", func(r *config.Resource) {
r.Kind = "ProjectOrganizationPolicy"
})
p.AddResourceConfigurator("google_project_service", func(r *config.Resource) {
r.Kind = "ProjectService"
})
p.AddResourceConfigurator("google_service_account", func(r *config.Resource) {
r.Kind = "ServiceAccount"
})
p.AddResourceConfigurator("google_service_account_iam", func(r *config.Resource) {
r.Kind = "ServiceAccountIAM"
})
p.AddResourceConfigurator("google_service_account_key", func(r *config.Resource) {
// Note(turkenh): We have to modify schema of "keepers", since it is a
// map where elements configured as nil, but needs to be String:
r.TerraformResource.
Schema["keepers"].Elem = schema.TypeString
r.Kind = "ServiceAccountKey"
})
p.AddResourceConfigurator("google_service_networking_peered_dns_domain", func(r *config.Resource) {
r.Kind = "ServiceNetworkingPeeredDNSDomain"
})
p.AddResourceConfigurator("google_project_service_identity", func(r *config.Resource) {
r.Kind = "ProjectServiceIdentity"
})

p.AddResourceConfigurator("google_organization_iam_audit_config", func(r *config.Resource) {
r.Kind = "OrganizationIAMAuditConfig"
})
p.AddResourceConfigurator("google_organization_iam_binding", func(r *config.Resource) {
r.Kind = "OrganizationIAMBinding"
})
p.AddResourceConfigurator("google_organization_iam_member", func(r *config.Resource) {
r.Kind = "OrganizationIAMMember"
})
p.AddResourceConfigurator("google_organization_iam_policy", func(r *config.Resource) {
r.Kind = "OrganizationIAMPolicy"
})

p.AddResourceConfigurator("google_project_iam_audit_config", func(r *config.Resource) {
r.Kind = "ProjectIAMAuditConfig"
})
p.AddResourceConfigurator("google_project_iam_binding", func(r *config.Resource) {
r.Kind = "ProjectIAMBinding"
})
p.AddResourceConfigurator("google_project_iam_member", func(r *config.Resource) {
r.Kind = "ProjectIAMMember"
})
p.AddResourceConfigurator("google_project_iam_policy", func(r *config.Resource) {
r.Kind = "ProjectIAMPolicy"
})
p.AddResourceConfigurator("google_project_usage_export_bucket", func(r *config.Resource) {
r.Kind = "ProjectUsageExportBucket"
})

p.AddResourceConfigurator("google_folder_iam_audit_config", func(r *config.Resource) {
r.Kind = "FolderIAMAuditConfig"
})
p.AddResourceConfigurator("google_folder_iam_binding", func(r *config.Resource) {
r.Kind = "FolderIAMBinding"
})
p.AddResourceConfigurator("google_folder_iam_member", func(r *config.Resource) {
r.Kind = "FolderIAMMember"
})
p.AddResourceConfigurator("google_folder_iam_policy", func(r *config.Resource) {
r.Kind = "FolderIAMPolicy"
})

p.AddResourceConfigurator("google_service_account_iam_binding", func(r *config.Resource) {
r.Kind = "ServiceAccountIAMBinding"
})
p.AddResourceConfigurator("google_service_account_iam_member", func(r *config.Resource) {
r.Kind = "ServiceAccountIAMMember"
})
p.AddResourceConfigurator("google_service_account_iam_policy", func(r *config.Resource) {
r.Kind = "ServiceAccountIAMPolicy"
})
}

// Resources in "Cloud Platform" group.
// Note(turkenh): The following resources are listed under "Cloud Platform"
// section in Terraform Documentation.
var Resources = map[string]struct{}{
"google_billing_subaccount": {},
"google_folder": {},
"google_folder_iam": {},
"google_folder_organization_policy": {},
"google_organization_iam": {},
"google_organization_iam_custom_role": {},
"google_organization_policy": {},
"google_project": {},
"google_project_default_service_accounts": {},
"google_project_iam": {},
"google_project_iam_custom_role": {},
"google_project_organization_policy": {},
"google_project_service": {},
"google_service_account": {},
"google_service_account_iam": {},
"google_service_account_key": {},
"google_service_networking_peered_dns_domain": {},
"google_project_service_identity": {},

"google_organization_iam_audit_config": {},
"google_organization_iam_binding": {},
"google_organization_iam_member": {},
"google_organization_iam_policy": {},

"google_project_iam_audit_config": {},
"google_project_iam_binding": {},
"google_project_iam_member": {},
"google_project_iam_policy": {},
"google_project_usage_export_bucket": {},

"google_folder_iam_audit_config": {},
"google_folder_iam_binding": {},
"google_folder_iam_member": {},
"google_folder_iam_policy": {},

"google_service_account_iam_binding": {},
"google_service_account_iam_member": {},
"google_service_account_iam_policy": {},
}
145 changes: 99 additions & 46 deletions config/overrides.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,114 @@
package config

import (
"regexp"
"strings"

"github.com/iancoleman/strcase"

tjconfig "github.com/crossplane/terrajet/pkg/config"

"github.com/crossplane-contrib/provider-jet-gcp/config/accessapproval"
"github.com/crossplane-contrib/provider-jet-gcp/config/cloudplatform"
"github.com/crossplane/terrajet/pkg/types/name"
"github.com/pkg/errors"
)

func groupOverrides() tjconfig.ResourceOption { //nolint: gocyclo
// Note(turkenh): The todo below will fix this linter error, disabling until
// then
// GroupKindCalculator returns the correct group and kind name for given TF
// resource.
type GroupKindCalculator func(resource string) (string, string)

func externalNameConfig() tjconfig.ResourceOption {
return func(r *tjconfig.Resource) {
// Todo(turkenh): Use an single map for all resource => group mapping
words := strings.Split(r.Name, "_")
if _, ok := cloudplatform.Resources[r.Name]; ok {
r.ShortGroup = "cloudplatform"
} else if _, ok := accessapproval.Resources[r.Name]; ok { //nolint: gocritic
// Todo(turkenh): Check how to fix linter "ifElseChain: rewrite if-else"
// to switch statement that covers all checks here.
r.ShortGroup = "accessapproval"
} else if strings.HasPrefix(r.Name, "google_access_context_manager") ||
strings.HasPrefix(r.Name, "google_data_loss_prevention") {
r.ShortGroup = words[1] + words[2] + words[3]
r.Kind = strcase.ToCamel(strings.Join(words[4:], "_"))
} else if strings.HasPrefix(r.Name, "google_active_directory") ||
strings.HasPrefix(r.Name, "google_app_engine") ||
strings.HasPrefix(r.Name, "google_assured_workloads") ||
strings.HasPrefix(r.Name, "google_binary_authorization") ||
strings.HasPrefix(r.Name, "google_deployment_manager") ||
strings.HasPrefix(r.Name, "google_essential_contacts") ||
strings.HasPrefix(r.Name, "google_game_services") ||
strings.HasPrefix(r.Name, "google_gke_hub") ||
strings.HasPrefix(r.Name, "google_identity_platform") ||
strings.HasPrefix(r.Name, "google_ml_engine") ||
strings.HasPrefix(r.Name, "google_network_management") ||
strings.HasPrefix(r.Name, "google_network_services") ||
strings.HasPrefix(r.Name, "google_service_networking") ||
strings.HasPrefix(r.Name, "google_resource_manager") ||
strings.HasPrefix(r.Name, "google_secret_manager") ||
strings.HasPrefix(r.Name, "google_org_policy") ||
strings.HasPrefix(r.Name, "google_vpc_access_connector") ||
// Examples: google_cloud_identity, google_cloud_run,
// google_cloud_asset, google_data_fusion, google_data_source...
strings.HasPrefix(r.Name, "google_cloud_") || strings.HasPrefix(r.Name, "google_data_") {

r.ShortGroup = words[1] + words[2]
r.Kind = strcase.ToCamel(strings.Join(words[3:], "_"))
}
r.ExternalName = tjconfig.IdentifierFromProvider
}
}

func externalNameConfig() tjconfig.ResourceOption {
func groupOverrides() tjconfig.ResourceOption {
return func(r *tjconfig.Resource) {
r.ExternalName = tjconfig.IdentifierFromProvider
for k, v := range groupMap {
ok, err := regexp.MatchString(k, r.Name)
if err != nil {
panic(errors.Wrap(err, "cannot match regular expression"))
}
if ok {
r.ShortGroup, r.Kind = v(r.Name)
return
}
}
}
}

var groupMap = map[string]GroupKindCalculator{
// Note(turkenh): The following resources are listed under "Cloud Platform"
// section in Terraform Documentation.
"google_billing_subaccount$": ReplaceGroupWords("cloudplatform", 0),
"google_folder$": ReplaceGroupWords("cloudplatform", 0),
"google_folder_iam.*": ReplaceGroupWords("cloudplatform", 0),
"google_folder_organization.*": ReplaceGroupWords("cloudplatform", 0),
"google_organization_iam.*": ReplaceGroupWords("cloudplatform", 0),
"google_organization_policy.*": ReplaceGroupWords("cloudplatform", 0),
"google_project$": ReplaceGroupWords("cloudplatform", 0),
"google_project_iam.*": ReplaceGroupWords("cloudplatform", 0),
"google_project_service.*": ReplaceGroupWords("cloudplatform", 0),
"google_project_default_service_accounts$": ReplaceGroupWords("cloudplatform", 0),
"google_project_organization_policy$": ReplaceGroupWords("cloudplatform", 0),
"google_project_usage_export_bucket$": ReplaceGroupWords("cloudplatform", 0),
"google_service_account.*": ReplaceGroupWords("cloudplatform", 0),
"google_service_networking_peered_dns_domain$": ReplaceGroupWords("cloudplatform", 0),

// Resources in "Access Approval" group.
// Note(turkenh): The following resources are listed under "Access Approval"
// section in Terraform Documentation.
"google_.+_approval_settings$": ReplaceGroupWords("accessapproval", 0),

"google_access_context_manager.+": ReplaceGroupWords("", 3),
"google_data_loss_prevention.+": ReplaceGroupWords("", 3),

"google_service_networking_connection$": ReplaceGroupWords("", 2),
"google_active_directory.+": ReplaceGroupWords("", 2),
"google_app_engine.+": ReplaceGroupWords("", 2),
"google_assured_workloads.+": ReplaceGroupWords("", 2),
"google_binary_authorization.+": ReplaceGroupWords("", 2),
"google_container_analysis.+": ReplaceGroupWords("", 2),
"google_deployment_manager.+": ReplaceGroupWords("", 2),
"google_dialogflow_cx.+": ReplaceGroupWords("", 2),
"google_essential_contacts.+": ReplaceGroupWords("", 2),
"google_game_services.+": ReplaceGroupWords("", 2),
"google_gke_hub.+": ReplaceGroupWords("", 2),
"google_identity_platform.+": ReplaceGroupWords("", 2),
"google_ml_engine.+": ReplaceGroupWords("", 2),
"google_network_management.+": ReplaceGroupWords("", 2),
"google_network_services.+": ReplaceGroupWords("", 2),
"google_resource_manager.+": ReplaceGroupWords("", 2),
"google_secret_manager.+": ReplaceGroupWords("", 2),
"google_storage_transfer.+": ReplaceGroupWords("", 2),
"google_org_policy.+": ReplaceGroupWords("", 2),
"google_vertex_ai.+": ReplaceGroupWords("", 2),
"google_vpc_access.+": ReplaceGroupWords("", 2),

"google_cloud_asset.+": ReplaceGroupWords("", 2),
"google_cloud_build.+": ReplaceGroupWords("", 2),
"google_cloud_functions.+": ReplaceGroupWords("", 2),
"google_cloud_identity.+": ReplaceGroupWords("", 2),
"google_cloud_iot.+": ReplaceGroupWords("", 2),
"google_cloud_tasks.+": ReplaceGroupWords("", 2),
"google_cloud_scheduler.+": ReplaceGroupWords("", 2),
"google_cloud_run.+": ReplaceGroupWords("", 2),

"google_data_catalog.+": ReplaceGroupWords("", 2),
"google_data_flow.+": ReplaceGroupWords("", 2),
"google_data_fusion.+": ReplaceGroupWords("", 2),

"google_os_config.+": ReplaceGroupWords("", 2),
"google_os_login.+": ReplaceGroupWords("", 2),
}

// ReplaceGroupWords uses given group as the group of the resource and removes
// a number of words in resource name before calculating the kind of the resource.
func ReplaceGroupWords(group string, count int) GroupKindCalculator {
return func(resource string) (string, string) {
// "google_cloud_run_domain_mapping": "cloudrun" -> (cloudrun, DomainMapping)
words := strings.Split(strings.TrimPrefix(resource, "google_"), "_")
if group == "" {
group = strings.Join(words[:count], "")
}
snakeKind := strings.Join(words[count:], "_")
return group, name.NewFromSnake(snakeKind).Camel
}
}
11 changes: 11 additions & 0 deletions config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package config

import (
tjconfig "github.com/crossplane/terrajet/pkg/config"
"github.com/crossplane/terrajet/pkg/types/name"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
tf "github.com/hashicorp/terraform-provider-google/google"

"github.com/crossplane-contrib/provider-jet-gcp/config/accessapproval"
"github.com/crossplane-contrib/provider-jet-gcp/config/bigtable"
"github.com/crossplane-contrib/provider-jet-gcp/config/cloudfunctions"
"github.com/crossplane-contrib/provider-jet-gcp/config/cloudiot"
"github.com/crossplane-contrib/provider-jet-gcp/config/cloudplatform"
Expand Down Expand Up @@ -60,6 +62,7 @@ func GetProvider() *tjconfig.Provider {

for _, configure := range []func(provider *tjconfig.Provider){
accessapproval.Configure,
bigtable.Configure,
cloudfunctions.Configure,
cloudiot.Configure,
cloudplatform.Configure,
Expand All @@ -83,3 +86,11 @@ func DefaultResource(opts ...tjconfig.ResourceOption) tjconfig.DefaultResourceFn
return tjconfig.DefaultResource(name, terraformResource, append(orgOpts, opts...)...)
}
}

func init() {
// GCP specific acronyms

// Todo(turkenh): move to Terrajet?
name.AddAcronym("idp", "IdP")
name.AddAcronym("oauth", "OAuth")
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
// Commit for v4.0.0 tag https://github.com/hashicorp/terraform-provider-google/commit/f004d2d203fa86da2e344fc23dc6fe509e6bbfae
github.com/hashicorp/terraform-provider-google v1.20.1-0.20211102210101-f004d2d203fa
github.com/iancoleman/strcase v0.2.0
github.com/pkg/errors v0.9.1
go.uber.org/multierr v1.7.0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
Expand Down

0 comments on commit 9cfad6c

Please sign in to comment.