Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datastream check private connection state after create and update #6862

Merged
merged 7 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:
melinath marked this conversation as resolved.
Show resolved Hide resolved
- :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
iperetz-goo marked this conversation as resolved.
Show resolved Hide resolved
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)
}