Skip to content

Commit

Permalink
Datastream check private connection state after create and update (#6862
Browse files Browse the repository at this point in the history
)

* Add desired state field to make sure PC is created in CREATED - b/260057913

* Revert virtual field

* Add state field check to make sure PC is created in CREATED - b/260057913

* Add error details and message in failure cases.

* Fix message formatting

* Added a post import impl
  • Loading branch information
iperetz-goo committed Dec 1, 2022
1 parent 9372b5d commit ff00705
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
25 changes: 25 additions & 0 deletions mmv1/products/datastream/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,31 @@ objects:
name: 'displayName'
required: true
description: Display name.
- !ruby/object:Api::Type::Enum
name: 'state'
description: |
State of the PrivateConnection.
output: true
values:
- :CREATING
- :CREATED
- :FAILED
- :DELETING
- :FAILED_TO_DELETE
- !ruby/object:Api::Type::NestedObject
name: 'error'
output: true
description: |
The PrivateConnection error in case of failure.
properties:
- !ruby/object:Api::Type::String
name: 'message'
description: |
A message containing more information about the error that occurred.
- !ruby/object:Api::Type::KeyValuePairs
name: 'details'
description: |
A list of messages that carry the error details.
- !ruby/object:Api::Type::NestedObject
name: 'vpcPeeringConfig'
required: true
Expand Down
11 changes: 11 additions & 0 deletions mmv1/products/datastream/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ overrides: !ruby/object:Overrides::ResourceOverrides
vars:
private_connection_id: "my-connection"
network_name: "my-network"
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/private_connection.go.erb
post_create: templates/terraform/post_create/private_connection.go.erb
post_import: templates/terraform/post_import/private_connection.go.erb

# This is for copying files over
files: !ruby/object:Provider::Config::Files
# These files have templating (ERB) code that will be run.
# This is usually to add licensing info, autogeneration notices, etc.
compile:
<%= lines(indent(compile('provider/terraform/product~compile.yaml'), 4)) -%>
34 changes: 34 additions & 0 deletions mmv1/templates/terraform/constants/private_connection.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<% unless compiler == "terraformvalidator-codegen" -%>

func extractError(d *schema.ResourceData) error {
// Casts are not safe since the logic that populate it is type deterministic.
error := d.Get("error").([]interface{})[0].(map[string]interface{})
message := error["message"].(string)
details := error["details"].(map[string]interface{})
detailsJSON, _ := json.Marshal(details)
return fmt.Errorf("Failed to create PrivateConnection. %s details = %s", message, string(detailsJSON))
}

// waitForPrivateConnectionReady waits for a private connection state to become
// CREATED, if the state is FAILED propegate the error to the user.
func waitForPrivateConnectionReady(d *schema.ResourceData, config *Config, timeout time.Duration) error {
return resource.Retry(timeout, func() *resource.RetryError {
if err := resourceDatastreamPrivateConnectionRead(d, config); err != nil {
return resource.NonRetryableError(err)
}

name := d.Get("name").(string)
state := d.Get("state").(string)
if state == "CREATING" {
return resource.RetryableError(fmt.Errorf("PrivateConnection %q has state %q.", name, state))
} else if state == "CREATED" {
log.Printf("[DEBUG] PrivateConnection %q has state %q.", name, state)
return nil
} else if state == "FAILED" {
return resource.NonRetryableError(extractError(d))
} else {
return resource.NonRetryableError(fmt.Errorf("PrivateConnection %q has state %q.", name, state))
}
})
}
<% end -%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if err := waitForPrivateConnectionReady(d, config, d.Timeout(schema.TimeoutCreate) - time.Minute); err != nil {
return fmt.Errorf("Error waiting for PrivateConnection %q to be CREATED. %q", d.Get("name").(string), err)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if err := waitForPrivateConnectionReady(d, config, d.Timeout(schema.TimeoutCreate) - time.Minute); err != nil {
return nil, fmt.Errorf("Error waiting for PrivateConnection %q to be CREATED during importing: %q", d.Get("name").(string), err)
}

0 comments on commit ff00705

Please sign in to comment.