@@ -44,15 +44,15 @@ func TestTopic(t *testing.T) {
4444 {
4545 Config : testPulsarPartitionTopic ,
4646 Check : resource .ComposeTestCheckFunc (
47- testPulsarTopicExists ("pulsar_topic.sample-topic-1" ),
48- testPulsarTopicExists ("pulsar_topic.sample-topic-2" ),
47+ testPulsarTopicExists ("pulsar_topic.sample-topic-1" , t ),
48+ testPulsarTopicExists ("pulsar_topic.sample-topic-2" , t ),
4949 ),
5050 },
5151 {
5252 Config : testPulsarNonPartitionTopic ,
5353 Check : resource .ComposeTestCheckFunc (
54- testPulsarTopicExists ("pulsar_topic.sample-topic-3" ),
55- testPulsarTopicExists ("pulsar_topic.sample-topic-4" ),
54+ testPulsarTopicExists ("pulsar_topic.sample-topic-3" , t ),
55+ testPulsarTopicExists ("pulsar_topic.sample-topic-4" , t ),
5656 ),
5757 },
5858 },
@@ -65,11 +65,20 @@ func TestImportExistingTopic(t *testing.T) {
6565 pnum := 10
6666
6767 fullID := strings .Join ([]string {ttype + ":/" , "public" , "default" , tname }, "/" )
68+ topicName , err := utils .GetTopicName (fullID )
69+ if err != nil {
70+ t .Fatalf ("ERROR_GETTING_TOPIC_NAME: %v" , err )
71+ }
6872
6973 resource .Test (t , resource.TestCase {
7074 PreCheck : func () {
7175 testAccPreCheck (t )
7276 createTopic (t , fullID , pnum )
77+ t .Cleanup (func () {
78+ if err := getClientFromMeta (testAccProvider .Meta ()).Topics ().Delete (* topicName , true , pnum == 0 ); err != nil {
79+ t .Fatalf ("ERROR_DELETING_TEST_TOPIC: %v" , err )
80+ }
81+ })
7382 },
7483 ProviderFactories : testAccProviderFactories ,
7584 CheckDestroy : testPulsarTopicDestroy ,
@@ -93,6 +102,61 @@ func TestPartionedTopicWithPermissionGrantUpdate(t *testing.T) {
93102 testTopicWithPermissionGrantUpdate (t , 10 )
94103}
95104
105+ func TestTopicNamespaceExternallyRemoved (t * testing.T ) {
106+
107+ resourceName := "pulsar_topic.test"
108+ tName := acctest .RandString (10 )
109+ nsName := acctest .RandString (10 )
110+ topicName := acctest .RandString (10 )
111+
112+ resource .Test (t , resource.TestCase {
113+ PreCheck : func () { testAccPreCheck (t ) },
114+ ProviderFactories : testAccProviderFactories ,
115+ CheckDestroy : testPulsarTopicDestroy ,
116+ Steps : []resource.TestStep {
117+ {
118+ Config : testPulsarNamespaceWithTopic (testWebServiceURL , tName , nsName , topicName ),
119+ Check : resource .ComposeTestCheckFunc (
120+ testPulsarTopicExists (resourceName , t ),
121+ ),
122+ ExpectError : nil ,
123+ },
124+ {
125+ PreConfig : func () {
126+ client := getClientFromMeta (testAccProvider .Meta ())
127+ topicName , err := utils .GetTopicName (fmt .Sprintf ("persistent://%s/%s/%s" , tName , nsName , topicName ))
128+ if err != nil {
129+ t .Fatalf ("ERROR_GETTING_TOPIC_NAME: %v" , err )
130+ }
131+ namespace , err := utils .GetNameSpaceName (topicName .GetTenant (), topicName .GetNamespace ())
132+ if err != nil {
133+ t .Fatalf ("ERROR_READ_NAMESPACE: %v" , err )
134+ }
135+ partitionedTopics , nonPartitionedTopics , err := client .Topics ().List (* namespace )
136+ if err != nil {
137+ t .Fatalf ("ERROR_READ_TOPIC_DATA: %v" , err )
138+ }
139+
140+ for _ , topic := range append (partitionedTopics , nonPartitionedTopics ... ) {
141+ if topicName .String () == topic {
142+ if err = client .Topics ().Delete (* topicName , true , true ); err != nil {
143+ t .Fatalf ("ERROR_DELETING_TEST_TOPIC: %v" , err )
144+ }
145+ }
146+ }
147+ if err = client .Namespaces ().DeleteNamespace (tName + "/" + nsName ); err != nil {
148+ t .Fatalf ("ERROR_DELETING_TEST_NS: %v" , err )
149+ }
150+ },
151+ Config : testPulsarNamespaceWithTopic (testWebServiceURL , tName , nsName , topicName ),
152+ PlanOnly : true ,
153+ ExpectNonEmptyPlan : true ,
154+ ExpectError : nil ,
155+ },
156+ },
157+ })
158+ }
159+
96160func testTopicWithPermissionGrantUpdate (t * testing.T , pnum int ) {
97161 resourceName := "pulsar_topic.test"
98162 tname := acctest .RandString (10 )
@@ -114,7 +178,7 @@ func testTopicWithPermissionGrantUpdate(t *testing.T, pnum int) {
114178 actions = ["produce", "consume"]
115179 }` ),
116180 Check : resource .ComposeTestCheckFunc (
117- testPulsarTopicExists (resourceName ),
181+ testPulsarTopicExists (resourceName , t ),
118182 resource .TestCheckResourceAttr (resourceName , "permission_grant.#" , "2" ),
119183 resource .TestCheckResourceAttr (resourceName , "permission_grant.0.role" , "some-role-1" ),
120184 resource .TestCheckResourceAttr (resourceName , "permission_grant.0.actions.#" , "3" ),
@@ -134,7 +198,7 @@ func testTopicWithPermissionGrantUpdate(t *testing.T, pnum int) {
134198 actions = ["produce"]
135199 }` ),
136200 Check : resource .ComposeTestCheckFunc (
137- testPulsarTopicExists (resourceName ),
201+ testPulsarTopicExists (resourceName , t ),
138202 resource .TestCheckResourceAttr (resourceName , "permission_grant.#" , "1" ),
139203 resource .TestCheckResourceAttr (resourceName , "permission_grant.0.role" , "some-role-2" ),
140204 resource .TestCheckResourceAttr (resourceName , "permission_grant.0.actions.#" , "1" ),
@@ -177,7 +241,7 @@ func testPulsarTopicDestroy(s *terraform.State) error {
177241 return nil
178242}
179243
180- func testPulsarTopicExists (topic string ) resource.TestCheckFunc {
244+ func testPulsarTopicExists (topic string , t * testing. T ) resource.TestCheckFunc {
181245 return func (s * terraform.State ) error {
182246 rs , ok := s .RootModule ().Resources [topic ]
183247 if ! ok {
@@ -188,6 +252,7 @@ func testPulsarTopicExists(topic string) resource.TestCheckFunc {
188252 if err != nil {
189253 return fmt .Errorf ("ERROR_READ_TOPIC: %w" , err )
190254 }
255+ t .Logf ("topicName: %v" , topicName )
191256 namespace , err := utils .GetNameSpaceName (topicName .GetTenant (), topicName .GetNamespace ())
192257 if err != nil {
193258 return fmt .Errorf ("ERROR_READ_NAMESPACE: %w" , err )
@@ -338,3 +403,41 @@ resource "pulsar_topic" "test" {
338403}
339404` , url , ttype , tname , pnum , permissionGrants )
340405}
406+
407+ func testPulsarNamespaceWithTopic (wsURL , tenant , ns , topicName string ) string {
408+ return fmt .Sprintf (`
409+ provider "pulsar" {
410+ web_service_url = "%s"
411+ }
412+
413+ resource "pulsar_tenant" "test_tenant" {
414+ tenant = "%s"
415+ allowed_clusters = ["standalone"]
416+ }
417+
418+ resource "pulsar_namespace" "test" {
419+ tenant = pulsar_tenant.test_tenant.tenant
420+ namespace = "%s"
421+
422+ topic_auto_creation {
423+ enable = false
424+ }
425+
426+ depends_on = [
427+ pulsar_tenant.test_tenant
428+ ]
429+ }
430+
431+ resource "pulsar_topic" "test" {
432+ tenant = "%s"
433+ namespace = "%s"
434+ topic_type = "persistent"
435+ topic_name = "%s"
436+ partitions = 0
437+
438+ depends_on = [
439+ pulsar_namespace.test
440+ ]
441+ }
442+ ` , wsURL , tenant , ns , tenant , ns , topicName )
443+ }
0 commit comments