Skip to content

Commit

Permalink
Move Cloud Build Worker Pool to GA.
Browse files Browse the repository at this point in the history
Note: this includes some unusual changes due to deviations between the beta and GA APIs.
  • Loading branch information
nat-henderson committed Nov 8, 2021
1 parent 5ce1982 commit 640ea27
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<% autogen_exception -%>
package google

<% unless version == 'ga' -%>
import (
"fmt"
"strings"
Expand Down Expand Up @@ -190,4 +189,3 @@ func funcAccTestCloudbuildWorkerPoolCheckDestroy(t *testing.T) func(s *terraform
return nil
}
}
<% end -%>
1 change: 0 additions & 1 deletion mmv1/third_party/terraform/utils/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ type Config struct {
<% unless version == 'ga' -%>
// CloudBuild WorkerPool uses a different endpoint (v1beta1) than any other CloudBuild resources
CloudBuildWorkerPoolBasePath string
<% end -%>
}

<% products.each do |product| -%>
Expand Down
6 changes: 0 additions & 6 deletions mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ func Provider() *schema.Provider {
VmwareEndpointEntryKey: VmwareEndpointEntry,
<% end -%>

<% unless version == "ga" -%>
CloudBuildWorkerPoolEndpointEntryKey: CloudBuildWorkerPoolEndpointEntry,
<% end -%>
},

ProviderMetaSchema: map[string]*schema.Schema{
Expand Down Expand Up @@ -456,9 +454,7 @@ end # products.each do
// resources implemented within tpgtools
map[string]*schema.Resource{
"google_assured_workloads_workload": resourceAssuredWorkloadsWorkload(),
<% unless version == 'ga' -%>
"google_cloudbuild_worker_pool": resourceCloudbuildWorkerPool(),
<% end -%>
"google_compute_firewall_policy_association": resourceComputeFirewallPolicyAssociation(),
"google_compute_firewall_policy": resourceComputeFirewallPolicy(),
"google_compute_firewall_policy_rule": resourceComputeFirewallPolicyRule(),
Expand Down Expand Up @@ -655,9 +651,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
config.GkeHubBasePath = d.Get(GkeHubFeatureCustomEndpointEntryKey).(string)
config.OrgPolicyBasePath = d.Get(OrgPolicyEndpointEntryKey).(string)
config.PrivatecaBasePath = d.Get(PrivatecaCertificateTemplateEndpointEntryKey).(string)
<% unless version == "ga" -%>
config.CloudBuildWorkerPoolBasePath = d.Get(CloudBuildWorkerPoolEndpointEntryKey).(string)
<% end -%>

stopCtx, ok := schema.StopContext(ctx)
if !ok {
Expand Down
6 changes: 6 additions & 0 deletions tpgtools/overrides/cloudbuild/tpgtools_product.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- type: PRODUCT_BASE_PATH
details:
basepathidentifier: cloud_build_worker_pool
- type: PRODUCT_TITLE # used to align with mmv1 product
details:
title: "cloudbuild"
4 changes: 4 additions & 0 deletions tpgtools/overrides/cloudbuild/worker_pool.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- type: DIFF_SUPPRESS_FUNC
field: network_config.peered_network
details:
diffsuppressfunc: compareResourceNames
120 changes: 120 additions & 0 deletions tpgtools/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

assuredworkloads "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/assuredworkloads"
assuredworkloadsBeta "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/assuredworkloads/beta"
cloudbuild "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudbuild"
cloudbuildBeta "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudbuild/beta"
cloudresourcemanager "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudresourcemanager"
cloudresourcemanagerBeta "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudresourcemanager/beta"
Expand Down Expand Up @@ -88,6 +89,8 @@ func DCLToTerraformReference(resourceType, version string) (string, error) {
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":
Expand Down Expand Up @@ -123,6 +126,8 @@ func DCLToTerraformSampleName(service, resource string) (string, string, error)
switch service + resource {
case "assuredworkloadsworkload":
return "AssuredWorkloads", "Workload", nil
case "cloudbuildworkerpool":
return "cloudbuild", "WorkerPool", nil
case "cloudresourcemanagerfolder":
return "CloudResourceManager", "Folder", nil
case "cloudresourcemanagerproject":
Expand Down Expand Up @@ -263,6 +268,12 @@ func ConvertSampleJSONToHCL(resourceType string, version string, hasGAEquivalent
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 {
Expand Down Expand Up @@ -2708,6 +2719,71 @@ func convertAssuredWorkloadsWorkloadResourcesToHCL(r *assuredworkloads.WorkloadR
return outputConfig + "}"
}

// CloudbuildWorkerPoolAsHCL returns a string representation of the specified resource in HCL.
// The generated HCL will include every settable field as a literal - that is, no
// variables, no references. This may not be the best possible representation, but
// the crucial point is that `terraform import; terraform apply` will not produce
// any changes. We do not validate that the resource specified will pass terraform
// validation unless is an object returned from the API after an Apply.
func CloudbuildWorkerPoolAsHCL(r cloudbuild.WorkerPool, hasGAEquivalent bool) (string, error) {
outputConfig := "resource \"google_cloudbuild_worker_pool\" \"output\" {\n"
if r.Location != nil {
outputConfig += fmt.Sprintf("\tlocation = %#v\n", *r.Location)
}
if r.Name != nil {
outputConfig += fmt.Sprintf("\tname = %#v\n", *r.Name)
}
if r.DisplayName != nil {
outputConfig += fmt.Sprintf("\tdisplay_name = %#v\n", *r.DisplayName)
}
if v := convertCloudbuildWorkerPoolNetworkConfigToHCL(r.NetworkConfig); v != "" {
outputConfig += fmt.Sprintf("\tnetwork_config %s\n", v)
}
if r.Project != nil {
outputConfig += fmt.Sprintf("\tproject = %#v\n", *r.Project)
}
if v := convertCloudbuildWorkerPoolWorkerConfigToHCL(r.WorkerConfig); v != "" {
outputConfig += fmt.Sprintf("\tworker_config %s\n", v)
}
formatted, err := formatHCL(outputConfig + "}")
if err != nil {
return "", err
}
if !hasGAEquivalent {
// The formatter will not accept the google-beta symbol because it is injected during testing.
return withProviderLine(formatted), nil
}
return formatted, nil
}

func convertCloudbuildWorkerPoolNetworkConfigToHCL(r *cloudbuild.WorkerPoolNetworkConfig) string {
if r == nil {
return ""
}
outputConfig := "{\n"
if r.PeeredNetwork != nil {
outputConfig += fmt.Sprintf("\tpeered_network = %#v\n", *r.PeeredNetwork)
}
return outputConfig + "}"
}

func convertCloudbuildWorkerPoolWorkerConfigToHCL(r *cloudbuild.WorkerPoolWorkerConfig) string {
if r == nil {
return ""
}
outputConfig := "{\n"
if r.DiskSizeGb != nil {
outputConfig += fmt.Sprintf("\tdisk_size_gb = %#v\n", *r.DiskSizeGb)
}
if r.MachineType != nil {
outputConfig += fmt.Sprintf("\tmachine_type = %#v\n", *r.MachineType)
}
if r.NoExternalIP != nil {
outputConfig += fmt.Sprintf("\tno_external_ip = %#v\n", *r.NoExternalIP)
}
return outputConfig + "}"
}

// CloudResourceManagerFolderAsHCL returns a string representation of the specified resource in HCL.
// The generated HCL will include every settable field as a literal - that is, no
// variables, no references. This may not be the best possible representation, but
Expand Down Expand Up @@ -6692,6 +6768,50 @@ func convertAssuredWorkloadsWorkloadResourcesList(i interface{}) (out []map[stri
return out
}

func convertCloudbuildWorkerPoolNetworkConfig(i interface{}) map[string]interface{} {
if i == nil {
return nil
}
in := i.(map[string]interface{})
return map[string]interface{}{
"peeredNetwork": in["peered_network"],
}
}

func convertCloudbuildWorkerPoolNetworkConfigList(i interface{}) (out []map[string]interface{}) {
if i == nil {
return nil
}

for _, v := range i.([]interface{}) {
out = append(out, convertCloudbuildWorkerPoolNetworkConfig(v))
}
return out
}

func convertCloudbuildWorkerPoolWorkerConfig(i interface{}) map[string]interface{} {
if i == nil {
return nil
}
in := i.(map[string]interface{})
return map[string]interface{}{
"diskSizeGb": in["disk_size_gb"],
"machineType": in["machine_type"],
"noExternalIP": in["no_external_ip"],
}
}

func convertCloudbuildWorkerPoolWorkerConfigList(i interface{}) (out []map[string]interface{}) {
if i == nil {
return nil
}

for _, v := range i.([]interface{}) {
out = append(out, convertCloudbuildWorkerPoolWorkerConfig(v))
}
return out
}

func convertComputeFirewallPolicyRuleMatch(i interface{}) map[string]interface{} {
if i == nil {
return nil
Expand Down

0 comments on commit 640ea27

Please sign in to comment.