task.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package builders
  2. import (
  3. "time"
  4. "github.com/docker/docker/api/types/swarm"
  5. )
  6. var (
  7. defaultTime = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  8. )
  9. // Task creates a task with default values .
  10. // Any number of task function builder can be pass to augment it.
  11. func Task(taskBuilders ...func(*swarm.Task)) *swarm.Task {
  12. task := &swarm.Task{
  13. ID: "taskID",
  14. Meta: swarm.Meta{
  15. CreatedAt: defaultTime,
  16. },
  17. Annotations: swarm.Annotations{
  18. Name: "defaultTaskName",
  19. },
  20. Spec: *TaskSpec(),
  21. ServiceID: "rl02d5gwz6chzu7il5fhtb8be",
  22. Slot: 1,
  23. Status: *TaskStatus(),
  24. DesiredState: swarm.TaskStateReady,
  25. }
  26. for _, builder := range taskBuilders {
  27. builder(task)
  28. }
  29. return task
  30. }
  31. // TaskID sets the task ID
  32. func TaskID(id string) func(*swarm.Task) {
  33. return func(task *swarm.Task) {
  34. task.ID = id
  35. }
  36. }
  37. // ServiceID sets the task service's ID
  38. func ServiceID(id string) func(*swarm.Task) {
  39. return func(task *swarm.Task) {
  40. task.ServiceID = id
  41. }
  42. }
  43. // WithStatus sets the task status
  44. func WithStatus(statusBuilders ...func(*swarm.TaskStatus)) func(*swarm.Task) {
  45. return func(task *swarm.Task) {
  46. task.Status = *TaskStatus(statusBuilders...)
  47. }
  48. }
  49. // TaskStatus creates a task status with default values .
  50. // Any number of taskStatus function builder can be pass to augment it.
  51. func TaskStatus(statusBuilders ...func(*swarm.TaskStatus)) *swarm.TaskStatus {
  52. timestamp := defaultTime.Add(1 * time.Hour)
  53. taskStatus := &swarm.TaskStatus{
  54. State: swarm.TaskStateReady,
  55. Timestamp: timestamp,
  56. }
  57. for _, builder := range statusBuilders {
  58. builder(taskStatus)
  59. }
  60. return taskStatus
  61. }
  62. // Timestamp sets the task status timestamp
  63. func Timestamp(t time.Time) func(*swarm.TaskStatus) {
  64. return func(taskStatus *swarm.TaskStatus) {
  65. taskStatus.Timestamp = t
  66. }
  67. }
  68. // StatusErr sets the tasks status error
  69. func StatusErr(err string) func(*swarm.TaskStatus) {
  70. return func(taskStatus *swarm.TaskStatus) {
  71. taskStatus.Err = err
  72. }
  73. }
  74. // PortStatus sets the tasks port config status
  75. // FIXME(vdemeester) should be a sub builder 👼
  76. func PortStatus(portConfigs []swarm.PortConfig) func(*swarm.TaskStatus) {
  77. return func(taskStatus *swarm.TaskStatus) {
  78. taskStatus.PortStatus.Ports = portConfigs
  79. }
  80. }
  81. // TaskSpec creates a task spec with default values .
  82. // Any number of taskSpec function builder can be pass to augment it.
  83. func TaskSpec(specBuilders ...func(*swarm.TaskSpec)) *swarm.TaskSpec {
  84. taskSpec := &swarm.TaskSpec{
  85. ContainerSpec: swarm.ContainerSpec{
  86. Image: "myimage:mytag",
  87. },
  88. }
  89. for _, builder := range specBuilders {
  90. builder(taskSpec)
  91. }
  92. return taskSpec
  93. }