Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(baremetal): add offer data source #361

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions scaleway/data_source_baremetal_offer_beta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package scaleway

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayBaremetalOfferBeta() *schema.Resource {
return &schema.Resource{
Read: dataSourceScalewayBaremetalOfferBetaRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Exact name of the desired offer",
ConflictsWith: []string{"offer_id"},
},
"offer_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the desired offer",
ConflictsWith: []string{"name"},
},
"include_disabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Include disabled offers",
},
"zone": zoneSchema(),

"bandwidth": {
Type: schema.TypeInt,
Computed: true,
Description: "Available Bandwidth with the offer",
},
"commercial_range": {
Type: schema.TypeString,
Computed: true,
Description: "Commercial range of the offer",
},
"cpu": {
Type: schema.TypeList,
Computed: true,
Description: "CPU specifications of the offer",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "CPU name",
},
"core_count": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of cores",
},
"frequency": {
Type: schema.TypeInt,
Computed: true,
Description: "Frequency of the CPU",
},
"thread_count": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of threads",
},
},
},
},
"disk": {
Type: schema.TypeList,
Computed: true,
Description: "Disk specifications of the offer",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of disk",
},
"capacity": {
Type: schema.TypeInt,
Computed: true,
Description: "Capacity of the disk in byte",
},
},
},
},
"memory": {
Type: schema.TypeList,
Computed: true,
Description: "Memory specifications of the offer",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of memory",
},
"capacity": {
Type: schema.TypeInt,
Computed: true,
Description: "Capacity of the memory in byte",
},
"frequency": {
Type: schema.TypeInt,
Computed: true,
Description: "Frequency of the memory",
},
"ecc": {
Type: schema.TypeBool,
Computed: true,
Description: "True if error-correcting code is available on this memory",
},
},
},
},
"stock": {
Type: schema.TypeString,
Computed: true,
Description: "Stock status for this offer",
},
},
}
}

func dataSourceScalewayBaremetalOfferBetaRead(d *schema.ResourceData, m interface{}) error {
meta := m.(*Meta)
baremetalApi, fallBackZone, err := getBaremetalAPIWithZone(d, meta)
if err != nil {
return err
}

zone, offerID, _ := parseZonedID(datasourceNewZonedID(d.Get("offer_id"), fallBackZone))
res, err := baremetalApi.ListOffers(&baremetal.ListOffersRequest{
Zone: zone,
}, scw.WithAllPages())
if err != nil {
return err
}

matches := []*baremetal.Offer(nil)
for _, offer := range res.Offers {
if offer.Name == d.Get("name") || offer.ID == offerID {
if !offer.Enable && !d.Get("include_disabled").(bool) {
return fmt.Errorf("offer %s (%s) found in zone %s but is disabled. Add allow_disabled=true in your terraform config to use it.", offer.Name, offer.Name, zone)
kindermoumoute marked this conversation as resolved.
Show resolved Hide resolved
}
matches = append(matches, offer)
}
}
if len(matches) == 0 {
return fmt.Errorf("no offer found with the name %s in zone %s", d.Get("name"), zone)
}
if len(matches) > 1 {
return fmt.Errorf("%d offers found with the same name %s in zone %s", len(matches), d.Get("name"), zone)
}

offer := matches[0]
zonedID := datasourceNewZonedID(offer.ID, zone)
d.SetId(zonedID)
d.Set("offer_id", zonedID)
d.Set("zone", zone)

if err != nil {
kindermoumoute marked this conversation as resolved.
Show resolved Hide resolved
return err
}

d.Set("name", offer.Name)
d.Set("include_disabled", !offer.Enable)
d.Set("bandwidth", offer.Bandwidth)
d.Set("commercial_range", offer.CommercialRange)
d.Set("cpu", flattenBaremetalCPUs(offer.CPU))
d.Set("disk", flattenBaremetalDisks(offer.Disk))
d.Set("memory", flattenBaremetalMemory(offer.Memory))
d.Set("stock", offer.Stock.String())

return nil
}
99 changes: 99 additions & 0 deletions scaleway/data_source_baremetal_offer_beta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package scaleway

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func TestAccScalewayDataSourceBaremetalOffer_Basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: `
data "scaleway_baremetal_offer_beta" "test1" {
zone = "fr-par-2"
name = "HC-BM1-L"
}

data "scaleway_baremetal_offer_beta" "test2" {
zone = "fr-par-2"
offer_id = "3ab0dc29-2fd4-486e-88bf-d08fbf49214b"
}

data "scaleway_baremetal_offer_beta" "test3" {
offer_id = "fr-par-2/3ab0dc29-2fd4-486e-88bf-d08fbf49214b"
}`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayBaremetalOfferExists("data.scaleway_baremetal_offer_beta.test1"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test1", "name", "HC-BM1-L"),
testAccCheckScalewayBaremetalOfferExists("data.scaleway_baremetal_offer_beta.test2"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "offer_id", "fr-par-2/3ab0dc29-2fd4-486e-88bf-d08fbf49214b"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "name", "HC-BM1-L"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "include_disabled", "false"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "bandwidth", "1000"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "commercial_range", "high_cpu"),
//resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "stock", "available"), // skipping this as stocks vary too much
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "cpu.0.name", "Intel Xeon Gold 5120"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "cpu.0.core_count", "28"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "cpu.0.frequency", "2200"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "cpu.0.thread_count", "56"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "cpu.1.name", "Intel Xeon Gold 5120"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "cpu.1.core_count", "28"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "cpu.1.frequency", "2200"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "cpu.1.thread_count", "56"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "disk.0.type", "NVMe"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "disk.0.capacity", "1024"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "disk.1.type", "NVMe"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "disk.1.capacity", "1024"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "disk.2.type", "NVMe"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "disk.3.capacity", "1024"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "memory.0.type", "DDR4"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "memory.0.capacity", "384"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "memory.0.frequency", "2133"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test2", "memory.0.ecc", "true"),
testAccCheckScalewayBaremetalOfferExists("data.scaleway_baremetal_offer_beta.test3"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer_beta.test3", "name", "HC-BM1-L"),
),
},
},
})
}

func testAccCheckScalewayBaremetalOfferExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

if !ok {
return fmt.Errorf("not found: %s", n)
}
fmt.Println(rs)

zone, id, err := parseZonedID(rs.Primary.ID)
if err != nil {
return err
}

meta := testAccProvider.Meta().(*Meta)
baremetalApi := baremetal.NewAPI(meta.scwClient)
resp, err := baremetalApi.ListOffers(&baremetal.ListOffersRequest{
Zone: zone,
}, scw.WithAllPages())

if err != nil {
return err
}
for _, offer := range resp.Offers {
if offer.ID == id {
return nil
}
}
return fmt.Errorf("offer %s not found in zone %s", id, zone)
}
}
46 changes: 46 additions & 0 deletions scaleway/helpers_baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,49 @@ func getBaremetalOfferByID(baremetalAPI *baremetal.API, zone scw.Zone, offerID s
}
return nil, fmt.Errorf("cannot find the offer %s", offerID)
}

func flattenBaremetalCPUs(cpus []*baremetal.CPU) interface{} {
if cpus == nil {
return nil
}
flattenedCPUs := []map[string]interface{}(nil)
for _, cpu := range cpus {
flattenedCPUs = append(flattenedCPUs, map[string]interface{}{
"name": cpu.Name,
"core_count": cpu.Cores,
"frequency": cpu.Frequency,
"thread_count": cpu.Threads,
})
}
return flattenedCPUs
}

func flattenBaremetalDisks(disks []*baremetal.Disk) interface{} {
if disks == nil {
return nil
}
flattenedCPUs := []map[string]interface{}(nil)
kindermoumoute marked this conversation as resolved.
Show resolved Hide resolved
for _, disk := range disks {
flattenedCPUs = append(flattenedCPUs, map[string]interface{}{
"type": disk.Type,
"capacity": disk.Capacity,
})
}
return flattenedCPUs
}

func flattenBaremetalMemory(memories []*baremetal.Memory) interface{} {
if memories == nil {
return nil
}
flattenedCPUs := []map[string]interface{}(nil)
kindermoumoute marked this conversation as resolved.
Show resolved Hide resolved
for _, memory := range memories {
flattenedCPUs = append(flattenedCPUs, map[string]interface{}{
"type": memory.Type,
"capacity": memory.Capacity,
"frequency": memory.Frequency,
"ecc": memory.Ecc,
})
}
return flattenedCPUs
}
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func Provider() terraform.ResourceProvider {
"scaleway_instance_server": dataSourceScalewayInstanceServer(),
"scaleway_instance_image": dataSourceScalewayInstanceImage(),
"scaleway_instance_volume": dataSourceScalewayInstanceVolume(),
"scaleway_baremetal_offer_beta": dataSourceScalewayBaremetalOfferBeta(),
QuentinBrosse marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand Down
Loading