ops.go 2.1 KB

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