diff --git a/tpgtools/handwritten.go b/tpgtools/handwritten.go index 79ec036feed8..fb0c1137edab 100644 --- a/tpgtools/handwritten.go +++ b/tpgtools/handwritten.go @@ -74,7 +74,7 @@ func copyHandwrittenFiles(inPath string, outPath string) { } // Write copied file. - err = ioutil.WriteFile(path.Join(outPath, terraformResourceDirectory, "tpgresource", f.Name()), b, 0644) + err = ioutil.WriteFile(path.Join(outPath, terraformResourceDirectory, "tpgdclresource", f.Name()), b, 0644) if err != nil { glog.Exit(err) } diff --git a/tpgtools/handwritten/dcl.go b/tpgtools/handwritten/dcl.go index 88e6faee839e..ad5c5f2ce219 100644 --- a/tpgtools/handwritten/dcl.go +++ b/tpgtools/handwritten/dcl.go @@ -1,4 +1,4 @@ -package tpgresource +package tpgdclresource import ( dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" diff --git a/tpgtools/handwritten/expanders.go b/tpgtools/handwritten/expanders.go index 6285e3f8ad3a..b73a1594d975 100644 --- a/tpgtools/handwritten/expanders.go +++ b/tpgtools/handwritten/expanders.go @@ -1,9 +1,10 @@ -package tpgresource +package tpgdclresource import ( "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-google/google/tpgresource" ) func ExpandStringArray(v interface{}) []string { @@ -14,10 +15,10 @@ func ExpandStringArray(v interface{}) []string { } if arr, ok := v.(*schema.Set); ok { - return ConvertStringSet(arr) + return tpgresource.ConvertStringSet(arr) } - arr = ConvertStringArr(v.([]interface{})) + arr = tpgresource.ConvertStringArr(v.([]interface{})) if arr == nil { // Send empty array specifically instead of nil return make([]string, 0) diff --git a/tpgtools/handwritten/expanders_test.go b/tpgtools/handwritten/expanders_test.go index f2237d714821..4d5ff02ba871 100644 --- a/tpgtools/handwritten/expanders_test.go +++ b/tpgtools/handwritten/expanders_test.go @@ -1,4 +1,4 @@ -package tpgresource +package tpgdclresource import ( "reflect" diff --git a/tpgtools/handwritten/flatteners.go b/tpgtools/handwritten/flatteners.go index f632aa0e47d7..b2bffefe9045 100644 --- a/tpgtools/handwritten/flatteners.go +++ b/tpgtools/handwritten/flatteners.go @@ -1,4 +1,4 @@ -package tpgresource +package tpgdclresource // Returns the terraform representation of a three-state boolean value represented by a pointer to bool in DCL. func FlattenEnumBool(v interface{}) string { diff --git a/tpgtools/handwritten/orgpolicy_utils.go b/tpgtools/handwritten/orgpolicy_utils.go index f103ed4887bb..24e0d70283bf 100644 --- a/tpgtools/handwritten/orgpolicy_utils.go +++ b/tpgtools/handwritten/orgpolicy_utils.go @@ -1,9 +1,10 @@ -package tpgresource +package tpgdclresource import ( "fmt" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-google/google/tpgresource" transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" ) @@ -11,7 +12,7 @@ import ( // to represent the type of parent (e.g. projects/{project_id}). func ResourceOrgPolicyPolicyCustomImport(d *schema.ResourceData, meta interface{}) error { config := meta.(*transport_tpg.Config) - if err := ParseImportId([]string{ + if err := tpgresource.ParseImportId([]string{ "^(?P[^/]+/?[^/]*)/policies/(?P[^/]+)", "^(?P[^/]+/?[^/]*)/(?P[^/]+)", }, d, config); err != nil { @@ -19,7 +20,7 @@ func ResourceOrgPolicyPolicyCustomImport(d *schema.ResourceData, meta interface{ } // Replace import id for the resource id - id, err := ReplaceVarsRecursive(d, config, "{{parent}}/policies/{{name}}", false, 0) + id, err := tpgresource.ReplaceVarsRecursive(d, config, "{{parent}}/policies/{{name}}", false, 0) if err != nil { return fmt.Errorf("Error constructing id: %s", err) } diff --git a/tpgtools/handwritten/tpgtools_utils.go b/tpgtools/handwritten/tpgtools_utils.go index 27fcb6c18bd5..68fa33e1d0e7 100644 --- a/tpgtools/handwritten/tpgtools_utils.go +++ b/tpgtools/handwritten/tpgtools_utils.go @@ -1,4 +1,4 @@ -package tpgresource +package tpgdclresource import ( "fmt" diff --git a/tpgtools/main.go b/tpgtools/main.go index 5cbb67955c9a..1c6dd8e03661 100644 --- a/tpgtools/main.go +++ b/tpgtools/main.go @@ -123,6 +123,12 @@ func main() { return } + // Copy DCL helper files into the folder tpgdclresource to make it easier to remove these files later. + dirPath := path.Join(*oPath, terraformResourceDirectory, "tpgdclresource") + if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { + glog.Error(fmt.Errorf("error creating Terraform tpgdclresource directory %v: %v", dirPath, err)) + } + copyHandwrittenFiles(*cPath, *oPath) } @@ -361,11 +367,9 @@ func loadOverrides(packagePath Filepath, fileName string) Overrides { } func getParentDir(res *Resource) string { - dirPath := path.Join(*oPath, terraformResourceDirectory) - - servicePath := path.Join(dirPath, "services", string(res.Package())) + servicePath := path.Join(*oPath, terraformResourceDirectory, "services", string(res.Package())) if err := os.MkdirAll(servicePath, os.ModePerm); err != nil { - glog.Error(fmt.Errorf("error creating Terraform services directory %v: %v", servicePath, err)) + glog.Error(fmt.Errorf("error creating Terraform the service directory %v: %v", servicePath, err)) } return servicePath } diff --git a/tpgtools/overrides/orgpolicy/beta/policy.yaml b/tpgtools/overrides/orgpolicy/beta/policy.yaml index 0b1e861eb588..c27653ff9807 100644 --- a/tpgtools/overrides/orgpolicy/beta/policy.yaml +++ b/tpgtools/overrides/orgpolicy/beta/policy.yaml @@ -1,6 +1,6 @@ - type: CUSTOM_IMPORT_FUNCTION details: - function: tpgresource.ResourceOrgPolicyPolicyCustomImport + function: tpgdclresource.ResourceOrgPolicyPolicyCustomImport - type: ENUM_BOOL field: spec.rules.allow_all - type: ENUM_BOOL diff --git a/tpgtools/overrides/orgpolicy/policy.yaml b/tpgtools/overrides/orgpolicy/policy.yaml index 0b1e861eb588..c27653ff9807 100644 --- a/tpgtools/overrides/orgpolicy/policy.yaml +++ b/tpgtools/overrides/orgpolicy/policy.yaml @@ -1,6 +1,6 @@ - type: CUSTOM_IMPORT_FUNCTION details: - function: tpgresource.ResourceOrgPolicyPolicyCustomImport + function: tpgdclresource.ResourceOrgPolicyPolicyCustomImport - type: ENUM_BOOL field: spec.rules.allow_all - type: ENUM_BOOL diff --git a/tpgtools/property.go b/tpgtools/property.go index c27aa6528711..0ee26f2af299 100644 --- a/tpgtools/property.go +++ b/tpgtools/property.go @@ -222,7 +222,7 @@ func (p Property) DefaultStateGetter() string { } func (p Property) ChangeStateGetter() string { - return buildGetter(p, fmt.Sprintf("tpgresource.OldValue(d.GetChange(%q))", p.Name())) + return buildGetter(p, fmt.Sprintf("tpgdclresource.OldValue(d.GetChange(%q))", p.Name())) } // Builds a Getter for constructing a shallow @@ -256,7 +256,7 @@ func buildGetter(p Property, rawGetter string) string { return fmt.Sprintf("%s.%sEnumRef(%s.(string))", p.resource.Package(), p.ObjectType(), rawGetter) } if p.EnumBool { - return fmt.Sprintf("tpgresource.ExpandEnumBool(%s.(string))", rawGetter) + return fmt.Sprintf("tpgdclresource.ExpandEnumBool(%s.(string))", rawGetter) } if p.Computed { return fmt.Sprintf("dcl.StringOrNil(%s.(string))", rawGetter) @@ -279,11 +279,11 @@ func buildGetter(p Property, rawGetter string) string { return fmt.Sprintf("expand%s%sArray(%s)", p.resource.PathType(), p.PackagePath(), rawGetter) } if p.Type.typ.Items != nil && p.Type.typ.Items.Type == "string" { - return fmt.Sprintf("tpgresource.ExpandStringArray(%s)", rawGetter) + return fmt.Sprintf("tpgdclresource.ExpandStringArray(%s)", rawGetter) } if p.Type.typ.Items != nil && p.Type.typ.Items.Type == "integer" { - return fmt.Sprintf("tpgresource.ExpandIntegerArray(%s)", rawGetter) + return fmt.Sprintf("tpgdclresource.ExpandIntegerArray(%s)", rawGetter) } if p.Type.typ.Items != nil && len(p.Properties) > 0 { @@ -354,7 +354,7 @@ func (p Property) flattenGetterWithParent(parent string) string { fallthrough case SchemaTypeMap: if p.EnumBool { - return fmt.Sprintf("tpgresource.FlattenEnumBool(%s.%s)", parent, p.PackageName) + return fmt.Sprintf("tpgdclresource.FlattenEnumBool(%s.%s)", parent, p.PackageName) } return fmt.Sprintf("%s.%s", parent, p.PackageName) case SchemaTypeList, SchemaTypeSet: @@ -837,9 +837,9 @@ func createPropertiesFromSchema(schema *openapi.Schema, typeFetcher *TypeFetcher } else { parent = "obj" } - enumBoolSS := fmt.Sprintf("d.Set(%q, tpgresource.FlattenEnumBool(%s.%s))", p.Name(), parent, p.PackageName) + enumBoolSS := fmt.Sprintf("d.Set(%q, tpgdclresource.FlattenEnumBool(%s.%s))", p.Name(), parent, p.PackageName) p.StateSetter = &enumBoolSS - enumBoolSG := fmt.Sprintf("tpgresource.ExpandEnumBool(d.Get(%q))", p.Name()) + enumBoolSG := fmt.Sprintf("tpgdclresource.ExpandEnumBool(d.Get(%q))", p.Name()) p.StateGetter = &enumBoolSG } diff --git a/tpgtools/templates/resource.go.tmpl b/tpgtools/templates/resource.go.tmpl index 42d99ffb3258..532dd4c4c75b 100644 --- a/tpgtools/templates/resource.go.tmpl +++ b/tpgtools/templates/resource.go.tmpl @@ -51,6 +51,7 @@ import( dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" {{$.Package}} "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/{{$.DCLPackage}}" + "github.com/hashicorp/terraform-provider-google/google/tpgdclresource" "github.com/hashicorp/terraform-provider-google/google/tpgresource" transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" ) @@ -258,10 +259,10 @@ should be converted to use the DCL's ID method, so normalization can be uniform. {{- if $.CustomCreateDirectiveFunction }} directive := {{ $.CustomCreateDirectiveFunction }}(obj) {{- else if $.HasCreate }} - directive := tpgresource.CreateDirective + directive := tpgdclresource.CreateDirective {{- else }} {{/* Resource has no create method, so we skip the BlockModification parameter. */}} - directive := tpgresource.UpdateDirective + directive := tpgdclresource.UpdateDirective {{- end }} userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) if err != nil { @@ -383,7 +384,7 @@ func resource{{$.PathType}}Read(d *schema.ResourceData, meta interface{}) error res, err := client.Get{{$.DCLTitle}}(context.Background(), obj) if err != nil { resourceName := fmt.Sprintf("{{$.PathType}} %q", d.Id()) - return tpgresource.HandleNotFoundDCLError(err, d, resourceName) + return tpgdclresource.HandleNotFoundDCLError(err, d, resourceName) } {{ range $v := .Properties -}} @@ -438,7 +439,7 @@ func resource{{$.PathType}}Update(d *schema.ResourceData, meta interface{}) erro defer transport_tpg.MutexStore.Unlock(lockName) {{ end }} - directive := tpgresource.UpdateDirective + directive := tpgdclresource.UpdateDirective {{- if $.StateHint }} directive = append(directive, dcl.WithStateHint(old)) {{- end }}