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()) +}