ops.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package network
  2. import (
  3. "github.com/docker/docker/api/types"
  4. "github.com/docker/docker/api/types/network"
  5. )
  6. // WithDriver sets the driver of the network
  7. func WithDriver(driver string) func(*types.NetworkCreate) {
  8. return func(n *types.NetworkCreate) {
  9. n.Driver = driver
  10. }
  11. }
  12. // WithIPv6 Enables IPv6 on the network
  13. func WithIPv6() func(*types.NetworkCreate) {
  14. return func(n *types.NetworkCreate) {
  15. n.EnableIPv6 = true
  16. }
  17. }
  18. // WithCheckDuplicate enables CheckDuplicate on the create network request
  19. func WithCheckDuplicate() func(*types.NetworkCreate) {
  20. return func(n *types.NetworkCreate) {
  21. n.CheckDuplicate = true
  22. }
  23. }
  24. // WithMacvlan sets the network as macvlan with the specified parent
  25. func WithMacvlan(parent string) func(*types.NetworkCreate) {
  26. return func(n *types.NetworkCreate) {
  27. n.Driver = "macvlan"
  28. if parent != "" {
  29. n.Options = map[string]string{
  30. "parent": parent,
  31. }
  32. }
  33. }
  34. }
  35. // WithOption adds the specified key/value pair to network's options
  36. func WithOption(key, value string) func(*types.NetworkCreate) {
  37. return func(n *types.NetworkCreate) {
  38. if n.Options == nil {
  39. n.Options = map[string]string{}
  40. }
  41. n.Options[key] = value
  42. }
  43. }
  44. // WithIPAM adds an IPAM with the specified Subnet and Gateway to the network
  45. func WithIPAM(subnet, gateway string) func(*types.NetworkCreate) {
  46. return func(n *types.NetworkCreate) {
  47. if n.IPAM == nil {
  48. n.IPAM = &network.IPAM{}
  49. }
  50. n.IPAM.Config = append(n.IPAM.Config, network.IPAMConfig{
  51. Subnet: subnet,
  52. Gateway: gateway,
  53. AuxAddress: map[string]string{},
  54. })
  55. }
  56. }