From 2ffe897885e989537c50534f0eff3965817e1f11 Mon Sep 17 00:00:00 2001 From: Zhenhua Li Date: Fri, 26 May 2023 22:04:09 -0700 Subject: [PATCH 1/2] Move DCL resources to the service package --- tpgtools/handwritten.go | 2 +- tpgtools/handwritten/dcl.go | 2 +- tpgtools/handwritten/expanders.go | 12 +++++------ tpgtools/handwritten/expanders_test.go | 4 ++-- tpgtools/handwritten/flatteners.go | 4 ++-- tpgtools/handwritten/orgpolicy_utils.go | 9 ++++----- tpgtools/handwritten/tpgtools_utils.go | 6 +++--- tpgtools/main.go | 13 +++++++++++- .../cloudbuild/beta/worker_pool.yaml | 2 +- .../overrides/cloudbuild/worker_pool.yaml | 2 +- .../compute/beta/firewall_policy_rule.yaml | 2 +- .../compute/firewall_policy_rule.yaml | 2 +- tpgtools/overrides/orgpolicy/beta/policy.yaml | 2 +- tpgtools/overrides/orgpolicy/policy.yaml | 2 +- tpgtools/property.go | 20 +++++++++---------- .../templates/provider_dcl_resources.go.tmpl | 7 ++++++- tpgtools/templates/resource.go.tmpl | 10 +++++----- 17 files changed, 58 insertions(+), 43 deletions(-) diff --git a/tpgtools/handwritten.go b/tpgtools/handwritten.go index 48b6f99cb858..79ec036feed8 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, f.Name()), b, 0644) + err = ioutil.WriteFile(path.Join(outPath, terraformResourceDirectory, "tpgresource", f.Name()), b, 0644) if err != nil { glog.Exit(err) } diff --git a/tpgtools/handwritten/dcl.go b/tpgtools/handwritten/dcl.go index 4ec72490b7f8..88e6faee839e 100644 --- a/tpgtools/handwritten/dcl.go +++ b/tpgtools/handwritten/dcl.go @@ -1,4 +1,4 @@ -package google +package tpgresource import ( dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" diff --git a/tpgtools/handwritten/expanders.go b/tpgtools/handwritten/expanders.go index 3f47248811a9..6285e3f8ad3a 100644 --- a/tpgtools/handwritten/expanders.go +++ b/tpgtools/handwritten/expanders.go @@ -1,4 +1,4 @@ -package google +package tpgresource import ( "strings" @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func expandStringArray(v interface{}) []string { +func ExpandStringArray(v interface{}) []string { arr, ok := v.([]string) if ok { @@ -14,10 +14,10 @@ func expandStringArray(v interface{}) []string { } if arr, ok := v.(*schema.Set); ok { - return convertStringSet(arr) + return ConvertStringSet(arr) } - arr = convertStringArr(v.([]interface{})) + arr = ConvertStringArr(v.([]interface{})) if arr == nil { // Send empty array specifically instead of nil return make([]string, 0) @@ -25,7 +25,7 @@ func expandStringArray(v interface{}) []string { return arr } -func expandIntegerArray(v interface{}) []int64 { +func ExpandIntegerArray(v interface{}) []int64 { arr, ok := v.([]int64) if ok { @@ -52,7 +52,7 @@ func convertIntegerArr(v []interface{}) []int64 { } // Returns the DCL representation of a three-state boolean value represented by a string in terraform. -func expandEnumBool(v interface{}) *bool { +func ExpandEnumBool(v interface{}) *bool { s, ok := v.(string) if !ok { return nil diff --git a/tpgtools/handwritten/expanders_test.go b/tpgtools/handwritten/expanders_test.go index e3170fe2b6d2..f2237d714821 100644 --- a/tpgtools/handwritten/expanders_test.go +++ b/tpgtools/handwritten/expanders_test.go @@ -1,4 +1,4 @@ -package google +package tpgresource import ( "reflect" @@ -66,7 +66,7 @@ func TestExpandEnumBool(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - if got, want := expandEnumBool(tc.input), tc.exp; !reflect.DeepEqual(got, want) { + if got, want := ExpandEnumBool(tc.input), tc.exp; !reflect.DeepEqual(got, want) { t.Errorf("expected %v to be %v", got, want) } }) diff --git a/tpgtools/handwritten/flatteners.go b/tpgtools/handwritten/flatteners.go index 707d823b112e..f632aa0e47d7 100644 --- a/tpgtools/handwritten/flatteners.go +++ b/tpgtools/handwritten/flatteners.go @@ -1,7 +1,7 @@ -package google +package tpgresource // Returns the terraform representation of a three-state boolean value represented by a pointer to bool in DCL. -func flattenEnumBool(v interface{}) string { +func FlattenEnumBool(v interface{}) string { b, ok := v.(*bool) if !ok || b == nil { return "" diff --git a/tpgtools/handwritten/orgpolicy_utils.go b/tpgtools/handwritten/orgpolicy_utils.go index f39af61946a8..f103ed4887bb 100644 --- a/tpgtools/handwritten/orgpolicy_utils.go +++ b/tpgtools/handwritten/orgpolicy_utils.go @@ -1,18 +1,17 @@ -package google +package tpgresource 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" ) // OrgPolicyPolicy has a custom import method because the parent field needs to allow an additional forward slash // to represent the type of parent (e.g. projects/{project_id}). -func resourceOrgPolicyPolicyCustomImport(d *schema.ResourceData, meta interface{}) error { +func ResourceOrgPolicyPolicyCustomImport(d *schema.ResourceData, meta interface{}) error { config := meta.(*transport_tpg.Config) - if err := tpgresource.ParseImportId([]string{ + if err := ParseImportId([]string{ "^(?P[^/]+/?[^/]*)/policies/(?P[^/]+)", "^(?P[^/]+/?[^/]*)/(?P[^/]+)", }, d, config); err != nil { @@ -20,7 +19,7 @@ func resourceOrgPolicyPolicyCustomImport(d *schema.ResourceData, meta interface{ } // Replace import id for the resource id - id, err := tpgresource.ReplaceVarsRecursive(d, config, "{{parent}}/policies/{{name}}", false, 0) + id, err := 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 cdcb287d27b1..27fcb6c18bd5 100644 --- a/tpgtools/handwritten/tpgtools_utils.go +++ b/tpgtools/handwritten/tpgtools_utils.go @@ -1,4 +1,4 @@ -package google +package tpgresource import ( "fmt" @@ -9,11 +9,11 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func oldValue(old, new interface{}) interface{} { +func OldValue(old, new interface{}) interface{} { return old } -func handleNotFoundDCLError(err error, d *schema.ResourceData, resourceName string) error { +func HandleNotFoundDCLError(err error, d *schema.ResourceData, resourceName string) error { if dcl.IsNotFound(err) { log.Printf("[WARN] Removing %s because it's gone", resourceName) // The resource doesn't exist anymore diff --git a/tpgtools/main.go b/tpgtools/main.go index 16d2e72bc177..5cbb67955c9a 100644 --- a/tpgtools/main.go +++ b/tpgtools/main.go @@ -360,6 +360,16 @@ func loadOverrides(packagePath Filepath, fileName string) Overrides { return overrides } +func getParentDir(res *Resource) string { + dirPath := path.Join(*oPath, terraformResourceDirectory) + + servicePath := path.Join(dirPath, "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)) + } + return servicePath +} + func generateResourceFile(res *Resource) { // Generate resource file tmplInput := ResourceInput{ @@ -391,7 +401,8 @@ func generateResourceFile(res *Resource) { fmt.Printf("%v", string(formatted)) } else { outname := fmt.Sprintf("resource_%s_%s.go", res.ProductName(), res.Name()) - err := ioutil.WriteFile(path.Join(*oPath, terraformResourceDirectory, outname), formatted, 0644) + parentDir := getParentDir(res) + err = ioutil.WriteFile(path.Join(parentDir, outname), formatted, 0644) if err != nil { glog.Exit(err) } diff --git a/tpgtools/overrides/cloudbuild/beta/worker_pool.yaml b/tpgtools/overrides/cloudbuild/beta/worker_pool.yaml index e077a3db6f77..4deb00daaee1 100644 --- a/tpgtools/overrides/cloudbuild/beta/worker_pool.yaml +++ b/tpgtools/overrides/cloudbuild/beta/worker_pool.yaml @@ -1,6 +1,6 @@ - type: DIFF_SUPPRESS_FUNC field: network_config.peered_network details: - diffsuppressfunc: compareResourceNames + diffsuppressfunc: tpgresource.CompareResourceNames - type: EXCLUDE field: etag diff --git a/tpgtools/overrides/cloudbuild/worker_pool.yaml b/tpgtools/overrides/cloudbuild/worker_pool.yaml index e077a3db6f77..4deb00daaee1 100644 --- a/tpgtools/overrides/cloudbuild/worker_pool.yaml +++ b/tpgtools/overrides/cloudbuild/worker_pool.yaml @@ -1,6 +1,6 @@ - type: DIFF_SUPPRESS_FUNC field: network_config.peered_network details: - diffsuppressfunc: compareResourceNames + diffsuppressfunc: tpgresource.CompareResourceNames - type: EXCLUDE field: etag diff --git a/tpgtools/overrides/compute/beta/firewall_policy_rule.yaml b/tpgtools/overrides/compute/beta/firewall_policy_rule.yaml index 09206fdc5ec8..e743d253be12 100644 --- a/tpgtools/overrides/compute/beta/firewall_policy_rule.yaml +++ b/tpgtools/overrides/compute/beta/firewall_policy_rule.yaml @@ -1,4 +1,4 @@ - type: DIFF_SUPPRESS_FUNC field: target_resources details: - diffsuppressfunc: compareSelfLinkOrResourceName + diffsuppressfunc: tpgresource.CompareSelfLinkOrResourceName diff --git a/tpgtools/overrides/compute/firewall_policy_rule.yaml b/tpgtools/overrides/compute/firewall_policy_rule.yaml index 09206fdc5ec8..e743d253be12 100644 --- a/tpgtools/overrides/compute/firewall_policy_rule.yaml +++ b/tpgtools/overrides/compute/firewall_policy_rule.yaml @@ -1,4 +1,4 @@ - type: DIFF_SUPPRESS_FUNC field: target_resources details: - diffsuppressfunc: compareSelfLinkOrResourceName + diffsuppressfunc: tpgresource.CompareSelfLinkOrResourceName diff --git a/tpgtools/overrides/orgpolicy/beta/policy.yaml b/tpgtools/overrides/orgpolicy/beta/policy.yaml index 495fab27c0c2..0b1e861eb588 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: resourceOrgPolicyPolicyCustomImport + function: tpgresource.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 495fab27c0c2..0b1e861eb588 100644 --- a/tpgtools/overrides/orgpolicy/policy.yaml +++ b/tpgtools/overrides/orgpolicy/policy.yaml @@ -1,6 +1,6 @@ - type: CUSTOM_IMPORT_FUNCTION details: - function: resourceOrgPolicyPolicyCustomImport + function: tpgresource.ResourceOrgPolicyPolicyCustomImport - type: ENUM_BOOL field: spec.rules.allow_all - type: ENUM_BOOL diff --git a/tpgtools/property.go b/tpgtools/property.go index 19f9df439b61..c27aa6528711 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("oldValue(d.GetChange(%q))", p.Name())) + return buildGetter(p, fmt.Sprintf("tpgresource.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("expandEnumBool(%s.(string))", rawGetter) + return fmt.Sprintf("tpgresource.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("expandStringArray(%s)", rawGetter) + return fmt.Sprintf("tpgresource.ExpandStringArray(%s)", rawGetter) } if p.Type.typ.Items != nil && p.Type.typ.Items.Type == "integer" { - return fmt.Sprintf("expandIntegerArray(%s)", rawGetter) + return fmt.Sprintf("tpgresource.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("flattenEnumBool(%s.%s)", parent, p.PackageName) + return fmt.Sprintf("tpgresource.FlattenEnumBool(%s.%s)", parent, p.PackageName) } return fmt.Sprintf("%s.%s", parent, p.PackageName) case SchemaTypeList, SchemaTypeSet: @@ -396,7 +396,7 @@ func (p Property) DefaultDiffSuppress() *string { case SchemaTypeString: // Field is reference to another resource if _, ok := p.typ.Extension["x-dcl-references"]; ok { - dsf := "compareSelfLinkOrResourceName" + dsf := "tpgresource.CompareSelfLinkOrResourceName" return &dsf } } @@ -629,7 +629,7 @@ func createPropertiesFromSchema(schema *openapi.Schema, typeFetcher *TypeFetcher i := Type{typ: v.Items} e := fmt.Sprintf("&schema.Schema{Type: schema.%s}", i.String()) if _, ok := v.Extension["x-dcl-references"]; ok { - e = fmt.Sprintf("&schema.Schema{Type: schema.%s, DiffSuppressFunc: compareSelfLinkOrResourceName, }", i.String()) + e = fmt.Sprintf("&schema.Schema{Type: schema.%s, DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, }", i.String()) } p.Elem = &e p.ElemIsBasicType = true @@ -697,7 +697,7 @@ func createPropertiesFromSchema(schema *openapi.Schema, typeFetcher *TypeFetcher if p.customName != "" { propertyName = p.customName } - ig := fmt.Sprintf("get%s(d, config)", renderSnakeAsTitle(miscellaneousNameSnakeCase(propertyName))) + ig := fmt.Sprintf("tpgresource.Get%s(d, config)", renderSnakeAsTitle(miscellaneousNameSnakeCase(propertyName))) if cigOk { ig = fmt.Sprintf("%s(d, config)", cig.Function) } @@ -837,9 +837,9 @@ func createPropertiesFromSchema(schema *openapi.Schema, typeFetcher *TypeFetcher } else { parent = "obj" } - enumBoolSS := fmt.Sprintf("d.Set(%q, flattenEnumBool(%s.%s))", p.Name(), parent, p.PackageName) + enumBoolSS := fmt.Sprintf("d.Set(%q, tpgresource.FlattenEnumBool(%s.%s))", p.Name(), parent, p.PackageName) p.StateSetter = &enumBoolSS - enumBoolSG := fmt.Sprintf("expandEnumBool(d.Get(%q))", p.Name()) + enumBoolSG := fmt.Sprintf("tpgresource.ExpandEnumBool(d.Get(%q))", p.Name()) p.StateGetter = &enumBoolSG } diff --git a/tpgtools/templates/provider_dcl_resources.go.tmpl b/tpgtools/templates/provider_dcl_resources.go.tmpl index a39a0cebd4a6..7b6f3520bf37 100644 --- a/tpgtools/templates/provider_dcl_resources.go.tmpl +++ b/tpgtools/templates/provider_dcl_resources.go.tmpl @@ -33,12 +33,17 @@ package google import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + {{- range $res := . }} + {{- if not $res.SkipInProvider }} + "github.com/hashicorp/terraform-provider-google/google/services/{{$res.Package}}" + {{- end }} + {{- end }} ) var dclResources = map[string]*schema.Resource{ {{- range $res := . }} {{- if not $res.SkipInProvider }} - "{{$res.TerraformName}}": Resource{{$res.PathType}}(), + "{{$res.TerraformName}}": {{$res.Package}}.Resource{{$res.PathType}}(), {{- end }} {{- end }} } diff --git a/tpgtools/templates/resource.go.tmpl b/tpgtools/templates/resource.go.tmpl index 5fa6bcd6149c..42d99ffb3258 100644 --- a/tpgtools/templates/resource.go.tmpl +++ b/tpgtools/templates/resource.go.tmpl @@ -29,7 +29,7 @@ // // ---------------------------------------------------------------------------- -package google +package {{$.Package}} import( "context" @@ -258,10 +258,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 := CreateDirective + directive := tpgresource.CreateDirective {{- else }} {{/* Resource has no create method, so we skip the BlockModification parameter. */}} - directive := UpdateDirective + directive := tpgresource.UpdateDirective {{- end }} userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) if err != nil { @@ -383,7 +383,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 handleNotFoundDCLError(err, d, resourceName) + return tpgresource.HandleNotFoundDCLError(err, d, resourceName) } {{ range $v := .Properties -}} @@ -438,7 +438,7 @@ func resource{{$.PathType}}Update(d *schema.ResourceData, meta interface{}) erro defer transport_tpg.MutexStore.Unlock(lockName) {{ end }} - directive := UpdateDirective + directive := tpgresource.UpdateDirective {{- if $.StateHint }} directive = append(directive, dcl.WithStateHint(old)) {{- end }} From 035b8e78feebd854528138257d941460d065a2a8 Mon Sep 17 00:00:00 2001 From: Zhenhua Li Date: Wed, 31 May 2023 09:55:36 -0700 Subject: [PATCH 2/2] Copy DCL helper files into the folder tpgdclresource --- tpgtools/handwritten.go | 2 +- tpgtools/handwritten/dcl.go | 2 +- tpgtools/handwritten/expanders.go | 7 ++++--- tpgtools/handwritten/expanders_test.go | 2 +- tpgtools/handwritten/flatteners.go | 2 +- tpgtools/handwritten/orgpolicy_utils.go | 7 ++++--- tpgtools/handwritten/tpgtools_utils.go | 2 +- tpgtools/main.go | 12 ++++++++---- tpgtools/overrides/orgpolicy/beta/policy.yaml | 2 +- tpgtools/overrides/orgpolicy/policy.yaml | 2 +- tpgtools/property.go | 14 +++++++------- tpgtools/templates/resource.go.tmpl | 9 +++++---- 12 files changed, 35 insertions(+), 28 deletions(-) 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 }}