Skip to content

Commit

Permalink
Go rewrite constants templates (GoogleCloudPlatform#10409)
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored Apr 11, 2024
1 parent 6bbb3ef commit 8bc480c
Show file tree
Hide file tree
Showing 73 changed files with 3,060 additions and 9 deletions.
10 changes: 10 additions & 0 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ type Resource struct {
Parameters []*Type

ProductMetadata *Product

// The version name provided by the user through CI
TargetVersionName string

// The compiler to generate the downstream files, for example "terraformgoogleconversion-codegen".
Compiler string
}

func (r *Resource) UnmarshalYAML(n *yaml.Node) error {
Expand Down Expand Up @@ -927,3 +933,7 @@ func (r Resource) IgnoreReadPropertiesToString(e resource.Examples) string {

return fmt.Sprintf("[]string{%s}", strings.Join(props, ", "))
}

func (r *Resource) SetCompiler(t string) {
r.Compiler = fmt.Sprintf("%s-codegen", strings.ToLower(t))
}
1 change: 1 addition & 0 deletions mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func main() {
resource := &api.Resource{}
api.Compile(resourceYamlPath, resource)

resource.TargetVersionName = *version
resource.Properties = resource.AddLabelsRelatedFields(resource.PropertiesWithExcluded(), nil)
resource.SetDefault(productApi)
resource.Validate()
Expand Down
1 change: 0 additions & 1 deletion mmv1/products/filestore/Instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ examples:
vars:
instance_name: 'test-instance'
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/filestore.erb
pre_create: templates/terraform/pre_create/filestore_instance.go.erb
parameters:
- !ruby/object:Api::Type::String
Expand Down
4 changes: 4 additions & 0 deletions mmv1/provider/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"log"
"os"
"path"
"reflect"
"strings"

"github.com/GoogleCloudPlatform/magic-modules/mmv1/api"
Expand Down Expand Up @@ -55,6 +56,9 @@ func NewTerraform(product *api.Product, versionName string) *Terraform {
Version: *product.VersionObjOrClosest(versionName)}

t.Product.SetPropertiesBasedOnVersion(&t.Version)
for _, r := range t.Product.Objects {
r.SetCompiler(reflect.TypeOf(t).Name())
}

return &t
}
Expand Down
8 changes: 0 additions & 8 deletions mmv1/templates/terraform/constants/filestore.erb

This file was deleted.

34 changes: 34 additions & 0 deletions mmv1/templates/terraform/constants/go/access_approval.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{/*
The license inside this block applies to this file
Copyright 2024 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
var accessApprovalCloudProductMapping = map[string]string{
"appengine.googleapis.com": "App Engine",
"bigquery.googleapis.com": "BigQuery",
"bigtable.googleapis.com": "Cloud Bigtable",
"cloudkms.googleapis.com": "Cloud Key Management Service",
"compute.googleapis.com": "Compute Engine",
"dataflow.googleapis.com": "Cloud Dataflow",
"iam.googleapis.com": "Cloud Identity and Access Management",
"pubsub.googleapis.com": "Cloud Pub/Sub",
"storage.googleapis.com": "Cloud Storage",
}
func accessApprovalEnrolledServicesHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
cp := m["cloud_product"].(string)
if n, ok := accessApprovalCloudProductMapping[cp]; ok {
cp = n
}
buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(cp))) // ToLower just in case
buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["enrollment_level"].(string))))
return tpgresource.Hashcode(buf.String())
}
22 changes: 22 additions & 0 deletions mmv1/templates/terraform/constants/go/agent_pool.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{ if ne $.Compiler "terraformgoogleconversion-codegen" }}
// waitForAgentPoolReady waits for an agent pool to leave the
// "CREATING" state and become "CREATED", to indicate that it's ready.
func waitForAgentPoolReady(d *schema.ResourceData, config *transport_tpg.Config, timeout time.Duration) error {
return resource.Retry(timeout, func() *resource.RetryError {
if err := resourceStorageTransferAgentPoolRead(d, config); err != nil {
return resource.NonRetryableError(err)
}

name := d.Get("name").(string)
state := d.Get("state").(string)
if state == "CREATING" {
return resource.RetryableError(fmt.Errorf("AgentPool %q has state %q.", name, state))
} else if state == "CREATED" {
log.Printf("[DEBUG] AgentPool %q has state %q.", name, state)
return nil
} else {
return resource.NonRetryableError(fmt.Errorf("AgentPool %q has state %q.", name, state))
}
})
}
{{ end -}}
24 changes: 24 additions & 0 deletions mmv1/templates/terraform/constants/go/apigee_instance.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Supress diffs when the lists of project have the same number of entries to handle the case that
// API does not return what the user originally provided. Instead, API does some transformation.
// For example, user provides a list of project number, but API returns a list of project Id.
func projectListDiffSuppress(_, _, _ string, d *schema.ResourceData) bool {
return ProjectListDiffSuppressFunc(d)
}

func ProjectListDiffSuppressFunc(d tpgresource.TerraformResourceDataChange) bool {
kLength := "consumer_accept_list.#"
oldLength, newLength := d.GetChange(kLength)

oldInt, ok := oldLength.(int)
if !ok {
return false
}

newInt, ok := newLength.(int)
if !ok {
return false
}
log.Printf("[DEBUG] - suppressing diff with oldInt %d, newInt %d", oldInt, newInt)

return oldInt == newInt
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Suppress all diff for the field Service Project
func ServiceProjectDiffSuppress(_, _, _ string, _ *schema.ResourceData) bool {
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{{/*
The license inside this block applies to this file
Copyright 2024 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
func upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
o, n := d.GetChange("virtual_repository_config.0.upstream_policies")
oldPolicies, ok := o.([]any)
if !ok {
return false
}
newPolicies, ok := n.([]any)
if !ok {
return false
}

var oldHashes, newHashes []interface{}
for _, policy := range oldPolicies {
data, ok := policy.(map[string]any)
if !ok {
return false
}
hashStr := fmt.Sprintf("[id:%v priority:%v repository:%v]", data["id"], data["priority"], data["repository"])
oldHashes = append(oldHashes, hashStr)
}
for _, policy := range newPolicies {
data, ok := policy.(map[string]any)
if !ok {
return false
}
hashStr := fmt.Sprintf("[id:%v priority:%v repository:%v]", data["id"], data["priority"], data["repository"])
newHashes = append(newHashes, hashStr)
}

oldSet := schema.NewSet(schema.HashString, oldHashes)
newSet := schema.NewSet(schema.HashString, newHashes)
return oldSet.Equal(newSet)
}
158 changes: 158 additions & 0 deletions mmv1/templates/terraform/constants/go/backend_service.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{{/*
The license inside this block applies to this file
Copyright 2024 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
// suppress changes on sample_rate if log_config is set to disabled.
func suppressWhenDisabled(k, old, new string, d *schema.ResourceData) bool {
_, n := d.GetChange("log_config.0.enable")
if tpgresource.IsEmptyValue(reflect.ValueOf(n)) {
return true
}
return false
}

// Whether the backend is a global or regional NEG
func isNegBackend(backend map[string]interface{}) bool {
backendGroup, ok := backend["group"]
if !ok {
return false
}

match, err := regexp.MatchString("(?:global|regions/[^/]+)/networkEndpointGroups", backendGroup.(string))
if err != nil {
// should not happen as long as the regexp pattern compiled correctly
return false
}
return match
}

func resourceGoogleComputeBackendServiceBackendHash(v interface{}) int {
if v == nil {
return 0
}

var buf bytes.Buffer
m := v.(map[string]interface{})
log.Printf("[DEBUG] hashing %v", m)

if group, err := tpgresource.GetRelativePath(m["group"].(string)); err != nil {
log.Printf("[WARN] Error on retrieving relative path of instance group: %s", err)
buf.WriteString(fmt.Sprintf("%s-", m["group"].(string)))
} else {
buf.WriteString(fmt.Sprintf("%s-", group))
}

if v, ok := m["balancing_mode"]; ok {
if v == nil {
v = ""
}

buf.WriteString(fmt.Sprintf("%v-", v))
}
if v, ok := m["capacity_scaler"]; ok {
if v == nil {
v = 0.0
}

// floats can't be added to the hash with %v as the other values are because
// %v and %f are not equivalent strings so this must remain as a float so that
// the hash function doesn't return something else.
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["description"]; ok {
if v == nil {
v = ""
}

log.Printf("[DEBUG] writing description %s", v)
buf.WriteString(fmt.Sprintf("%v-", v))
}
if v, ok := m["max_rate"]; ok {
if v == nil {
v = 0
}

buf.WriteString(fmt.Sprintf("%v-", v))
}
if v, ok := m["max_rate_per_instance"]; ok {
if v == nil {
v = 0.0
}

// floats can't be added to the hash with %v as the other values are because
// %v and %f are not equivalent strings so this must remain as a float so that
// the hash function doesn't return something else.
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["max_connections"]; ok {
if v == nil {
v = 0
}

buf.WriteString(fmt.Sprintf("%v-", v))
}
if v, ok := m["max_connections_per_instance"]; ok {
if v == nil {
v = 0
}

buf.WriteString(fmt.Sprintf("%v-", v))
}
if v, ok := m["max_rate_per_instance"]; ok {
if v == nil {
v = 0.0
}

// floats can't be added to the hash with %v as the other values are because
// %v and %f are not equivalent strings so this must remain as a float so that
// the hash function doesn't return something else.
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["max_connections_per_endpoint"]; ok {
if v == nil {
v = 0
}

buf.WriteString(fmt.Sprintf("%v-", v))
}
if v, ok := m["max_rate_per_endpoint"]; ok {
if v == nil {
v = 0.0
}

// floats can't be added to the hash with %v as the other values are because
// %v and %f are not equivalent strings so this must remain as a float so that
// the hash function doesn't return something else.
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["max_utilization"]; ok && !isNegBackend(m) {
if v == nil {
v = 0.0
}

// floats can't be added to the hash with %v as the other values are because
// %v and %f are not equivalent strings so this must remain as a float so that
// the hash function doesn't return something else.
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}

// This is in region backend service, but not in backend service. Should be a no-op
// if it's not present.
if v, ok := m["failover"]; ok {
if v == nil {
v = false
}
buf.WriteString(fmt.Sprintf("%v-", v.(bool)))
}

log.Printf("[DEBUG] computed hash value of %v from %v", tpgresource.Hashcode(buf.String()), buf.String())
return tpgresource.Hashcode(buf.String())
}
Loading

0 comments on commit 8bc480c

Please sign in to comment.