Files

324 lines
10 KiB
Go

package resource
import (
"context"
"fmt"
kmsclient "github.com/hanzokms/terraform-provider/internal/client"
"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/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &IntegrationDatabricksResource{}
)
// IntegrationDatabricksResource is a helper function to simplify the provider implementation.
func NewIntegrationDatabricksResource() resource.Resource {
return &IntegrationDatabricksResource{}
}
// IntegrationDatabricksResource is the resource implementation.
type IntegrationDatabricksResource struct {
client *kmsclient.Client
}
// IntegrationDatabricksResourceModel describes the data source data model.
type IntegrationDatabricksResourceModel struct {
ProjectID types.String `tfsdk:"project_id"`
Environment types.String `tfsdk:"environment"`
SecretPath types.String `tfsdk:"secret_path"`
DatabricksHostURL types.String `tfsdk:"databricks_host"`
DatabricksAccessToken types.String `tfsdk:"databricks_token"`
DatabricksSecretScope types.String `tfsdk:"databricks_secret_scope"`
IntegrationAuthID types.String `tfsdk:"integration_auth_id"`
IntegrationID types.String `tfsdk:"integration_id"`
}
// Metadata returns the resource type name.
func (r *IntegrationDatabricksResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_integration_databricks"
}
// Schema defines the schema for the resource.
func (r *IntegrationDatabricksResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "**Deprecated:** This resource is deprecated and will be removed in a future version. Use `kms_secret_sync_databricks` instead.\n\nCreate Databricks integration & save to Kms. Only Machine Identity authentication is supported for this resource.",
Attributes: map[string]schema.Attribute{
"integration_auth_id": schema.StringAttribute{
Computed: true,
Description: "The ID of the integration auth, used internally by Kms.",
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"integration_id": schema.StringAttribute{
Computed: true,
Description: "The ID of the integration, used internally by Kms.",
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"project_id": schema.StringAttribute{
Required: true,
Description: "The ID of your Kms project.",
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"environment": schema.StringAttribute{
Required: true,
Description: "The slug of the environment to sync to Databricks (prod, dev, staging, etc).",
},
"secret_path": schema.StringAttribute{
Required: true,
Description: "The secret path in Kms to sync secrets from.",
},
"databricks_host": schema.StringAttribute{
Required: true,
Description: "The Databricks host URL.",
},
"databricks_token": schema.StringAttribute{
Required: true,
Sensitive: true,
Description: "The Databricks access token.",
},
"databricks_secret_scope": schema.StringAttribute{
Required: true,
Description: "The Databricks secret scope. Example: your-secret-scope",
},
},
}
}
// Configure adds the provider configured client to the resource.
func (r *IntegrationDatabricksResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*kmsclient.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
// Create creates the resource and sets the initial Terraform state.
func (r *IntegrationDatabricksResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
if !r.client.Config.IsMachineIdentityAuth {
resp.Diagnostics.AddError(
"Unable to create integration",
"Only Machine Identity authentication is supported for this operation",
)
return
}
// Retrieve values from plan
var plan IntegrationDatabricksResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Create integration auth first
auth, err := r.client.CreateIntegrationAuth(kmsclient.CreateIntegrationAuthRequest{
AccessToken: plan.DatabricksAccessToken.ValueString(),
URL: plan.DatabricksHostURL.ValueString(),
ProjectID: plan.ProjectID.ValueString(),
Integration: kmsclient.IntegrationAuthTypeDatabricks,
})
if err != nil {
resp.Diagnostics.AddError(
"Unable to create integration auth",
err.Error(),
)
return
}
// Create the integration
integration, err := r.client.CreateIntegration(kmsclient.CreateIntegrationRequest{
IntegrationAuthID: auth.IntegrationAuth.ID,
App: plan.DatabricksSecretScope.ValueString(),
SourceEnvironment: plan.Environment.ValueString(),
SecretPath: plan.SecretPath.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError(
"Unable to create integration",
err.Error(),
)
return
}
plan.IntegrationAuthID = types.StringValue(auth.IntegrationAuth.ID)
plan.IntegrationID = types.StringValue(integration.Integration.ID)
plan.Environment = types.StringValue(integration.Integration.Environment.Slug)
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Read refreshes the Terraform state with the latest data.
func (r *IntegrationDatabricksResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
if !r.client.Config.IsMachineIdentityAuth {
resp.Diagnostics.AddError(
"Unable to read integration",
"Only Machine Identity authentication is supported for this operation",
)
return
}
// Get current state
var state IntegrationDatabricksResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
integration, err := r.client.GetIntegration(kmsclient.GetIntegrationRequest{
ID: state.IntegrationID.ValueString(),
})
if err != nil {
if err == kmsclient.ErrNotFound {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError(
"Unable to get integration",
err.Error(),
)
}
return
}
state.SecretPath = types.StringValue(integration.Integration.SecretPath)
state.IntegrationAuthID = types.StringValue(integration.Integration.IntegrationAuthID)
state.Environment = types.StringValue(integration.Integration.Environment.Slug)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *IntegrationDatabricksResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
if !r.client.Config.IsMachineIdentityAuth {
resp.Diagnostics.AddError(
"Unable to update integration",
"Only Machine Identity authentication is supported for this operation",
)
return
}
// Retrieve values from plan
var plan IntegrationDatabricksResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
var state IntegrationDatabricksResourceModel
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
_, err := r.client.UpdateIntegrationAuth(kmsclient.UpdateIntegrationAuthRequest{
Integration: kmsclient.IntegrationAuthTypeDatabricks,
IntegrationAuthId: plan.IntegrationAuthID.ValueString(),
AccessToken: plan.DatabricksAccessToken.ValueString(),
URL: plan.DatabricksHostURL.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError(
"Error updating integration auth",
err.Error(),
)
return
}
// Update the integration
updatedIntegration, err := r.client.UpdateIntegration(kmsclient.UpdateIntegrationRequest{
ID: state.IntegrationID.ValueString(),
Environment: plan.Environment.ValueString(),
SecretPath: plan.SecretPath.ValueString(),
App: plan.DatabricksSecretScope.ValueString(),
IsActive: true,
})
if err != nil {
resp.Diagnostics.AddError(
"Error updating integration",
err.Error(),
)
return
}
plan.SecretPath = types.StringValue(updatedIntegration.Integration.SecretPath)
plan.IntegrationAuthID = types.StringValue(updatedIntegration.Integration.IntegrationAuthID)
plan.Environment = types.StringValue(updatedIntegration.Integration.Environment.Slug)
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *IntegrationDatabricksResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
if !r.client.Config.IsMachineIdentityAuth {
resp.Diagnostics.AddError(
"Unable to delete Databricks Store integration",
"Only Machine Identity authentication is supported for this operation",
)
return
}
var state IntegrationDatabricksResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
_, err := r.client.DeleteIntegrationAuth(kmsclient.DeleteIntegrationAuthRequest{
ID: state.IntegrationAuthID.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError(
"Error deleting Databricks Integration",
"Couldn't delete Databricks integration from your Infiscial project, unexpected error: "+err.Error(),
)
return
}
}