Skip to content

Commit

Permalink
Some additional complexities in moving to GA - regional endpoints and…
Browse files Browse the repository at this point in the history
… divergence in naming between this and OSPolicy.
  • Loading branch information
nat-henderson committed Dec 10, 2021
1 parent 68b4d28 commit ea932c6
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 8 deletions.
4 changes: 2 additions & 2 deletions mmv1/third_party/terraform/utils/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ var DefaultBasePaths = map[string]string{
StorageTransferBasePathKey : "https://storagetransfer.googleapis.com/v1/",
BigtableAdminBasePathKey : "https://bigtableadmin.googleapis.com/v2/",
GkeHubFeatureBasePathKey: "https://gkehub.googleapis.com/v1beta/",
ContainerAwsBasePathKey: "https://autopush-gkemulticloud.sandbox.googleapis.com/v1/",
ContainerAzureBasePathKey: "https://autopush-gkemulticloud.sandbox.googleapis.com/v1/",
ContainerAwsBasePathKey: "https://{{location}}-gkemulticloud.googleapis.com/v1/",
ContainerAzureBasePathKey: "https://{{location}}-gkemulticloud.googleapis.com/v1/",
}

var DefaultClientScopes = []string{
Expand Down
3 changes: 0 additions & 3 deletions tpgtools/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ func (r *Resource) fillLinksFromExtensionsMap(m map[string]interface{}) {
}
}

// TODO: there are hundreds of lines of name munging here. Surely it is not *all* required.
// DO NOT SUBMIT. MUST FIX.

// Name is the shortname of a resource. For example, "instance".
func (r Resource) Name() string {
return r.title
Expand Down
171 changes: 169 additions & 2 deletions tpgtools/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func DCLToTerraformReference(resourceType string, version string) (string, error
return "google_container_aws_cluster", nil
case "ContainerAwsNodePool":
return "google_container_aws_node_pool", nil
case "ContainerAzureAzureClient":
case "ContainerAzureClient":
return "google_container_azure_client", nil
case "ContainerAzureCluster":
return "google_container_azure_cluster", nil
Expand All @@ -149,6 +149,49 @@ func DCLToTerraformReference(resourceType string, version string) (string, error
return "google_privateca_certificate_template", nil
case "RecaptchaEnterpriseKey":
return "google_recaptcha_enterprise_key", nil
}
// If still not found, try alternate names for resources.
switch resourceType {
case "AssuredWorkloadsWorkload":
return "google_assured_workloads_workload", nil
case "CloudbuildWorkerPool":
return "google_cloudbuild_worker_pool", nil
case "CloudResourceManagerFolder":
return "google_folder", nil
case "CloudResourceManagerProject":
return "google_project", nil
case "ComputeFirewallPolicy":
return "google_compute_firewall_policy", nil
case "ComputeFirewallPolicyAssociation":
return "google_compute_firewall_policy_association", nil
case "ComputeFirewallPolicyRule":
return "google_compute_firewall_policy_rule", nil
case "ComputeForwardingRule":
return "google_compute_forwarding_rule", nil
case "ComputeGlobalForwardingRule":
return "google_compute_global_forwarding_rule", nil
case "ContainerAwsCluster":
return "google_container_aws_cluster", nil
case "ContainerAwsNodePool":
return "google_container_aws_node_pool", nil
case "ContainerAzureAzureClient":
return "google_container_azure_client", nil
case "ContainerAzureCluster":
return "google_container_azure_cluster", nil
case "ContainerAzureNodePool":
return "google_container_azure_node_pool", nil
case "DataprocWorkflowTemplate":
return "google_dataproc_workflow_template", nil
case "EventarcTrigger":
return "google_eventarc_trigger", nil
case "OrgPolicyPolicy":
return "google_org_policy_policy", nil
case "OsConfigOSPolicyAssignment":
return "google_os_config_os_policy_assignment", nil
case "PrivatecaCertificateTemplate":
return "google_privateca_certificate_template", nil
case "RecaptchaEnterpriseKey":
return "google_recaptcha_enterprise_key", nil
default:
return "", fmt.Errorf("Error retrieving Terraform name from DCL resource type: %s not found", resourceType)
}
Expand Down Expand Up @@ -420,7 +463,7 @@ func ConvertSampleJSONToHCL(resourceType string, version string, hasGAEquivalent
return "", err
}
return ContainerAwsNodePoolAsHCL(*r, hasGAEquivalent)
case "ContainerAzureAzureClient":
case "ContainerAzureClient":
r := &containerazure.AzureClient{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
Expand Down Expand Up @@ -474,6 +517,130 @@ func ConvertSampleJSONToHCL(resourceType string, version string, hasGAEquivalent
return "", err
}
return RecaptchaEnterpriseKeyAsHCL(*r, hasGAEquivalent)
}

// If still not found, try alternative resource names.
switch resourceType {
case "AssuredWorkloadsWorkload":
r := &assuredworkloads.Workload{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return AssuredWorkloadsWorkloadAsHCL(*r, hasGAEquivalent)
case "CloudbuildWorkerPool":
r := &cloudbuild.WorkerPool{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return CloudbuildWorkerPoolAsHCL(*r, hasGAEquivalent)
case "CloudResourceManagerFolder":
r := &cloudresourcemanager.Folder{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return CloudResourceManagerFolderAsHCL(*r, hasGAEquivalent)
case "CloudResourceManagerProject":
r := &cloudresourcemanager.Project{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return serializeGAProjectToHCL(*r, hasGAEquivalent)
case "ComputeFirewallPolicy":
r := &compute.FirewallPolicy{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ComputeFirewallPolicyAsHCL(*r, hasGAEquivalent)
case "ComputeFirewallPolicyAssociation":
r := &compute.FirewallPolicyAssociation{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ComputeFirewallPolicyAssociationAsHCL(*r, hasGAEquivalent)
case "ComputeFirewallPolicyRule":
r := &compute.FirewallPolicyRule{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ComputeFirewallPolicyRuleAsHCL(*r, hasGAEquivalent)
case "ComputeForwardingRule":
r := &compute.ForwardingRule{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ComputeForwardingRuleAsHCL(*r, hasGAEquivalent)
case "ComputeGlobalForwardingRule":
r := &compute.ForwardingRule{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ComputeGlobalForwardingRuleAsHCL(*r, hasGAEquivalent)
case "ContainerAwsCluster":
r := &containeraws.Cluster{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ContainerAwsClusterAsHCL(*r, hasGAEquivalent)
case "ContainerAwsNodePool":
r := &containeraws.NodePool{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ContainerAwsNodePoolAsHCL(*r, hasGAEquivalent)
case "ContainerAzureAzureClient":
r := &containerazure.AzureClient{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ContainerAzureClientAsHCL(*r, hasGAEquivalent)
case "ContainerAzureCluster":
r := &containerazure.Cluster{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ContainerAzureClusterAsHCL(*r, hasGAEquivalent)
case "ContainerAzureNodePool":
r := &containerazure.NodePool{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return ContainerAzureNodePoolAsHCL(*r, hasGAEquivalent)
case "DataprocWorkflowTemplate":
r := &dataproc.WorkflowTemplate{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return DataprocWorkflowTemplateAsHCL(*r, hasGAEquivalent)
case "EventarcTrigger":
r := &eventarc.Trigger{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return EventarcTriggerAsHCL(*r, hasGAEquivalent)
case "OrgPolicyPolicy":
r := &orgpolicy.Policy{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return OrgPolicyPolicyAsHCL(*r, hasGAEquivalent)
case "OsConfigOSPolicyAssignment":
r := &osconfig.OSPolicyAssignment{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return OSConfigOSPolicyAssignmentAsHCL(*r, hasGAEquivalent)
case "PrivatecaCertificateTemplate":
r := &privateca.CertificateTemplate{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return PrivatecaCertificateTemplateAsHCL(*r, hasGAEquivalent)
case "RecaptchaEnterpriseKey":
r := &recaptchaenterprise.Key{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
return RecaptchaEnterpriseKeyAsHCL(*r, hasGAEquivalent)
default:
//return fmt.Sprintf("%s resource not supported in tpgtools", resourceType), nil
return "", fmt.Errorf("Error converting sample JSON to HCL: %s not found", resourceType)
Expand Down
24 changes: 24 additions & 0 deletions tpgtools/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ should be converted to use the DCL's ID method, so normalization can be uniform.
{{- if $.ReplaceInBasePath.Present }}
client.Config.BasePath = strings.ReplaceAll(client.Config.BasePath, "{{$.ReplaceInBasePath.Old}}", "{{$.ReplaceInBasePath.New}}")
{{- end }}
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.Apply{{$.DCLTitle}}(context.Background(), obj, createDirective...)

if _, ok := err.(dcl.DiffAfterApplyError); ok {
Expand Down Expand Up @@ -352,6 +358,12 @@ func resource{{$.PathType}}Read(d *schema.ResourceData, meta interface{}) error
{{- if $.ReplaceInBasePath.Present }}
client.Config.BasePath = strings.ReplaceAll(client.Config.BasePath, "{{$.ReplaceInBasePath.Old}}", "{{$.ReplaceInBasePath.New}}")
{{- end }}
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.Get{{$.DCLTitle}}(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("{{$.PathType}} %q", d.Id())
Expand Down Expand Up @@ -431,6 +443,12 @@ func resource{{$.PathType}}Update(d *schema.ResourceData, meta interface{}) erro
{{- if $.ReplaceInBasePath.Present }}
client.Config.BasePath = strings.ReplaceAll(client.Config.BasePath, "{{$.ReplaceInBasePath.Old}}", "{{$.ReplaceInBasePath.New}}")
{{- end }}
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.Apply{{$.DCLTitle}}(context.Background(), obj, directive...)

if _, ok := err.(dcl.DiffAfterApplyError); ok {
Expand Down Expand Up @@ -513,6 +531,12 @@ func resource{{$.PathType}}Delete(d *schema.ResourceData, meta interface{}) erro
{{- if $.ReplaceInBasePath.Present }}
client.Config.BasePath = strings.ReplaceAll(client.Config.BasePath, "{{$.ReplaceInBasePath.Old}}", "{{$.ReplaceInBasePath.New}}")
{{- end }}
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
if err := client.Delete{{$.DCLTitle}}(context.Background(), obj); err != nil {
return fmt.Errorf("Error deleting {{$.DCLTitle}}: %s", err)
}
Expand Down
26 changes: 25 additions & 1 deletion tpgtools/templates/serialization.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func DCLToTerraformReference(resourceType string, version string) (string, error
// If not found in sample version, fallthrough to GA
switch resourceType {
{{- range $res := $resList }}
case "{{$res.PathType}}":
return "{{$res.TerraformName}}", nil
{{- end }}
}
// If still not found, try alternate names for resources.
switch resourceType {
{{- range $res := $resList }}
case "{{$res.SampleTypeName}}":
return "{{$res.TerraformName}}", nil
{{- end }}
Expand Down Expand Up @@ -109,6 +116,22 @@ func ConvertSampleJSONToHCL(resourceType string, version string, hasGAEquivalent
}
{{- else }}
// If not found in sample version, fallthrough to GA
switch resourceType {
{{- range $res := $resList }}
case "{{$res.PathType}}":
r := &{{$res.Package}}{{$version.SerializationSuffix}}.{{$res.DCLStructName}}{}
if err := json.Unmarshal(b, r); err != nil {
return "", err
}
{{- if $res.CustomSerializer }}
return {{$res.CustomSerializer}}(*r, hasGAEquivalent)
{{- else }}
return {{$res.PathType}}{{$version.SerializationSuffix}}AsHCL(*r, hasGAEquivalent)
{{- end }}
{{- end }}
}

// If still not found, try alternative resource names.
switch resourceType {
{{- range $res := $resList }}
case "{{$res.SampleTypeName}}":
Expand All @@ -128,6 +151,7 @@ func ConvertSampleJSONToHCL(resourceType string, version string, hasGAEquivalent
}
{{ end }}
{{- end }}

}


Expand Down Expand Up @@ -293,4 +317,4 @@ func formatHCL(hcl string) (string, error) {
return "", err
}
return b.String(), nil
}
}

0 comments on commit ea932c6

Please sign in to comment.