VPCUUID and ConnectWithPrivateIP have been added to builder/digitalocean

This commit is contained in:
Ufuk 2020-10-13 18:26:00 +03:00
parent 01ed398756
commit 49cd4baa0c
5 changed files with 38 additions and 2 deletions

View File

@ -85,6 +85,14 @@ type Config struct {
UserDataFile string `mapstructure:"user_data_file" required:"false"`
// Tags to apply to the droplet when it is created
Tags []string `mapstructure:"tags" required:"false"`
// UUID of the VPC which the droplet will be created in. Before using this,
// private_networking should be enabled.
VPCUUID string `mapstructure:"vpc_uuid" required:"false"`
// Wheter the communicators should use private IP or not (public IP in that case).
// If the droplet is or going to be accessible only from the local network because
// it is at behind a firewall, then communicators should use the private IP
// instead of the public IP. Before using this, private_networking should be enabled.
ConnectWithPrivateIP bool `mapstructure:"connect_with_private_ip" required:"false"`
ctx interpolate.Context
}
@ -187,6 +195,20 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
}
}
// Check if the PrivateNetworking is enabled by user before use VPC UUID
if c.VPCUUID != "" {
if c.PrivateNetworking != true {
errs = packer.MultiErrorAppend(errs, errors.New("private networking should be enabled to use vpc_uuid"))
}
}
// Check if the PrivateNetworking is enabled by user before use ConnectWithPrivateIP
if c.ConnectWithPrivateIP == true {
if c.PrivateNetworking != true {
errs = packer.MultiErrorAppend(errs, errors.New("private networking should be enabled to use connect_with_private_ip"))
}
}
if errs != nil && len(errs.Errors) > 0 {
return nil, errs
}

View File

@ -79,6 +79,8 @@ type FlatConfig struct {
UserData *string `mapstructure:"user_data" required:"false" cty:"user_data" hcl:"user_data"`
UserDataFile *string `mapstructure:"user_data_file" required:"false" cty:"user_data_file" hcl:"user_data_file"`
Tags []string `mapstructure:"tags" required:"false" cty:"tags" hcl:"tags"`
VPCUUID *string `mapstructure:"vpc_uuid" required:"false" cty:"vpc_uuid" hcl:"vpc_uuid"`
ConnectWithPrivateIP *bool `mapstructure:"connect_with_private_ip" required:"false" cty:"connect_with_private_ip" hcl:"connect_with_private_ip"`
}
// FlatMapstructure returns a new FlatConfig.
@ -163,6 +165,8 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"user_data": &hcldec.AttrSpec{Name: "user_data", Type: cty.String, Required: false},
"user_data_file": &hcldec.AttrSpec{Name: "user_data_file", Type: cty.String, Required: false},
"tags": &hcldec.AttrSpec{Name: "tags", Type: cty.List(cty.String), Required: false},
"vpc_uuid": &hcldec.AttrSpec{Name: "vpc_uuid", Type: cty.String, Required: false},
"connect_with_private_ip": &hcldec.AttrSpec{Name: "connect_with_private_ip", Type: cty.Bool, Required: false},
}
return s
}

View File

@ -52,6 +52,7 @@ func (s *stepCreateDroplet) Run(ctx context.Context, state multistep.StateBag) m
IPv6: c.IPv6,
UserData: userData,
Tags: c.Tags,
VPCUUID: c.VPCUUID,
}
log.Printf("[DEBUG] Droplet create paramaters: %s", godo.Stringify(dropletCreateReq))

View File

@ -46,10 +46,11 @@ func (s *stepDropletInfo) Run(ctx context.Context, state multistep.StateBag) mul
return multistep.ActionHalt
}
// Find a public IPv4 network
// Find the ip address which will be used by communicator
foundNetwork := false
for _, network := range droplet.Networks.V4 {
if network.Type == "public" {
if (c.ConnectWithPrivateIP && network.Type == "private") ||
(!(c.ConnectWithPrivateIP) && network.Type == "public") {
state.Put("droplet_ip", network.IPAddress)
foundNetwork = true
break

View File

@ -43,3 +43,11 @@
data when launching the Droplet.
- `tags` ([]string) - Tags to apply to the droplet when it is created
- `vpc_uuid` (string) - UUID of the VPC which the droplet will be created in. Before using this,
private_networking should be enabled.
- `connect_with_private_ip` (bool) - Wheter the communicators should use private IP or not (public IP in that case).
If the droplet is or going to be accessible only from the local network because
it is at behind a firewall, then communicators should use the private IP
instead of the public IP. Before using this, private_networking should be enabled.