Skip to content

Commit

Permalink
Add hive dw virtual warehouse creation
Browse files Browse the repository at this point in the history
Added creation code, and tf file for testing. Tested creation and
deletion.
  • Loading branch information
tevesz authored and gregito committed Jul 8, 2024
1 parent 1bf5c29 commit 6c48c97
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 124 deletions.
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 @@ -13,6 +13,7 @@ package provider
import (
"context"
"fmt"
"github.com/cloudera/terraform-provider-cdp/resources/dw"
"os"
"reflect"
"regexp"
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
2 changes: 1 addition & 1 deletion resources/dw/model_hive_vw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
103 changes: 80 additions & 23 deletions resources/dw/resource_hive_vw.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,123 @@ 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
}

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
}
Expand Down
2 changes: 1 addition & 1 deletion resources/dw/schema_hive_vw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 0 additions & 34 deletions utils/ptr/ptr.go

This file was deleted.

63 changes: 0 additions & 63 deletions utils/ptr/ptr_test.go

This file was deleted.

0 comments on commit 6c48c97

Please sign in to comment.