From 42321156d68a0a7d62619e4166c7da54f7863c6d Mon Sep 17 00:00:00 2001 From: Viktor Csomor Date: Mon, 3 Jun 2024 14:58:13 +0200 Subject: [PATCH 1/4] CDPCP-11471 CDP DW PoC Add simple Hive VW Resource --- resources/dw/model_hive_vw.go | 20 ++++++++ resources/dw/resource_hive_vw.go | 82 ++++++++++++++++++++++++++++++++ resources/dw/schema_hive_vw.go | 46 ++++++++++++++++++ utils/ptr/ptr.go | 34 +++++++++++++ utils/ptr/ptr_test.go | 63 ++++++++++++++++++++++++ 5 files changed, 245 insertions(+) create mode 100644 resources/dw/model_hive_vw.go create mode 100644 resources/dw/resource_hive_vw.go create mode 100644 resources/dw/schema_hive_vw.go create mode 100644 utils/ptr/ptr.go create mode 100644 utils/ptr/ptr_test.go diff --git a/resources/dw/model_hive_vw.go b/resources/dw/model_hive_vw.go new file mode 100644 index 00000000..41076199 --- /dev/null +++ b/resources/dw/model_hive_vw.go @@ -0,0 +1,20 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package dw + +import "github.com/hashicorp/terraform-plugin-framework/types" + +type hiveVwResourceModel struct { + ID types.String `tfsdk:"id"` + ClusterID types.String `tfsdk:"cluster_id"` + DbCatalogID types.String `tfsdk:"database_catalog_id"` + Name types.String `tfsdk:"name"` +} diff --git a/resources/dw/resource_hive_vw.go b/resources/dw/resource_hive_vw.go new file mode 100644 index 00000000..3b6d9cd7 --- /dev/null +++ b/resources/dw/resource_hive_vw.go @@ -0,0 +1,82 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package dw + +import ( + "context" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" + "github.com/cloudera/terraform-provider-cdp/utils" + "github.com/cloudera/terraform-provider-cdp/utils/ptr" + "github.com/hashicorp/terraform-plugin-framework/resource" +) + +type hiveVwResource struct { + client *cdp.Client +} + +var ( + _ resource.Resource = (*hiveVwResource)(nil) + _ resource.ResourceWithConfigure = (*hiveVwResource)(nil) +) + +func (r *hiveVwResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + r.client = utils.GetCdpClientForResource(req, resp) +} + +func (r *hiveVwResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_ml_workspace" +} + +func (r *hiveVwResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = hiveVwSchema +} + +func (r *hiveVwResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + //TODO implement me + panic("implement me") +} + +func (r *hiveVwResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + //TODO implement me + panic("implement me") +} + +func (r *hiveVwResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + //TODO implement me + panic("implement me") +} + +func (r *hiveVwResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var data hiveVwResourceModel + + // Read Terraform prior state data into the model + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + op := operations.NewDeleteVwParamsWithContext(ctx). + WithInput(&models.DeleteVwRequest{ + ClusterID: ptr.To(data.ClusterID.ValueString()), + VwID: ptr.To(data.ID.ValueString()), + }) + + if _, err := r.client.Dw.Operations.DeleteVw(op); err != nil { + resp.Diagnostics.AddError( + "Error deleting Hive Virtual Warehouse", + "Could not deleting Hive Virtual Warehouse, unexpected error: "+err.Error(), + ) + return + } +} diff --git a/resources/dw/schema_hive_vw.go b/resources/dw/schema_hive_vw.go new file mode 100644 index 00000000..78fe6681 --- /dev/null +++ b/resources/dw/schema_hive_vw.go @@ -0,0 +1,46 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package dw + +import ( + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" +) + +var hiveVwSchema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "cluster_id": schema.StringAttribute{ + Required: true, + MarkdownDescription: "The id of the CDW Cluster which the Hive Virtual Warehouse is attached to.", + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "database_catalog_id": schema.StringAttribute{ + Required: true, + MarkdownDescription: "The id of the Database Catalog which the Hive Virtual Warehouse is attached to.", + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "name": schema.StringAttribute{ + Required: true, + MarkdownDescription: "The name of the Hive Virtual Warehouse.", + }, + }, +} diff --git a/utils/ptr/ptr.go b/utils/ptr/ptr.go new file mode 100644 index 00000000..33e8fca8 --- /dev/null +++ b/utils/ptr/ptr.go @@ -0,0 +1,34 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package ptr + +// To creates a pointer value from `v` +func To[T any](v T) *T { + return &v +} + +// DerefDefault dereferences the `v` value if not nil otherwise it returns nil +func DerefDefault[T any](v *T, def T) T { + if v == nil { + return def + } + return *v +} + +// Copy creates a copy of the pointer which will point to the same memory address +func Copy[T any](v *T) *T { + if v == nil { + return nil + } + + cp := *v + return &cp +} diff --git a/utils/ptr/ptr_test.go b/utils/ptr/ptr_test.go new file mode 100644 index 00000000..eb671dd2 --- /dev/null +++ b/utils/ptr/ptr_test.go @@ -0,0 +1,63 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package ptr + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTo(t *testing.T) { + intVal := 10 + assert.Equal(t, intVal, *To(intVal)) + + stringValVal := "str" + assert.Equal(t, stringValVal, *To(stringValVal)) + + type myStruct struct { + x int + y int + } + assert.Equal(t, myStruct{1, 2}, *To(myStruct{1, 2})) +} + +func TestDerefDefault(t *testing.T) { + intVal := 10 + assert.Equal(t, 200, DerefDefault(nil, 200)) + assert.Equal(t, 10, DerefDefault(&intVal, 200)) + + stringValVal := "str" + assert.Equal(t, "default", DerefDefault(nil, "default")) + assert.Equal(t, "str", DerefDefault(&stringValVal, "default")) + + type myType struct { + x int + y int + } + + structVal := myType{1, 1} + assert.Equal(t, myType{2, 3}, DerefDefault(nil, myType{2, 3})) + assert.Equal(t, myType{1, 1}, DerefDefault(&structVal, myType{2, 3})) +} + +func TestCopy(t *testing.T) { + orig := &[]bool{true}[0] + cp := Copy(orig) + + *orig = false + assert.True(t, *cp) +} + +func TestCopyNil(t *testing.T) { + var input *bool = nil + assert.Nil(t, Copy(input)) +} From 4ccb1360f42d3469b7b3c9fe61381161629ae452 Mon Sep 17 00:00:00 2001 From: Agnes Tevesz Date: Thu, 6 Jun 2024 20:02:48 -0500 Subject: [PATCH 2/4] Add hive dw virtual warehouse creation Added creation code, and tf file for testing. Tested creation and deletion. --- examples/provider/provider.tf | 3 +- examples/resources/cdp_dw_hive/resource.tf | 15 +++ provider/provider.go | 2 + provider/provider_test.go | 2 + resources/dw/model_hive_vw.go | 2 +- resources/dw/resource_hive_vw.go | 103 ++++++++++++++++----- resources/dw/schema_hive_vw.go | 2 +- utils/ptr/ptr.go | 34 ------- utils/ptr/ptr_test.go | 63 ------------- 9 files changed, 102 insertions(+), 124 deletions(-) create mode 100644 examples/resources/cdp_dw_hive/resource.tf delete mode 100644 utils/ptr/ptr.go delete mode 100644 utils/ptr/ptr_test.go diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index 472471f0..71f4d528 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -17,8 +17,7 @@ terraform { } } -provider "cdp" { -} +provider "cdp" {} resource "cdp_environments_aws_credential" "example" { name = "example-cdp-aws-credential" diff --git a/examples/resources/cdp_dw_hive/resource.tf b/examples/resources/cdp_dw_hive/resource.tf new file mode 100644 index 00000000..6a4e2595 --- /dev/null +++ b/examples/resources/cdp_dw_hive/resource.tf @@ -0,0 +1,15 @@ +## Copyright 2024 Cloudera. All Rights Reserved. +# +# This file is licensed under the Apache License Version 2.0 (the "License"). +# You may not use this file except in compliance with the License. +# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +# +# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +# OF ANY KIND, either express or implied. Refer to the License for the specific +# permissions and limitations governing your use of the file. + +resource "cdp_vw_hive" "example" { + cluster_id = var.cluster_id + database_catalog_id = var.database_catalog_id + name = var.name +} diff --git a/provider/provider.go b/provider/provider.go index 155f5767..a3f7f5f4 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -19,6 +19,7 @@ import ( "github.com/cloudera/terraform-provider-cdp/resources/datahub" "github.com/cloudera/terraform-provider-cdp/resources/de" + "github.com/cloudera/terraform-provider-cdp/resources/dw" "github.com/cloudera/terraform-provider-cdp/resources/iam" "github.com/cloudera/terraform-provider-cdp/resources/ml" "github.com/cloudera/terraform-provider-cdp/resources/opdb" @@ -246,6 +247,7 @@ func (p *CdpProvider) Resources(_ context.Context) []func() resource.Resource { opdb.NewDatabaseResource, ml.NewWorkspaceResource, de.NewServiceResource, + dw.NewHiveResource, } } diff --git a/provider/provider_test.go b/provider/provider_test.go index 683ecf09..b2558bdc 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -13,6 +13,7 @@ package provider import ( "context" "fmt" + "github.com/cloudera/terraform-provider-cdp/resources/dw" "os" "reflect" "regexp" @@ -632,6 +633,7 @@ func TestCdpProvider_Resources(t *testing.T) { opdb.NewDatabaseResource, ml.NewWorkspaceResource, de.NewServiceResource, + dw.NewHiveResource, } provider := CdpProvider{testVersion} diff --git a/resources/dw/model_hive_vw.go b/resources/dw/model_hive_vw.go index 41076199..b5838453 100644 --- a/resources/dw/model_hive_vw.go +++ b/resources/dw/model_hive_vw.go @@ -12,7 +12,7 @@ package dw import "github.com/hashicorp/terraform-plugin-framework/types" -type hiveVwResourceModel struct { +type hiveResourceModel struct { ID types.String `tfsdk:"id"` ClusterID types.String `tfsdk:"cluster_id"` DbCatalogID types.String `tfsdk:"database_catalog_id"` diff --git a/resources/dw/resource_hive_vw.go b/resources/dw/resource_hive_vw.go index 3b6d9cd7..fec485fb 100644 --- a/resources/dw/resource_hive_vw.go +++ b/resources/dw/resource_hive_vw.go @@ -16,51 +16,108 @@ import ( "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" "github.com/cloudera/terraform-provider-cdp/utils" - "github.com/cloudera/terraform-provider-cdp/utils/ptr" "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" ) -type hiveVwResource struct { +type hiveResource struct { client *cdp.Client } var ( - _ resource.Resource = (*hiveVwResource)(nil) - _ resource.ResourceWithConfigure = (*hiveVwResource)(nil) + _ resource.Resource = (*hiveResource)(nil) + _ resource.ResourceWithConfigure = (*hiveResource)(nil) ) -func (r *hiveVwResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { +func NewHiveResource() resource.Resource { + return &hiveResource{} +} + +func (r *hiveResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { r.client = utils.GetCdpClientForResource(req, resp) } -func (r *hiveVwResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = req.ProviderTypeName + "_ml_workspace" +func (r *hiveResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_vw_hive" } -func (r *hiveVwResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { - resp.Schema = hiveVwSchema +func (r *hiveResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = hiveSchema } -func (r *hiveVwResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - //TODO implement me - panic("implement me") +func (r *hiveResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + // Retrieve values from plan + var plan hiveResourceModel + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + // Generate API request body from plan + vw := operations.NewCreateVwParamsWithContext(ctx). + WithInput(&models.CreateVwRequest{ + Name: plan.Name.ValueStringPointer(), + ClusterID: plan.ClusterID.ValueStringPointer(), + DbcID: plan.DbCatalogID.ValueStringPointer(), + VwType: models.VwTypeHive.Pointer(), + }) + + // Create new virtual warehouse + response, err := r.client.Dw.Operations.CreateVw(vw) + if err != nil { + resp.Diagnostics.AddError( + "Error creating hive virtual warehouse", + "Could not create hive, unexpected error: "+err.Error(), + ) + return + } + + payload := response.GetPayload() + desc := operations.NewDescribeVwParamsWithContext(ctx). + WithInput(&models.DescribeVwRequest{VwID: &payload.VwID, ClusterID: plan.ClusterID.ValueStringPointer()}) + describe, err := r.client.Dw.Operations.DescribeVw(desc) + if err != nil { + resp.Diagnostics.AddError( + "Error creating hive virtual warehouse", + "Could not describe hive, unexpected error: "+err.Error(), + ) + return + } + + hive := describe.GetPayload() + + // Map response body to schema and populate Computed attribute values + plan.ID = types.StringValue(hive.Vw.ID) + plan.DbCatalogID = types.StringValue(hive.Vw.DbcID) + plan.Name = types.StringValue(hive.Vw.Name) + // TODO why is this not accepted with error: An unexpected error was encountered trying to convert tftypes.Value into dw.hiveResourceModel. This is always an error in the provider. Please report the following to the provider developer: + //plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850)) + + // Set state to fully populated data + diags = resp.State.Set(ctx, plan) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } } -func (r *hiveVwResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { +func (r *hiveResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { //TODO implement me - panic("implement me") + tflog.Warn(ctx, "Read operation is not implemented yet.") } -func (r *hiveVwResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { +func (r *hiveResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { //TODO implement me - panic("implement me") + tflog.Warn(ctx, "Update operation is not implemented yet.") } -func (r *hiveVwResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var data hiveVwResourceModel +func (r *hiveResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state hiveResourceModel - // Read Terraform prior state data into the model - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + // Read Terraform prior state into the model + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return @@ -68,14 +125,14 @@ func (r *hiveVwResource) Delete(ctx context.Context, req resource.DeleteRequest, op := operations.NewDeleteVwParamsWithContext(ctx). WithInput(&models.DeleteVwRequest{ - ClusterID: ptr.To(data.ClusterID.ValueString()), - VwID: ptr.To(data.ID.ValueString()), + ClusterID: state.ClusterID.ValueStringPointer(), + VwID: state.ID.ValueStringPointer(), }) if _, err := r.client.Dw.Operations.DeleteVw(op); err != nil { resp.Diagnostics.AddError( "Error deleting Hive Virtual Warehouse", - "Could not deleting Hive Virtual Warehouse, unexpected error: "+err.Error(), + "Could not delete Hive Virtual Warehouse, unexpected error: "+err.Error(), ) return } diff --git a/resources/dw/schema_hive_vw.go b/resources/dw/schema_hive_vw.go index 78fe6681..838d3b7a 100644 --- a/resources/dw/schema_hive_vw.go +++ b/resources/dw/schema_hive_vw.go @@ -16,7 +16,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" ) -var hiveVwSchema = schema.Schema{ +var hiveSchema = schema.Schema{ Attributes: map[string]schema.Attribute{ "id": schema.StringAttribute{ Computed: true, diff --git a/utils/ptr/ptr.go b/utils/ptr/ptr.go deleted file mode 100644 index 33e8fca8..00000000 --- a/utils/ptr/ptr.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2024 Cloudera. All Rights Reserved. -// -// This file is licensed under the Apache License Version 2.0 (the "License"). -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. -// -// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS -// OF ANY KIND, either express or implied. Refer to the License for the specific -// permissions and limitations governing your use of the file. - -package ptr - -// To creates a pointer value from `v` -func To[T any](v T) *T { - return &v -} - -// DerefDefault dereferences the `v` value if not nil otherwise it returns nil -func DerefDefault[T any](v *T, def T) T { - if v == nil { - return def - } - return *v -} - -// Copy creates a copy of the pointer which will point to the same memory address -func Copy[T any](v *T) *T { - if v == nil { - return nil - } - - cp := *v - return &cp -} diff --git a/utils/ptr/ptr_test.go b/utils/ptr/ptr_test.go deleted file mode 100644 index eb671dd2..00000000 --- a/utils/ptr/ptr_test.go +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2024 Cloudera. All Rights Reserved. -// -// This file is licensed under the Apache License Version 2.0 (the "License"). -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. -// -// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS -// OF ANY KIND, either express or implied. Refer to the License for the specific -// permissions and limitations governing your use of the file. - -package ptr - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestTo(t *testing.T) { - intVal := 10 - assert.Equal(t, intVal, *To(intVal)) - - stringValVal := "str" - assert.Equal(t, stringValVal, *To(stringValVal)) - - type myStruct struct { - x int - y int - } - assert.Equal(t, myStruct{1, 2}, *To(myStruct{1, 2})) -} - -func TestDerefDefault(t *testing.T) { - intVal := 10 - assert.Equal(t, 200, DerefDefault(nil, 200)) - assert.Equal(t, 10, DerefDefault(&intVal, 200)) - - stringValVal := "str" - assert.Equal(t, "default", DerefDefault(nil, "default")) - assert.Equal(t, "str", DerefDefault(&stringValVal, "default")) - - type myType struct { - x int - y int - } - - structVal := myType{1, 1} - assert.Equal(t, myType{2, 3}, DerefDefault(nil, myType{2, 3})) - assert.Equal(t, myType{1, 1}, DerefDefault(&structVal, myType{2, 3})) -} - -func TestCopy(t *testing.T) { - orig := &[]bool{true}[0] - cp := Copy(orig) - - *orig = false - assert.True(t, *cp) -} - -func TestCopyNil(t *testing.T) { - var input *bool = nil - assert.Nil(t, Copy(input)) -} From 9eeeadc6b6b334f04b79aeb432ac2d8078707e48 Mon Sep 17 00:00:00 2001 From: Agnes Tevesz Date: Thu, 20 Jun 2024 18:18:41 -0500 Subject: [PATCH 3/4] DWX-18731 Add hive creation and deletion tests - acceptance tests - unit tests --- provider/provider_test.go | 2 +- resources/dw/resource_hive_vw_acc_test.go | 115 ++++++++++++++++ resources/dw/resource_hive_vw_test.go | 159 ++++++++++++++++++++++ 3 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 resources/dw/resource_hive_vw_acc_test.go create mode 100644 resources/dw/resource_hive_vw_test.go diff --git a/provider/provider_test.go b/provider/provider_test.go index b2558bdc..18197622 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -13,7 +13,6 @@ package provider import ( "context" "fmt" - "github.com/cloudera/terraform-provider-cdp/resources/dw" "os" "reflect" "regexp" @@ -31,6 +30,7 @@ import ( "github.com/cloudera/terraform-provider-cdp/resources/datahub" "github.com/cloudera/terraform-provider-cdp/resources/datalake" "github.com/cloudera/terraform-provider-cdp/resources/de" + "github.com/cloudera/terraform-provider-cdp/resources/dw" "github.com/cloudera/terraform-provider-cdp/resources/environments" "github.com/cloudera/terraform-provider-cdp/resources/iam" "github.com/cloudera/terraform-provider-cdp/resources/ml" diff --git a/resources/dw/resource_hive_vw_acc_test.go b/resources/dw/resource_hive_vw_acc_test.go new file mode 100644 index 00000000..823e55da --- /dev/null +++ b/resources/dw/resource_hive_vw_acc_test.go @@ -0,0 +1,115 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package dw_test + +import ( + "context" + "fmt" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" + "github.com/cloudera/terraform-provider-cdp/cdpacctest" + "github.com/cloudera/terraform-provider-cdp/utils" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "os" + "strings" + "sync" + "testing" +) + +type hiveTestParameters struct { + Name string + ClusterID string + DatabaseCatalogID string +} + +var ( + preCheckOnce sync.Once +) + +func HivePreCheck(t *testing.T) { + preCheckOnce.Do(func() { + errMsg := "AWS Terraform acceptance testing requires environment variable %s to be set" + if _, ok := os.LookupEnv("CDW_CLUSTER_ID"); !ok { + t.Fatalf(errMsg, "CDW_CLUSTER_ID") + } + if _, ok := os.LookupEnv("CDW_DATABASE_CATALOG_ID"); !ok { + t.Fatalf(errMsg, "CDW_DATABASE_CATALOG_ID") + } + }) +} + +func TestAccHive_basic(t *testing.T) { + params := hiveTestParameters{ + Name: cdpacctest.RandomShortWithPrefix(cdpacctest.ResourcePrefix), + ClusterID: os.Getenv("CDW_CLUSTER_ID"), + DatabaseCatalogID: os.Getenv("CDW_DATABASE_CATALOG_ID"), + } + resource.Test(t, resource.TestCase{ + PreCheck: func() { + cdpacctest.PreCheck(t) + HivePreCheck(t) + }, + ProtoV6ProviderFactories: cdpacctest.TestAccProtoV6ProviderFactories, + CheckDestroy: testCheckHiveDestroy, + Steps: []resource.TestStep{ + // Create and Read testing + { + Config: utils.Concat( + cdpacctest.TestAccCdpProviderConfig(), + testAccHiveBasicConfig(params)), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("cdp_vw_hive.test_hive", "name", params.Name), + resource.TestCheckResourceAttr("cdp_vw_hive.test_hive", "cluster_id", params.ClusterID), + resource.TestCheckResourceAttr("cdp_vw_hive.test_hive", "database_catalog_id", params.DatabaseCatalogID), + ), + }, + // TODO ImportState testing + // TODO Update and Read testing + // Delete testing automatically occurs in TestCase + }, + }) +} + +func testAccHiveBasicConfig(params hiveTestParameters) string { + return fmt.Sprintf(` +resource "cdp_vw_hive" "test_hive" { + cluster_id = %[1]q + database_catalog_id = %[2]q + name = %[3]q +} +`, params.ClusterID, params.DatabaseCatalogID, params.Name) +} + +func testCheckHiveDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "cdp_vw_hive" { + continue + } + + cdpClient := cdpacctest.GetCdpClientForAccTest() + params := operations.NewDescribeVwParamsWithContext(context.Background()) + clusterID := rs.Primary.Attributes["cluster_id"] + params.WithInput(&models.DescribeVwRequest{ + VwID: &rs.Primary.ID, + ClusterID: &clusterID, + }) + + _, err := cdpClient.Dw.Operations.DescribeVw(params) + if err != nil { + if strings.Contains(err.Error(), "404") { + continue + } + return err + } + } + return nil +} diff --git a/resources/dw/resource_hive_vw_test.go b/resources/dw/resource_hive_vw_test.go new file mode 100644 index 00000000..bd4fa653 --- /dev/null +++ b/resources/dw/resource_hive_vw_test.go @@ -0,0 +1,159 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package dw + +import ( + "context" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" + dwclient "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" + mocks "github.com/cloudera/terraform-provider-cdp/mocks/github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" + "github.com/go-openapi/runtime" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" + "github.com/hashicorp/terraform-plugin-go/tftypes" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/suite" + "testing" +) + +type MockTransport struct { + runtime.ClientTransport +} + +func NewDwApi(client *mocks.MockDwClientService) *hiveResource { + return &hiveResource{ + client: &cdp.Client{ + Dw: &dwclient.Dw{ + Operations: client, + Transport: MockTransport{}, + }}} +} + +func createRawHiveResource() tftypes.Value { + return tftypes.NewValue( + tftypes.Object{ + AttributeTypes: map[string]tftypes.Type{ + "id": tftypes.String, + "cluster_id": tftypes.String, + "database_catalog_id": tftypes.String, + "name": tftypes.String, + }}, + map[string]tftypes.Value{ + "id": tftypes.NewValue(tftypes.String, ""), + "cluster_id": tftypes.NewValue(tftypes.String, "cluster-id"), + "database_catalog_id": tftypes.NewValue(tftypes.String, "database-catalog-id"), + "name": tftypes.NewValue(tftypes.String, ""), + }) +} + +type HiveTestSuite struct { + suite.Suite + expectedCreateResponse *operations.CreateVwOK +} + +func TestHive(t *testing.T) { + suite.Run(t, new(HiveTestSuite)) +} + +func (suite *HiveTestSuite) SetupTest() { + suite.expectedCreateResponse = &operations.CreateVwOK{Payload: &models.CreateVwResponse{ + VwID: "test-id", + }} + +} + +func (suite *HiveTestSuite) TestHiveMetadata() { + ctx := context.TODO() + client := new(mocks.MockDwClientService) + dwApi := NewDwApi(client) + + req := resource.MetadataRequest{ProviderTypeName: "dw"} + resp := &resource.MetadataResponse{} + + // Function under test + dwApi.Metadata(ctx, req, resp) + suite.Equal("dw_vw_hive", resp.TypeName) +} + +func (suite *HiveTestSuite) TestHiveSchema() { + ctx := context.TODO() + client := new(mocks.MockDwClientService) + dwApi := NewDwApi(client) + + req := resource.SchemaRequest{} + resp := &resource.SchemaResponse{} + + // Function under test + dwApi.Schema(ctx, req, resp) + suite.Equal(hiveSchema, resp.Schema) +} + +func (suite *HiveTestSuite) TestHiveCreate_basic() { + ctx := context.TODO() + expectedDescribeResponse := &operations.DescribeVwOK{ + Payload: &models.DescribeVwResponse{ + Vw: &models.VwSummary{ + ID: "test-id", + DbcID: "database-catalog-id", + Name: "test-name", + VwType: models.VwTypeHive, + }}} + + client := new(mocks.MockDwClientService) + client.On("CreateVw", mock.Anything).Return(suite.expectedCreateResponse, nil) + client.On("DescribeVw", mock.Anything).Return(expectedDescribeResponse, nil) + dwApi := NewDwApi(client) + + req := resource.CreateRequest{ + Plan: tfsdk.Plan{ + Raw: createRawHiveResource(), + Schema: hiveSchema, + }, + } + + resp := &resource.CreateResponse{ + State: tfsdk.State{ + Schema: hiveSchema, + }, + } + + // Function under test + dwApi.Create(ctx, req, resp) + var result hiveResourceModel + resp.State.Get(ctx, &result) + suite.False(resp.Diagnostics.HasError()) + suite.Equal("test-id", result.ID.ValueString()) + suite.Equal("database-catalog-id", result.DbCatalogID.ValueString()) + suite.Equal("cluster-id", result.ClusterID.ValueString()) + suite.Equal("test-name", result.Name.ValueString()) +} + +func (suite *HiveTestSuite) TestHiveDeletion_basic() { + ctx := context.TODO() + client := new(mocks.MockDwClientService) + client.On("DeleteVw", mock.Anything).Return(&operations.DeleteVwOK{}, nil) + dwApi := NewDwApi(client) + + req := resource.DeleteRequest{ + State: tfsdk.State{ + Schema: hiveSchema, + Raw: createRawHiveResource(), + }, + } + + resp := &resource.DeleteResponse{} + + // Function under test + dwApi.Delete(ctx, req, resp) + suite.False(resp.Diagnostics.HasError()) +} From d5530f2fc6a94075d85959c7045ecc3c136ea1d5 Mon Sep 17 00:00:00 2001 From: Agnes Tevesz Date: Fri, 28 Jun 2024 15:17:44 -0500 Subject: [PATCH 4/4] Fix review comments --- resources/dw/resource_hive_vw.go | 15 +- resources/dw/resource_hive_vw_acc_test.go | 45 +++--- resources/dw/resource_hive_vw_test.go | 165 ++++++++++++++++++---- 3 files changed, 164 insertions(+), 61 deletions(-) diff --git a/resources/dw/resource_hive_vw.go b/resources/dw/resource_hive_vw.go index fec485fb..0d922527 100644 --- a/resources/dw/resource_hive_vw.go +++ b/resources/dw/resource_hive_vw.go @@ -12,13 +12,15 @@ package dw import ( "context" + + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" "github.com/cloudera/terraform-provider-cdp/utils" - "github.com/hashicorp/terraform-plugin-framework/resource" - "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-log/tflog" ) type hiveResource struct { @@ -92,24 +94,17 @@ func (r *hiveResource) Create(ctx context.Context, req resource.CreateRequest, r plan.ID = types.StringValue(hive.Vw.ID) plan.DbCatalogID = types.StringValue(hive.Vw.DbcID) plan.Name = types.StringValue(hive.Vw.Name) - // TODO why is this not accepted with error: An unexpected error was encountered trying to convert tftypes.Value into dw.hiveResourceModel. This is always an error in the provider. Please report the following to the provider developer: - //plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850)) // Set state to fully populated data diags = resp.State.Set(ctx, plan) resp.Diagnostics.Append(diags...) - if resp.Diagnostics.HasError() { - return - } } func (r *hiveResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - //TODO implement me tflog.Warn(ctx, "Read operation is not implemented yet.") } func (r *hiveResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - //TODO implement me tflog.Warn(ctx, "Update operation is not implemented yet.") } diff --git a/resources/dw/resource_hive_vw_acc_test.go b/resources/dw/resource_hive_vw_acc_test.go index 823e55da..8afd0786 100644 --- a/resources/dw/resource_hive_vw_acc_test.go +++ b/resources/dw/resource_hive_vw_acc_test.go @@ -13,16 +13,17 @@ package dw_test import ( "context" "fmt" + "os" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" "github.com/cloudera/terraform-provider-cdp/cdpacctest" "github.com/cloudera/terraform-provider-cdp/utils" - "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" - "os" - "strings" - "sync" - "testing" ) type hiveTestParameters struct { @@ -31,20 +32,14 @@ type hiveTestParameters struct { DatabaseCatalogID string } -var ( - preCheckOnce sync.Once -) - func HivePreCheck(t *testing.T) { - preCheckOnce.Do(func() { - errMsg := "AWS Terraform acceptance testing requires environment variable %s to be set" - if _, ok := os.LookupEnv("CDW_CLUSTER_ID"); !ok { - t.Fatalf(errMsg, "CDW_CLUSTER_ID") - } - if _, ok := os.LookupEnv("CDW_DATABASE_CATALOG_ID"); !ok { - t.Fatalf(errMsg, "CDW_DATABASE_CATALOG_ID") - } - }) + errMsg := "AWS Terraform acceptance testing requires environment variable %s to be set" + if _, ok := os.LookupEnv("CDW_CLUSTER_ID"); !ok { + t.Fatalf(errMsg, "CDW_CLUSTER_ID") + } + if _, ok := os.LookupEnv("CDW_DATABASE_CATALOG_ID"); !ok { + t.Fatalf(errMsg, "CDW_DATABASE_CATALOG_ID") + } } func TestAccHive_basic(t *testing.T) { @@ -81,12 +76,12 @@ func TestAccHive_basic(t *testing.T) { func testAccHiveBasicConfig(params hiveTestParameters) string { return fmt.Sprintf(` -resource "cdp_vw_hive" "test_hive" { - cluster_id = %[1]q - database_catalog_id = %[2]q - name = %[3]q -} -`, params.ClusterID, params.DatabaseCatalogID, params.Name) + resource "cdp_vw_hive" "test_hive" { + cluster_id = %[1]q + database_catalog_id = %[2]q + name = %[3]q + } + `, params.ClusterID, params.DatabaseCatalogID, params.Name) } func testCheckHiveDestroy(s *terraform.State) error { diff --git a/resources/dw/resource_hive_vw_test.go b/resources/dw/resource_hive_vw_test.go index bd4fa653..ebcf7416 100644 --- a/resources/dw/resource_hive_vw_test.go +++ b/resources/dw/resource_hive_vw_test.go @@ -12,20 +12,55 @@ package dw import ( "context" - "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" - dwclient "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client" - "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" - "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" - mocks "github.com/cloudera/terraform-provider-cdp/mocks/github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" + "fmt" + "testing" + "github.com/go-openapi/runtime" "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-go/tftypes" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/suite" - "testing" + + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" + dwclient "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" + mocks "github.com/cloudera/terraform-provider-cdp/mocks/github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" ) +var testHiveSchema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "cluster_id": schema.StringAttribute{ + Required: true, + MarkdownDescription: "The id of the CDW Cluster which the Hive Virtual Warehouse is attached to.", + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "database_catalog_id": schema.StringAttribute{ + Required: true, + MarkdownDescription: "The id of the Database Catalog which the Hive Virtual Warehouse is attached to.", + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "name": schema.StringAttribute{ + Required: true, + MarkdownDescription: "The name of the Hive Virtual Warehouse.", + }, + }, +} + type MockTransport struct { runtime.ClientTransport } @@ -73,32 +108,32 @@ func (suite *HiveTestSuite) SetupTest() { } func (suite *HiveTestSuite) TestHiveMetadata() { - ctx := context.TODO() - client := new(mocks.MockDwClientService) - dwApi := NewDwApi(client) - - req := resource.MetadataRequest{ProviderTypeName: "dw"} - resp := &resource.MetadataResponse{} + dwApi := NewDwApi(new(mocks.MockDwClientService)) + resp := resource.MetadataResponse{} // Function under test - dwApi.Metadata(ctx, req, resp) + dwApi.Metadata( + context.TODO(), + resource.MetadataRequest{ProviderTypeName: "dw"}, + &resp, + ) suite.Equal("dw_vw_hive", resp.TypeName) } func (suite *HiveTestSuite) TestHiveSchema() { - ctx := context.TODO() - client := new(mocks.MockDwClientService) - dwApi := NewDwApi(client) - - req := resource.SchemaRequest{} - resp := &resource.SchemaResponse{} + dwApi := NewDwApi(new(mocks.MockDwClientService)) + resp := resource.SchemaResponse{} // Function under test - dwApi.Schema(ctx, req, resp) - suite.Equal(hiveSchema, resp.Schema) + dwApi.Schema( + context.TODO(), + resource.SchemaRequest{}, + &resp, + ) + suite.Equal(testHiveSchema, resp.Schema) } -func (suite *HiveTestSuite) TestHiveCreate_basic() { +func (suite *HiveTestSuite) TestHiveCreate_Success() { ctx := context.TODO() expectedDescribeResponse := &operations.DescribeVwOK{ Payload: &models.DescribeVwResponse{ @@ -117,13 +152,13 @@ func (suite *HiveTestSuite) TestHiveCreate_basic() { req := resource.CreateRequest{ Plan: tfsdk.Plan{ Raw: createRawHiveResource(), - Schema: hiveSchema, + Schema: testHiveSchema, }, } resp := &resource.CreateResponse{ State: tfsdk.State{ - Schema: hiveSchema, + Schema: testHiveSchema, }, } @@ -138,7 +173,65 @@ func (suite *HiveTestSuite) TestHiveCreate_basic() { suite.Equal("test-name", result.Name.ValueString()) } -func (suite *HiveTestSuite) TestHiveDeletion_basic() { +func (suite *HiveTestSuite) TestHiveCreate_CreationError() { + ctx := context.TODO() + client := new(mocks.MockDwClientService) + client.On("CreateVw", mock.Anything).Return(&operations.CreateVwOK{}, fmt.Errorf("create failed")) + client.On("DescribeVw", mock.Anything).Return(&operations.DescribeVwOK{}, nil) + dwApi := NewDwApi(client) + + req := resource.CreateRequest{ + Plan: tfsdk.Plan{ + Raw: createRawHiveResource(), + Schema: testHiveSchema, + }, + } + + resp := &resource.CreateResponse{ + State: tfsdk.State{ + Schema: testHiveSchema, + }, + } + + // Function under test + dwApi.Create(ctx, req, resp) + var result hiveResourceModel + resp.State.Get(ctx, &result) + suite.True(resp.Diagnostics.HasError()) + suite.Contains(resp.Diagnostics.Errors()[0].Summary(), "Error creating hive virtual warehouse") + suite.Contains(resp.Diagnostics.Errors()[0].Detail(), "Could not create hive") +} + +func (suite *HiveTestSuite) TestHiveCreate_DescribeError() { + ctx := context.TODO() + client := new(mocks.MockDwClientService) + client.On("CreateVw", mock.Anything).Return(suite.expectedCreateResponse, nil) + client.On("DescribeVw", mock.Anything).Return(&operations.DescribeVwOK{}, fmt.Errorf("describe failed")) + dwApi := NewDwApi(client) + + req := resource.CreateRequest{ + Plan: tfsdk.Plan{ + Raw: createRawHiveResource(), + Schema: testHiveSchema, + }, + } + + resp := &resource.CreateResponse{ + State: tfsdk.State{ + Schema: testHiveSchema, + }, + } + + // Function under test + dwApi.Create(ctx, req, resp) + var result hiveResourceModel + resp.State.Get(ctx, &result) + suite.True(resp.Diagnostics.HasError()) + suite.Contains(resp.Diagnostics.Errors()[0].Summary(), "Error creating hive virtual warehouse") + suite.Contains(resp.Diagnostics.Errors()[0].Detail(), "Could not describe hive") +} + +func (suite *HiveTestSuite) TestHiveDeletion_Success() { ctx := context.TODO() client := new(mocks.MockDwClientService) client.On("DeleteVw", mock.Anything).Return(&operations.DeleteVwOK{}, nil) @@ -146,7 +239,7 @@ func (suite *HiveTestSuite) TestHiveDeletion_basic() { req := resource.DeleteRequest{ State: tfsdk.State{ - Schema: hiveSchema, + Schema: testHiveSchema, Raw: createRawHiveResource(), }, } @@ -157,3 +250,23 @@ func (suite *HiveTestSuite) TestHiveDeletion_basic() { dwApi.Delete(ctx, req, resp) suite.False(resp.Diagnostics.HasError()) } + +func (suite *HiveTestSuite) TestHiveDeletion_ReturnsError() { + ctx := context.TODO() + client := new(mocks.MockDwClientService) + client.On("DeleteVw", mock.Anything).Return(&operations.DeleteVwOK{}, fmt.Errorf("delete failed")) + dwApi := NewDwApi(client) + + req := resource.DeleteRequest{ + State: tfsdk.State{ + Schema: testHiveSchema, + Raw: createRawHiveResource(), + }, + } + + resp := &resource.DeleteResponse{} + + // Function under test + dwApi.Delete(ctx, req, resp) + suite.True(resp.Diagnostics.HasError()) +}