Skip to content

Commit

Permalink
feat(warehouse): BQ test credentials refactoring 5
Browse files Browse the repository at this point in the history
  • Loading branch information
ndopj committed Feb 18, 2024
1 parent 2cd7f30 commit a1d8cf0
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 167 deletions.
8 changes: 4 additions & 4 deletions client/monte_carlo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ type GetWarehouse struct {
} `json:"getWarehouse"`
}

const BigQueryConnectionType = "bigquery"
const BigQueryConnectionTypeResponse = "BIGQUERY"
const TransactionalConnectionType = "transactional-db"
const TransactionalConnectionTypeResponse = "TRANSACTIONAL_DB"
const BqConnectionType = "bigquery"
const BqConnectionTypeResponse = "BIGQUERY"
const TrxConnectionType = "transactional-db"
const TrxConnectionTypeResponse = "TRANSACTIONAL_DB"
const GetWarehouseQuery string = "query getWarehouse($uuid: UUID) { getWarehouse(uuid: $uuid) { name,connections{uuid,type,createdOn,updatedOn},dataCollector{uuid} } }"

type RemoveConnection struct {
Expand Down
106 changes: 25 additions & 81 deletions internal/warehouse/bigquery_warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ type BqCredentials struct {
UpdatedAt types.String `tfsdk:"updated_at"`
}

func (m BigQueryWarehouseResourceModel) GetUuid() types.String {
return m.Uuid
}

func (m BigQueryWarehouseResourceModel) GetCollectorUuid() types.String {
return m.CollectorUuid
}

func (m BigQueryWarehouseResourceModel) GetName() types.String {
return m.Name
}

func (m BigQueryWarehouseResourceModel) GetConnectionUuid() types.String {
return m.Credentials.ConnectionUuid
}

type BigQueryWarehouseResourceModelV1 struct {
Uuid types.String `tfsdk:"uuid"`
ConnectionUuid types.String `tfsdk:"connection_uuid"`
Expand Down Expand Up @@ -136,15 +152,15 @@ func (r *BigQueryWarehouseResource) Create(ctx context.Context, req resource.Cre
return
}

result, diags := r.addConnection(ctx, data)
result, diags := addConnection(ctx, r.client, r, data, client.BqConnectionType, BqKeyExtractor)
resp.Diagnostics.Append(diags...)
if result == nil {
return
}

data.Uuid = result.Uuid
data.Credentials.UpdatedAt = result.Credentials.UpdatedAt
data.Credentials.ConnectionUuid = result.Credentials.ConnectionUuid
data.Uuid = types.StringValue(result.AddConnection.Connection.Warehouse.Uuid)
data.Credentials.UpdatedAt = types.StringValue(result.AddConnection.Connection.CreatedOn)
data.Credentials.ConnectionUuid = types.StringValue(result.AddConnection.Connection.Uuid)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

Expand Down Expand Up @@ -195,7 +211,7 @@ func (r *BigQueryWarehouseResource) Read(ctx context.Context, req resource.ReadR

for _, connection := range getResult.GetWarehouse.Connections {
if connection.Uuid == data.Credentials.ConnectionUuid.ValueString() {
if connection.Type != client.BigQueryConnectionTypeResponse {
if connection.Type != client.BqConnectionTypeResponse {
resp.Diagnostics.AddError(
fmt.Sprintf("Obtained Warehouse [uuid: %s, connection_uuid: %s] but got unexpected connection "+
"type '%s'.", data.Uuid.ValueString(), connection.Uuid, connection.Type),
Expand Down Expand Up @@ -243,17 +259,17 @@ func (r *BigQueryWarehouseResource) Update(ctx context.Context, req resource.Upd
}

if data.Credentials.ConnectionUuid.IsUnknown() || data.Credentials.ConnectionUuid.IsNull() {
if result, diags := r.addConnection(ctx, data); result != nil {
if result, diags := addConnection(ctx, r.client, r, data, client.BqConnectionType, BqKeyExtractor); result != nil {
resp.Diagnostics.Append(diags...)
data.Credentials.UpdatedAt = result.Credentials.UpdatedAt
data.Credentials.ConnectionUuid = result.Credentials.ConnectionUuid
data.Credentials.UpdatedAt = types.StringValue(result.AddConnection.Connection.CreatedOn)
data.Credentials.ConnectionUuid = types.StringValue(result.AddConnection.Connection.Uuid)
} else {
resp.Diagnostics.Append(diags...)
return
}
}

if updateResult, diags := r.updateConnection(ctx, data); updateResult == nil {
if updateResult, diags := updateConnection(ctx, r.client, r, data, BqKeyExtractor); updateResult == nil {
resp.Diagnostics.Append(diags...)
} else {
data.Credentials.UpdatedAt = types.StringValue(updateResult.UpdateCredentialsV2.UpdatedAt)
Expand Down Expand Up @@ -331,78 +347,6 @@ func (r *BigQueryWarehouseResource) testCredentials(ctx context.Context, data Bi
}
}

func (r *BigQueryWarehouseResource) addConnection(ctx context.Context, data BigQueryWarehouseResourceModel) (*BigQueryWarehouseResourceModel, diag.Diagnostics) {
var diagsResult diag.Diagnostics
testResult, credentialsDiags := r.testCredentials(ctx, data)

if testResult == nil {
diagsResult.Append(credentialsDiags...)
return nil, diagsResult
}

addResult := client.AddConnection{}
var name, createWarehouseType *string = nil, nil
warehouseUuid := data.Uuid.ValueStringPointer()
collectorUuid := data.CollectorUuid.ValueStringPointer()

if warehouseUuid == nil || *warehouseUuid == "" {
warehouseUuid = nil
name = data.Name.ValueStringPointer()
temp := client.BigQueryConnectionType
createWarehouseType = &temp
}

variables := map[string]interface{}{
"dcId": (*client.UUID)(collectorUuid),
"dwId": (*client.UUID)(warehouseUuid),
"key": testResult.TestBqCredentialsV2.Key,
"jobTypes": []string{"metadata", "query_logs", "sql_query", "json_schema"},
"name": name,
"connectionType": client.BigQueryConnectionType,
"createWarehouseType": createWarehouseType,
}

if err := r.client.Mutate(ctx, &addResult, variables); err != nil {
toPrint := fmt.Sprintf("MC client 'AddConnection' mutation result - %s", err.Error())
diagsResult.AddError(toPrint, "")
return nil, diagsResult
}

data.Uuid = types.StringValue(addResult.AddConnection.Connection.Warehouse.Uuid)
data.Credentials.UpdatedAt = types.StringValue(addResult.AddConnection.Connection.CreatedOn)
data.Credentials.ConnectionUuid = types.StringValue(addResult.AddConnection.Connection.Uuid)
return &data, diagsResult
}

func (r *BigQueryWarehouseResource) updateConnection(ctx context.Context, data BigQueryWarehouseResourceModel) (*client.UpdateCredentialsV2, diag.Diagnostics) {
var diagsResult diag.Diagnostics
testResult, credentialsDiags := r.testCredentials(ctx, data)

if testResult == nil {
diagsResult.Append(credentialsDiags...)
return nil, diagsResult
}

updateResult := client.UpdateCredentialsV2{}
variables := map[string]interface{}{
"connectionId": client.UUID(data.Credentials.ConnectionUuid.ValueString()),
"tempCredentialsKey": testResult.TestBqCredentialsV2.Key,
}

if err := r.client.Mutate(ctx, &updateResult, variables); err != nil {
toPrint := fmt.Sprintf("MC client 'UpdateCredentials' mutation result - %s", err.Error())
diagsResult.AddError(toPrint, "")
return nil, diagsResult
} else if !updateResult.UpdateCredentialsV2.Success {
toPrint := "MC client 'UpdateCredentials' mutation - success = false, " +
"connection probably doesnt exists. Rerunning terraform operation usually helps."
diagsResult.AddError(toPrint, "")
return nil, diagsResult
} else {
return &updateResult, diagsResult
}
}

func bqTestDiagnosticToDiags[T client.BqTestWarnings | client.BqTestErrors](in T) diag.Diagnostics {
var diags diag.Diagnostics
switch any(in).(type) {
Expand Down
108 changes: 108 additions & 0 deletions internal/warehouse/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package warehouse

import (
"context"
"fmt"

"github.com/kiwicom/terraform-provider-montecarlo/client"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type WarehouseResource[T WarehouseResourceModel, K TestCredentials] interface {
*BigQueryWarehouseResource | *TransactionalWarehouseResource
testCredentials(ctx context.Context, data T) (K, diag.Diagnostics)
}

type WarehouseResourceModel interface {
BigQueryWarehouseResourceModel | TransactionalWarehouseResourceModel
GetUuid() types.String
GetCollectorUuid() types.String
GetName() types.String
GetConnectionUuid() types.String
}

type TestCredentials interface {
*client.TestBqCredentialsV2 | *client.TestDatabaseCredentials
}

func BqKeyExtractor(k *client.TestBqCredentialsV2) string {
return k.TestBqCredentialsV2.Key
}
func TrxKeyExtractor(k *client.TestDatabaseCredentials) string {
return k.TestDatabaseCredentials.Key
}

func addConnection[T WarehouseResource[J, K], J WarehouseResourceModel, K TestCredentials](
ctx context.Context, mcClient client.MonteCarloClient, warehouse T, data J, connectionType string, keyExtractor func(K) string,
) (*client.AddConnection, diag.Diagnostics) {
var diagsResult diag.Diagnostics
testResult, credentialsDiags := warehouse.testCredentials(ctx, data)

if testResult == nil {
diagsResult.Append(credentialsDiags...)
return nil, diagsResult
}

addResult := client.AddConnection{}
var name, createWarehouseType *string = nil, nil
warehouseUuid := data.GetUuid().ValueStringPointer()
collectorUuid := data.GetCollectorUuid().ValueStringPointer()

if warehouseUuid == nil || *warehouseUuid == "" {
warehouseUuid = nil
name = data.GetName().ValueStringPointer()
temp := connectionType
createWarehouseType = &temp
}

variables := map[string]interface{}{
"dcId": (*client.UUID)(collectorUuid),
"dwId": (*client.UUID)(warehouseUuid),
"key": keyExtractor(testResult),
"jobTypes": []string{"metadata", "query_logs", "sql_query", "json_schema"},
"name": name,
"connectionType": connectionType,
"createWarehouseType": createWarehouseType,
}

if err := mcClient.Mutate(ctx, &addResult, variables); err != nil {
toPrint := fmt.Sprintf("MC client 'AddConnection' mutation result - %s", err.Error())
diagsResult.AddError(toPrint, "")
return nil, diagsResult
} else {
return &addResult, diagsResult
}
}

func updateConnection[T WarehouseResource[J, K], J WarehouseResourceModel, K TestCredentials](
ctx context.Context, mcClient client.MonteCarloClient, warehouse T, data J, keyExtractor func(K) string,
) (*client.UpdateCredentialsV2, diag.Diagnostics) {
var diagsResult diag.Diagnostics
testResult, credentialsDiags := warehouse.testCredentials(ctx, data)

if testResult == nil {
diagsResult.Append(credentialsDiags...)
return nil, diagsResult
}

updateResult := client.UpdateCredentialsV2{}
variables := map[string]interface{}{
"connectionId": client.UUID(data.GetConnectionUuid().ValueString()),
"tempCredentialsKey": keyExtractor(testResult),
}

if err := mcClient.Mutate(ctx, &updateResult, variables); err != nil {
toPrint := fmt.Sprintf("MC client 'UpdateCredentials' mutation result - %s", err.Error())
diagsResult.AddError(toPrint, "")
return nil, diagsResult
} else if !updateResult.UpdateCredentialsV2.Success {
toPrint := "MC client 'UpdateCredentials' mutation - success = false, " +
"connection probably doesnt exists. Rerunning terraform operation usually helps."
diagsResult.AddError(toPrint, "")
return nil, diagsResult
} else {
return &updateResult, diagsResult
}
}
Loading

0 comments on commit a1d8cf0

Please sign in to comment.