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

Add transformation of SQL DB read error so HandleNotFound works #3396

Merged
merged 1 commit into from
Apr 21, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion overrides/terraform/resource_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ def self.attributes

# This enables resources that get their project via a reference to a different resource
# instead of a project field to use User Project Overrides
:supports_indirect_user_project_override
:supports_indirect_user_project_override,

# Function to transform a read error so that handleNotFound recognises
# it as a 404. This should be added as a handwritten fn that takes in
# an error and returns one.
:read_error_transform
]
end

Expand Down Expand Up @@ -108,6 +113,7 @@ def validate
check :skip_sweeper, type: :boolean, default: false
check :skip_delete, type: :boolean, default: false
check :supports_indirect_user_project_override, type: :boolean, default: false
check :read_error_transform, type: String
end

def apply(resource)
Expand Down
1 change: 1 addition & 0 deletions products/sql/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ client_name: 'SqlAdmin'
overrides: !ruby/object:Overrides::ResourceOverrides
Database: !ruby/object:Overrides::Terraform::ResourceOverride
mutex: "google-sql-database-instance-{{project}}-{{instance}}"
read_error_transform: "transformSQLDatabaseReadError"
import_format: ["projects/{{project}}/instances/{{instance}}/databases/{{name}}",
"{{project}}/{{instance}}/{{name}}",
"instances/{{instance}}/databases/{{name}}",
Expand Down
4 changes: 4 additions & 0 deletions templates/terraform/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ func resource<%= resource_name -%>Read(d *schema.ResourceData, meta interface{})
<% end -%>
res, err := sendRequest(config, "<%= object.read_verb.to_s.upcase -%>", <% if has_project || object.supports_indirect_user_project_override %>project<% else %>""<% end %>, url, nil<%= object.error_retry_predicates ? ", " + object.error_retry_predicates.join(',') : "" -%>)
if err != nil {
<% if object.read_error_transform -%>
return handleNotFoundError(<%= object.read_error_transform %>(err), d, fmt.Sprintf("<%= resource_name -%> %q", d.Id()))
<% else -%>
return handleNotFoundError(err, d, fmt.Sprintf("<%= resource_name -%> %q", d.Id()))
<% end -%>
}

<% if object.nested_query -%>
Expand Down
26 changes: 26 additions & 0 deletions third_party/terraform/utils/sql_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package google

import (
"log"
"strings"

"github.com/hashicorp/errwrap"
"google.golang.org/api/googleapi"
)

func transformSQLDatabaseReadError(err error) error {
if gErr, ok := errwrap.GetType(err, &googleapi.Error{}).(*googleapi.Error); ok {
if gErr.Code == 400 && strings.Contains(gErr.Message, "Invalid request since instance is not running") {
// This error occurs when attempting a GET after deleting the sql database and sql instance. It leads to to
// inconsistent behavior as handleNotFoundError(...) expects an error code of 404 when a resource does not
// exist. To get the desired behavior from handleNotFoundError, modify the return code to 404 so that
// handleNotFoundError(...) will treat this as a NotFound error
gErr.Code = 404
}

log.Printf("[DEBUG] Transformed SQLDatabase error")
return gErr
}

return err
}