From 7e683c3029af4a987476a09c6b62aadf663a6119 Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Sat, 23 Jun 2018 18:29:10 -0700 Subject: [PATCH] r/server: validate instance type availability --- scaleway/resource_server.go | 14 ++++++++++++++ .../nicolai86/scaleway-sdk/availability.go | 14 +++++++++++++- vendor/vendor.json | 6 +++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/scaleway/resource_server.go b/scaleway/resource_server.go index 8ac2c8b42..d1ca81aa5 100644 --- a/scaleway/resource_server.go +++ b/scaleway/resource_server.go @@ -179,6 +179,20 @@ func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error { req.DynamicIPRequired = Bool(d.Get("dynamic_ip_required").(bool)) req.CommercialType = d.Get("type").(string) + availabilities, err := scaleway.GetServerAvailabilities() + if err != nil { + log.Printf("[DEBUG] Unable to fetch server availability; won't validate availability: %q\n", err.Error()) + } else { + typeAvailability, ok := availabilities[req.CommercialType] + if !ok { + // this will most likely happen for new instance types + log.Printf("[DEBUG] no server availability for type %q. Ignoring\n", req.CommercialType) + } + if typeAvailability.Availability == api.InstanceTypeShortage { + return fmt.Errorf("InstanceType %s is currently out of stock", req.CommercialType) + } + } + if bootscript, ok := d.GetOk("bootscript"); ok { req.Bootscript = String(bootscript.(string)) } diff --git a/vendor/github.com/nicolai86/scaleway-sdk/availability.go b/vendor/github.com/nicolai86/scaleway-sdk/availability.go index ea2ad7355..58977127d 100644 --- a/vendor/github.com/nicolai86/scaleway-sdk/availability.go +++ b/vendor/github.com/nicolai86/scaleway-sdk/availability.go @@ -6,7 +6,19 @@ import ( "io/ioutil" ) -type ServerAvailabilities map[string]interface{} +type InstanceTypeAvailability string + +var ( + InstanceTypeAvailable InstanceTypeAvailability = "available" + InstanceTypeScarce InstanceTypeAvailability = "scarce" + InstanceTypeShortage InstanceTypeAvailability = "shortage" +) + +type ServerAvailability struct { + Availability InstanceTypeAvailability `json:"availability"` +} + +type ServerAvailabilities map[string]ServerAvailability func (a ServerAvailabilities) CommercialTypes() []string { types := []string{} diff --git a/vendor/vendor.json b/vendor/vendor.json index 25208bd91..ef9370efd 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -570,10 +570,10 @@ "revisionTime": "2016-10-03T17:45:16Z" }, { - "checksumSHA1": "5PC3eHJZwTMQyGJltpcIJp/aCNw=", + "checksumSHA1": "ucJEyEBIAPbSsctcjxHTqN/1aoo=", "path": "github.com/nicolai86/scaleway-sdk", - "revision": "4871b0a66c85ce1a78bb5d8a62714ed1a5669650", - "revisionTime": "2018-05-12T06:04:43Z" + "revision": "a7b61129ca818b3a2d1ebb3d276ed82b5b7eabfe", + "revisionTime": "2018-06-24T01:24:03Z" }, { "checksumSHA1": "u5s2PZ7fzCOqQX7bVPf9IJ+qNLQ=",