Browse Source

Rackspace/Openstack - Add/enhance --*-net-* create switches.

Signed-off-by: Robert Jones <robert@justjones.org>
Robert Jones 3 years ago
parent
commit
c29d944831
2 changed files with 65 additions and 52 deletions
  1. 25
    16
      drivers/openstack/client.go
  2. 40
    36
      drivers/openstack/openstack.go

+ 25
- 16
drivers/openstack/client.go View File

@@ -41,7 +41,8 @@ type Client interface {
41 41
 	GetPublicKey(keyPairName string) ([]byte, error)
42 42
 	CreateKeyPair(d *Driver, name string, publicKey string) error
43 43
 	DeleteKeyPair(d *Driver, name string) error
44
-	GetNetworkID(d *Driver) (string, error)
44
+	GetNetworkIDs(d *Driver) ([]string, error)
45
+	GetNetworkID(d *Driver, networkName string) (string, error)
45 46
 	GetFlavorID(d *Driver) (string, error)
46 47
 	GetImageID(d *Driver) (string, error)
47 48
 	AssignFloatingIP(d *Driver, floatingIP *FloatingIP) error
@@ -66,12 +67,12 @@ func (c *GenericClient) CreateInstance(d *Driver) (string, error) {
66 67
 		SecurityGroups:   d.SecurityGroups,
67 68
 		AvailabilityZone: d.AvailabilityZone,
68 69
 	}
69
-	if d.NetworkId != "" {
70
-		serverOpts.Networks = []servers.Network{
71
-			{
72
-				UUID: d.NetworkId,
73
-			},
70
+	if len(d.NetworkIds) > 0 {
71
+		networks := make([]servers.Network, len(d.NetworkIds))
72
+		for i, networkId := range d.NetworkIds {
73
+			networks[i] = servers.Network{UUID: networkId}
74 74
 		}
75
+		serverOpts.Networks = networks
75 76
 	}
76 77
 
77 78
 	log.Info("Creating machine...")
@@ -199,15 +200,23 @@ func (c *GenericClient) GetInstanceIPAddresses(d *Driver) ([]IPAddress, error) {
199 200
 	return addresses, nil
200 201
 }
201 202
 
202
-func (c *GenericClient) GetNetworkID(d *Driver) (string, error) {
203
-	return c.getNetworkID(d, d.NetworkName)
203
+func (c *GenericClient) GetNetworkIDs(d *Driver) ([]string, error) {
204
+	networkIDs := make([]string, len(d.NetworkNames))
205
+	for i, networkName := range d.NetworkNames {
206
+		id, err := c.GetNetworkID(d, networkName)
207
+		if err != nil {
208
+			return nil, err
209
+		}
210
+		networkIDs[i] = id
211
+	}
212
+	return networkIDs, nil
204 213
 }
205 214
 
206 215
 func (c *GenericClient) GetFloatingIPPoolID(d *Driver) (string, error) {
207
-	return c.getNetworkID(d, d.FloatingIpPool)
216
+	return c.GetNetworkID(d, d.FloatingIPPool)
208 217
 }
209 218
 
210
-func (c *GenericClient) getNetworkID(d *Driver, networkName string) (string, error) {
219
+func (c *GenericClient) GetNetworkID(d *Driver, networkName string) (string, error) {
211 220
 	opts := networks.ListOpts{Name: networkName}
212 221
 	pager := networks.List(c.Network, opts)
213 222
 	networkID := ""
@@ -345,7 +354,7 @@ func (c *GenericClient) AssignFloatingIP(d *Driver, floatingIP *FloatingIP) erro
345 354
 func (c *GenericClient) assignNovaFloatingIP(d *Driver, floatingIP *FloatingIP) error {
346 355
 	if floatingIP.Ip == "" {
347 356
 		f, err := compute_ips.Create(c.Compute, compute_ips.CreateOpts{
348
-			Pool: d.FloatingIpPool,
357
+			Pool: d.FloatingIPPool,
349 358
 		}).Extract()
350 359
 		if err != nil {
351 360
 			return err
@@ -363,7 +372,7 @@ func (c *GenericClient) assignNeutronFloatingIP(d *Driver, floatingIP *FloatingI
363 372
 	}
364 373
 	if floatingIP.Id == "" {
365 374
 		f, err := floatingips.Create(c.Network, floatingips.CreateOpts{
366
-			FloatingNetworkID: d.FloatingIpPoolId,
375
+			FloatingNetworkID: d.FloatingIPPoolId,
367 376
 			PortID:            portID,
368 377
 		}).Extract()
369 378
 		if err != nil {
@@ -400,7 +409,7 @@ func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIP, erro
400 409
 		ipListing, err := compute_ips.ExtractFloatingIPs(page)
401 410
 
402 411
 		for _, ip := range ipListing {
403
-			if ip.InstanceID == "" && ip.Pool == d.FloatingIpPool {
412
+			if ip.InstanceID == "" && ip.Pool == d.FloatingIPPool {
404 413
 				ips = append(ips, FloatingIP{
405 414
 					Id:   ip.ID,
406 415
 					Ip:   ip.IP,
@@ -415,11 +424,11 @@ func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIP, erro
415 424
 
416 425
 func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIP, error) {
417 426
 	log.Debug("Listing floating IPs", map[string]string{
418
-		"FloatingNetworkId": d.FloatingIpPoolId,
427
+		"FloatingNetworkId": d.FloatingIPPoolId,
419 428
 		"TenantID":          d.TenantId,
420 429
 	})
421 430
 	pager := floatingips.List(c.Network, floatingips.ListOpts{
422
-		FloatingNetworkID: d.FloatingIpPoolId,
431
+		FloatingNetworkID: d.FloatingIPPoolId,
423 432
 		TenantID:          d.TenantId,
424 433
 	})
425 434
 
@@ -449,7 +458,7 @@ func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIP, e
449 458
 func (c *GenericClient) GetInstancePortID(d *Driver) (string, error) {
450 459
 	pager := ports.List(c.Network, ports.ListOpts{
451 460
 		DeviceID:  d.MachineId,
452
-		NetworkID: d.NetworkId,
461
+		NetworkID: d.NetworkIds[0],
453 462
 	})
454 463
 
455 464
 	var portID string

+ 40
- 36
drivers/openstack/openstack.go View File

@@ -35,13 +35,13 @@ type Driver struct {
35 35
 	ImageName        string
36 36
 	ImageId          string
37 37
 	KeyPairName      string
38
-	NetworkName      string
39
-	NetworkId        string
38
+	NetworkNames     []string
39
+	NetworkIds       []string
40 40
 	PrivateKeyFile   string
41 41
 	SecurityGroups   []string
42
-	FloatingIpPool   string
42
+	FloatingIPPool   string
43 43
 	ComputeNetwork   bool
44
-	FloatingIpPoolId string
44
+	FloatingIPPoolId string
45 45
 	IpVersion        int
46 46
 	client           Client
47 47
 }
@@ -149,22 +149,22 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
149 149
 			Usage:  "OpenStack keypair to use to SSH to the instance",
150 150
 			Value:  "",
151 151
 		},
152
-		mcnflag.StringFlag{
153
-			EnvVar: "OS_NETWORK_ID",
154
-			Name:   "openstack-net-id",
155
-			Usage:  "OpenStack network id the machine will be connected on",
156
-			Value:  "",
157
-		},
158 152
 		mcnflag.StringFlag{
159 153
 			EnvVar: "OS_PRIVATE_KEY_FILE",
160 154
 			Name:   "openstack-private-key-file",
161 155
 			Usage:  "Private keyfile to use for SSH (absolute path)",
162 156
 			Value:  "",
163 157
 		},
158
+		mcnflag.StringFlag{
159
+			EnvVar: "OS_NETWORK_ID",
160
+			Name:   "openstack-net-id",
161
+			Usage:  "OpenStack comma separated networks id the machine will be connected on",
162
+			Value:  "",
163
+		},
164 164
 		mcnflag.StringFlag{
165 165
 			EnvVar: "OS_NETWORK_NAME",
166 166
 			Name:   "openstack-net-name",
167
-			Usage:  "OpenStack network name the machine will be connected on",
167
+			Usage:  "OpenStack comma separated network names the machine will be connected on",
168 168
 			Value:  "",
169 169
 		},
170 170
 		mcnflag.StringFlag{
@@ -181,7 +181,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
181 181
 		mcnflag.StringFlag{
182 182
 			EnvVar: "OS_FLOATINGIP_POOL",
183 183
 			Name:   "openstack-floatingip-pool",
184
-			Usage:  "OpenStack floating IP pool to get an IP from to assign to the instance",
184
+			Usage:  "OpenStack floating IP pool to get an IP from to assign to the instance (first network only)",
185 185
 			Value:  "",
186 186
 		},
187 187
 		mcnflag.IntFlag{
@@ -258,12 +258,16 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
258 258
 	d.FlavorName = flags.String("openstack-flavor-name")
259 259
 	d.ImageId = flags.String("openstack-image-id")
260 260
 	d.ImageName = flags.String("openstack-image-name")
261
-	d.NetworkId = flags.String("openstack-net-id")
262
-	d.NetworkName = flags.String("openstack-net-name")
261
+	if flags.String("openstack-net-id") != "" {
262
+		d.NetworkIds = strings.Split(flags.String("openstack-net-id"), ",")
263
+	}
264
+	if flags.String("openstack-net-name") != "" {
265
+		d.NetworkNames = strings.Split(flags.String("openstack-net-name"), ",")
266
+	}
263 267
 	if flags.String("openstack-sec-groups") != "" {
264 268
 		d.SecurityGroups = strings.Split(flags.String("openstack-sec-groups"), ",")
265 269
 	}
266
-	d.FloatingIpPool = flags.String("openstack-floatingip-pool")
270
+	d.FloatingIPPool = flags.String("openstack-floatingip-pool")
267 271
 	d.IpVersion = flags.Int("openstack-ip-version")
268 272
 	d.ComputeNetwork = flags.Bool("openstack-nova-network")
269 273
 	d.SSHUser = flags.String("openstack-ssh-user")
@@ -298,7 +302,7 @@ func (d *Driver) GetIP() (string, error) {
298 302
 	}
299 303
 
300 304
 	addressType := Fixed
301
-	if d.FloatingIpPool != "" {
305
+	if d.FloatingIPPool != "" {
302 306
 		addressType = Floating
303 307
 	}
304 308
 
@@ -371,7 +375,7 @@ func (d *Driver) Create() error {
371 375
 	if err := d.waitForInstanceActive(); err != nil {
372 376
 		return err
373 377
 	}
374
-	if d.FloatingIpPool != "" {
378
+	if d.FloatingIPPool != "" {
375 379
 		if err := d.assignFloatingIP(); err != nil {
376 380
 			return err
377 381
 		}
@@ -468,8 +472,8 @@ func (d *Driver) checkConfig() error {
468 472
 		return fmt.Errorf(errorExclusiveOptions, "Image name", "Image id")
469 473
 	}
470 474
 
471
-	if d.NetworkName != "" && d.NetworkId != "" {
472
-		return fmt.Errorf(errorExclusiveOptions, "Network name", "Network id")
475
+	if len(d.NetworkNames) > 0 && len(d.NetworkIds) > 0 {
476
+		return fmt.Errorf(errorExclusiveOptions, "Network names", "Network ids")
473 477
 	}
474 478
 	if d.EndpointType != "" && (d.EndpointType != "publicURL" && d.EndpointType != "adminURL" && d.EndpointType != "internalURL") {
475 479
 		return fmt.Errorf(errorWrongEndpointType)
@@ -481,26 +485,26 @@ func (d *Driver) checkConfig() error {
481 485
 }
482 486
 
483 487
 func (d *Driver) resolveIds() error {
484
-	if d.NetworkName != "" && !d.ComputeNetwork {
488
+	if len(d.NetworkNames) > 0 && !d.ComputeNetwork {
485 489
 		if err := d.initNetwork(); err != nil {
486 490
 			return err
487 491
 		}
488
-
489
-		networkID, err := d.client.GetNetworkID(d)
490
-
492
+		networkIds, err := d.client.GetNetworkIDs(d)
491 493
 		if err != nil {
492 494
 			return err
493 495
 		}
494 496
 
495
-		if networkID == "" {
496
-			return fmt.Errorf(errorUnknownNetworkName, d.NetworkName)
497
+		if len(networkIds) == 0 {
498
+			return fmt.Errorf(errorUnknownNetworkName, strings.Join(d.NetworkNames, ",")) // TODO specific name
497 499
 		}
498 500
 
499
-		d.NetworkId = networkID
500
-		log.Debug("Found network id using its name", map[string]string{
501
-			"Name": d.NetworkName,
502
-			"ID":   d.NetworkId,
503
-		})
501
+		d.NetworkIds = networkIds
502
+		for i, networkName := range d.NetworkNames {
503
+			log.Debug("Found network id using its name", map[string]string{
504
+				"Name": networkName,
505
+				"ID":   d.NetworkIds[i],
506
+			})
507
+		}
504 508
 	}
505 509
 
506 510
 	if d.FlavorName != "" {
@@ -545,7 +549,7 @@ func (d *Driver) resolveIds() error {
545 549
 		})
546 550
 	}
547 551
 
548
-	if d.FloatingIpPool != "" && !d.ComputeNetwork {
552
+	if d.FloatingIPPool != "" && !d.ComputeNetwork {
549 553
 		if err := d.initNetwork(); err != nil {
550 554
 			return err
551 555
 		}
@@ -556,13 +560,13 @@ func (d *Driver) resolveIds() error {
556 560
 		}
557 561
 
558 562
 		if f == "" {
559
-			return fmt.Errorf(errorUnknownNetworkName, d.FloatingIpPool)
563
+			return fmt.Errorf(errorUnknownNetworkName, d.FloatingIPPool)
560 564
 		}
561 565
 
562
-		d.FloatingIpPoolId = f
566
+		d.FloatingIPPoolId = f
563 567
 		log.Debug("Found floating IP pool id using its name", map[string]string{
564
-			"Name": d.FloatingIpPool,
565
-			"ID":   d.FloatingIpPoolId,
568
+			"Name": d.FloatingIPPool,
569
+			"ID":   d.FloatingIPPoolId,
566 570
 		})
567 571
 	}
568 572
 
@@ -703,7 +707,7 @@ func (d *Driver) assignFloatingIP() error {
703 707
 
704 708
 	log.Debugf("Looking for an available floating IP", map[string]string{
705 709
 		"MachineId": d.MachineId,
706
-		"Pool":      d.FloatingIpPool,
710
+		"Pool":      d.FloatingIPPool,
707 711
 	})
708 712
 
709 713
 	for _, ip := range ips {

Loading…
Cancel
Save