diff --git a/CHANGELOG.md b/CHANGELOG.md index de4b8cb3c..1c65b6f41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/go.mod b/go.mod index 415f53cdd..84a9a04b3 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3ac8b7b4b..1da15b22b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/schemautil/service.go b/internal/schemautil/service.go index 4ce618aa8..7151dbee4 100644 --- a/internal/schemautil/service.go +++ b/internal/schemautil/service.go @@ -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 @@ -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 @@ -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) } diff --git a/internal/schemautil/userconfig/stateupgrader/v0/m3/m3db.go b/internal/schemautil/userconfig/stateupgrader/v0/m3/m3db.go index c8c07edd7..67e0ebe62 100644 --- a/internal/schemautil/userconfig/stateupgrader/v0/m3/m3db.go +++ b/internal/schemautil/userconfig/stateupgrader/v0/m3/m3db.go @@ -9,12 +9,12 @@ 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", @@ -22,9 +22,8 @@ func aivenM3DBSchema() map[string]*schema.Schema { 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 { diff --git a/internal/sdkprovider/service/cassandra/cassandra.go b/internal/sdkprovider/service/cassandra/cassandra.go index a141c1e19..4d01489e5 100644 --- a/internal/sdkprovider/service/cassandra/cassandra.go +++ b/internal/sdkprovider/service/cassandra/cassandra.go @@ -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 { @@ -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 } diff --git a/internal/sdkprovider/service/clickhouse/clickhouse.go b/internal/sdkprovider/service/clickhouse/clickhouse.go index b7ab4d006..7e7545b60 100644 --- a/internal/sdkprovider/service/clickhouse/clickhouse.go +++ b/internal/sdkprovider/service/clickhouse/clickhouse.go @@ -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 { @@ -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, diff --git a/internal/sdkprovider/service/dragonfly/dragonfly.go b/internal/sdkprovider/service/dragonfly/dragonfly.go index 015d53f47..03c987370 100644 --- a/internal/sdkprovider/service/dragonfly/dragonfly.go +++ b/internal/sdkprovider/service/dragonfly/dragonfly.go @@ -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 { @@ -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 } diff --git a/internal/sdkprovider/service/flink/flink.go b/internal/sdkprovider/service/flink/flink.go index d40d3b601..6ca98342c 100644 --- a/internal/sdkprovider/service/flink/flink.go +++ b/internal/sdkprovider/service/flink/flink.go @@ -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 { @@ -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 } diff --git a/internal/sdkprovider/service/grafana/grafana.go b/internal/sdkprovider/service/grafana/grafana.go index befe69c77..fe142c224 100644 --- a/internal/sdkprovider/service/grafana/grafana.go +++ b/internal/sdkprovider/service/grafana/grafana.go @@ -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 { @@ -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 } diff --git a/internal/sdkprovider/service/influxdb/influxdb.go b/internal/sdkprovider/service/influxdb/influxdb.go index 6e4a7dc46..4036b5dd6 100644 --- a/internal/sdkprovider/service/influxdb/influxdb.go +++ b/internal/sdkprovider/service/influxdb/influxdb.go @@ -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 { @@ -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 } diff --git a/internal/sdkprovider/service/kafka/kafka.go b/internal/sdkprovider/service/kafka/kafka.go index 9fc738555..50e858c44 100644 --- a/internal/sdkprovider/service/kafka/kafka.go +++ b/internal/sdkprovider/service/kafka/kafka.go @@ -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 { @@ -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 } diff --git a/internal/sdkprovider/service/kafka/kafka_connect.go b/internal/sdkprovider/service/kafka/kafka_connect.go index 90ed59498..c8f651f4d 100644 --- a/internal/sdkprovider/service/kafka/kafka_connect.go +++ b/internal/sdkprovider/service/kafka/kafka_connect.go @@ -6,12 +6,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 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", @@ -19,9 +19,8 @@ func aivenKafkaConnectSchema() map[string]*schema.Schema { 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 { diff --git a/internal/sdkprovider/service/kafka/kafka_mirrormaker.go b/internal/sdkprovider/service/kafka/kafka_mirrormaker.go index 589241239..1fc4447ca 100644 --- a/internal/sdkprovider/service/kafka/kafka_mirrormaker.go +++ b/internal/sdkprovider/service/kafka/kafka_mirrormaker.go @@ -6,12 +6,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 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", @@ -19,9 +19,8 @@ func aivenKafkaMirrormakerSchema() map[string]*schema.Schema { 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{ diff --git a/internal/sdkprovider/service/m3db/m3aggregator.go b/internal/sdkprovider/service/m3db/m3aggregator.go index 6e0593a34..8721708df 100644 --- a/internal/sdkprovider/service/m3db/m3aggregator.go +++ b/internal/sdkprovider/service/m3db/m3aggregator.go @@ -6,12 +6,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 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", @@ -19,9 +19,8 @@ func aivenM3AggregatorSchema() map[string]*schema.Schema { 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{ diff --git a/internal/sdkprovider/service/m3db/m3db.go b/internal/sdkprovider/service/m3db/m3db.go index e4ae79567..d43affaf7 100644 --- a/internal/sdkprovider/service/m3db/m3db.go +++ b/internal/sdkprovider/service/m3db/m3db.go @@ -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 { @@ -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 { diff --git a/internal/sdkprovider/service/mysql/mysql.go b/internal/sdkprovider/service/mysql/mysql.go index 11f3a0c7d..38f8ceb6f 100644 --- a/internal/sdkprovider/service/mysql/mysql.go +++ b/internal/sdkprovider/service/mysql/mysql.go @@ -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 { @@ -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 { diff --git a/internal/sdkprovider/service/opensearch/opensearch.go b/internal/sdkprovider/service/opensearch/opensearch.go index 2478b8538..18ca88591 100644 --- a/internal/sdkprovider/service/opensearch/opensearch.go +++ b/internal/sdkprovider/service/opensearch/opensearch.go @@ -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 { @@ -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 } diff --git a/internal/sdkprovider/service/pg/pg.go b/internal/sdkprovider/service/pg/pg.go index a6d85cd19..f84ed4e0b 100644 --- a/internal/sdkprovider/service/pg/pg.go +++ b/internal/sdkprovider/service/pg/pg.go @@ -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, @@ -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 { diff --git a/internal/sdkprovider/service/redis/redis.go b/internal/sdkprovider/service/redis/redis.go index 985de4efd..5d3cc9e5a 100644 --- a/internal/sdkprovider/service/redis/redis.go +++ b/internal/sdkprovider/service/redis/redis.go @@ -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 redisSchema() map[string]*schema.Schema { @@ -19,8 +19,7 @@ func redisSchema() map[string]*schema.Schema { Schema: map[string]*schema.Schema{}, }, } - s[schemautil.ServiceTypeRedis+"_user_config"] = service.GetUserConfig(schemautil.ServiceTypeRedis) - + converters.SetUserConfig(converters.ServiceUserConfig, schemautil.ServiceTypeRedis, s) return s } diff --git a/internal/sdkprovider/service/serviceintegration/service_integration.go b/internal/sdkprovider/service/serviceintegration/service_integration.go index 51d313a93..7818f94d7 100644 --- a/internal/sdkprovider/service/serviceintegration/service_integration.go +++ b/internal/sdkprovider/service/serviceintegration/service_integration.go @@ -82,7 +82,7 @@ func aivenServiceIntegrationSchema() map[string]*schema.Schema { // Adds user configs for _, k := range serviceintegration.UserConfigTypes() { - s[k+"_user_config"] = serviceintegration.GetUserConfig(k) + converters.SetUserConfig(converters.ServiceIntegrationUserConfig, k, s) } return s } @@ -144,7 +144,7 @@ func resourceServiceIntegrationCreate(ctx context.Context, d *schema.ResourceDat } if hasIntegrationConfig(integrationType) { - uc, err := converters.Expand(integrationType, serviceintegration.GetUserConfig(integrationType), d) + uc, err := converters.Expand(converters.ServiceIntegrationUserConfig, integrationType, d) if err != nil { return diag.FromErr(err) } @@ -359,16 +359,10 @@ func resourceServiceIntegrationCopyAPIResponseToTerraform( } if hasIntegrationConfig(integrationType) { - userConfig, err := converters.Flatten(integrationType, serviceintegration.GetUserConfig(integrationType), d, res.UserConfig) + err := converters.Flatten(converters.ServiceIntegrationUserConfig, integrationType, d, res.UserConfig) if err != nil { return err } - if len(userConfig) > 0 { - err := d.Set(integrationType+"_user_config", userConfig) - if err != nil { - return err - } - } } return nil diff --git a/internal/sdkprovider/service/serviceintegration/service_integration_endpoint.go b/internal/sdkprovider/service/serviceintegration/service_integration_endpoint.go index eee32f757..b7ec9bc88 100644 --- a/internal/sdkprovider/service/serviceintegration/service_integration_endpoint.go +++ b/internal/sdkprovider/service/serviceintegration/service_integration_endpoint.go @@ -13,8 +13,6 @@ 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" - "github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/apiconvert" "github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader" "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/converters" "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/serviceintegrationendpoint" @@ -24,15 +22,6 @@ func hasEndpointConfig(kind string) bool { return slices.Contains(serviceintegrationendpoint.UserConfigTypes(), kind) } -func endpointUserConfigKey(kind string) string { - switch kind { - case "external_google_cloud_bigquery", "external_postgresql": - // legacy fields - return kind - } - return kind + "_user_config" -} - func aivenServiceIntegrationEndpointSchema() map[string]*schema.Schema { s := map[string]*schema.Schema{ "project": { @@ -65,7 +54,7 @@ func aivenServiceIntegrationEndpointSchema() map[string]*schema.Schema { // Adds user configs for _, k := range serviceintegrationendpoint.UserConfigTypes() { - s[endpointUserConfigKey(k)] = serviceintegrationendpoint.GetUserConfig(k) + converters.SetUserConfig(converters.ServiceIntegrationEndpointUserConfig, k, s) } return s } @@ -96,14 +85,17 @@ func resourceServiceIntegrationEndpointCreate(ctx context.Context, d *schema.Res req := aiven.CreateServiceIntegrationEndpointRequest{ EndpointName: d.Get("endpoint_name").(string), EndpointType: endpointType, + UserConfig: make(map[string]interface{}), } if hasEndpointConfig(endpointType) { - uc, err := converters.Expand(endpointType, serviceintegrationendpoint.GetUserConfig(endpointType), d) + uc, err := converters.Expand(converters.ServiceIntegrationEndpointUserConfig, endpointType, d) if err != nil { return diag.FromErr(err) } - req.UserConfig = uc + if uc != nil { + req.UserConfig = uc + } } endpoint, err := client.ServiceIntegrationEndpoints.Create(ctx, projectName, req) @@ -147,20 +139,21 @@ func resourceServiceIntegrationEndpointUpdate(ctx context.Context, d *schema.Res } endpointType := d.Get("endpoint_type").(string) + req := aiven.UpdateServiceIntegrationEndpointRequest{ + UserConfig: make(map[string]interface{}), + } - userConfig, err := apiconvert.ToAPI(userconfig.IntegrationEndpointTypes, endpointType, d) - if err != nil { - return diag.FromErr(err) + if hasEndpointConfig(endpointType) { + uc, err := converters.Expand(converters.ServiceIntegrationEndpointUserConfig, endpointType, d) + if err != nil { + return diag.FromErr(err) + } + if uc != nil { + req.UserConfig = uc + } } - _, err = client.ServiceIntegrationEndpoints.Update( - ctx, - projectName, - endpointID, - aiven.UpdateServiceIntegrationEndpointRequest{ - UserConfig: userConfig, - }, - ) + _, err = client.ServiceIntegrationEndpoints.Update(ctx, projectName, endpointID, req) if err != nil { return diag.FromErr(err) } @@ -213,15 +206,10 @@ func copyServiceIntegrationEndpointPropertiesFromAPIResponseToTerraform( } if hasEndpointConfig(endpointType) { - userConfig, err := converters.Flatten(endpointType, serviceintegrationendpoint.GetUserConfig(endpointType), d, endpoint.UserConfig) + err := converters.Flatten(converters.ServiceIntegrationEndpointUserConfig, endpointType, d, endpoint.UserConfig) if err != nil { return err } - if len(userConfig) > 0 { - if err := d.Set(endpointUserConfigKey(endpointType), userConfig); err != nil { - return err - } - } } return nil } diff --git a/internal/sdkprovider/service/serviceintegration/service_integration_endpoint_test.go b/internal/sdkprovider/service/serviceintegration/service_integration_endpoint_test.go index 6ed61c6b0..ebd31bd19 100644 --- a/internal/sdkprovider/service/serviceintegration/service_integration_endpoint_test.go +++ b/internal/sdkprovider/service/serviceintegration/service_integration_endpoint_test.go @@ -283,3 +283,53 @@ func testAccCheckAivenServiceEndpointIntegrationAttributes(n string) resource.Te return nil } } + +func TestAccAivenServiceIntegrationEndpointExternalPostgresql(t *testing.T) { + resourceName := "aiven_service_integration_endpoint.pg" + rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + ProtoV6ProviderFactories: acc.TestProtoV6ProviderFactories, + CheckDestroy: testAccCheckAivenServiceIntegraitonEndpointResourceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAivenServiceIntegrationEndpointExternalPostgresql(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAivenServiceEndpointIntegrationAttributes(resourceName), + resource.TestCheckResourceAttr(resourceName, "project", os.Getenv("AIVEN_PROJECT_NAME")), + resource.TestCheckResourceAttr(resourceName, "endpoint_name", "test"), + resource.TestCheckResourceAttr(resourceName, "endpoint_type", "external_postgresql"), + resource.TestCheckResourceAttr(resourceName, "external_postgresql.0.port", "1234"), + resource.TestCheckResourceAttr(resourceName, "external_postgresql.0.ssl_mode", "disable"), + ), + }, + }, + }) +} + +func testAccAivenServiceIntegrationEndpointExternalPostgresql(name string) string { + return fmt.Sprintf(` +resource "aiven_pg" "pg" { + project = %q + cloud_name = "google-europe-west1" + plan = "startup-4" + service_name = %q +} + + +resource "aiven_service_integration_endpoint" "pg" { + project = aiven_pg.pg.project + endpoint_name = "test" + endpoint_type = "external_postgresql" + + external_postgresql { + username = aiven_pg.pg.service_username + password = aiven_pg.pg.service_password + host = aiven_pg.pg.service_host + port = 1234 + ssl_mode = "disable" + } +} +`, os.Getenv("AIVEN_PROJECT_NAME"), name) +} diff --git a/internal/sdkprovider/userconfig/converters/converters.go b/internal/sdkprovider/userconfig/converters/converters.go index 5e7796924..98d230134 100644 --- a/internal/sdkprovider/userconfig/converters/converters.go +++ b/internal/sdkprovider/userconfig/converters/converters.go @@ -21,18 +21,57 @@ import ( "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/service" + "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/serviceintegration" + "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/serviceintegrationendpoint" ) -const userConfigSuffix = "_user_config" +type userConfigType int + +const ( + ServiceUserConfig userConfigType = iota + ServiceIntegrationUserConfig + ServiceIntegrationEndpointUserConfig +) + +// userConfigKey provides a single source of truth for field naming +func userConfigKey(kind userConfigType, name string) string { + if kind == ServiceIntegrationEndpointUserConfig { + switch name { + case "external_google_cloud_bigquery", "external_postgresql": + // legacy fields + return name + } + } + return name + "_user_config" +} + +func getUserConfig(kind userConfigType, name string) *schema.Schema { + key := userConfigKey(kind, name) + switch kind { + case ServiceUserConfig: + return service.GetUserConfig(key) + case ServiceIntegrationUserConfig: + return serviceintegration.GetUserConfig(key) + case ServiceIntegrationEndpointUserConfig: + return serviceintegrationendpoint.GetUserConfig(key) + } + panic(fmt.Sprintf("unknown user config name %q with kind %q", name, kind)) +} + +func SetUserConfig(kind userConfigType, name string, s map[string]*schema.Schema) { + s[userConfigKey(kind, name)] = getUserConfig(kind, name) +} // Expand expands schema.ResourceData into a DTO map. // It takes schema.Schema to know how to turn a TF item into json. -func Expand(kind string, s *schema.Schema, d *schema.ResourceData) (map[string]any, error) { - key := kind + userConfigSuffix +func Expand(kind userConfigType, name string, d *schema.ResourceData) (map[string]any, error) { + key := userConfigKey(kind, name) state := &stateCompose{ key: key, path: key + ".0", // starts from root user config - schema: s, + schema: getUserConfig(kind, name), resource: d, } @@ -241,9 +280,10 @@ func expandAttr(state *stateCompose) (any, error) { return items, nil } -// Flatten flattens DTO into a terraform compatible object -func Flatten(kind string, s *schema.Schema, d *schema.ResourceData, dto map[string]any) ([]map[string]any, error) { - prefix := fmt.Sprintf("%s%s.0.", kind, userConfigSuffix) +// flatten flattens DTO into a terraform compatible object +func flatten(kind userConfigType, name string, d *schema.ResourceData, dto map[string]any) error { + key := userConfigKey(kind, name) + prefix := fmt.Sprintf("%s.0.", key) // Renames ip_filter field if _, ok := dto["ip_filter"]; ok { @@ -264,12 +304,22 @@ func Flatten(kind string, s *schema.Schema, d *schema.ResourceData, dto map[stri } } + s := getUserConfig(kind, name) r := s.Elem.(*schema.Resource) tfo, err := flattenObj(r.Schema, dto) if tfo == nil || err != nil { - return nil, err + return err + } + + return d.Set(key, []map[string]any{tfo}) +} + +func Flatten(kind userConfigType, name string, d *schema.ResourceData, dto map[string]any) error { + err := flatten(kind, name, d, dto) + if err != nil { + return fmt.Errorf("cannot set user_config: %w", err) } - return []map[string]any{tfo}, nil + return nil } func flattenObj(s map[string]*schema.Schema, dto map[string]any) (map[string]any, error) {