From 5ad69433addbd8c73b85206bc4429c3e360ecad3 Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Thu, 21 Mar 2024 12:36:06 +0100 Subject: [PATCH] feat: remove allow list, generate everything --- internal/filter/filter.go | 157 ------------------------------ internal/filter/filter_test.go | 171 --------------------------------- internal/gen/gen.go | 28 ++---- 3 files changed, 6 insertions(+), 350 deletions(-) delete mode 100644 internal/filter/filter.go delete mode 100644 internal/filter/filter_test.go diff --git a/internal/filter/filter.go b/internal/filter/filter.go deleted file mode 100644 index a00762a..0000000 --- a/internal/filter/filter.go +++ /dev/null @@ -1,157 +0,0 @@ -// Package filter is the package that contains the filter functionality. -package filter - -import ( - "errors" - - "github.com/aiven/aiven-go-client" - "github.com/mitchellh/copystructure" - "golang.org/x/exp/maps" -) - -// errUnexpected is the error that is returned when an unexpected error occurs. -var errUnexpected = errors.New("unexpected filtering error") - -// supportedServiceTypes is a pseudo-constant map of supported service types. -var supportedServiceTypes = func() map[string]struct{} { - return map[string]struct{}{ - "cassandra": {}, - "clickhouse": {}, - "datadog": {}, - "dragonfly": {}, - "elasticsearch": {}, - "external_aws_cloudwatch_logs": {}, - "external_aws_cloudwatch_metrics": {}, - "external_elasticsearch_logs": {}, - "external_google_cloud_bigquery": {}, - "external_google_cloud_logging": {}, - "external_kafka": {}, - "external_opensearch_logs": {}, - "external_postgresql": {}, - "flink": {}, - "grafana": {}, - "influxdb": {}, - "jolokia": {}, - "kafka": {}, - "kafka_connect": {}, - "kafka_mirrormaker": {}, - "m3aggregator": {}, - "m3coordinator": {}, - "m3db": {}, - "mysql": {}, - "opensearch": {}, - "pg": {}, - "prometheus": {}, - "redis": {}, - "rsyslog": {}, - } -} - -// ServiceTypes is a filter that filters out service types that are not supported. -func ServiceTypes(f map[string]aiven.ServiceType) (map[string]aiven.ServiceType, error) { - cf, err := copystructure.Copy(f) - if err != nil { - return nil, err - } - - acf, ok := cf.(map[string]aiven.ServiceType) - if !ok { - return nil, errUnexpected - } - - maps.DeleteFunc(acf, func(k string, _ aiven.ServiceType) bool { - _, ok = supportedServiceTypes()[k] - - return !ok - }) - - return acf, nil -} - -// IntegrationTypes is a filter that filters out integration types that are not supported. -func IntegrationTypes(f []aiven.IntegrationType) ([]aiven.IntegrationType, error) { - cf, err := copystructure.Copy(f) - if err != nil { - return nil, err - } - - acf, ok := cf.([]aiven.IntegrationType) - if !ok { - return nil, errUnexpected - } - - nf := make([]aiven.IntegrationType, 0, len(acf)) - - for _, v := range acf { - { - var dst []string - - for _, vn := range v.DestServiceTypes { - if _, ok = supportedServiceTypes()[vn]; ok { - dst = append(dst, vn) - } - } - - if len(dst) == 0 { - continue - } - - v.DestServiceTypes = dst - } - - { - var sst []string - - for _, vn := range v.SourceServiceTypes { - if _, ok = supportedServiceTypes()[vn]; ok { - sst = append(sst, vn) - } - } - - if len(sst) == 0 { - continue - } - - v.SourceServiceTypes = sst - } - - nf = append(nf, v) - } - - return nf, nil -} - -// IntegrationEndpointTypes is a filter that filters out integration endpoint types that are not supported. -func IntegrationEndpointTypes(f []aiven.IntegrationEndpointType) ([]aiven.IntegrationEndpointType, error) { - cf, err := copystructure.Copy(f) - if err != nil { - return nil, err - } - - acf, ok := cf.([]aiven.IntegrationEndpointType) - if !ok { - return nil, errUnexpected - } - - nf := make([]aiven.IntegrationEndpointType, 0, len(acf)) - - for _, v := range acf { - var st []string - - for _, vn := range v.ServiceTypes { - if _, ok = supportedServiceTypes()[vn]; ok { - st = append(st, vn) - } - } - - if len(st) == 0 { - break - } - - v.ServiceTypes = st - - nf = append(nf, v) - } - - return nf, nil -} diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go deleted file mode 100644 index f3b6a27..0000000 --- a/internal/filter/filter_test.go +++ /dev/null @@ -1,171 +0,0 @@ -// Package filter is the package that contains the filter functionality. -package filter - -import ( - "testing" - - "github.com/aiven/aiven-go-client" - "github.com/google/go-cmp/cmp" -) - -// TestServiceTypes is a test for ServiceTypes. -func TestServiceTypes(t *testing.T) { - type args struct { - f map[string]aiven.ServiceType - } - - tests := []struct { - name string - args args - want map[string]aiven.ServiceType - wantErr error - }{ - { - name: "basic", - args: args{ - f: map[string]aiven.ServiceType{ - "foo": {}, - "kafka": {}, - }, - }, - want: map[string]aiven.ServiceType{ - "kafka": {}, - }, - wantErr: nil, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := ServiceTypes(tt.args.f) - if !cmp.Equal(err, tt.wantErr) { - t.Errorf("ServiceTypes() error = %v, wantErr %v", err, tt.wantErr) - - return - } - - if !cmp.Equal(got, tt.want) { - t.Errorf("ServiceTypes() = %v, want %v", got, tt.want) - } - }) - } -} - -// TestIntegrationTypes is a test for IntegrationTypes. -func TestIntegrationTypes(t *testing.T) { - type args struct { - f []aiven.IntegrationType - } - - tests := []struct { - name string - args args - want []aiven.IntegrationType - wantErr error - }{ - { - name: "basic", - args: args{ - f: []aiven.IntegrationType{ - { - DestServiceTypes: []string{"pg"}, - SourceServiceTypes: []string{"kafka"}, - }, - { - DestServiceTypes: []string{"flink", "foo"}, - SourceServiceTypes: []string{"kafka"}, - }, - }, - }, - want: []aiven.IntegrationType{ - { - DestServiceTypes: []string{"pg"}, - SourceServiceTypes: []string{"kafka"}, - }, - { - DestServiceTypes: []string{"flink"}, - SourceServiceTypes: []string{"kafka"}, - }, - }, - wantErr: nil, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := IntegrationTypes(tt.args.f) - if !cmp.Equal(err, tt.wantErr) { - t.Errorf("IntegrationTypes() error = %v, wantErr %v", err, tt.wantErr) - - return - } - - if !cmp.Equal(got, tt.want) { - t.Errorf("IntegrationTypes() = %v, want %v", got, tt.want) - } - }) - } -} - -// TestIntegrationEndpointTypes is a test for IntegrationEndpointTypes. -func TestIntegrationEndpointTypes(t *testing.T) { - type args struct { - f []aiven.IntegrationEndpointType - } - - tests := []struct { - name string - args args - want []aiven.IntegrationEndpointType - wantErr error - }{ - { - name: "basic", - args: args{ - f: []aiven.IntegrationEndpointType{ - { - ServiceTypes: []string{ - "kafka", - "flink", - }, - }, - { - ServiceTypes: []string{ - "pg", - "foo", - }, - }, - }, - }, - want: []aiven.IntegrationEndpointType{ - { - ServiceTypes: []string{ - "kafka", - "flink", - }, - }, - { - ServiceTypes: []string{ - "pg", - }, - }, - }, - wantErr: nil, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := IntegrationEndpointTypes(tt.args.f) - if !cmp.Equal(err, tt.wantErr) { - t.Errorf("IntegrationEndpointTypes() error = %v, wantErr %v", err, tt.wantErr) - - return - } - - if !cmp.Equal(got, tt.want) { - t.Errorf("IntegrationEndpointTypes() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/internal/gen/gen.go b/internal/gen/gen.go index 97805e2..c88f0ea 100644 --- a/internal/gen/gen.go +++ b/internal/gen/gen.go @@ -7,7 +7,6 @@ import ( "golang.org/x/sync/errgroup" "github.com/aiven/go-api-schemas/internal/convert" - "github.com/aiven/go-api-schemas/internal/filter" "github.com/aiven/go-api-schemas/internal/pkg/types" "github.com/aiven/go-api-schemas/internal/pkg/util" ) @@ -40,14 +39,9 @@ func serviceTypes() error { return err } - filtered, err := filter.ServiceTypes(r) - if err != nil { - return err - } + out := make(map[string]types.UserConfigSchema, len(r)) - out := map[string]types.UserConfigSchema{} - - for k, v := range filtered { + for k, v := range r { cv, err := convert.UserConfigSchema(v.UserConfigSchema) if err != nil { return err @@ -72,14 +66,9 @@ func integrationTypes() error { return err } - filtered, err := filter.IntegrationTypes(r) - if err != nil { - return err - } - - out := map[string]types.UserConfigSchema{} + out := make(map[string]types.UserConfigSchema, len(r)) - for _, v := range filtered { + for _, v := range r { cv, err := convert.UserConfigSchema(v.UserConfigSchema) if err != nil { return err @@ -104,14 +93,9 @@ func integrationEndpointTypes() error { return err } - filtered, err := filter.IntegrationEndpointTypes(r) - if err != nil { - return err - } - - out := map[string]types.UserConfigSchema{} + out := make(map[string]types.UserConfigSchema, len(r)) - for _, v := range filtered { + for _, v := range r { cv, err := convert.UserConfigSchema(v.UserConfigSchema) if err != nil { return err