diff --git a/builder/digitalocean/step_create_droplet.go b/builder/digitalocean/step_create_droplet.go index 5f360bb..c98eaa6 100644 --- a/builder/digitalocean/step_create_droplet.go +++ b/builder/digitalocean/step_create_droplet.go @@ -21,7 +21,15 @@ func (s *stepCreateDroplet) Run(ctx context.Context, state multistep.StateBag) m client := state.Get("client").(*godo.Client) ui := state.Get("ui").(packersdk.Ui) c := state.Get("config").(*Config) - sshKeyId := state.Get("ssh_key_id").(int) + sshKeyId, hasSshKey := state.GetOk("ssh_key_id") + + sshKeys := []godo.DropletCreateSSHKey{} + + if hasSshKey { + sshKeys = append(sshKeys, godo.DropletCreateSSHKey{ + ID: sshKeyId.(int), + }) + } // Create the droplet based on configuration ui.Say("Creating droplet...") @@ -40,13 +48,11 @@ func (s *stepCreateDroplet) Run(ctx context.Context, state multistep.StateBag) m createImage := getImageType(c.Image) dropletCreateReq := &godo.DropletCreateRequest{ - Name: c.DropletName, - Region: c.Region, - Size: c.Size, - Image: createImage, - SSHKeys: []godo.DropletCreateSSHKey{ - {ID: sshKeyId}, - }, + Name: c.DropletName, + Region: c.Region, + Size: c.Size, + Image: createImage, + SSHKeys: sshKeys, PrivateNetworking: c.PrivateNetworking, Monitoring: c.Monitoring, IPv6: c.IPv6,