Skip to content

Commit

Permalink
allow recreate resources if resources been externally removed (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
freeznet authored Dec 13, 2024
1 parent bef7fdf commit fccccad
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pulsar/resource_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
3 changes: 2 additions & 1 deletion pulsar/resource_pulsar_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down
16 changes: 16 additions & 0 deletions pulsar/resource_pulsar_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
39 changes: 39 additions & 0 deletions pulsar/resource_pulsar_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pulsar/resource_pulsar_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
3 changes: 2 additions & 1 deletion pulsar/resource_pulsar_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
3 changes: 2 additions & 1 deletion pulsar/resource_pulsar_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
3 changes: 2 additions & 1 deletion pulsar/resource_pulsar_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit fccccad

Please sign in to comment.