Skip to content

Commit

Permalink
Handle HTTP 404 on create as a proper error
Browse files Browse the repository at this point in the history
* 404 status was handled as resource deletion upon the first Create request, which resulted in `Provider produced inconsistent result after apply`
* This commit removes that handling from `common.Resource#Create` and properly propagates error messages back to terraform process
* Fixes databricks#564 databricks#576
  • Loading branch information
nfx committed Apr 1, 2021
1 parent 875b2bc commit 943a98b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
6 changes: 4 additions & 2 deletions common/reflect_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,16 @@ func TestStructToData(t *testing.T) {

// Empty optional string should not be set.
{
// nolint: marked as deprecated, without viable alternative.
//lint:ignore SA1019
// nolint
_, ok := d.GetOkExists("addresses.0.optional_string")
assert.Falsef(t, ok, "Empty optional string should not be set in ResourceData")
}

// Empty required string should be set.
{
// nolint: marked as deprecated, without viable alternative.
//lint:ignore SA1019
// nolint
_, ok := d.GetOkExists("addresses.0.required_string")
assert.Truef(t, ok, "Empty required string should be set in ResourceData")
}
Expand Down
6 changes: 0 additions & 6 deletions common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ func (r Resource) ToResource() *schema.Resource {
CreateContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*DatabricksClient)
err := r.Create(ctx, d, c)
if e, ok := err.(APIError); ok && e.IsMissing() {
log.Printf("[INFO] %s[id=%s] is removed on backend",
ResourceName.GetOrUnknown(ctx), d.Id())
d.SetId("")
return nil
}
if err != nil {
return diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion sqlanalytics/acceptance/sql_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/databrickslabs/terraform-provider-databricks/internal/acceptance"
)

func TestAccSQLEndpoint(t *testing.T) {
func TestPreviewAccSQLEndpoint(t *testing.T) {
acceptance.Test(t, []acceptance.Step{
{
Template: `resource "databricks_sql_endpoint" "this" {
Expand Down
27 changes: 27 additions & 0 deletions sqlanalytics/resource_sql_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ func TestResourceSQLEndpointCreate(t *testing.T) {
assert.Equal(t, "abc", d.Id(), "Id should not be empty")
}

func TestResourceSQLEndpointCreate_ErrorDisabled(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "POST",
Resource: "/api/2.0/sql/endpoints",
ExpectedRequest: SQLEndpoint{
Name: "foo",
ClusterSize: "Small",
MaxNumClusters: 1,
},
Status: 404,
Response: common.APIError{
ErrorCode: "FEATURE_DISABLED",
Message: "SQL Analytics is not supported",
},
},
},
Resource: ResourceSQLEndpoint(),
Create: true,
HCL: `
name = "foo"
cluster_size = "Small"
`,
}.ExpectError(t, "SQL Analytics is not supported")
}

func TestResourceSQLEndpointRead(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
Expand Down

0 comments on commit 943a98b

Please sign in to comment.