99 "time"
1010
1111 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1213 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14+ "github.com/ovh/go-ovh/ovh"
1315 "github.com/ovh/terraform-provider-ovh/ovh/helpers"
1416)
1517
@@ -123,22 +125,35 @@ func resourceCloudProjectDatabaseKafkaTopicCreate(ctx context.Context, d *schema
123125 params := (& CloudProjectDatabaseKafkaTopicCreateOpts {}).FromResource (d )
124126 res := & CloudProjectDatabaseKafkaTopicResponse {}
125127
126- log .Printf ("[DEBUG] Will create topic: %+v for cluster %s from project %s" , params , clusterId , serviceName )
127- err := config .OVHClient .Post (endpoint , params , res )
128- if err != nil {
129- return diag .Errorf ("calling Post %s with params %+v:\n \t %q" , endpoint , params , err )
130- }
131-
132- log .Printf ("[DEBUG] Waiting for topic %s to be READY" , res .Id )
133- err = waitForCloudProjectDatabaseKafkaTopicReady (ctx , config .OVHClient , serviceName , clusterId , res .Id , d .Timeout (schema .TimeoutCreate ))
134- if err != nil {
135- return diag .Errorf ("timeout while waiting topic %s to be READY: %s" , res .Id , err .Error ())
136- }
137- log .Printf ("[DEBUG] topic %s is READY" , res .Id )
138-
139- d .SetId (res .Id )
140-
141- return resourceCloudProjectDatabaseKafkaTopicRead (ctx , d , meta )
128+ return diag .FromErr (
129+ resource .RetryContext (ctx , d .Timeout (schema .TimeoutCreate ),
130+ func () * resource.RetryError {
131+ log .Printf ("[DEBUG] Will create topic: %+v for cluster %s from project %s" , params , clusterId , serviceName )
132+ err := config .OVHClient .Post (endpoint , params , res )
133+ if err != nil {
134+ if errOvh , ok := err .(* ovh.APIError ); ok && (errOvh .Code == 409 ) {
135+ return resource .RetryableError (err )
136+ }
137+ return resource .NonRetryableError (fmt .Errorf ("calling Post %s with params %+v:\n \t %q" , endpoint , params , err ))
138+ }
139+
140+ log .Printf ("[DEBUG] Waiting for topic %s to be READY" , res .Id )
141+ err = waitForCloudProjectDatabaseKafkaTopicReady (ctx , config .OVHClient , serviceName , clusterId , res .Id , d .Timeout (schema .TimeoutCreate ))
142+ if err != nil {
143+ return resource .NonRetryableError (fmt .Errorf ("timeout while waiting topic %s to be READY: %s" , res .Id , err .Error ()))
144+ }
145+ log .Printf ("[DEBUG] topic %s is READY" , res .Id )
146+
147+ d .SetId (res .Id )
148+ readDiags := resourceCloudProjectDatabaseKafkaTopicRead (ctx , d , meta )
149+ err = diagnosticsToError (readDiags )
150+ if err != nil {
151+ return resource .NonRetryableError (err )
152+ }
153+ return nil
154+ },
155+ ),
156+ )
142157}
143158
144159func resourceCloudProjectDatabaseKafkaTopicRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
@@ -159,7 +174,6 @@ func resourceCloudProjectDatabaseKafkaTopicRead(ctx context.Context, d *schema.R
159174 return diag .FromErr (helpers .CheckDeleted (d , err , endpoint ))
160175 }
161176
162- diags := make (diag.Diagnostics , 0 )
163177 for k , v := range res .ToMap () {
164178 if k != "id" {
165179 d .Set (k , v )
@@ -168,9 +182,6 @@ func resourceCloudProjectDatabaseKafkaTopicRead(ctx context.Context, d *schema.R
168182 }
169183 }
170184
171- if len (diags ) > 0 {
172- return diags
173- }
174185 return nil
175186}
176187
@@ -186,20 +197,33 @@ func resourceCloudProjectDatabaseKafkaTopicDelete(ctx context.Context, d *schema
186197 url .PathEscape (id ),
187198 )
188199
189- log .Printf ("[DEBUG] Will delete topic %s from cluster %s from project %s" , id , clusterId , serviceName )
190- err := config .OVHClient .Delete (endpoint , nil )
191- if err != nil {
192- return diag .FromErr (helpers .CheckDeleted (d , err , endpoint ))
193- }
194-
195- log .Printf ("[DEBUG] Waiting for topic %s to be DELETED" , id )
196- err = waitForCloudProjectDatabaseKafkaTopicDeleted (ctx , config .OVHClient , serviceName , clusterId , id , d .Timeout (schema .TimeoutDelete ))
197- if err != nil {
198- return diag .Errorf ("timeout while waiting topic %s to be DELETED: %s" , id , err .Error ())
199- }
200- log .Printf ("[DEBUG] topic %s is DELETED" , id )
201-
202- d .SetId ("" )
203-
204- return nil
200+ return diag .FromErr (
201+ resource .RetryContext (ctx , d .Timeout (schema .TimeoutDelete ),
202+ func () * resource.RetryError {
203+ log .Printf ("[DEBUG] Will delete topic %s from cluster %s from project %s" , id , clusterId , serviceName )
204+ err := config .OVHClient .Delete (endpoint , nil )
205+ if err != nil {
206+ if errOvh , ok := err .(* ovh.APIError ); ok && (errOvh .Code == 409 ) {
207+ return resource .RetryableError (err )
208+ }
209+ err = helpers .CheckDeleted (d , err , endpoint )
210+ if err != nil {
211+ return resource .NonRetryableError (err )
212+ }
213+ return nil
214+ }
215+
216+ log .Printf ("[DEBUG] Waiting for topic %s to be DELETED" , id )
217+ err = waitForCloudProjectDatabaseKafkaTopicDeleted (ctx , config .OVHClient , serviceName , clusterId , id , d .Timeout (schema .TimeoutDelete ))
218+ if err != nil {
219+ return resource .NonRetryableError (fmt .Errorf ("timeout while waiting topic %s to be DELETED: %s" , id , err .Error ()))
220+ }
221+ log .Printf ("[DEBUG] topic %s is DELETED" , id )
222+
223+ d .SetId ("" )
224+
225+ return nil
226+ },
227+ ),
228+ )
205229}
0 commit comments