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 (
@@ -220,22 +223,22 @@ func genericOrderSchema(withOptions bool) map[string]*schema.Schema {
220
223
221
224
func orderCreateFromResource (d * schema.ResourceData , meta interface {}, product string ) error {
222
225
config := meta .(* Config )
223
- order := (& OrderCreateType {}).FromResource (d )
226
+ order := (& OrderModel {}).FromResource (d )
224
227
225
228
err := orderCreate (order , config , product )
226
229
if err != nil {
227
230
return err
228
231
}
229
232
230
- d .SetId (order .OrderID )
233
+ d .SetId (fmt . Sprint ( order .Order . OrderId . ValueInt64 ()) )
231
234
232
235
return nil
233
236
}
234
237
235
- func orderCreate (d * OrderCreateType , config * Config , product string ) error {
238
+ func orderCreate (d * OrderModel , config * Config , product string ) error {
236
239
// create Cart
237
240
cartParams := & OrderCartCreateOpts {
238
- OvhSubsidiary : strings .ToUpper (d .OvhSubsidiary ),
241
+ OvhSubsidiary : strings .ToUpper (d .OvhSubsidiary . ValueString () ),
239
242
}
240
243
241
244
cart , err := orderCartCreate (config , cartParams , true )
@@ -245,8 +248,9 @@ func orderCreate(d *OrderCreateType, config *Config, product string) error {
245
248
246
249
// Create Product Item
247
250
item := & OrderCartItem {}
248
- cartPlanParams := d .Plans [0 ]
249
- cartPlanParams .Quantity = 1
251
+ cartPlanParamsList := d .Plan .Elements ()
252
+ cartPlanParams := cartPlanParamsList [0 ].(* PlanValue )
253
+ cartPlanParams .Quantity = types.TfInt64Value {Int64Value : basetypes .NewInt64Value (1 )}
250
254
251
255
log .Printf ("[DEBUG] Will create order item %s for cart: %s" , product , cart .CartId )
252
256
endpoint := fmt .Sprintf ("/order/cart/%s/%s" , url .PathEscape (cart .CartId ), product )
@@ -255,7 +259,9 @@ func orderCreate(d *OrderCreateType, config *Config, product string) error {
255
259
}
256
260
257
261
// apply configurations
258
- for _ , cfg := range cartPlanParams .Configuration {
262
+ configs := cartPlanParams .Configuration .Elements ()
263
+
264
+ for _ , cfg := range configs {
259
265
log .Printf ("[DEBUG] Will create order cart item configuration for cart item: %s/%d" ,
260
266
item .CartId ,
261
267
item .ItemId ,
@@ -270,21 +276,25 @@ func orderCreate(d *OrderCreateType, config *Config, product string) error {
270
276
}
271
277
}
272
278
279
+ planOptionValue := d .PlanOption .Elements ()
280
+
273
281
// Create Product Options Items
274
- for _ , option := range d .PlanOptions {
282
+ for _ , option := range planOptionValue {
283
+ opt := option .(* PlanOptionValue )
284
+
275
285
log .Printf ("[DEBUG] Will create order item options %s for cart: %s" , product , cart .CartId )
276
286
productOptionsItem := & OrderCartItem {}
277
287
278
- option .ItemId = item .ItemId
279
- option .Quantity = 1
288
+ opt .ItemId = types. TfInt64Value { Int64Value : basetypes . NewInt64Value ( item .ItemId )}
289
+ opt .Quantity = types. TfInt64Value { Int64Value : basetypes . NewInt64Value ( 1 )}
280
290
281
291
endpoint := fmt .Sprintf ("/order/cart/%s/%s/options" , url .PathEscape (cart .CartId ), product )
282
- if err := config .OVHClient .Post (endpoint , option , productOptionsItem ); err != nil {
292
+ if err := config .OVHClient .Post (endpoint , opt , productOptionsItem ); err != nil {
283
293
return fmt .Errorf ("calling Post %s with params %v:\n \t %q" , endpoint , cartPlanParams , err )
284
294
}
285
295
286
- // apply configurations
287
- for _ , cfg := range option . Configuration {
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 ,
@@ -380,7 +390,7 @@ func orderCreate(d *OrderCreateType, config *Config, product string) error {
380
390
return fmt .Errorf ("waiting for order (%d): %s" , checkout .OrderID , err )
381
391
}
382
392
383
- d .OrderID = fmt . Sprint (checkout .OrderID )
393
+ d .Order . OrderId = types. TfInt64Value { Int64Value : basetypes . NewInt64Value (checkout .OrderID )}
384
394
385
395
return nil
386
396
}
@@ -422,7 +432,7 @@ func orderRead(orderId string, config *Config) (*MeOrder, []*MeOrderDetail, erro
422
432
}
423
433
424
434
if len (details ) < 1 {
425
- 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 )
426
436
}
427
437
428
438
return order , details , nil
@@ -522,6 +532,38 @@ func orderDetails(c *ovh.Client, orderId int64) ([]*MeOrderDetail, error) {
522
532
return details , nil
523
533
}
524
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
+
525
567
func waitForOrder (c * ovh.Client , id int64 ) resource.StateRefreshFunc {
526
568
return func () (interface {}, string , error ) {
527
569
var r string
0 commit comments