Skip to content

Commit

Permalink
Remove constants from CAI (#328)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Riley Karson <[email protected]>
  • Loading branch information
modular-magician and rileykarson committed Jan 22, 2020
1 parent 01e808d commit bcfa887
Show file tree
Hide file tree
Showing 17 changed files with 4 additions and 839 deletions.
20 changes: 1 addition & 19 deletions google/appengine_domain_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,7 @@

package google

import (
"reflect"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func sslSettingsDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// If certificate id is empty, and ssl management type is `MANUAL`, then
// ssl settings will not be configured, and ssl_settings block is not returned

if k == "ssl_settings.#" &&
old == "0" && new == "1" &&
d.Get("ssl_settings.0.certificate_id") == "" &&
d.Get("ssl_settings.0.ssl_management_type") == "MANUAL" {
return true
}

return false
}
import "reflect"

func GetAppEngineDomainMappingCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//appengine.googleapis.com/apps/{{project}}/domainMappings/{{domain_name}}")
Expand Down
28 changes: 0 additions & 28 deletions google/bigquery_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,11 @@
package google

import (
"fmt"
"reflect"
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

const datasetIdRegexp = `[0-9A-Za-z_]+`

func validateDatasetId(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(datasetIdRegexp).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_)", k))
}

if len(value) > 1024 {
errors = append(errors, fmt.Errorf(
"%q cannot be greater than 1,024 characters", k))
}

return
}

func validateDefaultTableExpirationMs(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 3600000 {
errors = append(errors, fmt.Errorf("%q cannot be shorter than 3600000 milliseconds (one hour)", k))
}

return
}

func GetBigQueryDatasetCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//bigquery.googleapis.com/projects/{{project}}/datasets/{{dataset_id}}")
if err != nil {
Expand Down
15 changes: 0 additions & 15 deletions google/binaryauthorization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func defaultBinaryAuthorizationPolicy(project string) map[string]interface{} {
return map[string]interface{}{
"name": fmt.Sprintf("projects/%s/policy", project),
"admissionWhitelistPatterns": []interface{}{
map[string]interface{}{
"namePattern": "gcr.io/google_containers/*",
},
},
"defaultAdmissionRule": map[string]interface{}{
"evaluationMode": "ALWAYS_ALLOW",
"enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG",
},
}
}

func GetBinaryAuthorizationPolicyCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//binaryauthorization.googleapis.com/projects/{{project}}/policy")
if err != nil {
Expand Down
45 changes: 1 addition & 44 deletions google/cloudbuild_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,7 @@

package google

import (
"fmt"
"reflect"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func stepTimeoutCustomizeDiff(diff *schema.ResourceDiff, v interface{}) error {
buildList := diff.Get("build").([]interface{})
if len(buildList) == 0 || buildList[0] == nil {
return nil
}
build := buildList[0].(map[string]interface{})
buildTimeoutString := build["timeout"].(string)

buildTimeout, err := time.ParseDuration(buildTimeoutString)
if err != nil {
return fmt.Errorf("Error parsing build timeout : %s", err)
}

var stepTimeoutSum time.Duration = 0
steps := build["step"].([]interface{})
for _, rawstep := range steps {
if rawstep == nil {
continue
}
step := rawstep.(map[string]interface{})
timeoutString := step["timeout"].(string)
if len(timeoutString) == 0 {
continue
}

timeout, err := time.ParseDuration(timeoutString)
if err != nil {
return fmt.Errorf("Error parsing build step timeout: %s", err)
}
stepTimeoutSum += timeout
}
if stepTimeoutSum > buildTimeout {
return fmt.Errorf("Step timeout sum (%v) cannot be greater than build timeout (%v)", stepTimeoutSum, buildTimeout)
}
return nil
}
import "reflect"

func GetCloudBuildTriggerCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//cloudbuild.googleapis.com/projects/{{project}}/triggers/{{trigger_id}}")
Expand Down
57 changes: 1 addition & 56 deletions google/cloudscheduler_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,7 @@

package google

import (
"fmt"
"reflect"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

// Both oidc and oauth headers cannot be set
func validateAuthHeaders(diff *schema.ResourceDiff, v interface{}) error {
httpBlock := diff.Get("http_target.0").(map[string]interface{})

if httpBlock != nil {
oauth := httpBlock["oauth_token"]
oidc := httpBlock["oidc_token"]

if oauth != nil && oidc != nil {
if len(oidc.([]interface{})) > 0 && len(oauth.([]interface{})) > 0 {
return fmt.Errorf("Error in http_target: only one of oauth_token or oidc_token can be specified, but not both.")
}
}
}

return nil
}

func authHeaderDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// If generating an `oauth_token` and `scope` is not provided in the configuration,
// the default "https://www.googleapis.com/auth/cloud-platform" scope will be used.
// Similarly, if generating an `oidc_token` and `audience` is not provided in the
// configuration, the URI specified in target will be used. Although not in the
// configuration, in both cases the default is returned in the object, but is not in.
// state. We suppress the diff if the values are these defaults but are not stored in state.

b := strings.Split(k, ".")
if b[0] == "http_target" && len(b) > 4 {
block := b[2]
attr := b[4]

if block == "oauth_token" && attr == "scope" {
if old == canonicalizeServiceScope("cloud-platform") && new == "" {
return true
}
}

if block == "oidc_token" && attr == "audience" {
uri := d.Get(strings.Join(b[0:2], ".") + ".uri")
if old == uri && new == "" {
return true
}
}

}

return false
}
import "reflect"

func GetCloudSchedulerJobCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//cloudscheduler.googleapis.com/projects/{{project}}/locations/{{region}}/jobs/{{name}}")
Expand Down
117 changes: 0 additions & 117 deletions google/compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,128 +15,11 @@
package google

import (
"bytes"
"fmt"
"log"
"reflect"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

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 := 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)))
}

// 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", hashcode.String(buf.String()), buf.String())
return hashcode.String(buf.String())
}

func GetComputeBackendServiceCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//compute.googleapis.com/projects/{{project}}/global/backendServices/{{name}}")
if err != nil {
Expand Down
Loading

0 comments on commit bcfa887

Please sign in to comment.