Browse Source

Another pass at linting

Signed-off-by: Olivier Gambier <olivier@docker.com>
Olivier Gambier 3 years ago
parent
commit
b153a9c218
2 changed files with 87 additions and 88 deletions
  1. 61
    63
      drivers/openstack/client.go
  2. 26
    25
      drivers/openstack/openstack.go

+ 61
- 63
drivers/openstack/client.go View File

@@ -35,16 +35,16 @@ type Client interface {
35 35
 	RestartInstance(d *Driver) error
36 36
 	DeleteInstance(d *Driver) error
37 37
 	WaitForInstanceStatus(d *Driver, status string) error
38
-	GetInstanceIpAddresses(d *Driver) ([]IpAddress, error)
38
+	GetInstanceIPAddresses(d *Driver) ([]IPAddress, error)
39 39
 	CreateKeyPair(d *Driver, name string, publicKey string) error
40 40
 	DeleteKeyPair(d *Driver, name string) error
41
-	GetNetworkId(d *Driver) (string, error)
42
-	GetFlavorId(d *Driver) (string, error)
43
-	GetImageId(d *Driver) (string, error)
44
-	AssignFloatingIP(d *Driver, floatingIp *FloatingIp) error
45
-	GetFloatingIPs(d *Driver) ([]FloatingIp, error)
46
-	GetFloatingIpPoolId(d *Driver) (string, error)
47
-	GetInstancePortId(d *Driver) (string, error)
41
+	GetNetworkID(d *Driver) (string, error)
42
+	GetFlavorID(d *Driver) (string, error)
43
+	GetImageID(d *Driver) (string, error)
44
+	AssignFloatingIP(d *Driver, floatingIP *FloatingIP) error
45
+	GetFloatingIPs(d *Driver) ([]FloatingIP, error)
46
+	GetFloatingIPPoolID(d *Driver) (string, error)
47
+	GetInstancePortID(d *Driver) (string, error)
48 48
 }
49 49
 
50 50
 type GenericClient struct {
@@ -86,7 +86,7 @@ const (
86 86
 	Fixed    string = "fixed"
87 87
 )
88 88
 
89
-type IpAddress struct {
89
+type IPAddress struct {
90 90
 	Network     string
91 91
 	AddressType string
92 92
 	Address     string
@@ -94,7 +94,7 @@ type IpAddress struct {
94 94
 	Mac         string
95 95
 }
96 96
 
97
-type FloatingIp struct {
97
+type FloatingIP struct {
98 98
 	Id        string
99 99
 	Ip        string
100 100
 	NetworkId string
@@ -158,12 +158,12 @@ func (c *GenericClient) WaitForInstanceStatus(d *Driver, status string) error {
158 158
 	}, (d.ActiveTimeout / 4), 4*time.Second)
159 159
 }
160 160
 
161
-func (c *GenericClient) GetInstanceIpAddresses(d *Driver) ([]IpAddress, error) {
161
+func (c *GenericClient) GetInstanceIPAddresses(d *Driver) ([]IPAddress, error) {
162 162
 	server, err := c.GetServerDetail(d)
163 163
 	if err != nil {
164 164
 		return nil, err
165 165
 	}
166
-	addresses := []IpAddress{}
166
+	addresses := []IPAddress{}
167 167
 	for network, networkAddresses := range server.Addresses {
168 168
 		for _, element := range networkAddresses.([]interface{}) {
169 169
 			address := element.(map[string]interface{})
@@ -173,7 +173,7 @@ func (c *GenericClient) GetInstanceIpAddresses(d *Driver) ([]IpAddress, error) {
173 173
 				version = 4
174 174
 			}
175 175
 
176
-			addr := IpAddress{
176
+			addr := IPAddress{
177 177
 				Network:     network,
178 178
 				AddressType: Fixed,
179 179
 				Address:     address["addr"].(string),
@@ -194,18 +194,18 @@ func (c *GenericClient) GetInstanceIpAddresses(d *Driver) ([]IpAddress, error) {
194 194
 	return addresses, nil
195 195
 }
196 196
 
197
-func (c *GenericClient) GetNetworkId(d *Driver) (string, error) {
198
-	return c.getNetworkId(d, d.NetworkName)
197
+func (c *GenericClient) GetNetworkID(d *Driver) (string, error) {
198
+	return c.getNetworkID(d, d.NetworkName)
199 199
 }
200 200
 
201
-func (c *GenericClient) GetFloatingIpPoolId(d *Driver) (string, error) {
202
-	return c.getNetworkId(d, d.FloatingIpPool)
201
+func (c *GenericClient) GetFloatingIPPoolID(d *Driver) (string, error) {
202
+	return c.getNetworkID(d, d.FloatingIpPool)
203 203
 }
204 204
 
205
-func (c *GenericClient) getNetworkId(d *Driver, networkName string) (string, error) {
205
+func (c *GenericClient) getNetworkID(d *Driver, networkName string) (string, error) {
206 206
 	opts := networks.ListOpts{Name: networkName}
207 207
 	pager := networks.List(c.Network, opts)
208
-	networkId := ""
208
+	networkID := ""
209 209
 
210 210
 	err := pager.EachPage(func(page pagination.Page) (bool, error) {
211 211
 		networkList, err := networks.ExtractNetworks(page)
@@ -215,7 +215,7 @@ func (c *GenericClient) getNetworkId(d *Driver, networkName string) (string, err
215 215
 
216 216
 		for _, n := range networkList {
217 217
 			if n.Name == networkName {
218
-				networkId = n.ID
218
+				networkID = n.ID
219 219
 				return false, nil
220 220
 			}
221 221
 		}
@@ -223,12 +223,12 @@ func (c *GenericClient) getNetworkId(d *Driver, networkName string) (string, err
223 223
 		return true, nil
224 224
 	})
225 225
 
226
-	return networkId, err
226
+	return networkID, err
227 227
 }
228 228
 
229
-func (c *GenericClient) GetFlavorId(d *Driver) (string, error) {
229
+func (c *GenericClient) GetFlavorID(d *Driver) (string, error) {
230 230
 	pager := flavors.ListDetail(c.Compute, nil)
231
-	flavorId := ""
231
+	flavorID := ""
232 232
 
233 233
 	err := pager.EachPage(func(page pagination.Page) (bool, error) {
234 234
 		flavorList, err := flavors.ExtractFlavors(page)
@@ -238,7 +238,7 @@ func (c *GenericClient) GetFlavorId(d *Driver) (string, error) {
238 238
 
239 239
 		for _, f := range flavorList {
240 240
 			if f.Name == d.FlavorName {
241
-				flavorId = f.ID
241
+				flavorID = f.ID
242 242
 				return false, nil
243 243
 			}
244 244
 		}
@@ -246,13 +246,13 @@ func (c *GenericClient) GetFlavorId(d *Driver) (string, error) {
246 246
 		return true, nil
247 247
 	})
248 248
 
249
-	return flavorId, err
249
+	return flavorID, err
250 250
 }
251 251
 
252
-func (c *GenericClient) GetImageId(d *Driver) (string, error) {
252
+func (c *GenericClient) GetImageID(d *Driver) (string, error) {
253 253
 	opts := images.ListOpts{Name: d.ImageName}
254 254
 	pager := images.ListDetail(c.Compute, opts)
255
-	imageId := ""
255
+	imageID := ""
256 256
 
257 257
 	err := pager.EachPage(func(page pagination.Page) (bool, error) {
258 258
 		imageList, err := images.ExtractImages(page)
@@ -262,7 +262,7 @@ func (c *GenericClient) GetImageId(d *Driver) (string, error) {
262 262
 
263 263
 		for _, i := range imageList {
264 264
 			if i.Name == d.ImageName {
265
-				imageId = i.ID
265
+				imageID = i.ID
266 266
 				return false, nil
267 267
 			}
268 268
 		}
@@ -270,7 +270,7 @@ func (c *GenericClient) GetImageId(d *Driver) (string, error) {
270 270
 		return true, nil
271 271
 	})
272 272
 
273
-	return imageId, err
273
+	return imageID, err
274 274
 }
275 275
 
276 276
 func (c *GenericClient) CreateKeyPair(d *Driver, name string, publicKey string) error {
@@ -299,49 +299,48 @@ func (c *GenericClient) GetServerDetail(d *Driver) (*servers.Server, error) {
299 299
 	return server, nil
300 300
 }
301 301
 
302
-func (c *GenericClient) AssignFloatingIP(d *Driver, floatingIp *FloatingIp) error {
302
+func (c *GenericClient) AssignFloatingIP(d *Driver, floatingIP *FloatingIP) error {
303 303
 	if d.ComputeNetwork {
304
-		return c.assignNovaFloatingIP(d, floatingIp)
305
-	} else {
306
-		return c.assignNeutronFloatingIP(d, floatingIp)
304
+		return c.assignNovaFloatingIP(d, floatingIP)
307 305
 	}
306
+	return c.assignNeutronFloatingIP(d, floatingIP)
308 307
 }
309 308
 
310
-func (c *GenericClient) assignNovaFloatingIP(d *Driver, floatingIp *FloatingIp) error {
311
-	if floatingIp.Ip == "" {
309
+func (c *GenericClient) assignNovaFloatingIP(d *Driver, floatingIP *FloatingIP) error {
310
+	if floatingIP.Ip == "" {
312 311
 		f, err := compute_ips.Create(c.Compute, compute_ips.CreateOpts{
313 312
 			Pool: d.FloatingIpPool,
314 313
 		}).Extract()
315 314
 		if err != nil {
316 315
 			return err
317 316
 		}
318
-		floatingIp.Ip = f.IP
319
-		floatingIp.Pool = f.Pool
317
+		floatingIP.Ip = f.IP
318
+		floatingIP.Pool = f.Pool
320 319
 	}
321
-	return compute_ips.Associate(c.Compute, d.MachineId, floatingIp.Ip).Err
320
+	return compute_ips.Associate(c.Compute, d.MachineId, floatingIP.Ip).Err
322 321
 }
323 322
 
324
-func (c *GenericClient) assignNeutronFloatingIP(d *Driver, floatingIp *FloatingIp) error {
325
-	portId, err := c.GetInstancePortId(d)
323
+func (c *GenericClient) assignNeutronFloatingIP(d *Driver, floatingIP *FloatingIP) error {
324
+	portID, err := c.GetInstancePortID(d)
326 325
 	if err != nil {
327 326
 		return err
328 327
 	}
329
-	if floatingIp.Id == "" {
328
+	if floatingIP.Id == "" {
330 329
 		f, err := floatingips.Create(c.Network, floatingips.CreateOpts{
331 330
 			FloatingNetworkID: d.FloatingIpPoolId,
332
-			PortID:            portId,
331
+			PortID:            portID,
333 332
 		}).Extract()
334 333
 		if err != nil {
335 334
 			return err
336 335
 		}
337
-		floatingIp.Id = f.ID
338
-		floatingIp.Ip = f.FloatingIP
339
-		floatingIp.NetworkId = f.FloatingNetworkID
340
-		floatingIp.PortId = f.PortID
336
+		floatingIP.Id = f.ID
337
+		floatingIP.Ip = f.FloatingIP
338
+		floatingIP.NetworkId = f.FloatingNetworkID
339
+		floatingIP.PortId = f.PortID
341 340
 		return nil
342 341
 	}
343
-	_, err = floatingips.Update(c.Network, floatingIp.Id, floatingips.UpdateOpts{
344
-		PortID: portId,
342
+	_, err = floatingips.Update(c.Network, floatingIP.Id, floatingips.UpdateOpts{
343
+		PortID: portID,
345 344
 	}).Extract()
346 345
 	if err != nil {
347 346
 		return err
@@ -349,25 +348,24 @@ func (c *GenericClient) assignNeutronFloatingIP(d *Driver, floatingIp *FloatingI
349 348
 	return nil
350 349
 }
351 350
 
352
-func (c *GenericClient) GetFloatingIPs(d *Driver) ([]FloatingIp, error) {
351
+func (c *GenericClient) GetFloatingIPs(d *Driver) ([]FloatingIP, error) {
353 352
 	if d.ComputeNetwork {
354 353
 		return c.getNovaNetworkFloatingIPs(d)
355
-	} else {
356
-		return c.getNeutronNetworkFloatingIPs(d)
357 354
 	}
355
+	return c.getNeutronNetworkFloatingIPs(d)
358 356
 }
359 357
 
360
-func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIp, error) {
358
+func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIP, error) {
361 359
 	pager := compute_ips.List(c.Compute)
362 360
 
363
-	ips := []FloatingIp{}
361
+	ips := []FloatingIP{}
364 362
 	err := pager.EachPage(func(page pagination.Page) (continue_paging bool, err error) {
365 363
 		continue_paging, err = true, nil
366
-		ip_listing, err := compute_ips.ExtractFloatingIPs(page)
364
+		ipListing, err := compute_ips.ExtractFloatingIPs(page)
367 365
 
368
-		for _, ip := range ip_listing {
366
+		for _, ip := range ipListing {
369 367
 			if ip.InstanceID == "" && ip.Pool == d.FloatingIpPool {
370
-				ips = append(ips, FloatingIp{
368
+				ips = append(ips, FloatingIP{
371 369
 					Id:   ip.ID,
372 370
 					Ip:   ip.IP,
373 371
 					Pool: ip.Pool,
@@ -379,19 +377,19 @@ func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIp, erro
379 377
 	return ips, err
380 378
 }
381 379
 
382
-func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIp, error) {
380
+func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIP, error) {
383 381
 	pager := floatingips.List(c.Network, floatingips.ListOpts{
384 382
 		FloatingNetworkID: d.FloatingIpPoolId,
385 383
 	})
386 384
 
387
-	ips := []FloatingIp{}
385
+	ips := []FloatingIP{}
388 386
 	err := pager.EachPage(func(page pagination.Page) (bool, error) {
389 387
 		floatingipList, err := floatingips.ExtractFloatingIPs(page)
390 388
 		if err != nil {
391 389
 			return false, err
392 390
 		}
393 391
 		for _, f := range floatingipList {
394
-			ips = append(ips, FloatingIp{
392
+			ips = append(ips, FloatingIP{
395 393
 				Id:        f.ID,
396 394
 				Ip:        f.FloatingIP,
397 395
 				NetworkId: f.FloatingNetworkID,
@@ -407,20 +405,20 @@ func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIp, e
407 405
 	return ips, nil
408 406
 }
409 407
 
410
-func (c *GenericClient) GetInstancePortId(d *Driver) (string, error) {
408
+func (c *GenericClient) GetInstancePortID(d *Driver) (string, error) {
411 409
 	pager := ports.List(c.Network, ports.ListOpts{
412 410
 		DeviceID:  d.MachineId,
413 411
 		NetworkID: d.NetworkId,
414 412
 	})
415 413
 
416
-	var portId string
414
+	var portID string
417 415
 	err := pager.EachPage(func(page pagination.Page) (bool, error) {
418 416
 		portList, err := ports.ExtractPorts(page)
419 417
 		if err != nil {
420 418
 			return false, err
421 419
 		}
422 420
 		for _, port := range portList {
423
-			portId = port.ID
421
+			portID = port.ID
424 422
 			return false, nil
425 423
 		}
426 424
 		return true, nil
@@ -429,7 +427,7 @@ func (c *GenericClient) GetInstancePortId(d *Driver) (string, error) {
429 427
 	if err != nil {
430 428
 		return "", err
431 429
 	}
432
-	return portId, nil
430
+	return portID, nil
433 431
 }
434 432
 
435 433
 func (c *GenericClient) InitComputeClient(d *Driver) error {

+ 26
- 25
drivers/openstack/openstack.go View File

@@ -222,6 +222,7 @@ func (d *Driver) SetClient(client Client) {
222 222
 	d.client = client
223 223
 }
224 224
 
225
+// DriverName returns the name of the driver
225 226
 func (d *Driver) DriverName() string {
226 227
 	return "openstack"
227 228
 }
@@ -289,7 +290,7 @@ func (d *Driver) GetIP() (string, error) {
289 290
 
290 291
 	// Looking for the IP address in a retry loop to deal with OpenStack latency
291 292
 	for retryCount := 0; retryCount < 200; retryCount++ {
292
-		addresses, err := d.client.GetInstanceIpAddresses(d)
293
+		addresses, err := d.client.GetInstanceIPAddresses(d)
293 294
 		if err != nil {
294 295
 			return "", err
295 296
 		}
@@ -352,11 +353,11 @@ func (d *Driver) Create() error {
352 353
 		return err
353 354
 	}
354 355
 	if d.FloatingIpPool != "" {
355
-		if err := d.assignFloatingIp(); err != nil {
356
+		if err := d.assignFloatingIP(); err != nil {
356 357
 			return err
357 358
 		}
358 359
 	}
359
-	if err := d.lookForIpAddress(); err != nil {
360
+	if err := d.lookForIPAddress(); err != nil {
360 361
 		return err
361 362
 	}
362 363
 	return nil
@@ -420,7 +421,7 @@ const (
420 421
 	errorMandatoryEnvOrOption    string = "%s must be specified either using the environment variable %s or the CLI option %s"
421 422
 	errorMandatoryOption         string = "%s must be specified using the CLI option %s"
422 423
 	errorExclusiveOptions        string = "Either %s or %s must be specified, not both"
423
-	errorMandatoryTenantNameOrId string = "Tenant id or name must be provided either using one of the environment variables OS_TENANT_ID and OS_TENANT_NAME or one of the CLI options --openstack-tenant-id and --openstack-tenant-name"
424
+	errorMandatoryTenantNameOrID string = "Tenant id or name must be provided either using one of the environment variables OS_TENANT_ID and OS_TENANT_NAME or one of the CLI options --openstack-tenant-id and --openstack-tenant-name"
424 425
 	errorWrongEndpointType       string = "Endpoint type must be 'publicURL', 'adminURL' or 'internalURL'"
425 426
 	errorUnknownFlavorName       string = "Unable to find flavor named %s"
426 427
 	errorUnknownImageName        string = "Unable to find image named %s"
@@ -438,7 +439,7 @@ func (d *Driver) checkConfig() error {
438 439
 		return fmt.Errorf(errorMandatoryEnvOrOption, "Password", "OS_PASSWORD", "--openstack-password")
439 440
 	}
440 441
 	if d.TenantName == "" && d.TenantId == "" {
441
-		return fmt.Errorf(errorMandatoryTenantNameOrId)
442
+		return fmt.Errorf(errorMandatoryTenantNameOrID)
442 443
 	}
443 444
 
444 445
 	if d.FlavorName == "" && d.FlavorId == "" {
@@ -470,17 +471,17 @@ func (d *Driver) resolveIds() error {
470 471
 			return err
471 472
 		}
472 473
 
473
-		networkId, err := d.client.GetNetworkId(d)
474
+		networkID, err := d.client.GetNetworkID(d)
474 475
 
475 476
 		if err != nil {
476 477
 			return err
477 478
 		}
478 479
 
479
-		if networkId == "" {
480
+		if networkID == "" {
480 481
 			return fmt.Errorf(errorUnknownNetworkName, d.NetworkName)
481 482
 		}
482 483
 
483
-		d.NetworkId = networkId
484
+		d.NetworkId = networkID
484 485
 		log.WithFields(log.Fields{
485 486
 			"Name": d.NetworkName,
486 487
 			"ID":   d.NetworkId,
@@ -491,17 +492,17 @@ func (d *Driver) resolveIds() error {
491 492
 		if err := d.initCompute(); err != nil {
492 493
 			return err
493 494
 		}
494
-		flavorId, err := d.client.GetFlavorId(d)
495
+		flavorID, err := d.client.GetFlavorID(d)
495 496
 
496 497
 		if err != nil {
497 498
 			return err
498 499
 		}
499 500
 
500
-		if flavorId == "" {
501
+		if flavorID == "" {
501 502
 			return fmt.Errorf(errorUnknownFlavorName, d.FlavorName)
502 503
 		}
503 504
 
504
-		d.FlavorId = flavorId
505
+		d.FlavorId = flavorID
505 506
 		log.WithFields(log.Fields{
506 507
 			"Name": d.FlavorName,
507 508
 			"ID":   d.FlavorId,
@@ -512,17 +513,17 @@ func (d *Driver) resolveIds() error {
512 513
 		if err := d.initCompute(); err != nil {
513 514
 			return err
514 515
 		}
515
-		imageId, err := d.client.GetImageId(d)
516
+		imageID, err := d.client.GetImageID(d)
516 517
 
517 518
 		if err != nil {
518 519
 			return err
519 520
 		}
520 521
 
521
-		if imageId == "" {
522
+		if imageID == "" {
522 523
 			return fmt.Errorf(errorUnknownImageName, d.ImageName)
523 524
 		}
524 525
 
525
-		d.ImageId = imageId
526
+		d.ImageId = imageID
526 527
 		log.WithFields(log.Fields{
527 528
 			"Name": d.ImageName,
528 529
 			"ID":   d.ImageId,
@@ -533,7 +534,7 @@ func (d *Driver) resolveIds() error {
533 534
 		if err := d.initNetwork(); err != nil {
534 535
 			return err
535 536
 		}
536
-		f, err := d.client.GetFloatingIpPoolId(d)
537
+		f, err := d.client.GetFloatingIPPoolID(d)
537 538
 
538 539
 		if err != nil {
539 540
 			return err
@@ -601,15 +602,15 @@ func (d *Driver) createMachine() error {
601 602
 	if err := d.initCompute(); err != nil {
602 603
 		return err
603 604
 	}
604
-	instanceId, err := d.client.CreateInstance(d)
605
+	instanceID, err := d.client.CreateInstance(d)
605 606
 	if err != nil {
606 607
 		return err
607 608
 	}
608
-	d.MachineId = instanceId
609
+	d.MachineId = instanceID
609 610
 	return nil
610 611
 }
611 612
 
612
-func (d *Driver) assignFloatingIp() error {
613
+func (d *Driver) assignFloatingIP() error {
613 614
 	var err error
614 615
 
615 616
 	if d.ComputeNetwork {
@@ -627,7 +628,7 @@ func (d *Driver) assignFloatingIp() error {
627 628
 		return err
628 629
 	}
629 630
 
630
-	var floatingIp *FloatingIp
631
+	var floatingIP *FloatingIP
631 632
 
632 633
 	log.WithFields(log.Fields{
633 634
 		"MachineId": d.MachineId,
@@ -640,22 +641,22 @@ func (d *Driver) assignFloatingIp() error {
640 641
 				"MachineId": d.MachineId,
641 642
 				"IP":        ip.Ip,
642 643
 			}).Debugf("Available floating IP found")
643
-			floatingIp = &ip
644
+			floatingIP = &ip
644 645
 			break
645 646
 		}
646 647
 	}
647 648
 
648
-	if floatingIp == nil {
649
-		floatingIp = &FloatingIp{}
649
+	if floatingIP == nil {
650
+		floatingIP = &FloatingIP{}
650 651
 		log.WithField("MachineId", d.MachineId).Debugf("No available floating IP found. Allocating a new one...")
651 652
 	} else {
652 653
 		log.WithField("MachineId", d.MachineId).Debugf("Assigning floating IP to the instance")
653 654
 	}
654 655
 
655
-	if err := d.client.AssignFloatingIP(d, floatingIp); err != nil {
656
+	if err := d.client.AssignFloatingIP(d, floatingIP); err != nil {
656 657
 		return err
657 658
 	}
658
-	d.IPAddress = floatingIp.Ip
659
+	d.IPAddress = floatingIP.Ip
659 660
 	return nil
660 661
 }
661 662
 
@@ -667,7 +668,7 @@ func (d *Driver) waitForInstanceActive() error {
667 668
 	return nil
668 669
 }
669 670
 
670
-func (d *Driver) lookForIpAddress() error {
671
+func (d *Driver) lookForIPAddress() error {
671 672
 	ip, err := d.GetIP()
672 673
 	if err != nil {
673 674
 		return err

Loading…
Cancel
Save