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): support server offer name #345

Merged
merged 3 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/nicolai86/scaleway-sdk v0.0.0-20181024210327-b20018e944c4
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191120144002-49d4c257183c
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191129152922-8bd96cf4ad2b
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191119165406-d81e1e1664e8
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191119165406-d81e1e1664e8/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191120144002-49d4c257183c h1:x1YLEU06F+OQDj/L/ruDd7zMbQcebAPRhjWh73rHocU=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191120144002-49d4c257183c/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191128164614-052727eef0bc h1:QVskZ9JZYB6XQ3yNLFJCZlhyTlX3EMeMSu/HXSErYsQ=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191128164614-052727eef0bc/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191129101337-5b36e82b6476 h1:IrbDuoVs3HYGcxPvRAnujlAzO8qeROTxf2VksXdjoVs=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191129101337-5b36e82b6476/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191129152922-8bd96cf4ad2b h1:Zw8mVAORIdn+eMTdpTlMXKIUrTpCY01ifhkKsRj63Ms=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.4.0.20191129152922-8bd96cf4ad2b/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
Expand Down
23 changes: 23 additions & 0 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,18 @@ func expandOrGenerateString(data interface{}, prefix string) string {
return data.(string)
}

func expandLabelUUID(data interface{}) (label, uuid string) {
parts := strings.Split(data.(string), "/")
if len(parts) != 2 {
panic(fmt.Errorf("'%s' is not a valid label/uuid format", data.(string)))
}
return parts[0], parts[1]
}

func flattenLabelUUID(label, uuid string) string {
return label + "/" + uuid
}

func expandStrings(data interface{}) []string {
if data == nil {
return nil
Expand Down Expand Up @@ -634,3 +646,14 @@ func diffSuppressFuncIgnoreCaseAndHyphen(k, old, new string, d *schema.ResourceD
}
return false
}

func diffSuppressFuncLabelUUID(k, old, new string, d *schema.ResourceData) bool {
if old == "" {
return false
}

// Get label and UUID.
label, uuid := expandLabelUUID(old)

return new == label || new == uuid
}
45 changes: 42 additions & 3 deletions scaleway/helpers_baremetal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package scaleway

import (
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -9,11 +11,11 @@ import (
)

const (
BaremetalServerWaitForTimeout = 60 * time.Minute
BaremetalServerRetryFuncTimeout = BaremetalServerWaitForTimeout + time.Minute // some RetryFunc are calling a WaitFor
baremetalServerWaitForTimeout = 60 * time.Minute
baremetalServerRetryFuncTimeout = baremetalServerWaitForTimeout + time.Minute // some RetryFunc are calling a WaitFor
)

var BaremetalServerResourceTimeout = BaremetalServerRetryFuncTimeout + time.Minute
var baremetalServerResourceTimeout = baremetalServerRetryFuncTimeout + time.Minute

// getInstanceAPIWithZone returns a new baremetal API and the zone for a Create request
func getBaremetalAPIWithZone(d *schema.ResourceData, m interface{}) (*baremetal.API, scw.Zone, error) {
Expand All @@ -32,3 +34,40 @@ func getBaremetalAPIWithZoneAndID(m interface{}, id string) (*baremetal.API, scw
zone, ID, err := parseZonedID(id)
return baremetalAPI, zone, ID, err
}

// TODO: Remove it when SDK will handle it.
// getBaremetalOfferByName call baremetal API to get an offer by its exact name.
func getBaremetalOfferByName(baremetalAPI *baremetal.API, zone scw.Zone, offerName string) (*baremetal.Offer, error) {
offerRes, err := baremetalAPI.ListOffers(&baremetal.ListOffersRequest{
Zone: zone,
}, scw.WithAllPages())
if err != nil {
return nil, err
}

offerName = strings.ToUpper(offerName)
for _, offer := range offerRes.Offers {
if offer.Name == offerName {
return offer, nil
}
}
return nil, fmt.Errorf("cannot find the offer %s", offerName)
}

// TODO: Remove it when SDK will handle it.
// getBaremetalOfferByID call baremetal API to get an offer by its exact name.
func getBaremetalOfferByID(baremetalAPI *baremetal.API, zone scw.Zone, offerID string) (*baremetal.Offer, error) {
offerRes, err := baremetalAPI.ListOffers(&baremetal.ListOffersRequest{
Zone: zone,
}, scw.WithAllPages())
if err != nil {
return nil, err
}

for _, offer := range offerRes.Offers {
if offer.ID == offerID {
return offer, nil
}
}
return nil, fmt.Errorf("cannot find the offer %s", offerID)
}
49 changes: 49 additions & 0 deletions scaleway/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -196,6 +197,54 @@ func TestGetRandomName(t *testing.T) {
assert.True(t, strings.HasPrefix(name, "tf-test-"))
}

func TestDiffSuppressFuncLabelUUID(t *testing.T) {
testCases := []struct {
name string
old string
new string
isEqual bool
}{
{
name: "no label changes",
old: "foo/11111111-1111-1111-1111-111111111111",
new: "foo",
isEqual: true,
},
{
name: "no UUID changes",
old: "foo/11111111-1111-1111-1111-111111111111",
new: "11111111-1111-1111-1111-111111111111",
isEqual: true,
},
{
name: "UUID changes",
old: "foo/11111111-1111-1111-1111-111111111111",
new: "22222222-2222-2222-2222-222222222222",
isEqual: false,
},
{
name: "To label",
old: "foo/11111111-1111-1111-1111-111111111111",
new: "foo",
isEqual: true,
},
{
name: "To label change",
old: "foo/11111111-1111-1111-1111-111111111111",
new: "bar",
isEqual: false,
},
}

fakeResourceData := &schema.ResourceData{}

for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
assert.Equal(t, c.isEqual, diffSuppressFuncLabelUUID("", c.old, c.new, fakeResourceData))
})
}
}

func testCheckResourceAttrFunc(name string, key string, test func(string) error) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down
52 changes: 37 additions & 15 deletions scaleway/resource_baremetal_server_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
sdkValidation "github.com/scaleway/scaleway-sdk-go/validation"
)

func resourceScalewayBaremetalServerBeta() *schema.Resource {
Expand All @@ -18,8 +19,8 @@ func resourceScalewayBaremetalServerBeta() *schema.Resource {
},
SchemaVersion: 0,
Timeouts: &schema.ResourceTimeout{
Create: &BaremetalServerResourceTimeout,
Delete: &BaremetalServerResourceTimeout,
Create: &baremetalServerResourceTimeout,
Delete: &baremetalServerResourceTimeout,
},
Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -28,17 +29,17 @@ func resourceScalewayBaremetalServerBeta() *schema.Resource {
Computed: true,
Description: "Name of the server",
},
"offer_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the server type",
ValidateFunc: validationUUID(),
"offer": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the server type",
DiffSuppressFunc: diffSuppressFuncLabelUUID,
},
"os_id": {
Type: schema.TypeString,
Required: true,
Description: "The base image of the server", // TODO: add in doc example with UUID
Description: "The base image of the server",
ValidateFunc: validationUUID(),
},
"ssh_key_ids": {
Expand Down Expand Up @@ -66,6 +67,13 @@ func resourceScalewayBaremetalServerBeta() *schema.Resource {
},
"zone": zoneSchema(),
"organization_id": organizationIDSchema(),

// TODO: Remove deleted attributes at the end of the beta.
"offer_id": {
Type: schema.TypeString,
Optional: true,
Removed: "Please use offer instead",
},
},
}
}
Expand All @@ -76,12 +84,21 @@ func resourceScalewayBaremetalServerBetaCreate(d *schema.ResourceData, m interfa
return err
}

offer := d.Get("offer").(string)
if !sdkValidation.IsUUID(offer) {
o, err := getBaremetalOfferByName(baremetalAPI, zone, offer)
if err != nil {
return err
}
offer = o.ID
}

createReq := &baremetal.CreateServerRequest{
Zone: zone,
Name: expandOrGenerateString(d.Get("name"), "bm"),
OrganizationID: d.Get("organization_id").(string),
Description: d.Get("description").(string),
OfferID: d.Get("offer_id").(string),
OfferID: offer,
}
if raw, ok := d.GetOk("tags"); ok {
for _, tag := range raw.([]interface{}) {
Expand All @@ -98,7 +115,7 @@ func resourceScalewayBaremetalServerBetaCreate(d *schema.ResourceData, m interfa
_, err = baremetalAPI.WaitForServer(&baremetal.WaitForServerRequest{
Zone: zone,
ServerID: res.ID,
Timeout: BaremetalServerWaitForTimeout,
Timeout: baremetalServerWaitForTimeout,
})
if err != nil {
return err
Expand All @@ -120,10 +137,10 @@ func resourceScalewayBaremetalServerBetaCreate(d *schema.ResourceData, m interfa
return err
}

_, err = baremetalAPI.WaitForServer(&baremetal.WaitForServerRequest{
_, err = baremetalAPI.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
Zone: zone,
ServerID: res.ID,
Timeout: BaremetalServerWaitForTimeout,
Timeout: baremetalServerWaitForTimeout,
})
if err != nil {
return err
Expand Down Expand Up @@ -151,11 +168,16 @@ func resourceScalewayBaremetalServerBetaRead(d *schema.ResourceData, m interface
return err
}

offer, err := getBaremetalOfferByID(baremetalAPI, zone, res.OfferID)
if err != nil {
return err
}

d.Set("name", res.Name)
d.Set("zone", string(zone))
d.Set("organization_id", res.OrganizationID)
d.Set("offer", flattenLabelUUID(offer.Name, offer.ID))
d.Set("tags", res.Tags)
d.Set("type", res.OfferID)
if res.Install != nil {
d.Set("os_id", res.Install.OsID)
d.Set("ssh_key_ids", res.Install.SSHKeyIDs)
Expand Down Expand Up @@ -242,7 +264,7 @@ func resourceScalewayBaremetalServerBetaDelete(d *schema.ResourceData, m interfa
_, err = baremetalAPI.WaitForServer(&baremetal.WaitForServerRequest{
Zone: zone,
ServerID: ID,
Timeout: BaremetalServerWaitForTimeout,
Timeout: baremetalServerWaitForTimeout,
})

if is404Error(err) {
Expand Down
16 changes: 8 additions & 8 deletions scaleway/resource_baremetal_server_beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccScalewayBaremetalServerBetaMinimal1(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayBaremetalServerBetaExists("scaleway_baremetal_server_beta.base"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "name", "namo-centos"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "offer_id", "964f9b38-577e-470f-a220-7d762f9e8672"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "offer", "GP-BM1-M/964f9b38-577e-470f-a220-7d762f9e8672"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "os_id", "d17d6872-0412-45d9-a198-af82c34d3c5c"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "description", "test a description"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "tags.0", "terraform-test"),
Expand All @@ -33,7 +33,7 @@ func TestAccScalewayBaremetalServerBetaMinimal1(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayBaremetalServerBetaExists("scaleway_baremetal_server_beta.base"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "name", "namo-centos"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "offer_id", "964f9b38-577e-470f-a220-7d762f9e8672"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "offer", "GP-BM1-M/964f9b38-577e-470f-a220-7d762f9e8672"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "os_id", "d17d6872-0412-45d9-a198-af82c34d3c5c"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "description", "test a description"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "tags.0", "terraform-test"),
Expand All @@ -47,7 +47,7 @@ func TestAccScalewayBaremetalServerBetaMinimal1(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayBaremetalServerBetaExists("scaleway_baremetal_server_beta.base"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "name", "namo-ubuntu"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "offer_id", "964f9b38-577e-470f-a220-7d762f9e8672"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "offer", "GP-BM1-M/964f9b38-577e-470f-a220-7d762f9e8672"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "os_id", "d859aa89-8b4a-4551-af42-ff7c0c27260a"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "description", "test a description"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "tags.0", "terraform-test"),
Expand All @@ -61,7 +61,7 @@ func TestAccScalewayBaremetalServerBetaMinimal1(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayBaremetalServerBetaExists("scaleway_baremetal_server_beta.base"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "name", "namo-ubuntu"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "offer_id", "964f9b38-577e-470f-a220-7d762f9e8672"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "offer", "GP-BM1-M/964f9b38-577e-470f-a220-7d762f9e8672"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "os_id", "d859aa89-8b4a-4551-af42-ff7c0c27260a"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "description", "test a description"),
resource.TestCheckResourceAttr("scaleway_baremetal_server_beta.base", "tags.0", "terraform-test"),
Expand Down Expand Up @@ -138,7 +138,7 @@ resource "scaleway_baremetal_server_beta" "base" {
name = "namo-centos"
zone = "fr-par-2"
description = "test a description"
offer_id = "964f9b38-577e-470f-a220-7d762f9e8672"
offer = "GP-BM1-M"
os_id = "d17d6872-0412-45d9-a198-af82c34d3c5c"

tags = [ "terraform-test", "scaleway_baremetal_server_beta", "minimal" ]
Expand All @@ -154,7 +154,7 @@ resource "scaleway_baremetal_server_beta" "base" {
name = "namo-centos"
zone = "fr-par-2"
description = "test a description"
offer_id = "964f9b38-577e-470f-a220-7d762f9e8672"
offer = "GP-BM1-M"
os_id = "d17d6872-0412-45d9-a198-af82c34d3c5c"

tags = [ "terraform-test", "scaleway_baremetal_server_beta", "minimal", "edited" ]
Expand All @@ -170,7 +170,7 @@ resource "scaleway_baremetal_server_beta" "base" {
name = "namo-ubuntu"
zone = "fr-par-2"
description = "test a description"
offer_id = "964f9b38-577e-470f-a220-7d762f9e8672"
offer = "GP-BM1-M"
os_id = "d859aa89-8b4a-4551-af42-ff7c0c27260a"

tags = [ "terraform-test", "scaleway_baremetal_server_beta", "minimal", "edited" ]
Expand All @@ -186,7 +186,7 @@ resource "scaleway_baremetal_server_beta" "base" {
name = "namo-ubuntu"
zone = "fr-par-2"
description = "test a description"
offer_id = "964f9b38-577e-470f-a220-7d762f9e8672"
offer = "964f9b38-577e-470f-a220-7d762f9e8672"
os_id = "d859aa89-8b4a-4551-af42-ff7c0c27260a"

tags = [ "terraform-test", "scaleway_baremetal_server_beta", "minimal", "edited" ]
Expand Down
Loading