From f48fc1e2cd41878f4eb9a772f25c7ce691c55f61 Mon Sep 17 00:00:00 2001 From: Kgespada Date: Fri, 17 Jan 2014 15:46:35 -0800 Subject: [PATCH] Adds security group support Allows security groups to be specified in the template. --- builder/openstack/builder.go | 7 ++++--- builder/openstack/run_config.go | 22 ++++++++++----------- builder/openstack/step_run_source_server.go | 22 ++++++++++++++------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go index e745b8db0..93dd18520 100644 --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -88,9 +88,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe DebugKeyPath: fmt.Sprintf("os_%s.pem", b.config.PackerBuildName), }, &StepRunSourceServer{ - Name: b.config.ImageName, - Flavor: b.config.Flavor, - SourceImage: b.config.SourceImage, + Name: b.config.ImageName, + Flavor: b.config.Flavor, + SourceImage: b.config.SourceImage, + SecurityGroups: b.config.SecurityGroups, }, &StepAllocateIp{ FloatingIpPool: b.config.FloatingIpPool, diff --git a/builder/openstack/run_config.go b/builder/openstack/run_config.go index 96388e254..40c75e452 100644 --- a/builder/openstack/run_config.go +++ b/builder/openstack/run_config.go @@ -10,15 +10,16 @@ import ( // RunConfig contains configuration for running an instance from a source // image and details on how to access that launched image. type RunConfig struct { - SourceImage string `mapstructure:"source_image"` - Flavor string `mapstructure:"flavor"` - RawSSHTimeout string `mapstructure:"ssh_timeout"` - SSHUsername string `mapstructure:"ssh_username"` - SSHPort int `mapstructure:"ssh_port"` - OpenstackProvider string `mapstructure:"openstack_provider"` - UseFloatingIp bool `mapstructure:"use_floating_ip"` - FloatingIpPool string `mapstructure:"floating_ip_pool"` - FloatingIp string `mapstructure:"floating_ip"` + SourceImage string `mapstructure:"source_image"` + Flavor string `mapstructure:"flavor"` + RawSSHTimeout string `mapstructure:"ssh_timeout"` + SSHUsername string `mapstructure:"ssh_username"` + SSHPort int `mapstructure:"ssh_port"` + OpenstackProvider string `mapstructure:"openstack_provider"` + UseFloatingIp bool `mapstructure:"use_floating_ip"` + FloatingIpPool string `mapstructure:"floating_ip_pool"` + FloatingIp string `mapstructure:"floating_ip"` + SecurityGroups []string `mapstructure:"security_groups"` // Unexported fields that are calculated from others sshTimeout time.Duration @@ -76,8 +77,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { var err error *ptr, err = t.Process(*ptr, nil) if err != nil { - errs = append( - errs, fmt.Errorf("Error processing %s: %s", n, err)) + errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err)) } } diff --git a/builder/openstack/step_run_source_server.go b/builder/openstack/step_run_source_server.go index c8e121e09..09062b4e6 100644 --- a/builder/openstack/step_run_source_server.go +++ b/builder/openstack/step_run_source_server.go @@ -9,9 +9,10 @@ import ( ) type StepRunSourceServer struct { - Flavor string - Name string - SourceImage string + Flavor string + Name string + SourceImage string + SecurityGroups []string server *gophercloud.Server } @@ -23,11 +24,18 @@ func (s *StepRunSourceServer) Run(state multistep.StateBag) multistep.StepAction // XXX - validate image and flavor is available + securityGroups := make([]map[string]interface{}, len(s.SecurityGroups)) + for i, groupName := range s.SecurityGroups { + securityGroups[i] = make(map[string]interface{}) + securityGroups[i]["name"] = groupName + } + server := gophercloud.NewServer{ - Name: s.Name, - ImageRef: s.SourceImage, - FlavorRef: s.Flavor, - KeyPairName: keyName, + Name: s.Name, + ImageRef: s.SourceImage, + FlavorRef: s.Flavor, + KeyPairName: keyName, + SecurityGroup: securityGroups, } serverResp, err := csp.CreateServer(server)