Skip to content

Commit

Permalink
refactor(aiven_service_integration_endpoint): new user configs
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Feb 27, 2024
1 parent 7d5dd47 commit 2fcf7db
Show file tree
Hide file tree
Showing 21 changed files with 764 additions and 88 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ nav_order: 1
<!-- TODO: uncomment when dragonfly is supported -->
<!-- - Dragonfly support -->

- Use new user config generator to generate service integration configs
- Use new user config generator to generate service integration and service integration endpoint configs
- Fix `aiven_kafka_schema` version update
- Add `external_aws_cloudwatch_logs`, `external_elasticsearch_logs_user_config`, `external_opensearch_logs_user_config`,
`prometheus_user_config` service integration configs
Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/service_integration_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data "aiven_service_integration_endpoint" "myendpoint" {

- `datadog_user_config` (List of Object) Datadog user configurable settings (see [below for nested schema](#nestedatt--datadog_user_config))
- `endpoint_config` (Map of String) Integration endpoint specific backend configuration
- `endpoint_type` (String) Type of the service integration endpoint. Possible values: `datadog`, `prometheus`, `rsyslog`, `external_elasticsearch_logs`, `external_opensearch_logs`, `external_aws_cloudwatch_logs`, `external_google_cloud_logging`, `external_kafka`, `jolokia`, `external_schema_registry`, `external_aws_cloudwatch_metrics`, `external_google_cloud_bigquery`, `external_postgresql`
- `endpoint_type` (String) Type of the service integration endpoint. Possible values: `autoscaler`, `datadog`, `external_aws_cloudwatch_logs`, `external_aws_cloudwatch_metrics`, `external_aws_s3`, `external_clickhouse`, `external_elasticsearch_logs`, `external_google_cloud_bigquery`, `external_google_cloud_logging`, `external_kafka`, `external_mysql`, `external_opensearch_logs`, `external_postgresql`, `external_redis`, `external_schema_registry`, `jolokia`, `prometheus`, `rsyslog`
- `external_aws_cloudwatch_logs_user_config` (List of Object) ExternalAwsCloudwatchLogs user configurable settings (see [below for nested schema](#nestedatt--external_aws_cloudwatch_logs_user_config))
- `external_aws_cloudwatch_metrics_user_config` (List of Object) ExternalAwsCloudwatchMetrics user configurable settings (see [below for nested schema](#nestedatt--external_aws_cloudwatch_metrics_user_config))
- `external_elasticsearch_logs_user_config` (List of Object) ExternalElasticsearchLogs user configurable settings (see [below for nested schema](#nestedatt--external_elasticsearch_logs_user_config))
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/service_integration_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The Service Integration Endpoint resource allows the creation and management of
### Required

- `endpoint_name` (String) Name of the service integration endpoint
- `endpoint_type` (String) Type of the service integration endpoint. Possible values: `datadog`, `prometheus`, `rsyslog`, `external_elasticsearch_logs`, `external_opensearch_logs`, `external_aws_cloudwatch_logs`, `external_google_cloud_logging`, `external_kafka`, `jolokia`, `external_schema_registry`, `external_aws_cloudwatch_metrics`, `external_google_cloud_bigquery`, `external_postgresql`
- `endpoint_type` (String) Type of the service integration endpoint. Possible values: `autoscaler`, `datadog`, `external_aws_cloudwatch_logs`, `external_aws_cloudwatch_metrics`, `external_aws_s3`, `external_clickhouse`, `external_elasticsearch_logs`, `external_google_cloud_bigquery`, `external_google_cloud_logging`, `external_kafka`, `external_mysql`, `external_opensearch_logs`, `external_postgresql`, `external_redis`, `external_schema_registry`, `jolokia`, `prometheus`, `rsyslog`
- `project` (String) Project the service integration endpoint belongs to

### Optional
Expand Down Expand Up @@ -52,7 +52,7 @@ Required:

Optional:

- `datadog_tags` (Block List, Max: 32) Custom tags provided by user. (see [below for nested schema](#nestedblock--datadog_user_config--datadog_tags))
- `datadog_tags` (Block List, Max: 32) Custom tags provided by user (see [below for nested schema](#nestedblock--datadog_user_config--datadog_tags))
- `disable_consumer_stats` (Boolean) Disable consumer group metrics.
- `kafka_consumer_check_instances` (Number) Number of separate instances to fetch kafka consumer statistics with.
- `kafka_consumer_stats_timeout` (Number) Number of seconds that datadog will wait to get consumer statistics from brokers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

const serviceIntegrationEndpointRegExp = "^[a-zA-Z0-9_-]*\\/{1}[a-zA-Z0-9_-]*$"

func hasConfig(kind string) bool {
func hasIntegrationConfig(kind string) bool {
return slices.Contains(serviceintegration.UserConfigTypes(), kind)
}

Expand Down Expand Up @@ -143,7 +143,7 @@ func resourceServiceIntegrationCreate(ctx context.Context, d *schema.ResourceDat
SourceService: schemautil.OptionalStringPointer(d, "source_service_name"),
}

if hasConfig(integrationType) {
if hasIntegrationConfig(integrationType) {
uc, err := converters.Expand(integrationType, serviceintegration.GetUserConfig(integrationType), d)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -358,7 +358,7 @@ func resourceServiceIntegrationCopyAPIResponseToTerraform(
return err
}

if hasConfig(integrationType) {
if hasIntegrationConfig(integrationType) {
userConfig, err := converters.Flatten(integrationType, serviceintegration.GetUserConfig(integrationType), d, res.UserConfig)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package serviceintegration
import (
"context"
"fmt"
"slices"

"github.com/aiven/aiven-go-client/v2"
codegenintegrations "github.com/aiven/go-client-codegen/handler/serviceintegration"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -13,66 +15,59 @@ import (
"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/dist"
"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"
)

var integrationEndpointTypes = []string{
"datadog",
"prometheus",
"rsyslog",
"external_elasticsearch_logs",
"external_opensearch_logs",
"external_aws_cloudwatch_logs",
"external_google_cloud_logging",
"external_kafka",
"jolokia",
"external_schema_registry",
"external_aws_cloudwatch_metrics",
"external_google_cloud_bigquery",
"external_postgresql",
func hasEndpointConfig(kind string) bool {
return slices.Contains(serviceintegrationendpoint.UserConfigTypes(), kind)
}

var aivenServiceIntegrationEndpointSchema = map[string]*schema.Schema{
"project": {
Description: "Project the service integration endpoint belongs to",
ForceNew: true,
Required: true,
Type: schema.TypeString,
},
"endpoint_name": {
ForceNew: true,
Description: "Name of the service integration endpoint",
Required: true,
Type: schema.TypeString,
},
"endpoint_type": {
Description: "Type of the service integration endpoint. Possible values: " +
schemautil.JoinQuoted(integrationEndpointTypes, ", ", "`"),
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(integrationEndpointTypes, false),
},
"endpoint_config": {
Description: "Integration endpoint specific backend configuration",
Computed: true,
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
},
"datadog_user_config": dist.IntegrationEndpointTypeDatadog(),
"prometheus_user_config": dist.IntegrationEndpointTypePrometheus(),
"rsyslog_user_config": dist.IntegrationEndpointTypeRsyslog(),
"external_elasticsearch_logs_user_config": dist.IntegrationEndpointTypeExternalElasticsearchLogs(),
"external_opensearch_logs_user_config": dist.IntegrationEndpointTypeExternalOpensearchLogs(),
"external_aws_cloudwatch_logs_user_config": dist.IntegrationEndpointTypeExternalAwsCloudwatchLogs(),
"external_google_cloud_logging_user_config": dist.IntegrationEndpointTypeExternalGoogleCloudLogging(),
"external_kafka_user_config": dist.IntegrationEndpointTypeExternalKafka(),
"jolokia_user_config": dist.IntegrationEndpointTypeJolokia(),
"external_schema_registry_user_config": dist.IntegrationEndpointTypeExternalSchemaRegistry(),
"external_aws_cloudwatch_metrics_user_config": dist.IntegrationEndpointTypeExternalAwsCloudwatchMetrics(),
"external_google_cloud_bigquery": dist.IntegrationEndpointTypeExternalGoogleCloudBigquery(),
"external_postgresql": dist.IntegrationEndpointTypeExternalPostgresql(),
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": {
Description: "Project the service integration endpoint belongs to",
ForceNew: true,
Required: true,
Type: schema.TypeString,
},
"endpoint_name": {
ForceNew: true,
Description: "Name of the service integration endpoint",
Required: true,
Type: schema.TypeString,
},
"endpoint_type": {
Description: "Type of the service integration endpoint. Possible values: " +
schemautil.JoinQuoted(codegenintegrations.EndpointTypeChoices(), ", ", "`"),
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(codegenintegrations.EndpointTypeChoices(), false),
},
"endpoint_config": {
Description: "Integration endpoint specific backend configuration",
Computed: true,
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
},
}

// Adds user configs
for _, k := range serviceintegrationendpoint.UserConfigTypes() {
s[endpointUserConfigKey(k)] = serviceintegrationendpoint.GetUserConfig(k)
}
return s
}

func ResourceServiceIntegrationEndpoint() *schema.Resource {
Expand All @@ -87,7 +82,7 @@ func ResourceServiceIntegrationEndpoint() *schema.Resource {
},
Timeouts: schemautil.DefaultResourceTimeouts(),

Schema: aivenServiceIntegrationEndpointSchema,
Schema: aivenServiceIntegrationEndpointSchema(),
SchemaVersion: 1,
StateUpgraders: stateupgrader.ServiceIntegrationEndpoint(),
}
Expand All @@ -98,20 +93,20 @@ func resourceServiceIntegrationEndpointCreate(ctx context.Context, d *schema.Res
projectName := d.Get("project").(string)
endpointType := d.Get("endpoint_type").(string)

userConfig, err := apiconvert.ToAPI(userconfig.IntegrationEndpointTypes, endpointType, d)
if err != nil {
return diag.FromErr(err)
req := aiven.CreateServiceIntegrationEndpointRequest{
EndpointName: d.Get("endpoint_name").(string),
EndpointType: endpointType,
}

endpoint, err := client.ServiceIntegrationEndpoints.Create(
ctx,
projectName,
aiven.CreateServiceIntegrationEndpointRequest{
EndpointName: d.Get("endpoint_name").(string),
EndpointType: endpointType,
UserConfig: userConfig,
},
)
if hasEndpointConfig(endpointType) {
uc, err := converters.Expand(endpointType, serviceintegrationendpoint.GetUserConfig(endpointType), d)
if err != nil {
return diag.FromErr(err)
}
req.UserConfig = uc
}

endpoint, err := client.ServiceIntegrationEndpoints.Create(ctx, projectName, req)

if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -205,16 +200,6 @@ func copyServiceIntegrationEndpointPropertiesFromAPIResponseToTerraform(
return err
}

userConfig, err := apiconvert.FromAPI(userconfig.IntegrationEndpointTypes, endpointType, endpoint.UserConfig)
if err != nil {
return err
}

if len(userConfig) > 0 {
if err := d.Set(endpointType+"_user_config", userConfig); err != nil {
return err
}
}
// Must coerse all values into strings
endpointConfig := map[string]string{}
if len(endpoint.EndpointConfig) > 0 {
Expand All @@ -223,5 +208,20 @@ func copyServiceIntegrationEndpointPropertiesFromAPIResponseToTerraform(
}
}

return d.Set("endpoint_config", endpointConfig)
if err := d.Set("endpoint_config", endpointConfig); err != nil {
return err
}

if hasEndpointConfig(endpointType) {
userConfig, err := converters.Flatten(endpointType, serviceintegrationendpoint.GetUserConfig(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
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func DatasourceServiceIntegrationEndpoint() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceServiceIntegrationEndpointRead,
Description: "The Service Integration Endpoint data source provides information about the existing Aiven Service Integration Endpoint.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(aivenServiceIntegrationEndpointSchema,
Schema: schemautil.ResourceSchemaAsDatasourceSchema(aivenServiceIntegrationEndpointSchema(),
"project", "endpoint_name"),
}
}
Expand Down

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

0 comments on commit 2fcf7db

Please sign in to comment.