Skip to content

Commit

Permalink
Set default values for object fields when paring Go yaml files (Googl…
Browse files Browse the repository at this point in the history
…eCloudPlatform#10304)

Co-authored-by: Cameron Thornton <camthornton@google.com>
  • Loading branch information
2 people authored and cmfeng committed Apr 5, 2024
1 parent 7d98060 commit 860ea3c
Show file tree
Hide file tree
Showing 20 changed files with 448 additions and 275 deletions.
22 changes: 13 additions & 9 deletions mmv1/api/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
)

// require 'api/object'
// require 'api/timeout'

// Base class from which other Async classes can inherit.
type Async struct {
// Embed YamlValidator object
Expand Down Expand Up @@ -95,6 +92,13 @@ type OpAsync struct {
Actions []string
}

func NewOpAsync() *OpAsync {
oa := new(OpAsync)
oa.Operation = NewOpAsyncOperation()
oa.Actions = []string{"create", "delete", "update"}
return oa
}

// def initialize(operation, result, status, error)
// super()
// @operation = operation
Expand Down Expand Up @@ -134,12 +138,12 @@ type OpAsyncOperation struct {
}

// def initialize(path, base_url, wait_ms, timeouts)
// super()
// @path = path
// @base_url = base_url
// @wait_ms = wait_ms
// @timeouts = timeouts
// end
func NewOpAsyncOperation() *OpAsyncOperation {
// super()
op := new(OpAsyncOperation)
op.Timeouts = NewTimeouts()
return op
}

// def validate
// super
Expand Down
31 changes: 31 additions & 0 deletions mmv1/api/compiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 Google Inc.
// 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.

package api

import (
"log"
"os"

"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
)

func Compile(yamlPath string, obj interface{}) {
objYaml, err := os.ReadFile(yamlPath)
if err != nil {
log.Fatalf("Cannot open the file: %v", objYaml)
}

yamlValidator := google.YamlValidator{}
yamlValidator.Parse(objYaml, obj)
}
30 changes: 20 additions & 10 deletions mmv1/api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ import (
"log"
"strings"

"gopkg.in/yaml.v3"

"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
"golang.org/x/exp/slices"
)

// require 'api/object'
// require 'api/product/version'
// require 'google/logger'
// require 'compile/core'
// require 'json'

// Represents a product to be managed
type Product struct {
NamedObject `yaml:",inline"`
Expand Down Expand Up @@ -74,15 +70,29 @@ type Product struct {
ClientName string `yaml:"client_name"`
}

func (p *Product) UnmarshalYAML(n *yaml.Node) error {
p.Async = NewOpAsync()

type productAlias Product
aliasObj := (*productAlias)(p)

err := n.Decode(&aliasObj)
if err != nil {
return err
}

p.SetApiName()
p.SetDisplayName()

return nil
}

func (p *Product) Validate() {
// TODO Q1 Rewrite super
// TODO Q2 Rewrite super
// super
for _, o := range p.Objects {
o.ProductMetadata = p
}

p.SetApiName()
p.SetDisplayName()
}

// def validate
Expand Down
28 changes: 24 additions & 4 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/resource"
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
"github.com/GoogleCloudPlatform/magic-modules/mmv1/provider/terraform"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)

type Resource struct {
Expand Down Expand Up @@ -175,9 +175,9 @@ type Resource struct {
// will allow that token to hold multiple /'s.
ImportFormat []string `yaml:"import_format"`

CustomCode terraform.CustomCode `yaml:"custom_code"`
CustomCode resource.CustomCode `yaml:"custom_code"`

Docs terraform.Docs
Docs resource.Docs

// This block inserts entries into the customdiff.All() block in the
// resource schema -- the code for these custom diff functions must
Expand All @@ -190,7 +190,7 @@ type Resource struct {

// Examples in documentation. Backed by generated tests, and have
// corresponding OiCS walkthroughs.
Examples []terraform.Examples
Examples []resource.Examples

// Virtual fields on the Terraform resource. Usage and differences from url_param_only
// are documented in provider/terraform/virtual_fields.rb
Expand Down Expand Up @@ -282,6 +282,26 @@ type Resource struct {
ProductMetadata *Product
}

func (r *Resource) UnmarshalYAML(n *yaml.Node) error {
r.CreateVerb = "POST"
r.ReadVerb = "GET"
r.DeleteVerb = "DELETE"
r.UpdateVerb = "PUT"

type resourceAlias Resource
aliasObj := (*resourceAlias)(r)

err := n.Decode(&aliasObj)
if err != nil {
return err
}

r.ApiName = r.Name
r.CollectionUrlKey = google.Camelize(google.Plural(r.Name), "lower")

return nil
}

// TODO: rewrite functions
func (r *Resource) Validate() {
// TODO Q1 Rewrite super
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package terraform

import (
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
)

// require 'uri'
// require 'api/object'
// require 'compile/core'
// require 'google/golang_utils'
package resource

// Inserts custom code into terraform resources.
type CustomCode struct {
google.YamlValidator
// google.YamlValidator

// Collection of fields allowed in the CustomCode section for
// Terraform.
Expand All @@ -39,9 +30,7 @@ type CustomCode struct {
// Extra Schema Entries go below all other schema entries in the
// resource's Resource.Schema map. They should be formatted as
// entries in the map, e.g. `"foo": &schema.Schema{ ... },`.

// attr_reader :
ExtraSchemaEntry string
ExtraSchemaEntry string `yaml:"extra_schema_entry"`

// ====================
// Encoders & Decoders
Expand All @@ -54,8 +43,6 @@ type CustomCode struct {
// Because the call signature of this function cannot be changed,
// the template will place the function header and closing } for
// you, and your custom code template should *not* include them.

// attr_reader :
Encoder string

// The update encoder is the encoder used in Update - if one is
Expand All @@ -65,16 +52,12 @@ type CustomCode struct {
// Update encoders are only used if object.input is false,
// because when object.input is true, only individual fields
// can be updated - in that case, use a custom expander.

// attr_reader :
UpdateEncoder string
UpdateEncoder string `yaml:"update_encoder"`

// The decoder is the opposite of the encoder - it's called
// after the Read succeeds, rather than before Create / Update
// are called. Like with encoders, the decoder should not
// include the function header or closing }.

// attr_reader :
Decoder string

// =====================
Expand All @@ -84,104 +67,74 @@ type CustomCode struct {
// things like methods that will be referred to by name elsewhere
// (e.g. "fooBarDiffSuppress") and regexes that are necessarily
// exported (e.g. "fooBarValidationRegex").

// attr_reader :
Constants string

// This code is run before the Create call happens. It's placed
// in the Create function, just before the Create call is made.

// attr_reader :
PreCreate string
PreCreate string `yaml:"pre_create"`

// This code is run after the Create call succeeds. It's placed
// in the Create function directly without modification.

// attr_reader :
PostCreate string
PostCreate string `yaml:"post_create"`

// This code is run after the Create call fails before the error is
// returned. It's placed in the Create function directly without
// modification.

// attr_reader :
PostCreateFailure string
PostCreateFailure string `yaml:"post_create_failure"`

// This code replaces the entire contents of the Create call. It
// should be used for resources that don't have normal creation
// semantics that cannot be supported well by other MM features.

// attr_reader :
CustomCreate string
CustomCreate string `yaml:"custom_create"`

// This code is run before the Read call happens. It's placed
// in the Read function.

// attr_reader :
PreRead string
PreRead string `yaml:"pre_read"`

// This code is run before the Update call happens. It's placed
// in the Update function, just after the encoder call, before
// the Update call. Just like the encoder, it is only used if
// object.input is false.

// attr_reader :
PreUpdate string
PreUpdate string `yaml:"pre_update"`

// This code is run after the Update call happens. It's placed
// in the Update function, just after the call succeeds.
// Just like the encoder, it is only used if object.input is
// false.

// attr_reader :
PostUpdate string
PostUpdate string `yaml:"post_update"`

// This code replaces the entire contents of the Update call. It
// should be used for resources that don't have normal update
// semantics that cannot be supported well by other MM features.

// attr_reader :
CustomUpdate string
CustomUpdate string `yaml:"custom_update"`

// This code is run just before the Delete call happens. It's
// useful to prepare an object for deletion, e.g. by detaching
// a disk before deleting it.

// attr_reader :
PreDelete string
PreDelete string `yaml:"pre_delete"`

// This code is run just after the Delete call happens.

// attr_reader :
PostDelete string
PostDelete string `yaml:"post_delete"`

// This code replaces the entire delete method. Since the delete
// method's function header can't be changed, the template
// inserts that for you - do not include it in your custom code.

// attr_reader :
CustomDelete string
CustomDelete string `yaml:"custom_delete"`

// This code replaces the entire import method. Since the import
// method's function header can't be changed, the template
// inserts that for you - do not include it in your custom code.

// attr_reader :
CustomImport string
CustomImport string `yaml:"custom_import"`

// This code is run just after the import method succeeds - it
// is useful for parsing attributes that are necessary for
// the Read() method to succeed.

// attr_reader :
PostImport string
PostImport string `yaml:"post_import"`

// This code is run in the generated test file to check that the
// resource was successfully deleted. Use this if the API responds
// with a success HTTP code for deleted resources

// attr_reader :
TestCheckDestroy string
TestCheckDestroy string `yaml:"test_check_destroy"`
}

// def validate
Expand Down
13 changes: 2 additions & 11 deletions mmv1/provider/terraform/docs.go → mmv1/api/resource/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package terraform

import (
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
)

// require 'uri'
// require 'api/object'
// require 'compile/core'
// require 'google/golang_utils'
package resource

// Inserts custom strings into terraform resource docs.
type Docs struct {
google.YamlValidator
// google.YamlValidator

// All these values should be strings, which will be inserted
// directly into the terraform resource documentation. The
Expand Down
Loading

0 comments on commit 860ea3c

Please sign in to comment.