Skip to content

Commit

Permalink
refactor: auto beta description (#1792)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored Jul 22, 2024
1 parent 60d4fcd commit 00f1f30
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 49 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ nav_order: 1
## [MAJOR.MINOR.PATCH] - YYYY-MM-DD

- Fix `aiven_transit_gateway_vpc_attachment`: remove `peer_region` deprecation, mark the field as create only.
- Add `aiven_valkey` resource
- Add `aiven_valkey_user` resource
- Add `aiven_valkey` resource as a beta resource
- Add `aiven_valkey_user` resource as a beta resource
- Temporarily remove the `aiven_project_user` deprecation until we find a suitable alternative.

## [4.20.0] - 2024-06-26
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/aiven/terraform-provider-aiven
go 1.22

require (
github.com/aiven/aiven-go-client/v2 v2.23.1-0.20240711065411-9fe89fbb76d1
github.com/aiven/aiven-go-client/v2 v2.24.0
github.com/aiven/go-client-codegen v0.15.0
github.com/avast/retry-go v3.0.0+incompatible
github.com/dave/jennifer v1.7.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,8 @@ github.com/ProtonMail/go-crypto v1.1.0-alpha.2/go.mod h1:rA3QumHc/FZ8pAHreoekgiA
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/aiven/aiven-go-client v1.36.0 h1:AUuldvYdk2b9wu4v7L9qx01d6ZB5VckiMgRW37yxZVE=
github.com/aiven/aiven-go-client v1.36.0/go.mod h1:3Hh1PDNcqNNCYrkU/jSAHMV/b/ynoy73fwhBPKnMe6I=
github.com/aiven/aiven-go-client/v2 v2.23.1-0.20240711065411-9fe89fbb76d1 h1:hJcbUO6BVWdMKmICVEF2O4A7oX4sWD1nepkLsra+ZKc=
github.com/aiven/aiven-go-client/v2 v2.23.1-0.20240711065411-9fe89fbb76d1/go.mod h1:KdHfLIlIRZIfCSEBd39j1Q81jlSb6Nd+oCQKqERfnuA=
github.com/aiven/aiven-go-client/v2 v2.24.0 h1:Ce9xXuTqs8EsjD0Vj67qhMnnYV/9UXzFL0OfGGFUaSs=
github.com/aiven/aiven-go-client/v2 v2.24.0/go.mod h1:KdHfLIlIRZIfCSEBd39j1Q81jlSb6Nd+oCQKqERfnuA=
github.com/aiven/go-api-schemas v1.79.0 h1:V6H7XKbsgfwWWLBazj53ZiQygCZdMB9os2ZP5wvgzIw=
github.com/aiven/go-api-schemas v1.79.0/go.mod h1:FYR22WcKLisZ1CYqyyGk6XqNSyxfAUtaQd+P2ydwc5A=
github.com/aiven/go-client-codegen v0.15.0 h1:ZM2qboMC7mrvpMJbjyd5C71XxfHDjh7G285N+HwiFH4=
Expand Down
38 changes: 30 additions & 8 deletions internal/sdkprovider/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"fmt"
"os"
"strings"

Expand All @@ -10,6 +11,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/plugin/util"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/account"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/cassandra"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/clickhouse"
Expand Down Expand Up @@ -272,10 +274,20 @@ func Provider(version string) *schema.Provider {
},
}

// Removes beta resources when the beta flag is missing
if !util.IsBeta() {
removeBetaResources(p.ResourcesMap)
removeBetaResources(p.DataSourcesMap)
// Adds "beta" warning to the description
betaResources := []string{
"aiven_thanos",
"aiven_valkey",
"aiven_valkey_user",
}

missing := append(
addBeta(p.ResourcesMap, betaResources...),
addBeta(p.DataSourcesMap, betaResources...)...,
)

if missing != nil {
panic(fmt.Errorf("not all beta resources/datasources are found: %s", strings.Join(missing, ", ")))
}

p.ConfigureContextFunc = func(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand All @@ -301,11 +313,21 @@ func Provider(version string) *schema.Provider {
return p
}

// removeBetaResources removes resources marked as beta
func removeBetaResources(m map[string]*schema.Resource) {
for k, v := range m {
if strings.Contains(v.Description, util.AivenEnableBeta) {
// addBeta adds resources as beta or removes them
func addBeta(m map[string]*schema.Resource, keys ...string) (missing []string) {
isBeta := util.IsBeta()
for _, k := range keys {
v, ok := m[k]
if !ok {
missing = append(missing, k)
continue
}

if isBeta {
v.Description = userconfig.Desc(v.Description).AvailabilityType(userconfig.Beta).Build()
} else {
delete(m, k)
}
}
return missing
}
8 changes: 1 addition & 7 deletions internal/sdkprovider/service/thanos/thanos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig"
)

func thanosSchema() map[string]*schema.Schema {
Expand Down Expand Up @@ -66,12 +65,7 @@ func thanosSchema() map[string]*schema.Schema {

func ResourceThanos() *schema.Resource {
return &schema.Resource{
Description: userconfig.Desc(
"Creates and manages an " +
"[Aiven for Metrics®](https://aiven.io/docs/products/metrics/concepts/metrics-overview) service.",
).
AvailabilityType(userconfig.Beta).
Build(),
Description: "Creates and manages an [Aiven for Metrics®](https://aiven.io/docs/products/metrics/concepts/metrics-overview) service.",
CreateContext: schemautil.ResourceServiceCreateWrapper(schemautil.ServiceTypeThanos),
ReadContext: schemautil.ResourceServiceRead,
UpdateContext: schemautil.ResourceServiceUpdate,
Expand Down
7 changes: 2 additions & 5 deletions internal/sdkprovider/service/thanos/thanos_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig"
)

func DatasourceThanos() *schema.Resource {
return &schema.Resource{
ReadContext: schemautil.DatasourceServiceRead,
Description: userconfig.Desc("Gets information about an Aiven for Thanos® service.").
AvailabilityType(userconfig.Beta).
Build(),
Schema: schemautil.ResourceSchemaAsDatasourceSchema(thanosSchema(), "project", "service_name"),
Description: "Gets information about an Aiven for Thanos® service.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(thanosSchema(), "project", "service_name"),
}
}
8 changes: 1 addition & 7 deletions internal/sdkprovider/service/valkey/valkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig"
)

func valkeySchema() map[string]*schema.Schema {
Expand Down Expand Up @@ -56,12 +55,7 @@ func valkeySchema() map[string]*schema.Schema {

func ResourceValkey() *schema.Resource {
return &schema.Resource{
Description: userconfig.Desc(
"Creates and manages an " +
"[Aiven for Valkey](https://aiven.io/docs/products/valkey) service.",
).
AvailabilityType(userconfig.Beta).
Build(),
Description: "Creates and manages an [Aiven for Valkey](https://aiven.io/docs/products/valkey) service.",
CreateContext: schemautil.ResourceServiceCreateWrapper(schemautil.ServiceTypeValkey),
ReadContext: schemautil.ResourceServiceRead,
UpdateContext: schemautil.ResourceServiceUpdate,
Expand Down
7 changes: 2 additions & 5 deletions internal/sdkprovider/service/valkey/valkey_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig"
)

func DatasourceValkey() *schema.Resource {
return &schema.Resource{
ReadContext: schemautil.DatasourceServiceRead,
Description: userconfig.Desc("Gets information about an Aiven for Valkey service.").
AvailabilityType(userconfig.Beta).
Build(),
Schema: schemautil.ResourceSchemaAsDatasourceSchema(valkeySchema(), "project", "service_name"),
Description: "Gets information about an Aiven for Valkey service.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(valkeySchema(), "project", "service_name"),
}
}
7 changes: 1 addition & 6 deletions internal/sdkprovider/service/valkey/valkey_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ var aivenValkeyUserSchema = map[string]*schema.Schema{

func ResourceValkeyUser() *schema.Resource {
return &schema.Resource{
Description: userconfig.Desc(
"Creates and manages an " +
"[Aiven for Valkey](https://aiven.io/docs/products/valkey) user.",
).
AvailabilityType(userconfig.Beta).
Build(),
Description: "Creates and manages an [Aiven for Valkey](https://aiven.io/docs/products/valkey) user.",
CreateContext: common.WithGenClient(resourceValkeyUserCreate),
UpdateContext: common.WithGenClient(resourceValkeyUserUpdate),
ReadContext: common.WithGenClient(resourceValkeyUserRead),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig"
)

func DatasourceValkeyUser() *schema.Resource {
return &schema.Resource{
ReadContext: common.WithGenClient(datasourceValkeyUserRead),
Description: userconfig.Desc("The Valkey User data source provides information about the existing Aiven for Valkey user.").
AvailabilityType(userconfig.Beta).
Build(),
Description: "The Valkey User data source provides information about the existing Aiven for Valkey user.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(aivenValkeyUserSchema,
"project", "service_name", "username"),
}
Expand Down

0 comments on commit 00f1f30

Please sign in to comment.