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

DWX-18731 CDP DW create and delete hive virtual warehouse #135

Merged
merged 4 commits into from
Jul 8, 2024
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
3 changes: 1 addition & 2 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ terraform {
}
}

provider "cdp" {
}
provider "cdp" {}

resource "cdp_environments_aws_credential" "example" {
name = "example-cdp-aws-credential"
Expand Down
15 changes: 15 additions & 0 deletions examples/resources/cdp_dw_hive/resource.tf
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -246,6 +247,7 @@ func (p *CdpProvider) Resources(_ context.Context) []func() resource.Resource {
opdb.NewDatabaseResource,
ml.NewWorkspaceResource,
de.NewServiceResource,
dw.NewHiveResource,
}
}

Expand Down
2 changes: 2 additions & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,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"
Expand Down Expand Up @@ -632,6 +633,7 @@ func TestCdpProvider_Resources(t *testing.T) {
opdb.NewDatabaseResource,
ml.NewWorkspaceResource,
de.NewServiceResource,
dw.NewHiveResource,
}

provider := CdpProvider{testVersion}
Expand Down
20 changes: 20 additions & 0 deletions resources/dw/model_hive_vw.go
Original file line number Diff line number Diff line change
@@ -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 hiveResourceModel struct {
ID types.String `tfsdk:"id"`
ClusterID types.String `tfsdk:"cluster_id"`
DbCatalogID types.String `tfsdk:"database_catalog_id"`
Name types.String `tfsdk:"name"`
}
134 changes: 134 additions & 0 deletions resources/dw/resource_hive_vw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// 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/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"
)

type hiveResource struct {
client *cdp.Client
}

var (
_ resource.Resource = (*hiveResource)(nil)
_ resource.ResourceWithConfigure = (*hiveResource)(nil)
)

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 *hiveResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_vw_hive"
}

func (r *hiveResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = hiveSchema
gregito marked this conversation as resolved.
Show resolved Hide resolved
}

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
}
vcsomor marked this conversation as resolved.
Show resolved Hide resolved

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)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
}

func (r *hiveResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
tflog.Warn(ctx, "Read operation is not implemented yet.")
}

func (r *hiveResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
tflog.Warn(ctx, "Update operation is not implemented yet.")
}

func (r *hiveResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state hiveResourceModel

// Read Terraform prior state into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)

if resp.Diagnostics.HasError() {
return
}

op := operations.NewDeleteVwParamsWithContext(ctx).
WithInput(&models.DeleteVwRequest{
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 delete Hive Virtual Warehouse, unexpected error: "+err.Error(),
)
return
}
}
110 changes: 110 additions & 0 deletions resources/dw/resource_hive_vw_acc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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"
"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"
)

type hiveTestParameters struct {
Name string
ClusterID string
DatabaseCatalogID string
}

func HivePreCheck(t *testing.T) {
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
gregito marked this conversation as resolved.
Show resolved Hide resolved
// 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
}
Loading
Loading