Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4603 from xinau/openstack-metadata
Browse files Browse the repository at this point in the history
drivers/openstack: added flag openstack-metadata
  • Loading branch information
dgageot authored Jan 13, 2019
2 parents d3f89ca + 1e3fa2d commit 621d44c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (c *GenericClient) CreateInstance(d *Driver) (string, error) {
SecurityGroups: d.SecurityGroups,
AvailabilityZone: d.AvailabilityZone,
ConfigDrive: d.ConfigDrive,
Metadata: d.GetMetadata(),
}
if d.NetworkId != "" {
serverOpts.Networks = []servers.Network{
Expand Down
24 changes: 24 additions & 0 deletions drivers/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Driver struct {
FloatingIpPoolId string
IpVersion int
ConfigDrive bool
metadata string
client Client
// ExistingKey keeps track of whether the key was created by us or we used an existing one. If an existing one was used, we shouldn't delete it when the machine is deleted.
ExistingKey bool
Expand Down Expand Up @@ -233,6 +234,12 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Name: "openstack-config-drive",
Usage: "Enables the OpenStack config drive for the instance",
},
mcnflag.StringFlag{
EnvVar: "OS_METADATA",
Name: "openstack-metadata",
Usage: "OpenStack Instance Metadata (e.g. key1,value1,key2,value2)",
Value: "",
},
}
}

Expand Down Expand Up @@ -286,6 +293,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.ImageName = flags.String("openstack-image-name")
d.NetworkId = flags.String("openstack-net-id")
d.NetworkName = flags.String("openstack-net-name")
d.metadata = flags.String("openstack-metadata")
if flags.String("openstack-sec-groups") != "" {
d.SecurityGroups = strings.Split(flags.String("openstack-sec-groups"), ",")
}
Expand Down Expand Up @@ -487,6 +495,22 @@ func (d *Driver) Remove() error {
return nil
}

func (d *Driver) GetMetadata() map[string]string {
metadata := make(map[string]string)

if d.metadata != "" {
items := strings.Split(d.metadata, ",")
if len(items) > 0 && len(items)%2 != 0 {
log.Warnf("Metadata are not key value in pairs. %d elements found", len(items))
}
for i := 0; i < len(items)-1; i += 2 {
metadata[items[i]] = items[i+1]
}
}

return metadata
}

const (
errorMandatoryEnvOrOption string = "%s must be specified either using the environment variable %s or the CLI option %s"
errorMandatoryOption string = "%s must be specified using the CLI option %s"
Expand Down

0 comments on commit 621d44c

Please sign in to comment.