@@ -393,6 +393,24 @@ func describeInputFromQuery(q ec2OfferingQuery, nextToken *string) *ec2.Describe
393393 }
394394}
395395
396+ // buildEC2QueryFromRec extracts ComputeDetails from rec, converts the payment
397+ // option, and assembles the ec2OfferingQuery used by findOfferingID.
398+ //
399+ // Pulled out of findOfferingID to keep that function under the cyclomatic limit.
400+ func (c * Client ) buildEC2QueryFromRec (rec common.Recommendation ) (ec2OfferingQuery , error ) {
401+ details , ok := rec .Details .(* common.ComputeDetails )
402+ if ! ok || details == nil {
403+ return ec2OfferingQuery {}, fmt .Errorf ("invalid service details for EC2" )
404+ }
405+ wantOfferingType , err := convertEC2PaymentOption (rec .PaymentOption )
406+ if err != nil {
407+ return ec2OfferingQuery {}, err
408+ }
409+ q := buildEC2OfferingQuery (rec , details , c .getDurationValue (rec .Term ))
410+ q .wantOfferingType = wantOfferingType
411+ return q , nil
412+ }
413+
396414// findOfferingID finds the appropriate EC2 Reserved Instance offering ID.
397415//
398416// The input is built from typed first-class fields on
@@ -407,24 +425,18 @@ func describeInputFromQuery(q ec2OfferingQuery, nextToken *string) *ec2.Describe
407425// execID is the purchase execution UUID for log correlation; pass "" when
408426// calling outside of a purchase flow (ValidateOffering, GetOfferingDetails).
409427func (c * Client ) findOfferingID (ctx context.Context , rec common.Recommendation , execID string ) (string , error ) {
410- details , ok := rec .Details .(* common.ComputeDetails )
411- if ! ok || details == nil {
412- return "" , fmt .Errorf ("invalid service details for EC2" )
413- }
414- wantOfferingType , err := convertEC2PaymentOption (rec .PaymentOption )
428+ q , err := c .buildEC2QueryFromRec (rec )
415429 if err != nil {
416430 return "" , err
417431 }
418- q := buildEC2OfferingQuery (rec , details , c .getDurationValue (rec .Term ))
419- q .wantOfferingType = wantOfferingType
420432
421433 tag := execID
422434 if tag == "" {
423435 tag = "no-exec"
424436 }
425437 t0 := time .Now ()
426438 log .Printf ("purchase[%s]: EC2 findOfferingID starting (instance=%s platform=%s tenancy=%s term=%s payment=%s)" ,
427- tag , rec .ResourceType , details . Platform , details . Tenancy , rec .Term , rec .PaymentOption )
439+ tag , rec .ResourceType , q . productDesc , q . tenancy , rec .Term , rec .PaymentOption )
428440
429441 var nextToken * string
430442 page := 0
@@ -435,7 +447,7 @@ func (c *Client) findOfferingID(ctx context.Context, rec common.Recommendation,
435447 page ++
436448 if page > maxOfferingPages {
437449 return "" , fmt .Errorf ("pagination cap reached after %d pages for EC2 %s %s %s (issue #688)" ,
438- maxOfferingPages , rec .ResourceType , details . Platform , rec .PaymentOption )
450+ maxOfferingPages , rec .ResourceType , q . productDesc , rec .PaymentOption )
439451 }
440452 pageStart := time .Now ()
441453 result , err := c .client .DescribeReservedInstancesOfferings (ctx , describeInputFromQuery (q , nextToken ))
@@ -446,7 +458,7 @@ func (c *Client) findOfferingID(ctx context.Context, rec common.Recommendation,
446458 }
447459 log .Printf ("purchase[%s]: EC2 findOfferingID page %d: %d offerings in %s" ,
448460 tag , page , len (result .ReservedInstancesOfferings ), time .Since (pageStart ))
449- if id := scanEC2OfferingPage (result .ReservedInstancesOfferings , wantOfferingType ); id != "" {
461+ if id := scanEC2OfferingPage (result .ReservedInstancesOfferings , q . wantOfferingType ); id != "" {
450462 log .Printf ("purchase[%s]: EC2 findOfferingID found match on page %d after %s total" ,
451463 tag , page , time .Since (t0 ))
452464 return id , nil
@@ -459,7 +471,7 @@ func (c *Client) findOfferingID(ctx context.Context, rec common.Recommendation,
459471 log .Printf ("purchase[%s]: EC2 findOfferingID exhausted %d page(s) in %s -- no match" ,
460472 tag , page , time .Since (t0 ))
461473 return "" , fmt .Errorf ("no offerings found for EC2 %s %s %s after %d page(s) (issue #688)" ,
462- rec .ResourceType , details . Platform , rec .PaymentOption , page )
474+ rec .ResourceType , q . productDesc , rec .PaymentOption , page )
463475}
464476
465477// isLastEC2Page reports whether a NextToken indicates the terminal page.
0 commit comments