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
This commit is contained in:
Jasmin Gacic 2017-05-03 21:29:03 +02:00 committed by Paul Stack
parent ef79fbeef0
commit 9b8bee8edd
6 changed files with 117 additions and 34 deletions

View File

@ -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"),
),
},

View File

@ -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 {

View File

@ -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 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{

View File

@ -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"`

View File

@ -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
}

6
vendor/vendor.json vendored
View File

@ -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=",