1
1
package ovh
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"log"
6
7
"net/url"
7
8
"regexp"
8
9
"strings"
9
10
"time"
10
11
12
+ "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
11
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
14
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
13
15
"github.com/ovh/go-ovh/ovh"
14
16
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
17
+ "github.com/ovh/terraform-provider-ovh/ovh/types"
15
18
)
16
19
17
20
var (
@@ -218,24 +221,36 @@ func genericOrderSchema(withOptions bool) map[string]*schema.Schema {
218
221
return orderSchema
219
222
}
220
223
221
- func orderCreate (d * schema.ResourceData , meta interface {}, product string ) error {
224
+ func orderCreateFromResource (d * schema.ResourceData , meta interface {}, product string ) error {
222
225
config := meta .(* Config )
226
+ order := (& OrderModel {}).FromResource (d )
223
227
228
+ err := orderCreate (order , config , product )
229
+ if err != nil {
230
+ return err
231
+ }
232
+
233
+ d .SetId (fmt .Sprint (order .Order .OrderId .ValueInt64 ()))
234
+
235
+ return nil
236
+ }
237
+
238
+ func orderCreate (d * OrderModel , config * Config , product string ) error {
224
239
// create Cart
225
240
cartParams := & OrderCartCreateOpts {
226
- OvhSubsidiary : strings .ToUpper (d .Get ( "ovh_subsidiary" ).( string )),
241
+ OvhSubsidiary : strings .ToUpper (d .OvhSubsidiary . ValueString ( )),
227
242
}
228
243
229
- cart , err := orderCartCreate (meta , cartParams , true )
244
+ cart , err := orderCartCreate (config , cartParams , true )
230
245
if err != nil {
231
246
return fmt .Errorf ("calling creating order cart: %q" , err )
232
247
}
233
248
234
249
// Create Product Item
235
250
item := & OrderCartItem {}
236
- cartPlanParams := ( & OrderCartPlanCreateOpts {
237
- Quantity : 1 ,
238
- }). FromResourceWithPath ( d , "plan.0" )
251
+ cartPlanParamsList := d . Plan . Elements ()
252
+ cartPlanParams := cartPlanParamsList [ 0 ].( PlanValue )
253
+ cartPlanParams . Quantity = types. TfInt64Value { Int64Value : basetypes . NewInt64Value ( 1 )}
239
254
240
255
log .Printf ("[DEBUG] Will create order item %s for cart: %s" , product , cart .CartId )
241
256
endpoint := fmt .Sprintf ("/order/cart/%s/%s" , url .PathEscape (cart .CartId ), product )
@@ -244,62 +259,53 @@ func orderCreate(d *schema.ResourceData, meta interface{}, product string) error
244
259
}
245
260
246
261
// apply configurations
247
- nbOfConfigurations := d .Get ("plan.0.configuration.#" ).(int )
248
- for i := 0 ; i < nbOfConfigurations ; i ++ {
262
+ configs := cartPlanParams .Configuration .Elements ()
263
+
264
+ for _ , cfg := range configs {
249
265
log .Printf ("[DEBUG] Will create order cart item configuration for cart item: %s/%d" ,
250
266
item .CartId ,
251
267
item .ItemId ,
252
268
)
253
269
itemConfig := & OrderCartItemConfiguration {}
254
- itemConfigParams := (& OrderCartItemConfigurationOpts {}).FromResourceWithPath (
255
- d ,
256
- fmt .Sprintf ("plan.0.configuration.%d" , i ),
257
- )
258
270
endpoint := fmt .Sprintf ("/order/cart/%s/item/%d/configuration" ,
259
271
url .PathEscape (item .CartId ),
260
272
item .ItemId ,
261
273
)
262
- if err := config .OVHClient .Post (endpoint , itemConfigParams , itemConfig ); err != nil {
263
- return fmt .Errorf ("calling Post %s with params %v:\n \t %q" , endpoint , itemConfigParams , err )
274
+ if err := config .OVHClient .Post (endpoint , cfg , itemConfig ); err != nil {
275
+ return fmt .Errorf ("calling Post %s with params %v:\n \t %q" , endpoint , cfg , err )
264
276
}
265
277
}
266
278
279
+ planOptionValue := d .PlanOption .Elements ()
280
+
267
281
// Create Product Options Items
268
- nbOfOptions := d . Get ( "plan_option.#" ).( int )
269
- for i := 0 ; i < nbOfOptions ; i ++ {
270
- optionPath := fmt . Sprintf ( "plan_option.%d" , i )
282
+ for _ , option := range planOptionValue {
283
+ opt := option .( PlanOptionValue )
284
+
271
285
log .Printf ("[DEBUG] Will create order item options %s for cart: %s" , product , cart .CartId )
272
286
productOptionsItem := & OrderCartItem {}
273
- cartPlanOptionsParams := (& OrderCartPlanOptionsCreateOpts {
274
- ItemId : item .ItemId ,
275
- Quantity : 1 ,
276
- }).FromResourceWithPath (
277
- d ,
278
- optionPath ,
279
- )
287
+
288
+ opt .ItemId = types.TfInt64Value {Int64Value : basetypes .NewInt64Value (item .ItemId )}
289
+ opt .Quantity = types.TfInt64Value {Int64Value : basetypes .NewInt64Value (1 )}
290
+
280
291
endpoint := fmt .Sprintf ("/order/cart/%s/%s/options" , url .PathEscape (cart .CartId ), product )
281
- if err := config .OVHClient .Post (endpoint , cartPlanOptionsParams , productOptionsItem ); err != nil {
292
+ if err := config .OVHClient .Post (endpoint , opt , productOptionsItem ); err != nil {
282
293
return fmt .Errorf ("calling Post %s with params %v:\n \t %q" , endpoint , cartPlanParams , err )
283
294
}
284
295
285
- // apply configurations
286
- nbOfConfigurations := d .Get (fmt .Sprintf ("%s.configuration.#" , optionPath )).(int )
287
- for j := 0 ; j < nbOfConfigurations ; j ++ {
296
+ optionConfigs := opt .Configuration .Elements ()
297
+ for _ , cfg := range optionConfigs {
288
298
log .Printf ("[DEBUG] Will create order cart item configuration for cart item: %s/%d" ,
289
299
item .CartId ,
290
300
item .ItemId ,
291
301
)
292
302
itemConfig := & OrderCartItemConfiguration {}
293
- itemConfigParams := (& OrderCartItemConfigurationOpts {}).FromResourceWithPath (
294
- d ,
295
- fmt .Sprintf ("%s.configuration.%d" , optionPath , j ),
296
- )
297
303
endpoint := fmt .Sprintf ("/order/cart/%s/item/%d/configuration" ,
298
304
url .PathEscape (item .CartId ),
299
305
item .ItemId ,
300
306
)
301
- if err := config .OVHClient .Post (endpoint , itemConfigParams , itemConfig ); err != nil {
302
- return fmt .Errorf ("calling Post %s with params %v:\n \t %q" , endpoint , itemConfigParams , err )
307
+ if err := config .OVHClient .Post (endpoint , cfg , itemConfig ); err != nil {
308
+ return fmt .Errorf ("calling Post %s with params %v:\n \t %q" , endpoint , cfg , err )
303
309
}
304
310
}
305
311
}
@@ -384,15 +390,33 @@ func orderCreate(d *schema.ResourceData, meta interface{}, product string) error
384
390
return fmt .Errorf ("waiting for order (%d): %s" , checkout .OrderID , err )
385
391
}
386
392
387
- d .SetId ( fmt . Sprint (checkout .OrderID ))
393
+ d .Order . OrderId = types. TfInt64Value { Int64Value : basetypes . NewInt64Value (checkout .OrderID )}
388
394
389
395
return nil
390
396
}
391
397
392
- func orderRead (d * schema.ResourceData , meta interface {}) (* MeOrder , []* MeOrderDetail , error ) {
398
+ func orderReadInResource (d * schema.ResourceData , meta interface {}) (* MeOrder , []* MeOrderDetail , error ) {
393
399
config := meta .(* Config )
394
400
orderId := d .Id ()
395
401
402
+ order , details , err := orderRead (orderId , config )
403
+ if err != nil {
404
+ return nil , nil , err
405
+ }
406
+
407
+ detailsData := make ([]map [string ]interface {}, len (details ))
408
+ for i , detail := range details {
409
+ detailsData [i ] = detail .ToMap ()
410
+ }
411
+
412
+ orderData := order .ToMap ()
413
+ orderData ["details" ] = detailsData
414
+ d .Set ("order" , []interface {}{orderData })
415
+
416
+ return order , details , nil
417
+ }
418
+
419
+ func orderRead (orderId string , config * Config ) (* MeOrder , []* MeOrderDetail , error ) {
396
420
order := & MeOrder {}
397
421
log .Printf ("[DEBUG] Will read order %s" , orderId )
398
422
endpoint := fmt .Sprintf ("/me/order/%s" ,
@@ -408,26 +432,31 @@ func orderRead(d *schema.ResourceData, meta interface{}) (*MeOrder, []*MeOrderDe
408
432
}
409
433
410
434
if len (details ) < 1 {
411
- return nil , nil , fmt .Errorf ("There is no order details for id %s. This shouldn't happen. This is a bug with the API. " , orderId )
435
+ return nil , nil , fmt .Errorf ("there is no order details for id %s. This shouldn't happen. This is a bug with the API" , orderId )
412
436
}
413
437
414
- detailsData := make ([]map [string ]interface {}, len (details ))
415
- for i , detail := range details {
416
- detailsData [i ] = detail .ToMap ()
417
- }
418
-
419
- orderData := order .ToMap ()
420
- orderData ["details" ] = detailsData
421
- d .Set ("order" , []interface {}{orderData })
422
-
423
438
return order , details , nil
424
439
}
425
440
426
441
type TerminateFunc func () (string , error )
427
442
type ConfirmTerminationFunc func (token string ) error
428
443
429
- func orderDelete (d * schema.ResourceData , meta interface {}, terminate TerminateFunc , confirm ConfirmTerminationFunc ) error {
430
- oldEmailsIds , err := notificationEmailSortedIds (meta )
444
+ func orderDeleteFromResource (d * schema.ResourceData , meta interface {}, terminate TerminateFunc , confirm ConfirmTerminationFunc ) error {
445
+ config := meta .(* Config )
446
+
447
+ if err := orderDelete (config , terminate , confirm ); err != nil {
448
+ return err
449
+ }
450
+
451
+ if d != nil {
452
+ d .SetId ("" )
453
+ }
454
+
455
+ return nil
456
+ }
457
+
458
+ func orderDelete (config * Config , terminate TerminateFunc , confirm ConfirmTerminationFunc ) error {
459
+ oldEmailsIds , err := notificationEmailSortedIds (config )
431
460
if err != nil {
432
461
return err
433
462
}
@@ -450,7 +479,7 @@ func orderDelete(d *schema.ResourceData, meta interface{}, terminate TerminateFu
450
479
var email * NotificationEmail
451
480
// wait for email
452
481
err = resource .Retry (30 * time .Minute , func () * resource.RetryError {
453
- email , err = getNewNotificationEmail (matches , oldEmailsIds , meta )
482
+ email , err = getNewNotificationEmail (matches , oldEmailsIds , config )
454
483
if err != nil {
455
484
log .Printf ("[DEBUG] error while getting email notification. retry: %v" , err )
456
485
return resource .RetryableError (err )
@@ -478,10 +507,6 @@ func orderDelete(d *schema.ResourceData, meta interface{}, terminate TerminateFu
478
507
return err
479
508
}
480
509
481
- if d != nil {
482
- d .SetId ("" )
483
- }
484
-
485
510
return nil
486
511
}
487
512
@@ -507,6 +532,38 @@ func orderDetails(c *ovh.Client, orderId int64) ([]*MeOrderDetail, error) {
507
532
return details , nil
508
533
}
509
534
535
+ func serviceNameFromOrder (c * ovh.Client , orderId int64 , plan string ) (string , error ) {
536
+ detailIds := []int64 {}
537
+ endpoint := fmt .Sprintf ("/me/order/%d/details" , orderId )
538
+ if err := c .Get (endpoint , & detailIds ); err != nil {
539
+ return "" , fmt .Errorf ("calling get %s:\n \t %q" , endpoint , err )
540
+ }
541
+
542
+ for _ , detailId := range detailIds {
543
+ detailExtension := & MeOrderDetailExtension {}
544
+ log .Printf ("[DEBUG] Will read order detail extension %d/%d" , orderId , detailId )
545
+ endpoint := fmt .Sprintf ("/me/order/%d/details/%d/extension" , orderId , detailId )
546
+ if err := c .Get (endpoint , detailExtension ); err != nil {
547
+ return "" , fmt .Errorf ("calling get %s:\n \t %q" , endpoint , err )
548
+ }
549
+
550
+ if detailExtension .Order .Plan .Code != plan {
551
+ continue
552
+ }
553
+
554
+ detail := & MeOrderDetail {}
555
+ log .Printf ("[DEBUG] Will read order detail %d/%d" , orderId , detailId )
556
+ endpoint = fmt .Sprintf ("/me/order/%d/details/%d" , orderId , detailId )
557
+ if err := c .Get (endpoint , detail ); err != nil {
558
+ return "" , fmt .Errorf ("calling get %s:\n \t %q" , endpoint , err )
559
+ }
560
+
561
+ return detail .Domain , nil
562
+ }
563
+
564
+ return "" , errors .New ("serviceName not found" )
565
+ }
566
+
510
567
func waitForOrder (c * ovh.Client , id int64 ) resource.StateRefreshFunc {
511
568
return func () (interface {}, string , error ) {
512
569
var r string
0 commit comments