Skip to content

Commit

Permalink
Copy DCL helper files into the folder tpgdclresource
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed May 31, 2023
1 parent 2ffe897 commit 035b8e7
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion tpgtools/handwritten.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tpgtools/handwritten/dcl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tpgresource
package tpgdclresource

import (
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
Expand Down
7 changes: 4 additions & 3 deletions tpgtools/handwritten/expanders.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tpgtools/handwritten/expanders_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tpgresource
package tpgdclresource

import (
"reflect"
Expand Down
2 changes: 1 addition & 1 deletion tpgtools/handwritten/flatteners.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions tpgtools/handwritten/orgpolicy_utils.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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"
)

// 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 {
config := meta.(*transport_tpg.Config)
if err := ParseImportId([]string{
if err := tpgresource.ParseImportId([]string{
"^(?P<parent>[^/]+/?[^/]*)/policies/(?P<name>[^/]+)",
"^(?P<parent>[^/]+/?[^/]*)/(?P<name>[^/]+)",
}, d, config); err != nil {
return err
}

// 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)
}
Expand Down
2 changes: 1 addition & 1 deletion tpgtools/handwritten/tpgtools_utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tpgresource
package tpgdclresource

import (
"fmt"
Expand Down
12 changes: 8 additions & 4 deletions tpgtools/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion tpgtools/overrides/orgpolicy/beta/policy.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tpgtools/overrides/orgpolicy/policy.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 7 additions & 7 deletions tpgtools/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 5 additions & 4 deletions tpgtools/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 -}}
Expand Down Expand Up @@ -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 }}
Expand Down

0 comments on commit 035b8e7

Please sign in to comment.