Skip to content

Commit

Permalink
fix: encapsulate user config field name logic
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Mar 15, 2024
1 parent 7baaba1 commit e423bc8
Show file tree
Hide file tree
Showing 24 changed files with 187 additions and 122 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ nav_order: 1
- Add `external_aws_cloudwatch_logs`, `external_elasticsearch_logs_user_config`, `external_opensearch_logs_user_config`,
`prometheus_user_config` service integration configs
- Fix `aiven_kafka_schema` Protobuf normalization
- Fix `aiven_service_integration_endpoint`, user config is required
- Add `AIVEN_ALLOW_IP_FILTER_PURGE` environment variable to allow purging of IP filters. This is a safety feature to
prevent accidental purging of IP filters, which can lead to loss of access to services. To enable purging, set the
environment variable to any value before running Terraform commands.
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.12.0
github.com/aiven/aiven-go-client/v2 v2.13.0
github.com/aiven/go-client-codegen v0.2.0
github.com/avast/retry-go v3.0.0+incompatible
github.com/dave/jennifer v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ github.com/ProtonMail/go-crypto v1.1.0-alpha.0/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/v2 v2.12.0 h1:VxOfn61AtfLjy2B+DdXF2/7OHyyFZ1aQaIlE3TOyIsE=
github.com/aiven/aiven-go-client/v2 v2.12.0/go.mod h1:x0xhzxWEKAwKv0xY5FvECiI6tesWshcPHvjwl0B/1SU=
github.com/aiven/aiven-go-client/v2 v2.13.0 h1:N2c+EdRh9gg2PBKAwII6vMy9+WnDnbVU4Uy1mQGmMKg=
github.com/aiven/aiven-go-client/v2 v2.13.0/go.mod h1:x0xhzxWEKAwKv0xY5FvECiI6tesWshcPHvjwl0B/1SU=
github.com/aiven/go-api-schemas v1.65.0 h1:r4ooY83kWwkQQPCq55W4oHitNv+SZ2fzVDxuY3KwU28=
github.com/aiven/go-api-schemas v1.65.0/go.mod h1:/bPxBUHza/2Aeer6hIIdB++GxKiw9K1KCBtRa2rtZ5I=
github.com/aiven/go-client-codegen v0.2.0 h1:f82CHhXCBbrBIUa3yworytXjiolp/Q73epnbRjQIwBk=
Expand Down
15 changes: 5 additions & 10 deletions internal/schemautil/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
)

// defaultTimeout is the default timeout for service operations. This is not a const because it can be changed during
Expand Down Expand Up @@ -713,15 +712,11 @@ func copyServicePropertiesFromAPIResponseToTerraform(
}
}

newUserConfig, err := FlattenService(serviceType, d, s.UserConfig)
err := FlattenService(serviceType, d, s.UserConfig)
if err != nil {
return err
}

if err := d.Set(serviceType+"_user_config", newUserConfig); err != nil {
return fmt.Errorf("cannot set `%s_user_config` : %w; Please make sure that all Aiven services have unique s names", serviceType, err)
}

params := s.URIParams
if err := d.Set("service_host", params["host"]); err != nil {
return err
Expand Down Expand Up @@ -886,10 +881,10 @@ func getContactEmailListForAPI(d *schema.ResourceData, field string) *[]aiven.Co
return &results
}

func ExpandService(kind string, d *schema.ResourceData) (map[string]any, error) {
return converters.Expand(kind, service.GetUserConfig(kind), d)
func ExpandService(name string, d *schema.ResourceData) (map[string]any, error) {
return converters.Expand(converters.ServiceUserConfig, name, d)
}

func FlattenService(kind string, d *schema.ResourceData, dto map[string]any) ([]map[string]any, error) {
return converters.Flatten(kind, service.GetUserConfig(kind), d, dto)
func FlattenService(name string, d *schema.ResourceData, dto map[string]any) error {
return converters.Flatten(converters.ServiceUserConfig, name, d, dto)
}
11 changes: 5 additions & 6 deletions internal/schemautil/userconfig/stateupgrader/v0/m3/m3db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader/typeupgrader"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader/v0/dist"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenM3DBSchema() map[string]*schema.Schema {
schemaM3 := schemautil.ServiceCommonSchema()
schemaM3[schemautil.ServiceTypeM3] = &schema.Schema{
s := schemautil.ServiceCommonSchema()
s[schemautil.ServiceTypeM3] = &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "M3 specific server provided values",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
}
schemaM3[schemautil.ServiceTypeM3+"_user_config"] = dist.ServiceTypeM3db()

return schemaM3
converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeM3, s)
return s
}

func ResourceM3DBResource() *schema.Resource {
Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func cassandraSchema() map[string]*schema.Schema {
Expand All @@ -19,8 +19,7 @@ func cassandraSchema() map[string]*schema.Schema {
Schema: map[string]*schema.Schema{},
},
}
s[schemautil.ServiceTypeCassandra+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeCassandra)

converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeCassandra, s)
return s
}

Expand Down
4 changes: 2 additions & 2 deletions internal/sdkprovider/service/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func clickhouseSchema() map[string]*schema.Schema {
Expand All @@ -18,7 +18,7 @@ func clickhouseSchema() map[string]*schema.Schema {
Schema: map[string]*schema.Schema{},
},
}
s[schemautil.ServiceTypeClickhouse+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeClickhouse)
converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeClickhouse, s)
s["service_integrations"] = &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/dragonfly/dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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/dist"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func dragonflySchema() map[string]*schema.Schema {
Expand All @@ -18,8 +18,7 @@ func dragonflySchema() map[string]*schema.Schema {
Schema: map[string]*schema.Schema{},
},
}
s[schemautil.ServiceTypeDragonfly+"_user_config"] = dist.ServiceTypeDragonfly()

converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeDragonfly, s)
return s
}

Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/flink/flink.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenFlinkSchema() map[string]*schema.Schema {
Expand All @@ -35,8 +35,7 @@ func aivenFlinkSchema() map[string]*schema.Schema {
},
},
}
aivenFlinkSchema[schemautil.ServiceTypeFlink+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeFlink)

converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeFlink, aivenFlinkSchema)
return aivenFlinkSchema
}

Expand Down
4 changes: 2 additions & 2 deletions internal/sdkprovider/service/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func grafanaSchema() map[string]*schema.Schema {
Expand All @@ -19,7 +19,7 @@ func grafanaSchema() map[string]*schema.Schema {
Schema: map[string]*schema.Schema{},
},
}
s[schemautil.ServiceTypeGrafana+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeGrafana)
converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeGrafana, s)
return s
}

Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func influxDBSchema() map[string]*schema.Schema {
Expand All @@ -25,8 +25,7 @@ func influxDBSchema() map[string]*schema.Schema {
},
},
}
s[schemautil.ServiceTypeInfluxDB+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeInfluxDB)

converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeInfluxDB, s)
return s
}

Expand Down
4 changes: 2 additions & 2 deletions internal/sdkprovider/service/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenKafkaSchema() map[string]*schema.Schema {
Expand Down Expand Up @@ -70,8 +70,8 @@ func aivenKafkaSchema() map[string]*schema.Schema {
},
},
}
aivenKafkaSchema[schemautil.ServiceTypeKafka+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeKafka)

converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeKafka, aivenKafkaSchema)
return aivenKafkaSchema
}

Expand Down
11 changes: 5 additions & 6 deletions internal/sdkprovider/service/kafka/kafka_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenKafkaConnectSchema() map[string]*schema.Schema {
kafkaConnectSchema := schemautil.ServiceCommonSchema()
kafkaConnectSchema[schemautil.ServiceTypeKafkaConnect] = &schema.Schema{
s := schemautil.ServiceCommonSchema()
s[schemautil.ServiceTypeKafkaConnect] = &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Kafka Connect server provided values",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
}
kafkaConnectSchema[schemautil.ServiceTypeKafkaConnect+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeKafkaConnect)

return kafkaConnectSchema
converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeKafkaConnect, s)
return s
}

func ResourceKafkaConnect() *schema.Resource {
Expand Down
11 changes: 5 additions & 6 deletions internal/sdkprovider/service/kafka/kafka_mirrormaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenKafkaMirrormakerSchema() map[string]*schema.Schema {
kafkaMMSchema := schemautil.ServiceCommonSchema()
kafkaMMSchema[schemautil.ServiceTypeKafkaMirrormaker] = &schema.Schema{
s := schemautil.ServiceCommonSchema()
s[schemautil.ServiceTypeKafkaMirrormaker] = &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Kafka MirrorMaker 2 server provided values",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
}
kafkaMMSchema[schemautil.ServiceTypeKafkaMirrormaker+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeKafkaMirrormaker)

return kafkaMMSchema
converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeKafkaMirrormaker, s)
return s
}
func ResourceKafkaMirrormaker() *schema.Resource {
return &schema.Resource{
Expand Down
11 changes: 5 additions & 6 deletions internal/sdkprovider/service/m3db/m3aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenM3AggregatorSchema() map[string]*schema.Schema {
schemaM3 := schemautil.ServiceCommonSchema()
schemaM3[schemautil.ServiceTypeM3Aggregator] = &schema.Schema{
s := schemautil.ServiceCommonSchema()
s[schemautil.ServiceTypeM3Aggregator] = &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "M3 aggregator specific server provided values",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
}
schemaM3[schemautil.ServiceTypeM3Aggregator+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeM3Aggregator)

return schemaM3
converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeM3Aggregator, s)
return s
}
func ResourceM3Aggregator() *schema.Resource {
return &schema.Resource{
Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/m3db/m3db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenM3DBSchema() map[string]*schema.Schema {
Expand All @@ -19,8 +19,7 @@ func aivenM3DBSchema() map[string]*schema.Schema {
Schema: map[string]*schema.Schema{},
},
}
schemaM3[schemautil.ServiceTypeM3+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeM3)

converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeM3, schemaM3)
return schemaM3
}
func ResourceM3DB() *schema.Resource {
Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenMySQLSchema() map[string]*schema.Schema {
Expand All @@ -19,8 +19,7 @@ func aivenMySQLSchema() map[string]*schema.Schema {
Schema: map[string]*schema.Schema{},
},
}
schemaMySQL[schemautil.ServiceTypeMySQL+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeMySQL)

converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeMySQL, schemaMySQL)
return schemaMySQL
}
func ResourceMySQL() *schema.Resource {
Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/opensearch/opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func opensearchSchema() map[string]*schema.Schema {
Expand All @@ -26,8 +26,7 @@ func opensearchSchema() map[string]*schema.Schema {
},
},
}
s[schemautil.ServiceTypeOpenSearch+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeOpenSearch)

converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeOpenSearch, s)
return s
}

Expand Down
11 changes: 5 additions & 6 deletions internal/sdkprovider/service/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters"
)

func aivenPGSchema() map[string]*schema.Schema {
schemaPG := schemautil.ServiceCommonSchema()
schemaPG[schemautil.ServiceTypePG] = &schema.Schema{
s := schemautil.ServiceCommonSchema()
s[schemautil.ServiceTypePG] = &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Expand Down Expand Up @@ -78,9 +78,8 @@ func aivenPGSchema() map[string]*schema.Schema {
},
},
}
schemaPG[schemautil.ServiceTypePG+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypePG)

return schemaPG
converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypePG, s)
return s
}

func ResourcePG() *schema.Resource {
Expand Down
Loading

0 comments on commit e423bc8

Please sign in to comment.