Skip to content

Commit

Permalink
Go rewrite - add expanders and flatteners to resource.go (GoogleCloud…
Browse files Browse the repository at this point in the history
  • Loading branch information
c2thorn authored May 3, 2024
1 parent 2e80516 commit ccc8cd3
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 31 deletions.
26 changes: 26 additions & 0 deletions mmv1/api/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,29 @@ func (t Type) PropertyNsPrefix() []string {
"Property",
}
}

// "Namespace" - prefix with product and resource - a property with
// information from the "object" variable

func (t Type) NamespaceProperty() string {
name := google.Camelize(t.Name, "lower")
p := t
for p.Parent() != nil {
p = *p.Parent()
name = fmt.Sprintf("%s%s", google.Camelize(p.Name, "lower"), name)
}

return fmt.Sprintf("%s%s%s", google.Camelize(t.ApiName, "lower"), t.ResourceMetadata.Name, name)
}

// def namespace_property_from_object(property, object)
//
// name = property.name.camelize
// until property.parent.nil?
// property = property.parent
// name = property.name.camelize + name
// end
//
// "#{property.__resource.__product.api_name.camelize(:lower)}#{object.name}#{name}"
//
// end
4 changes: 2 additions & 2 deletions mmv1/products/datafusion/go_instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ properties:
description: "The ID of the instance or a fully qualified identifier for the instance."
required: true
immutable: true
custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb'
custom_expand: 'templates/terraform/custom_expand/shortname_to_url.go.erb'
custom_flatten: 'templates/terraform/custom_flatten/go/name_from_self_link.tmpl'
custom_expand: 'templates/terraform/custom_expand/go/shortname_to_url.go.tmpl'
- name: 'description'
type: String
description: "An optional description of the instance."
Expand Down
20 changes: 12 additions & 8 deletions mmv1/provider/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package provider
import (
"bytes"
"errors"
"fmt"
"go/format"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -99,21 +101,23 @@ func NewTemplateData(outputFolder string, version product.Version) *TemplateData
func (td *TemplateData) GenerateResourceFile(filePath string, resource api.Resource) {
templatePath := "templates/terraform/resource.go.tmpl"
templates := []string{
templatePath,
"templates/terraform/schema_property.go.tmpl",
"templates/terraform/schema_subresource.go.tmpl",
templatePath,
"templates/terraform/expand_resource_ref.tmpl",
"templates/terraform/custom_flatten/go/bigquery_table_ref.go.tmpl",
"templates/terraform/flatten_property_method.go.tmpl",
"templates/terraform/expand_property_method.go.tmpl",
}
td.GenerateFile(filePath, templatePath, resource, true, templates...)
}

func (td *TemplateData) GenerateDocumentationFile(filePath string, resource api.Resource) {
templatePath := "templates/terraform/resource.html.markdown.tmpl"
templates := []string{
templatePath,
"templates/terraform/property_documentation.html.markdown.tmpl",
"templates/terraform/nested_property_documentation.html.markdown.tmpl",
templatePath,
}
td.GenerateFile(filePath, templatePath, resource, false, templates...)
}
Expand Down Expand Up @@ -163,12 +167,12 @@ func (td *TemplateData) GenerateFile(filePath, templatePath string, input any, g

sourceByte := contents.Bytes()

// if goFormat {
// sourceByte, err = format.Source(sourceByte)
// if err != nil {
// glog.Error(fmt.Errorf("error formatting %s", filePath))
// }
// }
if goFormat {
sourceByte, err = format.Source(sourceByte)
if err != nil {
glog.Error(fmt.Errorf("error formatting %s", filePath))
}
}

err = os.WriteFile(filePath, sourceByte, 0644)
if err != nil {
Expand Down
14 changes: 0 additions & 14 deletions mmv1/provider/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,20 +743,6 @@ func (t Terraform) replaceImportPath(outputFolder, target string) {
//
// end
//
// # "Namespace" - prefix with product and resource - a property with
// # information from the "object" variable
// def namespace_property_from_object(property, object)
//
// name = property.name.camelize
// until property.parent.nil?
// property = property.parent
// name = property.name.camelize + name
// end
//
// "#{property.__resource.__product.api_name.camelize(:lower)}#{object.name}#{name}"
//
// end
//
// # Converts between the Magic Modules type of an object and its type in the
// # TF schema
// def tf_types
Expand Down
166 changes: 166 additions & 0 deletions mmv1/templates/terraform/expand_property_method.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{{/* The license inside this block applies to this file
Copyright 2024 Google LLC. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */ -}}
{{- define "expandPropertyMethod" }}
{{- if $.CustomExpand }}
{{/* TODO custom flatten */}}
{{- else }}{{/* if $.CustomExpand */}}
{{- if $.IsA "Map" }}
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) {
if v == nil {
return map[string]interface{}{}, nil
}
m := make(map[string]interface{})
for _, raw := range v.(*schema.Set).List() {
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

{{- range $prop := $.NestedProperties }}
{{- if not (eq $prop.Name $prop.KeyName) }}
transformed{{$prop.TitlelizeProperty}}, err := expand{{$.GetPrefix}}{{$.TitlelizeProperty}}{{$prop.TitlelizeProperty}}(original["{{ underscore $prop.Name }}"], d, config)
if err != nil {
return nil, err
{{- if $prop.SendEmptyValue }}
} else {
transformed["{{$prop.ApiName}}"] = transformed{{$prop.TitlelizeProperty}}
{{- else }}
} else if val := reflect.ValueOf(transformed{{$prop.TitlelizeProperty}}); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["{{$prop.ApiName}}"] = transformed{{$prop.TitlelizeProperty}}
{{- end }}
}
{{- end }}
{{ end }}

transformed{{ camelize $.KeyName "upper" }}, err := {{ $.KeyExpander }}(original["{{ underscore $.KeyName }}"], d, config)
if err != nil {
return nil, err
}
m[transformed{{ camelize $.KeyName "upper" }}] = transformed
}
return m, nil
}
{{ else if $.IsA "KeyValuePairs" }}{{/* if $.IsA "Map" */}}
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}
{{ else if $.FlattenObject }}{{/* if $.IsA "Map" */}}
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
transformed := make(map[string]interface{})
{{- range $prop := $.NestedProperties }}
{{- if not (and ($prop.IsA "KeyValuePairs") $prop.IgnoreWrite) }}
transformed{{$prop.TitlelizeProperty}}, err := expand{{$.GetPrefix}}{{$.TitlelizeProperty}}{{$prop.TitlelizeProperty}}({{ if $prop.FlattenObject }}nil{{ else }}d.Get("{{ underscore $prop.Name }}"), d, config)
if err != nil {
return nil, err
{{- if $prop.SendEmptyValue }}
} else {
transformed["{{$prop.ApiName}}"] = transformed{{$prop.TitlelizeProperty}}
{{- else }}
} else if val := reflect.ValueOf(transformed{{$prop.TitlelizeProperty}}); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["{{$prop.ApiName}}"] = transformed{{$prop.TitlelizeProperty}}
{{- end }}
}
{{- end }}
{{- end }}
{{ end }}
return transformed, nil
}
{{ else }}{{/* if $.IsA "Map" */}}
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
{{- if $.IsSet }}
v = v.(*schema.Set).List()
{{- end }}
{{- if $.NestedProperties }}
l := v.([]interface{})
{{- if $.IsA "Array" }}
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
{{- else }}{{/* if $.IsA "Array */}}
{{- if $.AllowEmptyObject }}
if len(l) == 0 {
return nil, nil
}

if l[0] == nil {
transformed := make(map[string]interface{})
return transformed, nil
}
{{- else }}
if len(l) == 0 || l[0] == nil {
return nil, nil
}
{{- end }}
raw := l[0]
original := raw.(map[string]interface{})
{{- end }}{{/* if $.IsA "Array */}}
transformed := make(map[string]interface{})
{{ range $prop := $.NestedProperties }}
{{- if not (and ($prop.IsA "KeyValuePairs") $prop.IgnoreWrite) }}
transformed{{$prop.TitlelizeProperty}}, err := expand{{$.GetPrefix}}{{$.TitlelizeProperty}}{{$prop.TitlelizeProperty}}(original["{{ underscore $prop.Name }}"], d, config)
if err != nil {
return nil, err
{{- if $prop.SendEmptyValue }}
} else {
transformed["{{$prop.ApiName}}] = transformed{{$prop.TitlelizeProperty}}
{{- else }}
} else if val := reflect.ValueOf(transformed{{$prop.TitlelizeProperty}}); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["{{$prop.ApiName}}"] = transformed{{$prop.TitlelizeProperty}}
{{- end }}
}
{{ end }}
{{- end }}
{{- if $.IsA "Array" }}
req = append(req, transformed)
}
return req, nil
{{- else }}
return transformed, nil
{{- end }}
}

{{ else if and ($.IsA "Array") ($.ItemType.IsA "ResourceRef")}}{{/* if $.NestedProperties */}}
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
return nil, fmt.Errorf("Invalid value for {{ underscore $.Name }}: nil")
}
req = append(req, raw.(string))
}
return req, nil
}
{{- else }}
return v, nil
}
{{- end }}{{/* if $.NestedProperties */}}
{{- end }}{{/* if $.IsA "Map" */}}
{{ if $.NestedProperties }}
{{- range $prop := $.NestedProperties }}
{{- if not (and ($prop.IsA "KeyValuePairs") $prop.IgnoreWrite) }}
{{- template "expandPropertyMethod" $prop -}}
{{- end }}
{{- end }}
{{- end }}{{/* if $.NestedProperties */}}
{{- end }}{{/* if $.CustomExpand */}}
{{- end }}{{/* define */}}
Loading

0 comments on commit ccc8cd3

Please sign in to comment.