Skip to content

Commit

Permalink
Merge pull request equinix#64 from equinix/spotmarketprices-metro
Browse files Browse the repository at this point in the history
add metro to spotmarketprices datasource
  • Loading branch information
displague authored Apr 13, 2021
2 parents 41c9ff7 + 65f7643 commit c81aaf2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
49 changes: 35 additions & 14 deletions metal/datasource_metal_spot_market_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ func dataSourceSpotMarketPrice() *schema.Resource {
Read: dataSourceMetalSpotMarketPriceRead,
Schema: map[string]*schema.Schema{
"facility": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
ConflictsWith: []string{"metro"},
Optional: true,
},
"metro": {
Type: schema.TypeString,
ConflictsWith: []string{"facility"},
Optional: true,
},
"plan": {
Type: schema.TypeString,
Expand All @@ -29,26 +35,41 @@ func dataSourceSpotMarketPrice() *schema.Resource {

func dataSourceMetalSpotMarketPriceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*packngo.Client)

sms := client.SpotMarket.(*packngo.SpotMarketServiceOp)
facility := d.Get("facility").(string)
metro := d.Get("metro").(string)
plan := d.Get("plan").(string)

prices, _, err := client.SpotMarket.Prices()
if facility != "" && metro != "" {
return fmt.Errorf("Parameters facility and metro cannot be used together")
}

filter := facility
fn := sms.PricesByFacility
filterType := "facility"

if metro != "" {
filter = metro
fn = sms.PricesByMetro
filterType = "metro"
}

prices, _, err := fn()
if err != nil {
return err
}

var price float64
if fac, ok := prices[facility]; ok {
if pri, ok := fac[plan]; ok {
price = pri
} else {
return fmt.Errorf("Facility %s does not have prices for plan %s", facility, plan)
}
} else {
return fmt.Errorf("There is no facility %s", facility)
match, ok := prices[filter]
if !ok {
return fmt.Errorf("Cannot find %s %s", filterType, filter)
}

price, ok := match[plan]
if !ok {
return fmt.Errorf("Cannot find price for plan %s in %s %s", plan, filterType, filter)
}

d.Set("price", price)
d.SetId(facility)
d.SetId(fmt.Sprintf("%s-%s-%s", filterType, filter, plan))
return nil
}
41 changes: 41 additions & 0 deletions metal/datasource_metal_spot_market_price_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package metal

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceMetalSpotPrice_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMetalSpotMarketRequestDestroy,
Steps: []resource.TestStep{
{
Config: testDataSourceMetalSpotMarketPrice(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.metal_spot_market_price.metro", "price"),
resource.TestCheckResourceAttrSet(
"data.metal_spot_market_price.facility", "price"),
),
},
},
})
}

func testDataSourceMetalSpotMarketPrice() string {
return fmt.Sprintf(`
data "metal_spot_market_price" "metro" {
metro = "sv"
plan = "c3.medium.x86"
}
data "metal_spot_market_price" "facility" {
facility = "sjc1"
plan = "c3.medium.x86"
}
`)
}

0 comments on commit c81aaf2

Please sign in to comment.