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

[CLOUDGA-24556] Refactor DB Audit Logging resource and data source #126

Merged
merged 2 commits into from
Nov 12, 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

This file was deleted.

3 changes: 3 additions & 0 deletions examples/data-sources/ybm_db_audit_logging/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "ybm_db_audit_logging" "example_db_audit_log_config" {
cluster_id = "<cluster-id>"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Cluster associated with a db audit log configuration
resource "ybm_associate_db_audit_export_config_cluster" "sample-db-audit-log-config" {
cluster_id = "<Your-Cluster-Id>"
exporter_id = "<Your-Exported-Id>"
# DB audit log configuration for a cluster
resource "ybm_db_audit_logging" "sample-db-audit-log-config" {
cluster_id = "<Your-Cluster-Id>"
integration_name = "<Your-Integration-Name>"
ysql_config = {
log_settings = {
log_catalog = true
Expand Down
33 changes: 33 additions & 0 deletions managed/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,36 @@ func getAPIError(b []byte) *openapiclient.ApiError {
}
return apiError
}

func GetIntegrationDataByName(ctx context.Context, apiClient *openapiclient.APIClient, accountId string, projectId string, integrationName string) (*openapiclient.TelemetryProviderData, error) {
integrationConfig, _, err := apiClient.TelemetryProviderApi.
ListTelemetryProviders(ctx, accountId, projectId).
Name(integrationName).
Execute()

if err != nil {
return nil, fmt.Errorf(GetApiErrorDetails(err))
}

if len(integrationConfig.GetData()) < 1 {
return nil, fmt.Errorf("Integration %s not found", integrationName)
}

return &integrationConfig.GetData()[0], nil
}

func GetIntegrationByID(accountId string, projectId string, integrationId string, apiClient *openapiclient.APIClient) (*openapiclient.TelemetryProviderData, error) {
resp, _, err := apiClient.TelemetryProviderApi.ListTelemetryProviders(context.Background(), accountId, projectId).Limit(100).Execute()

if err != nil {
return nil, fmt.Errorf(GetApiErrorDetails(err))
}

for _, tpData := range resp.Data {
if tpData.GetInfo().Id == integrationId {
return &tpData, nil
}
}

return nil, fmt.Errorf("Could not find integration with id: %s", integrationId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package managed

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand All @@ -14,9 +15,9 @@ import (
//"github.com/hashicorp/terraform-plugin-log/tflog"
)

type dataSourceAssociateDbAuditExportConfigClusterType struct{}
type dataSourceDbAuditLoggingType struct{}

func (r dataSourceAssociateDbAuditExportConfigClusterType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (r dataSourceDbAuditLoggingType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Description: `The data source to fetch DB Audit log configuration for a cluster given cluster ID or configuration ID in YugabyteDB Aeon.`,
Attributes: map[string]tfsdk.Attribute{
Expand All @@ -30,13 +31,23 @@ func (r dataSourceAssociateDbAuditExportConfigClusterType) GetSchema(_ context.C
Type: types.StringType,
Computed: true,
},
"cluster_name": {
Description: "Name of the cluster from which DB Audit Logs will be exported",
Type: types.StringType,
Computed: true,
},
"cluster_id": {
Description: "ID of the cluster with which this DB Audit log config is associated ",
Description: "ID of the cluster from which DB Audit Logs will be exported",
Type: types.StringType,
Required: true,
},
"exporter_id": {
Description: "ID of the exporter to which the DB Audit logs will be exported",
"integration_name": {
Description: "Name of the integration to which the DB Audit Logs will be exported",
Type: types.StringType,
Computed: true,
},
"integration_id": {
Description: "ID of the integration to which the DB Audit Logs will be exported",
Type: types.StringType,
Computed: true,
},
Expand All @@ -46,7 +57,7 @@ func (r dataSourceAssociateDbAuditExportConfigClusterType) GetSchema(_ context.C
Computed: true,
},
"state": {
Description: "The stutus of association of cluster with DB Audit log config",
Description: "The status of DB Audit Logging on the cluster",
Type: types.StringType,
Computed: true,
},
Expand Down Expand Up @@ -102,17 +113,17 @@ func (r dataSourceAssociateDbAuditExportConfigClusterType) GetSchema(_ context.C
}, nil
}

func (r dataSourceAssociateDbAuditExportConfigClusterType) NewDataSource(_ context.Context, p tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
return dataSourceAssociateDbAuditExportConfigCluster{
func (r dataSourceDbAuditLoggingType) NewDataSource(_ context.Context, p tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
return dataSourceDbAuditLogging{
p: *(p.(*provider)),
}, nil
}

type dataSourceAssociateDbAuditExportConfigCluster struct {
type dataSourceDbAuditLogging struct {
p provider
}

func (r dataSourceAssociateDbAuditExportConfigCluster) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
func (r dataSourceDbAuditLogging) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {

if !r.p.configured {
resp.Diagnostics.AddError(
Expand All @@ -122,9 +133,9 @@ func (r dataSourceAssociateDbAuditExportConfigCluster) Read(ctx context.Context,
return
}

var daeConfig DbAuditExporterConfig
var dbAuditLoggingConfig DbAuditLoggingConfig

diags := req.Config.Get(ctx, &daeConfig)
diags := req.Config.Get(ctx, &dbAuditLoggingConfig)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -144,20 +155,14 @@ func (r dataSourceAssociateDbAuditExportConfigCluster) Read(ctx context.Context,
return
}

clusterId := daeConfig.ClusterID.Value
_, err := GetClusterByNameorID(accountId, projectId, clusterId, "", apiClient)
if err != nil {
resp.Diagnostics.AddError("Unable to fetch DB Audit Log configuration data", GetApiErrorDetails(err))
return
}

daeConfig, readOK, message := resourceAssociateDbAuditExporterConfigClusterRead(ctx, accountId, projectId, clusterId, apiClient)
clusterId := dbAuditLoggingConfig.ClusterID.Value
dbAuditLoggingConfig, readOK, err := resourceDbAuditLoggingRead(ctx, accountId, projectId, clusterId, apiClient)
if !readOK {
resp.Diagnostics.AddError("Unable to read the DB Audit Log Configuration associated to the cluster", message)
resp.Diagnostics.AddError(fmt.Sprintf("Unable to read the state of Db Audit logging on cluster %s ", clusterId), err.Error())
return
}

diags = resp.State.Set(ctx, &daeConfig)
diags = resp.State.Set(ctx, &dbAuditLoggingConfig)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down
18 changes: 10 additions & 8 deletions managed/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,16 @@ type AssociateMetricsExporterCluster struct {
ClusterName types.String `tfsdk:"cluster_name"`
}

type DbAuditExporterConfig struct {
AccountID types.String `tfsdk:"account_id"`
ProjectID types.String `tfsdk:"project_id"`
ClusterID types.String `tfsdk:"cluster_id"`
ExporterID types.String `tfsdk:"exporter_id"`
YsqlConfig *YsqlConfig `tfsdk:"ysql_config"`
State types.String `tfsdk:"state"`
ConfigID types.String `tfsdk:"config_id"`
type DbAuditLoggingConfig struct {
AccountID types.String `tfsdk:"account_id"`
ProjectID types.String `tfsdk:"project_id"`
ClusterID types.String `tfsdk:"cluster_id"`
ClusterName types.String `tfsdk:"cluster_name"`
IntegrationId types.String `tfsdk:"integration_id"`
IntegrationName types.String `tfsdk:"integration_name"`
YsqlConfig *YsqlConfig `tfsdk:"ysql_config"`
State types.String `tfsdk:"state"`
ConfigID types.String `tfsdk:"config_id"`
}

type YsqlConfig struct {
Expand Down
4 changes: 2 additions & 2 deletions managed/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (p *provider) GetResources(_ context.Context) (map[string]tfsdk.ResourceTyp

// Add DB Audit logging resource only if the feature flag is enabled
if fflags.IsFeatureFlagEnabled(fflags.DB_AUDIT_LOGGING) {
resources["ybm_associate_db_audit_export_config_cluster"] = resourceAssociateDbAuditExportConfigClusterType{}
resources["ybm_db_audit_logging"] = resourceDbAuditLoggingType{}
}

// Add DB Query logging resource only if the feature flag is enabled
Expand All @@ -183,7 +183,7 @@ func (p *provider) GetDataSources(_ context.Context) (map[string]tfsdk.DataSourc

// Add DB Audit logging data source only if the feature flag is enabled
if fflags.IsFeatureFlagEnabled(fflags.DB_AUDIT_LOGGING) {
dataSources["ybm_associate_db_audit_export_config_cluster"] = dataSourceAssociateDbAuditExportConfigClusterType{}
dataSources["ybm_db_audit_logging"] = dataSourceDbAuditLoggingType{}
}

return dataSources, nil
Expand Down
Loading
Loading