helpers.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package cluster
  2. import (
  3. "fmt"
  4. swarmapi "github.com/docker/swarmkit/api"
  5. "golang.org/x/net/context"
  6. )
  7. func getSwarm(ctx context.Context, c swarmapi.ControlClient) (*swarmapi.Cluster, error) {
  8. rl, err := c.ListClusters(ctx, &swarmapi.ListClustersRequest{})
  9. if err != nil {
  10. return nil, err
  11. }
  12. if len(rl.Clusters) == 0 {
  13. return nil, fmt.Errorf("swarm not found")
  14. }
  15. // TODO: assume one cluster only
  16. return rl.Clusters[0], nil
  17. }
  18. func getNode(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Node, error) {
  19. // GetNode to match via full ID.
  20. rg, err := c.GetNode(ctx, &swarmapi.GetNodeRequest{NodeID: input})
  21. if err != nil {
  22. // If any error (including NotFound), ListNodes to match via full name.
  23. rl, err := c.ListNodes(ctx, &swarmapi.ListNodesRequest{Filters: &swarmapi.ListNodesRequest_Filters{Names: []string{input}}})
  24. if err != nil || len(rl.Nodes) == 0 {
  25. // If any error or 0 result, ListNodes to match via ID prefix.
  26. rl, err = c.ListNodes(ctx, &swarmapi.ListNodesRequest{Filters: &swarmapi.ListNodesRequest_Filters{IDPrefixes: []string{input}}})
  27. }
  28. if err != nil {
  29. return nil, err
  30. }
  31. if len(rl.Nodes) == 0 {
  32. return nil, fmt.Errorf("node %s not found", input)
  33. }
  34. if l := len(rl.Nodes); l > 1 {
  35. return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)
  36. }
  37. return rl.Nodes[0], nil
  38. }
  39. return rg.Node, nil
  40. }
  41. func getService(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Service, error) {
  42. // GetService to match via full ID.
  43. rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input})
  44. if err != nil {
  45. // If any error (including NotFound), ListServices to match via full name.
  46. rl, err := c.ListServices(ctx, &swarmapi.ListServicesRequest{Filters: &swarmapi.ListServicesRequest_Filters{Names: []string{input}}})
  47. if err != nil || len(rl.Services) == 0 {
  48. // If any error or 0 result, ListServices to match via ID prefix.
  49. rl, err = c.ListServices(ctx, &swarmapi.ListServicesRequest{Filters: &swarmapi.ListServicesRequest_Filters{IDPrefixes: []string{input}}})
  50. }
  51. if err != nil {
  52. return nil, err
  53. }
  54. if len(rl.Services) == 0 {
  55. return nil, fmt.Errorf("service %s not found", input)
  56. }
  57. if l := len(rl.Services); l > 1 {
  58. return nil, fmt.Errorf("service %s is ambiguous (%d matches found)", input, l)
  59. }
  60. return rl.Services[0], nil
  61. }
  62. return rg.Service, nil
  63. }
  64. func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) {
  65. // GetTask to match via full ID.
  66. rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input})
  67. if err != nil {
  68. // If any error (including NotFound), ListTasks to match via full name.
  69. rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{Filters: &swarmapi.ListTasksRequest_Filters{Names: []string{input}}})
  70. if err != nil || len(rl.Tasks) == 0 {
  71. // If any error or 0 result, ListTasks to match via ID prefix.
  72. rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{Filters: &swarmapi.ListTasksRequest_Filters{IDPrefixes: []string{input}}})
  73. }
  74. if err != nil {
  75. return nil, err
  76. }
  77. if len(rl.Tasks) == 0 {
  78. return nil, fmt.Errorf("task %s not found", input)
  79. }
  80. if l := len(rl.Tasks); l > 1 {
  81. return nil, fmt.Errorf("task %s is ambiguous (%d matches found)", input, l)
  82. }
  83. return rl.Tasks[0], nil
  84. }
  85. return rg.Task, nil
  86. }