From bb4577cdd662d012a4a7898d53f7172ea8256ef6 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Sun, 29 Sep 2024 13:35:49 +0800 Subject: [PATCH] deprecate api_version in pulsar provider --- README.md | 3 -- docs/index.md | 2 +- examples/functions/main.tf | 1 - examples/sinks/main.tf | 1 - examples/sources/main.tf | 1 - pulsar/client.go | 6 +++- pulsar/provider.go | 51 ++++++++++++++++++++++++--- pulsar/resource_pulsar_function.go | 9 +++-- pulsar/resource_pulsar_sink.go | 9 +++-- pulsar/resource_pulsar_sink_test.go | 1 - pulsar/resource_pulsar_source.go | 9 +++-- pulsar/resource_pulsar_source_test.go | 1 - pulsar/testdata/function/main.tf | 1 - pulsar/testdata/sink/main.tf | 1 - pulsar/testdata/source/main.tf | 1 - 15 files changed, 64 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 4a4699b..0cfcfb8 100644 --- a/README.md +++ b/README.md @@ -363,7 +363,6 @@ A resource for creating and managing Apache Pulsar Functions. ```hcl provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } resource "pulsar_function" "function-1" { @@ -462,7 +461,6 @@ A resource for creating and managing Apache Pulsar Sources. ```hcl provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } resource "pulsar_source" "source-1" { @@ -514,7 +512,6 @@ A resource for creating and managing Apache Pulsar Sinks. ```hcl provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } resource "pulsar_sink" "sample-sink-1" { diff --git a/docs/index.md b/docs/index.md index 1cfb838..b417b00 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ description: |- ### Optional -- `api_version` (String) Api Version to be used for the pulsar admin interaction +- `api_version` (String) Api Version to be used for the pulsar admin interaction (DEPRECATED) - `audience` (String) The OAuth 2.0 resource server identifier for the Pulsar cluster - `client_id` (String) The OAuth 2.0 client identifier - `issuer_url` (String) The OAuth 2.0 URL of the authentication provider which allows the Pulsar client to obtain an access token diff --git a/examples/functions/main.tf b/examples/functions/main.tf index e7fb172..ef2cd07 100644 --- a/examples/functions/main.tf +++ b/examples/functions/main.tf @@ -26,7 +26,6 @@ terraform { provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } // Note: function resource requires v3 api. diff --git a/examples/sinks/main.tf b/examples/sinks/main.tf index 4ed2e2b..cb5c4d7 100644 --- a/examples/sinks/main.tf +++ b/examples/sinks/main.tf @@ -26,7 +26,6 @@ terraform { provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } // Note: sink resource requires v3 api. diff --git a/examples/sources/main.tf b/examples/sources/main.tf index 974e5b3..d13dff1 100644 --- a/examples/sources/main.tf +++ b/examples/sources/main.tf @@ -26,7 +26,6 @@ terraform { provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } resource "pulsar_source" "source-1" { diff --git a/pulsar/client.go b/pulsar/client.go index 7ddae4f..fdd21b4 100644 --- a/pulsar/client.go +++ b/pulsar/client.go @@ -22,5 +22,9 @@ import ( ) func getClientFromMeta(meta interface{}) admin.Client { - return meta.(admin.Client) + return meta.(PulsarClientBundle).Client +} + +func getV3ClientFromMeta(meta interface{}) admin.Client { + return meta.(PulsarClientBundle).V3Client } diff --git a/pulsar/provider.go b/pulsar/provider.go index 47aa5cd..8438cb4 100644 --- a/pulsar/provider.go +++ b/pulsar/provider.go @@ -24,7 +24,8 @@ import ( "os" "strconv" - "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" + pulsaradmin "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin" + adminconfig "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/pkg/errors" @@ -41,7 +42,7 @@ func init() { descriptions = map[string]string{ "web_service_url": "Web service url is used to connect to your apache pulsar cluster", "token": "Authentication Token used to grant terraform permissions to modify Apace Pulsar Entities", - "api_version": "Api Version to be used for the pulsar admin interaction", + "api_version": "Api Version to be used for the pulsar admin interaction, DEPRECATED: no need to set this value", "tls_trust_certs_file_path": "Path to a custom trusted TLS certificate file", "tls_key_file_path": "Path to the key to use when using TLS client authentication", "tls_cert_file_path": "Path to the cert to use when using TLS client authentication", @@ -69,6 +70,12 @@ func init() { } } +// PulsarClientBundle is a struct that holds the pulsar admin client for both v2 and v3 api versions +type PulsarClientBundle struct { + Client pulsaradmin.Client + V3Client pulsaradmin.Client +} + // Provider returns a schema.Provider func Provider() *schema.Provider { provider := &schema.Provider{ @@ -204,10 +211,17 @@ func providerConfigure(d *schema.ResourceData, tfVersion string) (interface{}, d return nil, diag.FromErr(fmt.Errorf("ERROR_PULSAR_CONFIG_tls_TRUST_FILE_NOTEXIST: %q", TLSTrustCertsFilePath)) } - config := &config.Config{ + configVersion := adminconfig.APIVersion(apiVersion) + // for backward compatibility, if user state api_version as 3 + // we will use v2 as the default client version because we have v3 as individual client + if configVersion == adminconfig.V3 { + configVersion = adminconfig.APIVersion(0) // v2 will be the default client version + } + + config := &adminconfig.Config{ WebServiceURL: clusterURL, Token: token, - PulsarAPIVersion: config.APIVersion(apiVersion), + PulsarAPIVersion: configVersion, TLSTrustCertsFilePath: TLSTrustCertsFilePath, TLSAllowInsecureConnection: TLSAllowInsecureConnection, IssuerEndpoint: issuerEndpoint, @@ -226,7 +240,34 @@ func providerConfigure(d *schema.ResourceData, tfVersion string) (interface{}, d return nil, diag.FromErr(err) } - return client, nil + configV3 := &adminconfig.Config{ + WebServiceURL: clusterURL, + Token: token, + PulsarAPIVersion: adminconfig.V3, + TLSTrustCertsFilePath: TLSTrustCertsFilePath, + TLSAllowInsecureConnection: TLSAllowInsecureConnection, + IssuerEndpoint: issuerEndpoint, + ClientID: clientID, + Audience: audience, + Scope: scope, + KeyFile: keyFilePath, + TLSKeyFile: TLSKeyFilePath, + TLSCertFile: TLSCertFilePath, + } + + clientV3, err := admin.NewPulsarAdminClient(&admin.PulsarAdminConfig{ + Config: configV3, + }) + if err != nil { + return nil, diag.FromErr(err) + } + + clientBundle := PulsarClientBundle{ + Client: client, + V3Client: clientV3, + } + + return clientBundle, nil } // Exists reports whether the named file or directory exists. diff --git a/pulsar/resource_pulsar_function.go b/pulsar/resource_pulsar_function.go index 715ebb2..7a3615f 100644 --- a/pulsar/resource_pulsar_function.go +++ b/pulsar/resource_pulsar_function.go @@ -23,7 +23,6 @@ import ( "fmt" "strings" - "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -380,7 +379,7 @@ func resourcePulsarFunction() *schema.Resource { } func resourcePulsarFunctionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Functions() + client := getV3ClientFromMeta(meta).Functions() tenant := d.Get(resourceFunctionTenantKey).(string) namespace := d.Get(resourceFunctionNamespaceKey).(string) @@ -402,7 +401,7 @@ func resourcePulsarFunctionRead(ctx context.Context, d *schema.ResourceData, met } func resourcePulsarFunctionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Functions() + client := getV3ClientFromMeta(meta).Functions() functionConfig, err := marshalFunctionConfig(d) if err != nil { @@ -434,7 +433,7 @@ func resourcePulsarFunctionCreate(ctx context.Context, d *schema.ResourceData, m } func resourcePulsarFunctionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Functions() + client := getV3ClientFromMeta(meta).Functions() functionConfig, err := marshalFunctionConfig(d) if err != nil { @@ -465,7 +464,7 @@ func resourcePulsarFunctionUpdate(ctx context.Context, d *schema.ResourceData, m } func resourcePulsarFunctionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Functions() + client := getV3ClientFromMeta(meta).Functions() tenant := d.Get(resourceFunctionTenantKey).(string) namespace := d.Get(resourceFunctionNamespaceKey).(string) diff --git a/pulsar/resource_pulsar_sink.go b/pulsar/resource_pulsar_sink.go index 2a2c52e..3e66e92 100644 --- a/pulsar/resource_pulsar_sink.go +++ b/pulsar/resource_pulsar_sink.go @@ -23,7 +23,6 @@ import ( "fmt" "strings" - "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -360,7 +359,7 @@ func resourcePulsarSink() *schema.Resource { } func resourcePulsarSinkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Sinks() + client := getV3ClientFromMeta(meta).Sinks() sinkConfig, err := marshalSinkConfig(d) if err != nil { @@ -380,7 +379,7 @@ func resourcePulsarSinkCreate(ctx context.Context, d *schema.ResourceData, meta } func resourcePulsarSinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Sinks() + client := getV3ClientFromMeta(meta).Sinks() tenant := d.Get(resourceSinkTenantKey).(string) namespace := d.Get(resourceSinkNamespaceKey).(string) @@ -599,7 +598,7 @@ func resourcePulsarSinkRead(ctx context.Context, d *schema.ResourceData, meta in } func resourcePulsarSinkUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Sinks() + client := getV3ClientFromMeta(meta).Sinks() sinkConfig, err := marshalSinkConfig(d) if err != nil { @@ -620,7 +619,7 @@ func resourcePulsarSinkUpdate(ctx context.Context, d *schema.ResourceData, meta } func resourcePulsarSinkDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Sinks() + client := getV3ClientFromMeta(meta).Sinks() tenant := d.Get(resourceSinkTenantKey).(string) namespace := d.Get(resourceSinkNamespaceKey).(string) diff --git a/pulsar/resource_pulsar_sink_test.go b/pulsar/resource_pulsar_sink_test.go index f5a3584..11c4f1b 100644 --- a/pulsar/resource_pulsar_sink_test.go +++ b/pulsar/resource_pulsar_sink_test.go @@ -204,7 +204,6 @@ func testSampleSink(name string) string { return fmt.Sprintf(` provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } resource "pulsar_sink" "test" { diff --git a/pulsar/resource_pulsar_source.go b/pulsar/resource_pulsar_source.go index 8f98753..d83aea6 100644 --- a/pulsar/resource_pulsar_source.go +++ b/pulsar/resource_pulsar_source.go @@ -23,7 +23,6 @@ import ( "fmt" "strings" - "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -305,7 +304,7 @@ func resourcePulsarSource() *schema.Resource { } func resourcePulsarSourceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Sources() + client := getV3ClientFromMeta(meta).Sources() sourceConfig, err := marshalSourceConfig(d) if err != nil { @@ -327,7 +326,7 @@ func resourcePulsarSourceCreate(ctx context.Context, d *schema.ResourceData, met } func resourcePulsarSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Sources() + client := getV3ClientFromMeta(meta).Sources() tenant := d.Get(resourceSourceTenantKey).(string) namespace := d.Get(resourceSourceNamespaceKey).(string) @@ -527,7 +526,7 @@ func resourcePulsarSourceRead(ctx context.Context, d *schema.ResourceData, meta } func resourcePulsarSourceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Sources() + client := getV3ClientFromMeta(meta).Sources() sourceConfig, err := marshalSourceConfig(d) if err != nil { @@ -548,7 +547,7 @@ func resourcePulsarSourceUpdate(ctx context.Context, d *schema.ResourceData, met } func resourcePulsarSourceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(admin.Client).Sources() + client := getV3ClientFromMeta(meta).Sources() tenant := d.Get(resourceSourceTenantKey).(string) namespace := d.Get(resourceSourceNamespaceKey).(string) diff --git a/pulsar/resource_pulsar_source_test.go b/pulsar/resource_pulsar_source_test.go index 9822d6c..c7f7441 100644 --- a/pulsar/resource_pulsar_source_test.go +++ b/pulsar/resource_pulsar_source_test.go @@ -223,7 +223,6 @@ func testSampleSource(name string) string { return fmt.Sprintf(` provider "pulsar" { web_service_url = "%s" - api_version = "3" } resource "pulsar_source" "test" { diff --git a/pulsar/testdata/function/main.tf b/pulsar/testdata/function/main.tf index 812d600..8997df4 100644 --- a/pulsar/testdata/function/main.tf +++ b/pulsar/testdata/function/main.tf @@ -1,7 +1,6 @@ provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } resource "pulsar_function" "function-1" { diff --git a/pulsar/testdata/sink/main.tf b/pulsar/testdata/sink/main.tf index af78548..30e78aa 100644 --- a/pulsar/testdata/sink/main.tf +++ b/pulsar/testdata/sink/main.tf @@ -17,7 +17,6 @@ provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } resource "pulsar_sink" "sink-1" { diff --git a/pulsar/testdata/source/main.tf b/pulsar/testdata/source/main.tf index aca0917..7263f98 100644 --- a/pulsar/testdata/source/main.tf +++ b/pulsar/testdata/source/main.tf @@ -17,7 +17,6 @@ provider "pulsar" { web_service_url = "http://localhost:8080" - api_version = "3" } resource "pulsar_source" "source-1" {