Skip to content

Commit

Permalink
Support provider default labels for DCL resources
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed Sep 8, 2023
1 parent 186acac commit 03973f9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func TestAccDataprocWorkflowTemplate_basic(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
// The "labels" field in the state are decided by the configuration.
// During importing, as the configuration is unavailableafter, the "labels" field in the state will be empty.
// During importing, as the configuration is unavailable, the "labels" field in the state will be empty.
// So add the "labels" to the ImportStateVerifyIgnore list.
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
ResourceName: "google_dataproc_workflow_template.template",
},
},
Expand Down
36 changes: 35 additions & 1 deletion tpgtools/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ func (p Property) IsResourceAnnotations() bool {
return p.Name() == "annotations" && p.parent == nil
}

func (p Property) ShouldShowUpInSamples() bool {
return (p.Settable && p.Name() != "effective_labels" && p.Name() != "effective_annotations") || p.IsResourceLabels() || p.IsResourceAnnotations()
}

// collapsedProperties returns the input list of properties with nested objects collapsed if needed.
func collapsedProperties(props []Property) (collapsed []Property) {
for _, v := range props {
Expand Down Expand Up @@ -897,7 +901,14 @@ func createPropertiesFromSchema(schema *openapi.Schema, typeFetcher *TypeFetcher
note := "**Note**: This field is non-authoritative, and will only manage the labels present in your configuration. " +
"Please refer to the field `effective_labels` for all of the labels present on the resource."
p.Description = fmt.Sprintf("%s\n\n%s", p.Description, note)
p.Settable = false
p.StateGetter = nil

props = append(props, build_effective_labels_field(p, resource, parent))

if p.IsResourceLabels() {
props = append(props, build_terraform_labels_field(p, resource, parent))
}
}

props = append(props, p)
Expand Down Expand Up @@ -929,14 +940,37 @@ func build_effective_labels_field(p Property, resource *Resource, parent *Proper
description := fmt.Sprintf("All of %s (key/value pairs) present on the resource in GCP, including the %s configured through Terraform, other clients and services.", p.title, p.title)
stateSetter := fmt.Sprintf("d.Set(%q, res.%s)", title, p.PackageName)

return Property{
effectiveLabels := Property{
title: title,
Type: p.Type,
Description: description,
resource: resource,
parent: parent,
Optional: false,
Computed: true,
PackageName: p.PackageName,
Settable: true,
StateSetter: &stateSetter,
}

stateGetter := effectiveLabels.DefaultStateGetter()
effectiveLabels.StateGetter = &stateGetter
return effectiveLabels
}

func build_terraform_labels_field(p Property, resource *Resource, parent *Property) Property {
title := fmt.Sprintf("terraform_%s", p.title)
description := fmt.Sprintf("The combination of %s configured directly on the resource and default %s configured on the provider.", p.title, p.title)
stateSetter := fmt.Sprintf("d.Set(%q, flatten%sTerraform%s(res.%s, d))", title, p.resource.PathType(), p.PackagePath(), p.PackageName)

return Property{
title: title,
Type: p.Type,
Description: description,
resource: resource,
parent: parent,
Computed: true,
PackageName: p.PackageName,
StateSetter: &stateSetter,
}
}
13 changes: 11 additions & 2 deletions tpgtools/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ func createResource(schema *openapi.Schema, info *openapi.Info, typeFetcher *Typ
res.CustomizeDiff = cdiff.Functions
}

if res.HasLabels() {
res.CustomizeDiff = append(res.CustomizeDiff, "tpgresource.SetLabelsDiff")
} else if res.HasAnnotations() {
res.CustomizeDiff = append(res.CustomizeDiff, "tpgresource.SetAnnotationsDiff")
}

// ListFields
if parameters, ok := typeFetcher.doc.Paths["list"]; ok {
for _, param := range parameters.Parameters {
Expand Down Expand Up @@ -835,7 +841,7 @@ func (r *Resource) loadHandWrittenSamples() []Sample {
// During importing, as the configuration is unavailableafter, the "labels" and "annotations" fields in the state will be empty.
// So add the "labels" and the "annotations" fields to the ImportStateVerifyIgnore list.
if r.HasLabels() {
sample.IgnoreRead = append(sample.IgnoreRead, "labels")
sample.IgnoreRead = append(sample.IgnoreRead, "labels", "terraform_labels")
}

if r.HasAnnotations() {
Expand Down Expand Up @@ -875,6 +881,7 @@ func (r *Resource) loadDCLSamples() []Sample {
continue
}
sample := Sample{}
glog.Infof("samples %#v", sample)
sampleOGFilePath := path.Join(string(samplesPath), file.Name())
var tc []byte
if pathExists(sampleFriendlyMetaPath) {
Expand All @@ -892,6 +899,8 @@ func (r *Resource) loadDCLSamples() []Sample {
glog.Exit(err)
}

glog.Infof("samples 1 %#v", sample)

versionMatch := false
for _, v := range sample.Versions {
if v == version {
Expand Down Expand Up @@ -930,7 +939,7 @@ func (r *Resource) loadDCLSamples() []Sample {
sample.TestSlug = RenderedString(sampleNameToTitleCase(*sample.Name).titlecase())

// The "labels" and "annotations" fields in the state are decided by the configuration.
// During importing, as the configuration is unavailableafter, the "labels" and "annotations" fields in the state will be empty.
// During importing, as the configuration is unavailable, the "labels" and "annotations" fields in the state will be empty.
// So add the "labels" and the "annotations" fields to the ImportStateVerifyIgnore list.
if r.HasLabels() {
sample.IgnoreRead = append(sample.IgnoreRead, "labels")
Expand Down
7 changes: 7 additions & 0 deletions tpgtools/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ func BuildDependency(fileName string, product SnakeCaseProductName, localname, v
}

block, err := ConvertSampleJSONToHCL(packageName, resourceName, version, hasGAEquivalent, b)
glog.Infof("block 0 %#v", block)

if err != nil {
glog.Errorf("failed to convert %q", fileName)
return nil, fmt.Errorf("Error generating sample dependency %s: %s", fileName, err)
Expand All @@ -204,6 +206,8 @@ func BuildDependency(fileName string, product SnakeCaseProductName, localname, v
re := regexp.MustCompile(`(resource "` + terraformResourceType + `" ")(\w*)`)
block = re.ReplaceAllString(block, "${1}"+localname)

glog.Infof("block 1 %#v", block)

d := &Dependency{
FileName: fileName,
HCLLocalName: localname,
Expand All @@ -218,6 +222,9 @@ func (s *Sample) generateSampleDependency(fileName string) Dependency {
}

func (s *Sample) generateSampleDependencyWithName(fileName, localname string) Dependency {
glog.Infof("fileName 1 %#v", fileName)
glog.Infof("s.SamplesPath 1 %#v", s.SamplesPath)

dFileNameParts := strings.Split(fileName, "samples/")
fileName = dFileNameParts[len(dFileNameParts)-1]
dependencyBytes, err := ioutil.ReadFile(path.Join(string(s.SamplesPath), fileName))
Expand Down
19 changes: 17 additions & 2 deletions tpgtools/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,22 @@ func flatten{{$.PathType}}Labels(v map[string]string, d *schema.ResourceData) in
transformed := make(map[string]interface{})
if l, ok := d.Get("labels").(map[string]interface{}); ok {
for k, _ := range l {
transformed[k] = l[k]
transformed[k] = v[k]
}
}

return transformed
}

func flatten{{$.PathType}}TerraformLabels(v map[string]string, d *schema.ResourceData) interface{} {
if v == nil {
return nil
}

transformed := make(map[string]interface{})
if l, ok := d.Get("terraform_labels").(map[string]interface{}); ok {
for k, _ := range l {
transformed[k] = v[k]
}
}

Expand All @@ -740,7 +755,7 @@ func flatten{{$.PathType}}Annotations(v map[string]string, d *schema.ResourceDat
transformed := make(map[string]interface{})
if l, ok := d.Get("annotations").(map[string]interface{}); ok {
for k, _ := range l {
transformed[k] = l[k]
transformed[k] = v[k]
}
}

Expand Down
2 changes: 1 addition & 1 deletion tpgtools/templates/serialization.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func ConvertSampleJSONToHCL(product DCLPackageName, resource miscellaneousNameSn
func {{ $res.TitleCaseFullName }}{{$version.SerializationSuffix}}AsHCL(r {{$res.Package}}{{$version.SerializationSuffix}}.{{$res.DCLStructName}}, hasGAEquivalent bool) (string, error) {
outputConfig := "resource \"{{$res.TerraformName}}\" \"output\" {\n"
{{- range $field := $res.Properties}}
{{- if $field.Settable }}
{{- if $field.ShouldShowUpInSamples }}
{{- if eq $field.Type.String "TypeString" "TypeInt" "TypeBool" "TypeFloat" }}
{{- if $field.Type.IsDateTime }}
if !r.{{$field.PackageName}}.IsZero() {
Expand Down

0 comments on commit 03973f9

Please sign in to comment.