From 9b8bee8eddbfacceb5754a2a414e5ef954528fe7 Mon Sep 17 00:00:00 2001 From: Jasmin Gacic Date: Wed, 3 May 2017 21:29:03 +0200 Subject: [PATCH] Private images Fix (#14173) * Allowing for volumes to be created with private ProfitBricks images without image password or ssh keys * Acceptance test fix * Errorf formatting fix * Dependencies update --- .../profitbricks/data_source_image_test.go | 2 +- .../resource_profitbricks_server.go | 42 ++++++----- .../resource_profitbricks_volume.go | 30 +++++--- .../profitbricks/profitbricks-sdk-go/nic.go | 2 +- .../profitbricks-sdk-go/request.go | 69 +++++++++++++++++++ vendor/vendor.json | 6 +- 6 files changed, 117 insertions(+), 34 deletions(-) diff --git a/builtin/providers/profitbricks/data_source_image_test.go b/builtin/providers/profitbricks/data_source_image_test.go index 3efe6d325..3f8f151a4 100644 --- a/builtin/providers/profitbricks/data_source_image_test.go +++ b/builtin/providers/profitbricks/data_source_image_test.go @@ -17,7 +17,7 @@ func TestAccDataSourceImage_basic(t *testing.T) { Config: testAccDataSourceProfitBricksImage_basic, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("data.profitbricks_image.img", "location", "us/las"), - resource.TestCheckResourceAttr("data.profitbricks_image.img", "name", "Ubuntu-16.04-LTS-server-2017-02-01"), + resource.TestCheckResourceAttr("data.profitbricks_image.img", "name", "Ubuntu-16.04-LTS-server-2017-05-01"), resource.TestCheckResourceAttr("data.profitbricks_image.img", "type", "HDD"), ), }, diff --git a/builtin/providers/profitbricks/resource_profitbricks_server.go b/builtin/providers/profitbricks/resource_profitbricks_server.go index bfcd1678a..c617f691d 100644 --- a/builtin/providers/profitbricks/resource_profitbricks_server.go +++ b/builtin/providers/profitbricks/resource_profitbricks_server.go @@ -254,34 +254,38 @@ func resourceProfitBricksServerCreate(d *schema.ResourceData, meta interface{}) var sshkey_path []interface{} var image, licenceType, availabilityZone string - if !IsValidUUID(rawMap["image_name"].(string)) { - if rawMap["image_name"] != nil { - image = getImageId(d.Get("datacenter_id").(string), rawMap["image_name"].(string), rawMap["disk_type"].(string)) - if image == "" { - dc := profitbricks.GetDatacenter(d.Get("datacenter_id").(string)) - return fmt.Errorf("Image '%s' doesn't exist. in location %s", rawMap["image_name"], dc.Properties.Location) - - } - } - } else { - image = rawMap["image_name"].(string) - } - - if rawMap["licence_type"] != nil { - licenceType = rawMap["licence_type"].(string) - } - if rawMap["image_password"] != nil { imagePassword = rawMap["image_password"].(string) } if rawMap["ssh_key_path"] != nil { sshkey_path = rawMap["ssh_key_path"].([]interface{}) } - if rawMap["image_name"] != nil { + + image_name := rawMap["image_name"].(string) + if !IsValidUUID(image_name) { if imagePassword == "" && len(sshkey_path) == 0 { - return fmt.Errorf("Either 'image_password' or 'ssh_key_path' must be provided.") + return fmt.Errorf("Either 'image_password' or 'sshkey' must be provided.") + } + image = getImageId(d.Get("datacenter_id").(string), image_name, rawMap["disk_type"].(string)) + } else { + img := profitbricks.GetImage(image_name) + if img.StatusCode > 299 { + return fmt.Errorf("Error fetching image: %s", img.Response) + } + if img.Properties.Public == true { + if imagePassword == "" && len(sshkey_path) == 0 { + return fmt.Errorf("Either 'image_password' or 'sshkey' must be provided.") + } + image = image_name + } else { + image = image_name } } + + if rawMap["licence_type"] != nil { + licenceType = rawMap["licence_type"].(string) + } + var publicKeys []string if len(sshkey_path) != 0 { for _, path := range sshkey_path { diff --git a/builtin/providers/profitbricks/resource_profitbricks_volume.go b/builtin/providers/profitbricks/resource_profitbricks_volume.go index 8fca17854..46d8ff47d 100644 --- a/builtin/providers/profitbricks/resource_profitbricks_volume.go +++ b/builtin/providers/profitbricks/resource_profitbricks_volume.go @@ -77,12 +77,6 @@ func resourceProfitBricksVolumeCreate(d *schema.ResourceData, meta interface{}) ssh_keypath = d.Get("ssh_key_path").([]interface{}) image_name := d.Get("image_name").(string) - if image_name != "" { - if imagePassword == "" && len(ssh_keypath) == 0 { - return fmt.Errorf("Either 'image_password' or 'sshkey' must be provided.") - } - } - licenceType := d.Get("licence_type").(string) if image_name == "" && licenceType == "" { @@ -102,10 +96,26 @@ func resourceProfitBricksVolumeCreate(d *schema.ResourceData, meta interface{}) } var image string - if !IsValidUUID(image_name) { - image = getImageId(d.Get("datacenter_id").(string), image_name, d.Get("disk_type").(string)) - } else { - image = image_name + if image_name != "" { + if !IsValidUUID(image_name) { + if imagePassword == "" && len(ssh_keypath) == 0 { + return fmt.Errorf("Either 'image_password' or 'sshkey' must be provided.") + } + image = getImageId(d.Get("datacenter_id").(string), image_name, d.Get("disk_type").(string)) + } else { + img := profitbricks.GetImage(image_name) + if img.StatusCode > 299 { + return fmt.Errorf("Error fetching image: %s", img.Response) + } + if img.Properties.Public == true { + if imagePassword == "" && len(ssh_keypath) == 0 { + return fmt.Errorf("Either 'image_password' or 'sshkey' must be provided.") + } + image = image_name + } else { + image = image_name + } + } } volume := profitbricks.Volume{ diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/nic.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/nic.go index 1d082a64d..86571ad01 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/nic.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/nic.go @@ -22,7 +22,7 @@ type NicProperties struct { Name string `json:"name,omitempty"` Mac string `json:"mac,omitempty"` Ips []string `json:"ips,omitempty"` - Dhcp bool `json:"dhcp,omitempty"` + Dhcp bool `json:"dhcp"` Lan int `json:"lan,omitempty"` FirewallActive bool `json:"firewallActive,omitempty"` Nat bool `json:"nat,omitempty"` diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/request.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/request.go index 023ae1a4a..98cbbffd9 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/request.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/request.go @@ -3,6 +3,7 @@ package profitbricks import ( "encoding/json" "net/http" + "time" ) type RequestStatus struct { @@ -26,6 +27,55 @@ type RequestTarget struct { Status string `json:"status,omitempty"` } +type Requests struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Items []Request `json:"items,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type Request struct { + ID string `json:"id"` + Type string `json:"type"` + Href string `json:"href"` + Metadata struct { + CreatedDate time.Time `json:"createdDate"` + CreatedBy string `json:"createdBy"` + Etag string `json:"etag"` + RequestStatus struct { + ID string `json:"id"` + Type string `json:"type"` + Href string `json:"href"` + } `json:"requestStatus"` + } `json:"metadata"` + Properties struct { + Method string `json:"method"` + Headers interface{} `json:"headers"` + Body interface{} `json:"body"` + URL string `json:"url"` + } `json:"properties"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +func ListRequests() Requests { + url := mk_url("/requests") + `?depth=` + Depth + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + return toRequests(do(req)) +} + +func GetRequest(req_id string) Request { + url := mk_url("/requests/" + req_id) + `?depth=` + Depth + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + return toRequest(do(req)) +} + func GetRequestStatus(path string) RequestStatus { url := mk_url(path) + `?depth=` + Depth req, _ := http.NewRequest("GET", url, nil) @@ -41,3 +91,22 @@ func toRequestStatus(resp Resp) RequestStatus { server.StatusCode = resp.StatusCode return server } + +func toRequests(resp Resp) Requests { + var server Requests + json.Unmarshal(resp.Body, &server) + server.Response = string(resp.Body) + server.Headers = &resp.Headers + server.StatusCode = resp.StatusCode + return server +} + +func toRequest(resp Resp) Request { + var server Request + json.Unmarshal(resp.Body, &server) + server.Response = string(resp.Body) + server.Headers = &resp.Headers + server.StatusCode = resp.StatusCode + return server +} + diff --git a/vendor/vendor.json b/vendor/vendor.json index 46ac7603c..7429ad0f2 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -2804,10 +2804,10 @@ "revisionTime": "2016-10-29T09:36:37Z" }, { - "checksumSHA1": "WPDz/Ed0OwaP7F/HvzZQ8OwT3fM=", + "checksumSHA1": "72v9EDEuAVJBbzmZ/TT1etVzZTU=", "path": "github.com/profitbricks/profitbricks-sdk-go", - "revision": "b279e1adcaf1c9cae4d6520c1bdbbf4674e7806e", - "revisionTime": "2017-01-11T22:35:15Z" + "revision": "6198377adc75ed630c2a0a4b3f42d131baf7f316", + "revisionTime": "2017-05-03T18:59:30Z" }, { "checksumSHA1": "hcyoctYs0NjsvAXXVRc4mVt+FBg=",