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 Oct 21, 2021
1 parent 2b31b18 commit 92de646
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 14 deletions.
2 changes: 1 addition & 1 deletion mmv1/third_party/terraform/go.mod.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/hashicorp/terraform-provider-google<%= "-" + version unless ve

require (
cloud.google.com/go/bigtable v1.10.1
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210929201636-4f607239a42a
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021035153-ee51cd3567e5
github.com/apparentlymart/go-cidr v1.1.0
github.com/client9/misspell v0.3.4
github.com/davecgh/go-spew v1.1.1
Expand Down
8 changes: 8 additions & 0 deletions mmv1/third_party/terraform/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1432,3 +1432,11 @@ github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-202109
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210929183012-8396494d598c/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210929201636-4f607239a42a h1:Qxf6GAn7dnwHI69E9fWhnVqBng4lpVjAJcLEuf1vffs=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210929201636-4f607239a42a/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211020195356-077101b205ea h1:XjquYMnXrqEdH/VEalqj+2GiGZ8Tzp90himjfSfbZL0=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211020195356-077101b205ea/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021015206-34ab8399a485 h1:kMwBIPZDH3VU1TchV09Wf8d6/TxiyDoqkgpXZQUKO3s=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021015206-34ab8399a485/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021015206-34ab8399a485 h1:kMwBIPZDH3VU1TchV09Wf8d6/TxiyDoqkgpXZQUKO3s=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021015206-34ab8399a485/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021035153-ee51cd3567e5 h1:dhdzL+Ot4NpDuTyx/YPSlimpgEPDEtU5Gg7/tVGkjH8=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021035153-ee51cd3567e5/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
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 -%>
2 changes: 0 additions & 2 deletions mmv1/third_party/terraform/utils/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ type Config struct {
EventarcBasePath string
GkeHubBasePath string
OrgPolicyBasePath string
<% 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 @@ -183,9 +183,7 @@ func Provider() *schema.Provider {
OrgPolicyEndpointEntryKey: OrgPolicyEndpointEntry,
PrivatecaCertificateTemplateEndpointEntryKey: PrivatecaCertificateTemplateCustomEndpointEntry,

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

ProviderMetaSchema: map[string]*schema.Schema{
Expand Down Expand Up @@ -448,9 +446,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 @@ -638,9 +634,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
2 changes: 1 addition & 1 deletion tpgtools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
bitbucket.org/creachadair/stringset v0.0.9
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210929201636-4f607239a42a
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021035153-ee51cd3567e5
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/hcl v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions tpgtools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210929201636-4f607239a42a h1:Qxf6GAn7dnwHI69E9fWhnVqBng4lpVjAJcLEuf1vffs=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210929201636-4f607239a42a/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021035153-ee51cd3567e5 h1:dhdzL+Ot4NpDuTyx/YPSlimpgEPDEtU5Gg7/tVGkjH8=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20211021035153-ee51cd3567e5/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
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 92de646

Please sign in to comment.