From fccccad1f1e831fb4dcbabf8152743c456da8316 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Sat, 14 Dec 2024 01:06:55 +0800 Subject: [PATCH] allow recreate resources if resources been externally removed (#129) --- pulsar/resource_pulsar_cluster.go | 3 +- pulsar/resource_pulsar_function.go | 3 +- pulsar/resource_pulsar_namespace.go | 16 ++++++++++ pulsar/resource_pulsar_namespace_test.go | 39 ++++++++++++++++++++++++ pulsar/resource_pulsar_sink.go | 3 +- pulsar/resource_pulsar_source.go | 3 +- pulsar/resource_pulsar_tenant.go | 3 +- pulsar/resource_pulsar_topic.go | 3 +- 8 files changed, 67 insertions(+), 6 deletions(-) diff --git a/pulsar/resource_pulsar_cluster.go b/pulsar/resource_pulsar_cluster.go index 7cf3260..fd0f9e4 100644 --- a/pulsar/resource_pulsar_cluster.go +++ b/pulsar/resource_pulsar_cluster.go @@ -122,7 +122,8 @@ func resourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, meta clusterData, err := client.Get(cluster) if err != nil { if cliErr, ok := err.(rest.Error); ok && cliErr.Code == 404 { - return diag.Errorf("ERROR_CLUSTER_NOT_FOUND") + d.SetId("") + return nil } return diag.FromErr(fmt.Errorf("ERROR_READ_CLUSTER_DATA: %w", err)) } diff --git a/pulsar/resource_pulsar_function.go b/pulsar/resource_pulsar_function.go index fcdfe8a..2676721 100644 --- a/pulsar/resource_pulsar_function.go +++ b/pulsar/resource_pulsar_function.go @@ -390,7 +390,8 @@ func resourcePulsarFunctionRead(ctx context.Context, d *schema.ResourceData, met functionConfig, err := client.GetFunction(tenant, namespace, name) if err != nil { if cliErr, ok := err.(rest.Error); ok && cliErr.Code == 404 { - return diag.Errorf("ERROR_FUNCTION_NOT_FOUND: %s", err.Error()) + d.SetId("") + return nil } return diag.FromErr(errors.Wrapf(err, "failed to get function %s", d.Id())) } diff --git a/pulsar/resource_pulsar_namespace.go b/pulsar/resource_pulsar_namespace.go index 8f8aa1b..42b59cc 100644 --- a/pulsar/resource_pulsar_namespace.go +++ b/pulsar/resource_pulsar_namespace.go @@ -320,6 +320,13 @@ func resourcePulsarNamespaceRead(ctx context.Context, d *schema.ResourceData, me return diag.FromErr(fmt.Errorf("ERROR_PARSE_NAMESPACE_NAME: %w", err)) } + if nss, err := client.GetNamespaces(tenant); err != nil { + return diag.FromErr(fmt.Errorf("ERROR_READ_NAMESPACE: GetNamespaces: %w", err)) + } else if !contains(nss, ns.String()) { + d.SetId("") + return nil + } + d.SetId(ns.String()) _ = d.Set("namespace", namespace) @@ -870,3 +877,12 @@ func unmarshalTopicAutoCreation(v *schema.Set) (*utils.TopicAutoCreationConfig, return &topicAutoCreation, nil } + +func contains(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} diff --git a/pulsar/resource_pulsar_namespace_test.go b/pulsar/resource_pulsar_namespace_test.go index 6edbacc..1e2b7a2 100644 --- a/pulsar/resource_pulsar_namespace_test.go +++ b/pulsar/resource_pulsar_namespace_test.go @@ -347,6 +347,45 @@ func TestImportExistingNamespace(t *testing.T) { }) } +func TestNamespaceExternallyRemoved(t *testing.T) { + + resourceName := "pulsar_namespace.test" + cName := acctest.RandString(10) + tName := acctest.RandString(10) + nsName := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + IDRefreshName: resourceName, + CheckDestroy: testPulsarNamespaceDestroy, + Steps: []resource.TestStep{ + { + Config: testPulsarNamespace(testWebServiceURL, cName, tName, nsName), + Check: resource.ComposeTestCheckFunc( + testPulsarNamespaceExists(resourceName), + ), + }, + { + PreConfig: func() { + client, err := sharedClient(testWebServiceURL) + if err != nil { + t.Fatalf("ERROR_GETTING_PULSAR_CLIENT: %v", err) + } + + conn := client.(admin.Client) + if err = conn.Namespaces().DeleteNamespace(tName + "/" + nsName); err != nil { + t.Fatalf("ERROR_DELETING_TEST_NS: %v", err) + } + }, + Config: testPulsarNamespace(testWebServiceURL, cName, tName, nsName), + PlanOnly: true, + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func createNamespace(t *testing.T, id string) { client, err := sharedClient(testWebServiceURL) if err != nil { diff --git a/pulsar/resource_pulsar_sink.go b/pulsar/resource_pulsar_sink.go index 3e66e92..ee79daf 100644 --- a/pulsar/resource_pulsar_sink.go +++ b/pulsar/resource_pulsar_sink.go @@ -390,7 +390,8 @@ func resourcePulsarSinkRead(ctx context.Context, d *schema.ResourceData, meta in sinkConfig, err := client.GetSink(tenant, namespace, name) if err != nil { if cliErr, ok := err.(rest.Error); ok && cliErr.Code == 404 { - return diag.Errorf("ERROR_SINK_NOT_FOUND") + d.SetId("") + return nil } return diag.FromErr(errors.Wrapf(err, "failed to get %s sink from %s/%s", name, tenant, namespace)) } diff --git a/pulsar/resource_pulsar_source.go b/pulsar/resource_pulsar_source.go index d83aea6..630f00d 100644 --- a/pulsar/resource_pulsar_source.go +++ b/pulsar/resource_pulsar_source.go @@ -337,7 +337,8 @@ func resourcePulsarSourceRead(ctx context.Context, d *schema.ResourceData, meta sourceConfig, err := client.GetSource(tenant, namespace, name) if err != nil { if cliErr, ok := err.(rest.Error); ok && cliErr.Code == 404 { - return diag.Errorf("ERROR_SOURCE_NOT_FOUND") + d.SetId("") + return nil } return diag.FromErr(errors.Wrapf(err, "failed to get %s source from %s/%s", name, tenant, namespace)) } diff --git a/pulsar/resource_pulsar_tenant.go b/pulsar/resource_pulsar_tenant.go index c6b8b0c..6bfc6f6 100644 --- a/pulsar/resource_pulsar_tenant.go +++ b/pulsar/resource_pulsar_tenant.go @@ -94,7 +94,8 @@ func resourcePulsarTenantRead(ctx context.Context, d *schema.ResourceData, meta td, err := client.Get(tenant) if err != nil { if cliErr, ok := err.(rest.Error); ok && cliErr.Code == 404 { - return diag.Errorf("ERROR_TENANT_NOT_FOUND") + d.SetId("") + return nil } return diag.FromErr(fmt.Errorf("ERROR_READ_TENANT: %w", err)) } diff --git a/pulsar/resource_pulsar_topic.go b/pulsar/resource_pulsar_topic.go index eaf2641..bb2e19c 100644 --- a/pulsar/resource_pulsar_topic.go +++ b/pulsar/resource_pulsar_topic.go @@ -165,7 +165,8 @@ func resourcePulsarTopicRead(ctx context.Context, d *schema.ResourceData, meta i return diag.Errorf("%v", err) } if !found { - return diag.Errorf("topic not found") + d.SetId("") + return nil } d.SetId(topicName.String())