helpers.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if rg, err := c.GetNode(ctx, &swarmapi.GetNodeRequest{NodeID: input}); err == nil {
  22. return rg.Node, nil
  23. }
  24. // If any error (including NotFound), ListNodes to match via full name.
  25. rl, err := c.ListNodes(ctx, &swarmapi.ListNodesRequest{
  26. Filters: &swarmapi.ListNodesRequest_Filters{
  27. Names: []string{input},
  28. },
  29. })
  30. if err != nil || len(rl.Nodes) == 0 {
  31. // If any error or 0 result, ListNodes to match via ID prefix.
  32. rl, err = c.ListNodes(ctx, &swarmapi.ListNodesRequest{
  33. Filters: &swarmapi.ListNodesRequest_Filters{
  34. IDPrefixes: []string{input},
  35. },
  36. })
  37. }
  38. if err != nil {
  39. return nil, err
  40. }
  41. if len(rl.Nodes) == 0 {
  42. err := fmt.Errorf("node %s not found", input)
  43. return nil, errors.NewRequestNotFoundError(err)
  44. }
  45. if l := len(rl.Nodes); l > 1 {
  46. return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)
  47. }
  48. return rl.Nodes[0], nil
  49. }
  50. func getService(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Service, error) {
  51. // GetService to match via full ID.
  52. if rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input}); err == nil {
  53. return rg.Service, nil
  54. }
  55. // If any error (including NotFound), ListServices to match via full name.
  56. rl, err := c.ListServices(ctx, &swarmapi.ListServicesRequest{
  57. Filters: &swarmapi.ListServicesRequest_Filters{
  58. Names: []string{input},
  59. },
  60. })
  61. if err != nil || len(rl.Services) == 0 {
  62. // If any error or 0 result, ListServices to match via ID prefix.
  63. rl, err = c.ListServices(ctx, &swarmapi.ListServicesRequest{
  64. Filters: &swarmapi.ListServicesRequest_Filters{
  65. IDPrefixes: []string{input},
  66. },
  67. })
  68. }
  69. if err != nil {
  70. return nil, err
  71. }
  72. if len(rl.Services) == 0 {
  73. err := fmt.Errorf("service %s not found", input)
  74. return nil, errors.NewRequestNotFoundError(err)
  75. }
  76. if l := len(rl.Services); l > 1 {
  77. return nil, fmt.Errorf("service %s is ambiguous (%d matches found)", input, l)
  78. }
  79. return rl.Services[0], nil
  80. }
  81. func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) {
  82. // GetTask to match via full ID.
  83. if rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input}); err == nil {
  84. return rg.Task, nil
  85. }
  86. // If any error (including NotFound), ListTasks to match via full name.
  87. rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{
  88. Filters: &swarmapi.ListTasksRequest_Filters{
  89. Names: []string{input},
  90. },
  91. })
  92. if err != nil || len(rl.Tasks) == 0 {
  93. // If any error or 0 result, ListTasks to match via ID prefix.
  94. rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{
  95. Filters: &swarmapi.ListTasksRequest_Filters{
  96. IDPrefixes: []string{input},
  97. },
  98. })
  99. }
  100. if err != nil {
  101. return nil, err
  102. }
  103. if len(rl.Tasks) == 0 {
  104. err := fmt.Errorf("task %s not found", input)
  105. return nil, errors.NewRequestNotFoundError(err)
  106. }
  107. if l := len(rl.Tasks); l > 1 {
  108. return nil, fmt.Errorf("task %s is ambiguous (%d matches found)", input, l)
  109. }
  110. return rl.Tasks[0], nil
  111. }
  112. func getSecret(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Secret, error) {
  113. // attempt to lookup secret by full ID
  114. if rg, err := c.GetSecret(ctx, &swarmapi.GetSecretRequest{SecretID: input}); err == nil {
  115. return rg.Secret, nil
  116. }
  117. // If any error (including NotFound), ListSecrets to match via full name.
  118. rl, err := c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
  119. Filters: &swarmapi.ListSecretsRequest_Filters{
  120. Names: []string{input},
  121. },
  122. })
  123. if err != nil || len(rl.Secrets) == 0 {
  124. // If any error or 0 result, ListSecrets to match via ID prefix.
  125. rl, err = c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
  126. Filters: &swarmapi.ListSecretsRequest_Filters{
  127. IDPrefixes: []string{input},
  128. },
  129. })
  130. }
  131. if err != nil {
  132. return nil, err
  133. }
  134. if len(rl.Secrets) == 0 {
  135. err := fmt.Errorf("secret %s not found", input)
  136. return nil, errors.NewRequestNotFoundError(err)
  137. }
  138. if l := len(rl.Secrets); l > 1 {
  139. return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", input, l)
  140. }
  141. return rl.Secrets[0], nil
  142. }
  143. func getNetwork(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Network, error) {
  144. // GetNetwork to match via full ID.
  145. if rg, err := c.GetNetwork(ctx, &swarmapi.GetNetworkRequest{NetworkID: input}); err == nil {
  146. return rg.Network, nil
  147. }
  148. // If any error (including NotFound), ListNetworks to match via ID prefix and full name.
  149. rl, err := c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
  150. Filters: &swarmapi.ListNetworksRequest_Filters{
  151. Names: []string{input},
  152. },
  153. })
  154. if err != nil || len(rl.Networks) == 0 {
  155. rl, err = c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
  156. Filters: &swarmapi.ListNetworksRequest_Filters{
  157. IDPrefixes: []string{input},
  158. },
  159. })
  160. }
  161. if err != nil {
  162. return nil, err
  163. }
  164. if len(rl.Networks) == 0 {
  165. return nil, fmt.Errorf("network %s not found", input)
  166. }
  167. if l := len(rl.Networks); l > 1 {
  168. return nil, fmt.Errorf("network %s is ambiguous (%d matches found)", input, l)
  169. }
  170. return rl.Networks[0], nil
  171. }