Add support for custom API url of compatible service

Closes #4
This commit is contained in:
Miłosz Smółka 2019-02-02 14:03:44 +01:00
parent 6921cc93a9
commit 5e8eade205
5 changed files with 50 additions and 20 deletions

View File

@ -34,6 +34,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
cfg.AddDefaultHeader("x-project", b.config.Project)
}
if b.config.APIURL != "" {
cfg.BasePath = b.config.APIURL
}
prefer := fmt.Sprintf("respond-async,wait=%d", int(b.config.StateTimeout.Seconds()))
cfg.AddDefaultHeader("Prefer", prefer)

View File

@ -33,6 +33,7 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
APIURL string `mapstructure:"api_url"`
Token string `mapstructure:"token"`
Project string `mapstructure:"project"`
TokenLogin string `mapstructure:"token_login"`
@ -53,9 +54,10 @@ type Config struct {
DiskType string `mapstructure:"disk_type"`
DiskSize float32 `mapstructure:"disk_size"`
Network string `mapstructure:"network"`
PrivateIP string `mapstructure:"private_ip"`
PublicIP string `mapstructure:"public_ip"`
Network string `mapstructure:"network"`
PrivateIP string `mapstructure:"private_ip"`
PublicIP string `mapstructure:"public_ip"`
PublicNetAdpService string `mapstructure:"public_netadp_service"`
ChrootDisk bool `mapstructure:"chroot_disk"`
ChrootDiskSize float32 `mapstructure:"chroot_disk_size"`
@ -112,6 +114,10 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
c.Comm.SSHTimeout = 10 * time.Minute
}
if c.APIURL == "" {
c.APIURL = os.Getenv("HYPERONE_API_URL")
}
if c.Token == "" {
c.Token = os.Getenv(tokenEnv)
@ -119,7 +125,8 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
c.Token = cliConfig.Profile.APIKey
}
if c.TokenLogin != "" {
// Fetching token by SSH is available only for the default API endpoint
if c.TokenLogin != "" && c.APIURL == "" {
c.Token, err = fetchTokenBySSH(c.TokenLogin)
if err != nil {
return nil, nil, err
@ -155,6 +162,10 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
c.DiskType = defaultDiskType
}
if c.PublicNetAdpService == "" {
c.PublicNetAdpService = "public"
}
if c.ChrootCommandWrapper == "" {
c.ChrootCommandWrapper = "{{.Command}}"
}

View File

@ -118,7 +118,7 @@ func pickNetAdapter(config *Config) openapi.VmCreateNetadp {
if config.Network == "" {
if config.PublicIP != "" {
return openapi.VmCreateNetadp{
Service: "public",
Service: config.PublicNetAdpService,
Ip: []string{config.PublicIP},
}
}
@ -139,7 +139,7 @@ func pickNetAdapter(config *Config) openapi.VmCreateNetadp {
}
return openapi.VmCreateNetadp{
Service: "public",
Service: config.PublicNetAdpService,
}
}

View File

@ -14,8 +14,10 @@ func TestPickNetAdapter(t *testing.T) {
Expected openapi.VmCreateNetadp
}{
{
Name: "no_network",
Config: Config{},
Name: "no_network",
Config: Config{
PublicNetAdpService: "public",
},
Expected: openapi.VmCreateNetadp{
Service: "public",
},
@ -23,7 +25,8 @@ func TestPickNetAdapter(t *testing.T) {
{
Name: "no_network_public_ip",
Config: Config{
PublicIP: "some-public-ip",
PublicIP: "some-public-ip",
PublicNetAdpService: "public",
},
Expected: openapi.VmCreateNetadp{
Service: "public",
@ -33,7 +36,8 @@ func TestPickNetAdapter(t *testing.T) {
{
Name: "no_network_private_ip",
Config: Config{
PrivateIP: "some-private-ip",
PrivateIP: "some-private-ip",
PublicNetAdpService: "public",
},
Expected: openapi.VmCreateNetadp{
Service: "public",
@ -42,8 +46,9 @@ func TestPickNetAdapter(t *testing.T) {
{
Name: "no_network_both_ip",
Config: Config{
PublicIP: "some-public-ip",
PrivateIP: "some-private-ip",
PublicIP: "some-public-ip",
PrivateIP: "some-private-ip",
PublicNetAdpService: "public",
},
Expected: openapi.VmCreateNetadp{
Service: "public",
@ -53,7 +58,8 @@ func TestPickNetAdapter(t *testing.T) {
{
Name: "network_no_ip",
Config: Config{
Network: "some-network",
Network: "some-network",
PublicNetAdpService: "public",
},
Expected: openapi.VmCreateNetadp{
Service: "private",
@ -63,8 +69,9 @@ func TestPickNetAdapter(t *testing.T) {
{
Name: "network_public_ip",
Config: Config{
Network: "some-network",
PublicIP: "some-public-ip",
Network: "some-network",
PublicIP: "some-public-ip",
PublicNetAdpService: "public",
},
Expected: openapi.VmCreateNetadp{
Service: "private",
@ -74,8 +81,9 @@ func TestPickNetAdapter(t *testing.T) {
{
Name: "network_private_ip",
Config: Config{
Network: "some-network",
PrivateIP: "some-private-ip",
Network: "some-network",
PrivateIP: "some-private-ip",
PublicNetAdpService: "public",
},
Expected: openapi.VmCreateNetadp{
Service: "private",
@ -86,9 +94,10 @@ func TestPickNetAdapter(t *testing.T) {
{
Name: "network_both_ip",
Config: Config{
Network: "some-network",
PublicIP: "some-public-ip",
PrivateIP: "some-private-ip",
Network: "some-network",
PublicIP: "some-public-ip",
PrivateIP: "some-private-ip",
PublicNetAdpService: "public",
},
Expected: openapi.VmCreateNetadp{
Service: "private",

View File

@ -112,6 +112,9 @@ builder.
### Optional:
- `api_url` (string) - Custom API endpoint URL, compatible with HyperOne.
It can also be specified via environment variable `HYPERONE_API_URL`.
- `disk_name` (string) - The name of the created disk.
- `disk_type` (string) - The type of the created disk. Defaults to `ssd`.
@ -136,6 +139,9 @@ builder.
the created server. If `network` is chosen, the public IP will be associated
with server's private IP.
- `public_netadp_service` (string) - Custom service of public network adapter.
Can be useful when using custom `api_url`. Defaults to `public`.
- `ssh_keys` (array of strings) - List of SSH keys by name or id to be added
to the server on launch.