Skip to content

Commit

Permalink
Fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tevesz authored and gregito committed Jul 8, 2024
1 parent afaec4e commit 0def7a9
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 61 deletions.
15 changes: 5 additions & 10 deletions resources/dw/resource_hive_vw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.")
}

Expand Down
45 changes: 20 additions & 25 deletions resources/dw/resource_hive_vw_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
165 changes: 139 additions & 26 deletions resources/dw/resource_hive_vw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand All @@ -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,
},
}

Expand All @@ -138,15 +173,73 @@ 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)
dwApi := NewDwApi(client)

req := resource.DeleteRequest{
State: tfsdk.State{
Schema: hiveSchema,
Schema: testHiveSchema,
Raw: createRawHiveResource(),
},
}
Expand All @@ -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())
}

0 comments on commit 0def7a9

Please sign in to comment.