ops.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 sets the CheckDuplicate field on create network request
  19. func WithCheckDuplicate() func(*types.NetworkCreate) {
  20. return func(n *types.NetworkCreate) {
  21. n.CheckDuplicate = true
  22. }
  23. }
  24. // WithInternal enables Internal flag on the create network request
  25. func WithInternal() func(*types.NetworkCreate) {
  26. return func(n *types.NetworkCreate) {
  27. n.Internal = true
  28. }
  29. }
  30. // WithAttachable sets Attachable flag on the create network request
  31. func WithAttachable() func(*types.NetworkCreate) {
  32. return func(n *types.NetworkCreate) {
  33. n.Attachable = true
  34. }
  35. }
  36. // WithMacvlan sets the network as macvlan with the specified parent
  37. func WithMacvlan(parent string) func(*types.NetworkCreate) {
  38. return func(n *types.NetworkCreate) {
  39. n.Driver = "macvlan"
  40. if parent != "" {
  41. n.Options = map[string]string{
  42. "parent": parent,
  43. }
  44. }
  45. }
  46. }
  47. // WithIPvlan sets the network as ipvlan with the specified parent and mode
  48. func WithIPvlan(parent, mode string) func(*types.NetworkCreate) {
  49. return func(n *types.NetworkCreate) {
  50. n.Driver = "ipvlan"
  51. if n.Options == nil {
  52. n.Options = map[string]string{}
  53. }
  54. if parent != "" {
  55. n.Options["parent"] = parent
  56. }
  57. if mode != "" {
  58. n.Options["ipvlan_mode"] = mode
  59. }
  60. }
  61. }
  62. // WithOption adds the specified key/value pair to network's options
  63. func WithOption(key, value string) func(*types.NetworkCreate) {
  64. return func(n *types.NetworkCreate) {
  65. if n.Options == nil {
  66. n.Options = map[string]string{}
  67. }
  68. n.Options[key] = value
  69. }
  70. }
  71. // WithIPAM adds an IPAM with the specified Subnet and Gateway to the network
  72. func WithIPAM(subnet, gateway string) func(*types.NetworkCreate) {
  73. return func(n *types.NetworkCreate) {
  74. if n.IPAM == nil {
  75. n.IPAM = &network.IPAM{}
  76. }
  77. n.IPAM.Config = append(n.IPAM.Config, network.IPAMConfig{
  78. Subnet: subnet,
  79. Gateway: gateway,
  80. AuxAddress: map[string]string{},
  81. })
  82. }
  83. }