Skip to content

Commit

Permalink
deprecate api_version in pulsar provider
Browse files Browse the repository at this point in the history
  • Loading branch information
freeznet committed Sep 29, 2024
1 parent 9f30205 commit bb4577c
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 33 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion examples/functions/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ terraform {

provider "pulsar" {
web_service_url = "http://localhost:8080"
api_version = "3"
}

// Note: function resource requires v3 api.
Expand Down
1 change: 0 additions & 1 deletion examples/sinks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ terraform {

provider "pulsar" {
web_service_url = "http://localhost:8080"
api_version = "3"
}

// Note: sink resource requires v3 api.
Expand Down
1 change: 0 additions & 1 deletion examples/sources/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ terraform {

provider "pulsar" {
web_service_url = "http://localhost:8080"
api_version = "3"
}

resource "pulsar_source" "source-1" {
Expand Down
6 changes: 5 additions & 1 deletion pulsar/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
51 changes: 46 additions & 5 deletions pulsar/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions pulsar/resource_pulsar_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions pulsar/resource_pulsar_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion pulsar/resource_pulsar_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
9 changes: 4 additions & 5 deletions pulsar/resource_pulsar_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion pulsar/resource_pulsar_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
1 change: 0 additions & 1 deletion pulsar/testdata/function/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

provider "pulsar" {
web_service_url = "http://localhost:8080"
api_version = "3"
}

resource "pulsar_function" "function-1" {
Expand Down
1 change: 0 additions & 1 deletion pulsar/testdata/sink/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

provider "pulsar" {
web_service_url = "http://localhost:8080"
api_version = "3"
}

resource "pulsar_sink" "sink-1" {
Expand Down
1 change: 0 additions & 1 deletion pulsar/testdata/source/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

provider "pulsar" {
web_service_url = "http://localhost:8080"
api_version = "3"
}

resource "pulsar_source" "source-1" {
Expand Down

0 comments on commit bb4577c

Please sign in to comment.