Skip to content

Commit

Permalink
Add a method for converting structs -> maps when bridging MM -> TF (h…
Browse files Browse the repository at this point in the history
…ashicorp#514)

<!-- This change is generated by MagicModules. -->
/cc @rileykarson
  • Loading branch information
modular-magician authored and rileykarson committed Mar 13, 2019
1 parent 6739f6b commit a5516b5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 51 deletions.
20 changes: 20 additions & 0 deletions google-beta/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ func Convert(item, out interface{}) error {
return nil
}

// When converting to a map, we can't use setOmittedFields because FieldByName
// fails. Luckily, we don't use the omitted fields anymore with generated
// resources, and this function is used to bridge from handwritten -> generated.
// Since this is a known type, we can create it inline instead of needing to
// pass an object in.
func ConvertToMap(item interface{}) (map[string]interface{}, error) {
out := make(map[string]interface{})
bytes, err := json.Marshal(item)
if err != nil {
return nil, err
}

err = json.Unmarshal(bytes, &out)
if err != nil {
return nil, err
}

return out, nil
}

func setOmittedFields(item, out interface{}) {
// Both inputs must be pointers, see https://blog.golang.org/laws-of-reflection:
// "To modify a reflection object, the value must be settable."
Expand Down
13 changes: 11 additions & 2 deletions google-beta/resource_google_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ func resourceGoogleFolderCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error creating folder '%s' in '%s': %s", displayName, parent, err)
}

err = resourceManagerV2Beta1OperationWait(config.clientResourceManager, op, "creating folder")
opAsMap, err := ConvertToMap(op)
if err != nil {
return err
}

err = resourceManagerOperationWaitTime(config, opAsMap, "", "creating folder", int(d.Timeout(schema.TimeoutCreate).Minutes()))
if err != nil {
return fmt.Errorf("Error creating folder '%s' in '%s': %s", displayName, parent, err)
}
Expand Down Expand Up @@ -132,7 +136,12 @@ func resourceGoogleFolderUpdate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error moving folder '%s' to '%s': %s", displayName, newParent, err)
}

err = resourceManagerV2Beta1OperationWait(config.clientResourceManager, op, "move folder")
opAsMap, err := ConvertToMap(op)
if err != nil {
return err
}

err = resourceManagerOperationWaitTime(config, opAsMap, "", "move folder", int(d.Timeout(schema.TimeoutCreate).Minutes()))
if err != nil {
return fmt.Errorf("Error moving folder '%s' to '%s': %s", displayName, newParent, err)
}
Expand Down
7 changes: 6 additions & 1 deletion google-beta/resource_google_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,12 @@ func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error
d.SetId(pid)

// Wait for the operation to complete
waitErr := resourceManagerOperationWait(config.clientResourceManager, op, "project to create")
opAsMap, err := ConvertToMap(op)
if err != nil {
return err
}

waitErr := resourceManagerOperationWaitTime(config, opAsMap, "", "creating folder", int(d.Timeout(schema.TimeoutCreate).Minutes()))
if waitErr != nil {
// The resource wasn't actually created
d.SetId("")
Expand Down
46 changes: 46 additions & 0 deletions google-beta/resource_manager_operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package google

import (
"fmt"
)

type ResourceManagerOperationWaiter struct {
Config *Config
CommonOperationWaiter
}

func (w *ResourceManagerOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
// Returns the proper get.
url := fmt.Sprintf("https://cloudresourcemanager.googleapis.com/v1/%s", w.CommonOperationWaiter.Op.Name)
return sendRequest(w.Config, "GET", url, nil)
}

func resourceManagerOperationWaitTime(config *Config, op map[string]interface{}, project, activity string, timeoutMinutes int) error {
if val, ok := op["name"]; !ok || val == "" {
// This was a synchronous call - there is no operation to wait for.
return nil
}
w := &ResourceManagerOperationWaiter{
Config: config,
}
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
return err
}
return OperationWait(w, activity, timeoutMinutes)
}
48 changes: 0 additions & 48 deletions google-beta/resourcemanager_operation.go

This file was deleted.

0 comments on commit a5516b5

Please sign in to comment.