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

feat: Add retention_type to datasource ovh_dbaas_logs_cluster_retention #806

Merged
merged 1 commit into from
Jan 21, 2025
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/terraform-plugin-framework v1.13.0
github.com/hashicorp/terraform-plugin-framework-validators v0.15.0
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0
github.com/hashicorp/terraform-plugin-go v0.25.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-mux v0.16.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7
github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A=
github.com/hashicorp/terraform-plugin-framework v1.13.0 h1:8OTG4+oZUfKgnfTdPTJwZ532Bh2BobF4H+yBiYJ/scw=
github.com/hashicorp/terraform-plugin-framework v1.13.0/go.mod h1:j64rwMGpgM3NYXTKuxrCnyubQb/4VKldEKlcG8cvmjU=
github.com/hashicorp/terraform-plugin-framework-validators v0.15.0 h1:RXMmu7JgpFjnI1a5QjMCBb11usrW2OtAG+iOTIj5c9Y=
github.com/hashicorp/terraform-plugin-framework-validators v0.15.0/go.mod h1:Bh89/hNmqsEWug4/XWKYBwtnw3tbz5BAy1L1OgvbIaY=
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0 h1:O9QqGoYDzQT7lwTXUsZEtgabeWW96zUBh47Smn2lkFA=
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0/go.mod h1:Bh89/hNmqsEWug4/XWKYBwtnw3tbz5BAy1L1OgvbIaY=
github.com/hashicorp/terraform-plugin-go v0.25.0 h1:oi13cx7xXA6QciMcpcFi/rwA974rdTxjqEhXJjbAyks=
github.com/hashicorp/terraform-plugin-go v0.25.0/go.mod h1:+SYagMYadJP86Kvn+TGeV+ofr/R3g4/If0O5sO96MVw=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
28 changes: 25 additions & 3 deletions ovh/data_dbaas_logs_cluster_retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-log/tflog"
ovhtypes "github.com/ovh/terraform-provider-ovh/ovh/types"
)

var _ datasource.DataSourceWithConfigure = (*dbaasLogsClusterRetentionDataSource)(nil)
Expand Down Expand Up @@ -75,7 +77,7 @@ func (d *dbaasLogsClusterRetentionDataSource) Read(ctx context.Context, req data
return
}

// No retention ID given, try to fetch a retention with given duration
// No retention ID given, try to fetch a retention with given type and duration
var (
retentionIDs []string
availableDurations []string
Expand All @@ -87,6 +89,12 @@ func (d *dbaasLogsClusterRetentionDataSource) Read(ctx context.Context, req data
return
}

// If no retention_type given, default on LOGS_INDEXING value
if data.RetentionType.IsNull() {
tflog.Info(ctx, "no retention type defined, defaulting to LOGS_INDEXING")
data.RetentionType = ovhtypes.NewTfStringValue("LOGS_INDEXING")
}

for _, id := range retentionIDs {
var (
retentionData DbaasLogsClusterRetentionModel
Expand All @@ -98,6 +106,16 @@ func (d *dbaasLogsClusterRetentionDataSource) Read(ctx context.Context, req data
return
}

if !data.RetentionType.Equal(retentionData.RetentionType) {
tflog.Info(ctx, fmt.Sprintf("skipping retention %s with wrong type %s", retentionData.RetentionId, retentionData.RetentionType))
continue
}

if !retentionData.IsSupported.ValueBool() {
tflog.Info(ctx, fmt.Sprintf("skipping retention %s as it is not supported", retentionData.RetentionId))
continue
}

availableDurations = append(availableDurations, retentionData.Duration.ValueString())
if data.Duration.Equal(retentionData.Duration) {
data.MergeWith(&retentionData)
Expand All @@ -108,7 +126,11 @@ func (d *dbaasLogsClusterRetentionDataSource) Read(ctx context.Context, req data
}
}

// No retention found with given duration, error
// No retention found with given duration and type, error
errorDetails := ""
if len(availableDurations) > 0 {
errorDetails = ", available values are: " + strings.Join(availableDurations, ",")
}
resp.Diagnostics.AddError("retention not found",
fmt.Sprintf("no retention was found with duration %s, available values are: %s", data.Duration.ValueString(), strings.Join(availableDurations, ",")))
fmt.Sprintf("no retention was found with duration %s and type %s%s", data.Duration.ValueString(), data.RetentionType.ValueString(), errorDetails))
}
50 changes: 40 additions & 10 deletions ovh/data_dbaas_logs_cluster_retention_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 116 additions & 1 deletion ovh/data_dbaas_logs_cluster_retention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func TestAccDataSourceDbaasLogsClusterRetention_basic(t *testing.T) {
"is_supported",
"true",
),
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_cluster_retention.ret",
"retention_type",
"LOGS_INDEXING",
),
),
},
},
Expand Down Expand Up @@ -90,6 +95,60 @@ func TestAccDataSourceDbaasLogsClusterRetention_by_duration(t *testing.T) {
"is_supported",
"true",
),
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_cluster_retention.ret",
"retention_type",
"LOGS_INDEXING",
),
),
},
},
})
}

func TestAccDataSourceDbaasLogsClusterRetention_by_duration_and_type(t *testing.T) {
serviceName := os.Getenv("OVH_DBAAS_LOGS_SERVICE_TEST")
clusterId := os.Getenv("OVH_DBAAS_LOGS_CLUSTER_ID")
retentionId := os.Getenv("OVH_DBAAS_LOGS_CLUSTER_RETENTION_ID")

config := fmt.Sprintf(`
data "ovh_dbaas_logs_cluster_retention" "ret" {
service_name = "%s"
cluster_id = "%s"
duration = "P1Y"
retention_type = "LOGS_INDEXING"
}`,
serviceName,
clusterId,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckDbaasLogsClusterRetention(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_cluster_retention.ret",
"service_name",
serviceName,
),
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_cluster_retention.ret",
"retention_id",
retentionId,
),
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_cluster_retention.ret",
"is_supported",
"true",
),
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_cluster_retention.ret",
"retention_type",
"LOGS_INDEXING",
),
),
},
},
Expand All @@ -116,7 +175,34 @@ func TestAccDataSourceDbaasLogsClusterRetention_by_duration_not_found(t *testing
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("no retention was found with duration P1000Y"),
ExpectError: regexp.MustCompile("no retention was found with duration P1000Y and type LOGS_INDEXING"),
},
},
})
}

func TestAccDataSourceDbaasLogsClusterRetention_by_duration_and_type_not_found(t *testing.T) {
serviceName := os.Getenv("OVH_DBAAS_LOGS_SERVICE_TEST")
clusterId := os.Getenv("OVH_DBAAS_LOGS_CLUSTER_ID")

config := fmt.Sprintf(`
data "ovh_dbaas_logs_cluster_retention" "ret" {
service_name = "%s"
cluster_id = "%s"
duration = "P1Y"
retention_type = "METRICS_TENANT"
}`,
serviceName,
clusterId,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckDbaasLogsClusterRetention(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("no retention was found with duration P1Y and type METRICS_TENANT"),
},
},
})
Expand Down Expand Up @@ -146,3 +232,32 @@ func TestAccDataSourceDbaasLogsClusterRetention_missing_params(t *testing.T) {
},
})
}

func TestAccDataSourceDbaasLogsClusterRetention_invalid_params(t *testing.T) {
serviceName := os.Getenv("OVH_DBAAS_LOGS_SERVICE_TEST")
clusterId := os.Getenv("OVH_DBAAS_LOGS_CLUSTER_ID")
retentionId := os.Getenv("OVH_DBAAS_LOGS_CLUSTER_RETENTION_ID")

config := fmt.Sprintf(`
data "ovh_dbaas_logs_cluster_retention" "ret" {
service_name = "%s"
cluster_id = "%s"
retention_id = "%s"
retention_type = "LOGS_INDEXING"
}`,
serviceName,
clusterId,
retentionId,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckDbaasLogsCluster(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile(`Attribute \"retention_type\" cannot be specified when \"retention_id\"`),
},
},
})
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading