Merge pull request #9212 from andrewsomething/asb/do-image-id
builder/digitalocean: Use correct image type for Droplet creates.
This commit is contained in:
commit
a17ef01ea9
|
@ -1,17 +1,29 @@
|
|||
package digitalocean
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/digitalocean/godo"
|
||||
builderT "github.com/hashicorp/packer/helper/builder/testing"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func TestBuilderAcc_basic(t *testing.T) {
|
||||
builderT.Test(t, builderT.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Builder: &Builder{},
|
||||
Template: testBuilderAccBasic,
|
||||
Template: fmt.Sprintf(testBuilderAccBasic, "ubuntu-20-04-x64"),
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuilderAcc_imageId(t *testing.T) {
|
||||
builderT.Test(t, builderT.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Builder: &Builder{},
|
||||
Template: makeTemplateWithImageId(t),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -21,15 +33,33 @@ func testAccPreCheck(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func makeTemplateWithImageId(t *testing.T) string {
|
||||
if os.Getenv(builderT.TestEnvVar) != "" {
|
||||
token := os.Getenv("DIGITALOCEAN_API_TOKEN")
|
||||
client := godo.NewClient(oauth2.NewClient(context.TODO(), &apiTokenSource{
|
||||
AccessToken: token,
|
||||
}))
|
||||
image, _, err := client.Images.GetBySlug(context.TODO(), "ubuntu-20-04-x64")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to retrieve image ID: %s", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(testBuilderAccBasic, image.ID)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
const testBuilderAccBasic = `
|
||||
{
|
||||
"builders": [{
|
||||
"type": "test",
|
||||
"region": "nyc2",
|
||||
"size": "512mb",
|
||||
"image": "ubuntu-12-04-x64",
|
||||
"user_date": "",
|
||||
"user_date_file": ""
|
||||
"size": "s-1vcpu-1gb",
|
||||
"image": "%v",
|
||||
"ssh_username": "root",
|
||||
"user_data": "",
|
||||
"user_data_file": ""
|
||||
}]
|
||||
}
|
||||
`
|
||||
|
|
|
@ -3,6 +3,8 @@ package digitalocean
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"io/ioutil"
|
||||
|
||||
|
@ -35,13 +37,13 @@ func (s *stepCreateDroplet) Run(ctx context.Context, state multistep.StateBag) m
|
|||
userData = string(contents)
|
||||
}
|
||||
|
||||
droplet, _, err := client.Droplets.Create(context.TODO(), &godo.DropletCreateRequest{
|
||||
createImage := getImageType(c.Image)
|
||||
|
||||
dropletCreateReq := &godo.DropletCreateRequest{
|
||||
Name: c.DropletName,
|
||||
Region: c.Region,
|
||||
Size: c.Size,
|
||||
Image: godo.DropletCreateImage{
|
||||
Slug: c.Image,
|
||||
},
|
||||
Image: createImage,
|
||||
SSHKeys: []godo.DropletCreateSSHKey{
|
||||
{ID: sshKeyId},
|
||||
},
|
||||
|
@ -50,7 +52,11 @@ func (s *stepCreateDroplet) Run(ctx context.Context, state multistep.StateBag) m
|
|||
IPv6: c.IPv6,
|
||||
UserData: userData,
|
||||
Tags: c.Tags,
|
||||
})
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Droplet create paramaters: %s", godo.Stringify(dropletCreateReq))
|
||||
|
||||
droplet, _, err := client.Droplets.Create(context.TODO(), dropletCreateReq)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error creating droplet: %s", err)
|
||||
state.Put("error", err)
|
||||
|
@ -87,3 +93,14 @@ func (s *stepCreateDroplet) Cleanup(state multistep.StateBag) {
|
|||
"Error destroying droplet. Please destroy it manually: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
func getImageType(image string) godo.DropletCreateImage {
|
||||
createImage := godo.DropletCreateImage{Slug: image}
|
||||
|
||||
imageId, err := strconv.Atoi(image)
|
||||
if err == nil {
|
||||
createImage = godo.DropletCreateImage{ID: imageId}
|
||||
}
|
||||
|
||||
return createImage
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package digitalocean
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/digitalocean/godo"
|
||||
)
|
||||
|
||||
func TestBuilder_GetImageType(t *testing.T) {
|
||||
imageTypeTests := []struct {
|
||||
in string
|
||||
out godo.DropletCreateImage
|
||||
}{
|
||||
{"ubuntu-20-04-x64", godo.DropletCreateImage{Slug: "ubuntu-20-04-x64"}},
|
||||
{"123456", godo.DropletCreateImage{ID: 123456}},
|
||||
}
|
||||
|
||||
for _, tt := range imageTypeTests {
|
||||
t.Run(tt.in, func(t *testing.T) {
|
||||
i := getImageType(tt.in)
|
||||
if i != tt.out {
|
||||
t.Errorf("got %q, want %q", godo.Stringify(i), godo.Stringify(tt.out))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue