This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
generated from crossplane-contrib/provider-jet-template
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hasan Turken <[email protected]>
- Loading branch information
Showing
7 changed files
with
99 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,114 @@ | ||
package config | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
tjconfig "github.com/crossplane/terrajet/pkg/config" | ||
"github.com/crossplane/terrajet/pkg/types/name" | ||
|
||
"github.com/crossplane-contrib/provider-jet-gcp/config/accessapproval" | ||
"github.com/crossplane-contrib/provider-jet-gcp/config/cloudplatform" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func groupOverrides() tjconfig.ResourceOption { //nolint: gocyclo | ||
// Note(turkenh): The todo below will fix this linter error, disabling until | ||
// then | ||
return func(r *tjconfig.Resource) { | ||
// Todo(turkenh): Use an single map for all resource => group mapping | ||
words := strings.Split(r.Name, "_") | ||
kindInSnake := strings.Join(words[2:], "_") | ||
// GroupKindCalculator returns the correct group and kind name for given TF | ||
// resource. | ||
type GroupKindCalculator func(resource string) (string, string) | ||
|
||
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] | ||
kindInSnake = 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_container_analysis") || | ||
strings.HasPrefix(r.Name, "google_deployment_manager") || | ||
strings.HasPrefix(r.Name, "google_dialogflow_cx") || | ||
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_storage_transfer") || | ||
strings.HasPrefix(r.Name, "google_org_policy") || | ||
strings.HasPrefix(r.Name, "google_vertex_ai") || | ||
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_") || | ||
strings.HasPrefix(r.Name, "google_os_") { | ||
func externalNameConfig() tjconfig.ResourceOption { | ||
return func(r *tjconfig.Resource) { | ||
r.ExternalName = tjconfig.IdentifierFromProvider | ||
} | ||
} | ||
|
||
r.ShortGroup = words[1] + words[2] | ||
kindInSnake = strings.Join(words[3:], "_") | ||
func groupOverrides() tjconfig.ResourceOption { | ||
return func(r *tjconfig.Resource) { | ||
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 | ||
} | ||
} | ||
|
||
r.Kind = name.NewFromSnake(kindInSnake).Camel | ||
} | ||
} | ||
|
||
func externalNameConfig() tjconfig.ResourceOption { | ||
return func(r *tjconfig.Resource) { | ||
r.ExternalName = tjconfig.IdentifierFromProvider | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters