helpers.go 4.4 KB

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