Merge pull request #8622 from datalot/pre-validate-with-filters

Allow aws builder pre validation to pass when subnet filters are present
This commit is contained in:
Wilken Rivera 2020-01-22 15:00:44 -05:00 committed by GitHub
commit 111bab86d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 2 deletions

View File

@ -23,6 +23,7 @@ type StepPreValidate struct {
AMISkipBuildRegion bool AMISkipBuildRegion bool
VpcId string VpcId string
SubnetId string SubnetId string
HasSubnetFilter bool
} }
func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
@ -121,10 +122,11 @@ func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul
} }
func (s *StepPreValidate) checkVpc(conn ec2iface.EC2API) error { func (s *StepPreValidate) checkVpc(conn ec2iface.EC2API) error {
if s.VpcId == "" || (s.VpcId != "" && s.SubnetId != "") { if s.VpcId == "" || (s.VpcId != "" && (s.SubnetId != "" || s.HasSubnetFilter)) {
// Skip validation if: // Skip validation if:
// * The user has not provided a VpcId. // * The user has not provided a VpcId.
// * Both VpcId and SubnetId are provided; AWS API will error if something is wrong. // * Both VpcId and SubnetId are provided; AWS API will error if something is wrong.
// * Both VpcId and SubnetFilter are provided
return nil return nil
} }
@ -135,7 +137,7 @@ func (s *StepPreValidate) checkVpc(conn ec2iface.EC2API) error {
if res != nil && len(res.Vpcs) == 1 && res.Vpcs[0] != nil { if res != nil && len(res.Vpcs) == 1 && res.Vpcs[0] != nil {
if isDefault := aws.BoolValue(res.Vpcs[0].IsDefault); !isDefault { if isDefault := aws.BoolValue(res.Vpcs[0].IsDefault); !isDefault {
return fmt.Errorf("Error: subnet_id must be provided for non-default VPCs (%s)", s.VpcId) return fmt.Errorf("Error: subnet_id or subnet_filter must be provided for non-default VPCs (%s)", s.VpcId)
} }
} }
return nil return nil

View File

@ -45,6 +45,7 @@ func TestStepPreValidate_checkVpc(t *testing.T) {
{"NonDefaultVpcWithSubnet", StepPreValidate{VpcId: "vpc-1234567890", SubnetId: "subnet-1234567890"}, false}, {"NonDefaultVpcWithSubnet", StepPreValidate{VpcId: "vpc-1234567890", SubnetId: "subnet-1234567890"}, false},
{"SubnetWithNoVpc", StepPreValidate{SubnetId: "subnet-1234567890"}, false}, {"SubnetWithNoVpc", StepPreValidate{SubnetId: "subnet-1234567890"}, false},
{"NoVpcInformation", StepPreValidate{}, false}, {"NoVpcInformation", StepPreValidate{}, false},
{"NonDefaultVpcWithSubnetFilter", StepPreValidate{VpcId: "vpc-1234567890", HasSubnetFilter: true}, false},
} }
mockConn, err := getMockConn(nil, "") mockConn, err := getMockConn(nil, "")

View File

@ -210,6 +210,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
AMISkipBuildRegion: b.config.AMISkipBuildRegion, AMISkipBuildRegion: b.config.AMISkipBuildRegion,
VpcId: b.config.VpcId, VpcId: b.config.VpcId,
SubnetId: b.config.SubnetId, SubnetId: b.config.SubnetId,
HasSubnetFilter: len(b.config.SubnetFilter.Filters) > 0,
}, },
&awscommon.StepSourceAMIInfo{ &awscommon.StepSourceAMIInfo{
SourceAmi: b.config.SourceAmi, SourceAmi: b.config.SourceAmi,

View File

@ -233,6 +233,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
ForceDeregister: b.config.AMIForceDeregister, ForceDeregister: b.config.AMIForceDeregister,
VpcId: b.config.VpcId, VpcId: b.config.VpcId,
SubnetId: b.config.SubnetId, SubnetId: b.config.SubnetId,
HasSubnetFilter: len(b.config.SubnetFilter.Filters) > 0,
}, },
&awscommon.StepSourceAMIInfo{ &awscommon.StepSourceAMIInfo{
SourceAmi: b.config.SourceAmi, SourceAmi: b.config.SourceAmi,

View File

@ -296,6 +296,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
ForceDeregister: b.config.AMIForceDeregister, ForceDeregister: b.config.AMIForceDeregister,
VpcId: b.config.VpcId, VpcId: b.config.VpcId,
SubnetId: b.config.SubnetId, SubnetId: b.config.SubnetId,
HasSubnetFilter: len(b.config.SubnetFilter.Filters) > 0,
}, },
&awscommon.StepSourceAMIInfo{ &awscommon.StepSourceAMIInfo{
SourceAmi: b.config.SourceAmi, SourceAmi: b.config.SourceAmi,