packer-cn/vendor/github.com/profitbricks/profitbricks-sdk-go/test_helpers.go

147 lines
3.1 KiB
Go
Raw Normal View History

2016-06-28 22:35:41 -04:00
package profitbricks
2016-08-01 07:15:21 -04:00
import (
"fmt"
"os"
"strings"
2016-08-01 07:15:21 -04:00
"time"
)
2016-06-28 22:35:41 -04:00
func mkVolume(dcID string) string {
var request = Volume{
Properties: VolumeProperties{
Size: 2,
Name: "Volume Test",
Type: "HDD",
ImagePassword: "test1234",
ImageAlias: "ubuntu:latest",
},
}
resp := CreateVolume(dcID, request)
waitTillProvisioned(resp.Headers.Get("Location"))
return resp.Id
}
func mkipid(name string) string {
var obj = IpBlock{
Properties: IpBlockProperties{
Name: "GO SDK Test",
Size: 1,
Location: "us/las",
},
}
resp := ReserveIpBlock(obj)
return resp.Id
}
func mksnapshotId(name string, dcId string) string {
svolumeId := mkVolume(dcId)
resp := CreateSnapshot(dcId, svolumeId, name, "description")
waitTillProvisioned(resp.Headers.Get("Location"))
return resp.Id
}
2016-06-28 22:35:41 -04:00
func mkdcid(name string) string {
2016-08-01 07:15:21 -04:00
request := Datacenter{
Properties: DatacenterProperties{
2016-06-28 22:35:41 -04:00
Name: name,
Description: "description",
Location: "us/las",
},
}
dc := CreateDatacenter(request)
return dc.Id
}
func mksrvid(srv_dcid string) string {
2016-08-01 07:15:21 -04:00
var req = Server{
Properties: ServerProperties{
2016-06-28 22:35:41 -04:00
Name: "GO SDK test",
Ram: 1024,
Cores: 2,
},
}
srv := CreateServer(srv_dcid, req)
2016-08-01 07:15:21 -04:00
waitTillProvisioned(srv.Headers.Get("Location"))
2016-06-28 22:35:41 -04:00
return srv.Id
}
func mknic(lbal_dcid, serverid string) string {
2016-08-01 07:15:21 -04:00
var request = Nic{
Properties: &NicProperties{
Lan: 1,
Name: "GO SDK Test",
Nat: false,
Dhcp: true,
FirewallActive: true,
Ips: []string{"10.0.0.1"},
2016-06-28 22:35:41 -04:00
},
}
resp := CreateNic(lbal_dcid, serverid, request)
waitTillProvisioned(resp.Headers.Get("Location"))
return resp.Id
}
2016-06-28 22:35:41 -04:00
func mknic_custom(lbal_dcid, serverid string, lanid int, ips []string) string {
var request = Nic{
Properties: &NicProperties{
Lan: lanid,
Name: "GO SDK Test",
Nat: false,
Dhcp: true,
FirewallActive: true,
Ips: ips,
},
}
resp := CreateNic(lbal_dcid, serverid, request)
waitTillProvisioned(resp.Headers.Get("Location"))
2016-06-28 22:35:41 -04:00
return resp.Id
}
2016-08-01 07:15:21 -04:00
func waitTillProvisioned(path string) {
waitCount := 120
2016-08-01 07:15:21 -04:00
for i := 0; i < waitCount; i++ {
request := GetRequestStatus(path)
if request.Metadata.Status == "DONE" {
break
}
time.Sleep(1 * time.Second)
2016-08-01 07:15:21 -04:00
i++
}
}
func getImageId(location string, imageName string, imageType string) string {
if imageName == "" {
return ""
}
SetAuth(os.Getenv("PROFITBRICKS_USERNAME"), os.Getenv("PROFITBRICKS_PASSWORD"))
images := ListImages()
if images.StatusCode > 299 {
fmt.Printf("Error while fetching the list of images %s", images.Response)
}
if len(images.Items) > 0 {
for _, i := range images.Items {
imgName := ""
if i.Properties.Name != "" {
imgName = i.Properties.Name
}
if imageType == "SSD" {
imageType = "HDD"
}
if imgName != "" && strings.Contains(strings.ToLower(imgName), strings.ToLower(imageName)) && i.Properties.ImageType == imageType && i.Properties.Location == location && i.Properties.Public == true {
return i.Id
}
}
}
return ""
}