Skip to content

Commit

Permalink
fix(warehouse|bigquery): read operation - inconsistent data collector (
Browse files Browse the repository at this point in the history
…#29)

* fix(warehouse|bigquery): read operation - inconsistent data collector uuid

* fix(warehouse|bigquery): read operation - inconsistent data collector uuid fixed tests
  • Loading branch information
ndopj authored Oct 4, 2023
1 parent 393bfd7 commit e27f656
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions monte_carlo/client/monte_carlo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type GetWarehouse struct {
Uuid string `json:"uuid"`
Type string `json:"type"`
} `json:"connections"`
DataCollector struct {
Uuid string `json:"uuid"`
} `json:"dataCollector"`
} `json:"getWarehouse"`
}

Expand Down
25 changes: 19 additions & 6 deletions monte_carlo/resources/bigquery_warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ func (r *BigQueryWarehouseResource) Read(ctx context.Context, req resource.ReadR
return
}

readDataCollectorUuid := getResult.GetWarehouse.DataCollector.Uuid
confDataCollectorUuid := data.DataCollectorUuid.ValueString()
if readDataCollectorUuid != confDataCollectorUuid {
resp.Diagnostics.AddError(
fmt.Sprintf("Obtained BigQuery warehouse with [uuid: %s] but its Data Collector UUID does not match with "+
"configured value [obtained: %s, configured: %s]. BigQuery warehouse might have been moved to other "+
"Data Collector externally", data.Uuid.ValueString(), readDataCollectorUuid, confDataCollectorUuid),
"Since its not possible for this provider to update Data Collector of BigQuery warehouse, this resource "+
"cannot continue to function properly. It is recommended to change Data Collector UUID for this "+
"resource directly in the Terraform configuration",
)
return
}

readConnectionUuid := types.StringNull()
readServiceAccountKey := types.StringNull()
for _, connection := range getResult.GetWarehouse.Connections {
Expand Down Expand Up @@ -264,15 +278,14 @@ func (r *BigQueryWarehouseResource) Delete(ctx context.Context, req resource.Del
}

func (r *BigQueryWarehouseResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
if idsImported := strings.Split(req.ID, ","); len(idsImported) == 2 && idsImported[0] != "" && idsImported[1] != "" {
idsImported := strings.Split(req.ID, ",")
if len(idsImported) == 3 && idsImported[0] != "" && idsImported[1] != "" && idsImported[2] != "" {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("uuid"), idsImported[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("connection_uuid"), idsImported[1])...)
// since the Read operation is not capable of reading service_account_key - force its update right after import
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("service_account_key"), (*string)(nil))...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("data_collector_uuid"), idsImported[2])...)
} else {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: <warehouse_uuid>,<connection_uuid>. Got: %q", req.ID),
resp.Diagnostics.AddError("Unexpected Import Identifier", fmt.Sprintf(
"Expected import identifier with format: <warehouse_uuid>,<connection_uuid>,<data_collector_uuid>. Got: %q", req.ID),
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions monte_carlo/resources/bigquery_warehouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func TestAccBigQueryWarehouseResource(t *testing.T) {
ResourceName: "montecarlo_bigquery_warehouse.test",
ImportState: true,
ImportStateVerify: true,
ImportStateId: "8bfc4,8cd5a",
ImportStateId: "8bfc4,8cd5a,dataCollector1",
ImportStateVerifyIdentifierAttribute: "uuid",
ImportStateVerifyIgnore: []string{"data_collector_uuid", "deletion_protection", "service_account_key"},
ImportStateVerifyIgnore: []string{"deletion_protection", "service_account_key"},
},
// Update and Read testing
{
Expand Down Expand Up @@ -98,7 +98,7 @@ func initMonteCarloClient() client.MonteCarloClient {
// Read operations
readQuery := "query getWarehouse($uuid: UUID) { getWarehouse(uuid: $uuid) { name,connections{uuid,type} } }"
readVariables1 := map[string]interface{}{"uuid": client.UUID("8bfc4")}
readResponse1 := []byte(`{"getWarehouse":{"name":"name1","connections":[{"uuid":"8cd5a"}]}}`)
readResponse1 := []byte(`{"getWarehouse":{"name":"name1","connections":[{"uuid":"8cd5a"}],"dataCollector":{"uuid":"dataCollector1"}}}`)
mcClient.On("ExecRaw", mock.Anything, readQuery, readVariables1).Return(readResponse1, nil)

// Delete operations
Expand All @@ -120,7 +120,7 @@ func initMonteCarloClient() client.MonteCarloClient {
arg.UpdateCredentials.Success = true
// after update, read operation must return new results
mcClient.On("ExecRaw", mock.Anything, readQuery, readVariables1).Unset()
readResponse := []byte(`{"getWarehouse":{"name":"name2","connections":[{"uuid":"8cd5a"}]}}`)
readResponse := []byte(`{"getWarehouse":{"name":"name2","connections":[{"uuid":"8cd5a"}],"dataCollector":{"uuid":"dataCollector1"}}}`)
mcClient.On("ExecRaw", mock.Anything, readQuery, readVariables1).Return(readResponse, nil)
})
return &mcClient
Expand Down

0 comments on commit e27f656

Please sign in to comment.