Skip to content

Commit

Permalink
refactor(userconfig): generate types list, rename integrations (#1615)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored Feb 23, 2024
1 parent 9a8276a commit 5296e0a
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

"github.com/aiven/terraform-provider-aiven/internal/common"
Expand All @@ -21,50 +20,50 @@ import (
"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/integration"
"github.com/aiven/terraform-provider-aiven/internal/sdkprovider/userconfig/serviceintegration"
)

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

// typesList integration type name as a key, and value is whether it has a config
func typesList() map[string]bool {
return map[string]bool{
"alertmanager": false,
"cassandra_cross_service_cluster": false,
"clickhouse_kafka": true,
"clickhouse_postgresql": true,
"dashboard": false,
"datadog": true,
"datasource": false,
"external_aws_cloudwatch_logs": false,
"external_aws_cloudwatch_metrics": true,
"external_elasticsearch_logs": false,
"external_google_cloud_logging": false,
"external_opensearch_logs": false,
"flink": false,
"internal_connectivity": false,
"jolokia": false,
"kafka_connect": true,
"kafka_logs": true,
"kafka_mirrormaker": true,
"logs": true,
"m3aggregator": false,
"m3coordinator": false,
"metrics": true,
"opensearch_cross_cluster_replication": false,
"opensearch_cross_cluster_search": false,
"prometheus": false,
"read_replica": false,
"rsyslog": false,
"schema_registry_proxy": false,
func hasConfig(kind string) bool {
return slices.Contains(serviceintegration.UserConfigTypes(), kind)
}

// typesList integration type list
func typesList() []string {
return []string{
"alertmanager",
"cassandra_cross_service_cluster",
"clickhouse_kafka",
"clickhouse_postgresql",
"dashboard",
"datadog",
"datasource",
"external_aws_cloudwatch_logs",
"external_aws_cloudwatch_metrics",
"external_elasticsearch_logs",
"external_google_cloud_logging",
"external_opensearch_logs",
"flink",
"internal_connectivity",
"jolokia",
"kafka_connect",
"kafka_logs",
"kafka_mirrormaker",
"logs",
"m3aggregator",
"m3coordinator",
"metrics",
"opensearch_cross_cluster_replication",
"opensearch_cross_cluster_search",
"prometheus",
"read_replica",
"rsyslog",
"schema_registry_proxy",
}
}

func aivenServiceIntegrationSchema() map[string]*schema.Schema {
types := typesList()
sortedTypes := maps.Keys(types)
slices.Sort(sortedTypes)

s := map[string]*schema.Schema{
"integration_id": {
Description: "Service Integration Id at aiven",
Expand All @@ -86,11 +85,11 @@ func aivenServiceIntegrationSchema() map[string]*schema.Schema {
Type: schema.TypeString,
},
"integration_type": {
Description: "Type of the service integration. Possible values: " + schemautil.JoinQuoted(sortedTypes, ", ", "`"),
Description: "Type of the service integration. Possible values: " + schemautil.JoinQuoted(typesList(), ", ", "`"),
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(sortedTypes, false),
ValidateFunc: validation.StringInSlice(typesList(), false),
},
"project": {
Description: "Project the integration belongs to",
Expand All @@ -115,10 +114,8 @@ func aivenServiceIntegrationSchema() map[string]*schema.Schema {
}

// Adds user configs
for _, k := range sortedTypes {
if types[k] {
s[k+"_user_config"] = integration.GetUserConfig(k)
}
for _, k := range serviceintegration.UserConfigTypes() {
s[k+"_user_config"] = serviceintegration.GetUserConfig(k)
}
return s
}
Expand Down Expand Up @@ -179,8 +176,8 @@ func resourceServiceIntegrationCreate(ctx context.Context, d *schema.ResourceDat
SourceService: schemautil.OptionalStringPointer(d, "source_service_name"),
}

if typesList()[integrationType] {
uc, err := converters.Expand(integrationType, integration.GetUserConfig(integrationType), d)
if hasConfig(integrationType) {
uc, err := converters.Expand(integrationType, serviceintegration.GetUserConfig(integrationType), d)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -394,8 +391,8 @@ func resourceServiceIntegrationCopyAPIResponseToTerraform(
return err
}

if typesList()[integrationType] {
userConfig, err := converters.Flatten(integrationType, integration.GetUserConfig(integrationType), d, res.UserConfig)
if hasConfig(integrationType) {
userConfig, err := converters.Flatten(integrationType, serviceintegration.GetUserConfig(integrationType), d, res.UserConfig)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions internal/sdkprovider/userconfig/service/service.go

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.

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.

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.

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.

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.

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

24 changes: 16 additions & 8 deletions ucgenerator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ const (

func main() {
var serviceList, integrationList string
flag.StringVar(&serviceList, "services", "", "Comma separated service list of names to generate for")
flag.StringVar(&integrationList, "integrations", "", "Comma separated integrations list of names to generate for")
flag.StringVar(&serviceList, "services", "", "Comma separated service list of names")
flag.StringVar(&integrationList, "integrations", "", "Comma separated integrations list of names")
flag.Parse()

if serviceList+integrationList == "" {
log.Fatal("--service or --integrations must be provided")
log.Fatal("--services or --integrations must be provided")
}

if serviceList != "" {
Expand All @@ -43,15 +43,17 @@ func main() {
}

if integrationList != "" {
err := generate("integration", dist.IntegrationTypes, strings.Split(integrationList, ","))
err := generate("serviceintegration", dist.IntegrationTypes, strings.Split(integrationList, ","))
if err != nil {
log.Fatal(err)
}
}
}

func generate(kind string, data []byte, keys []string) error {
slices.Sort(keys)
// Fixes imports order
imports.LocalPrefix = localPrefix

var root map[string]*object

err := yaml.Unmarshal(data, &root)
Expand All @@ -65,9 +67,7 @@ func generate(kind string, data []byte, keys []string) error {
return err
}

// Fixes imports order
imports.LocalPrefix = localPrefix

slices.Sort(keys)
doneKeys := make([]string, 0, len(keys))
doneNames := make([]string, 0, len(keys))

Expand Down Expand Up @@ -122,6 +122,14 @@ func generate(kind string, data []byte, keys []string) error {
f.Func().Id("GetUserConfig").Params(jen.Id("kind").String()).Op("*").Qual(importSchema, "Schema").Block(
jen.Switch(jen.Id("kind")).Block(cases...),
)

configTypes := make([]jen.Code, 0)
for _, v := range keys {
configTypes = append(configTypes, jen.Lit(v))
}
f.Func().Id("UserConfigTypes").Params().Index().String().Block(
jen.Return(jen.Index().String().Values(configTypes...)),
)
return f.Save(filepath.Join(dirPath, kind+".go"))
}

Expand Down

0 comments on commit 5296e0a

Please sign in to comment.