helpers.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, insertDefaults bool) (*swarmapi.Service, error) {
  51. // GetService to match via full ID.
  52. if rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input, InsertDefaults: insertDefaults}); 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. if !insertDefaults {
  80. return rl.Services[0], nil
  81. }
  82. rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: rl.Services[0].ID, InsertDefaults: true})
  83. if err == nil {
  84. return rg.Service, nil
  85. }
  86. return nil, err
  87. }
  88. func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) {
  89. // GetTask to match via full ID.
  90. if rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input}); err == nil {
  91. return rg.Task, nil
  92. }
  93. // If any error (including NotFound), ListTasks to match via full name.
  94. rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{
  95. Filters: &swarmapi.ListTasksRequest_Filters{
  96. Names: []string{input},
  97. },
  98. })
  99. if err != nil || len(rl.Tasks) == 0 {
  100. // If any error or 0 result, ListTasks to match via ID prefix.
  101. rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{
  102. Filters: &swarmapi.ListTasksRequest_Filters{
  103. IDPrefixes: []string{input},
  104. },
  105. })
  106. }
  107. if err != nil {
  108. return nil, err
  109. }
  110. if len(rl.Tasks) == 0 {
  111. err := fmt.Errorf("task %s not found", input)
  112. return nil, errors.NewRequestNotFoundError(err)
  113. }
  114. if l := len(rl.Tasks); l > 1 {
  115. return nil, fmt.Errorf("task %s is ambiguous (%d matches found)", input, l)
  116. }
  117. return rl.Tasks[0], nil
  118. }
  119. func getSecret(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Secret, error) {
  120. // attempt to lookup secret by full ID
  121. if rg, err := c.GetSecret(ctx, &swarmapi.GetSecretRequest{SecretID: input}); err == nil {
  122. return rg.Secret, nil
  123. }
  124. // If any error (including NotFound), ListSecrets to match via full name.
  125. rl, err := c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
  126. Filters: &swarmapi.ListSecretsRequest_Filters{
  127. Names: []string{input},
  128. },
  129. })
  130. if err != nil || len(rl.Secrets) == 0 {
  131. // If any error or 0 result, ListSecrets to match via ID prefix.
  132. rl, err = c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
  133. Filters: &swarmapi.ListSecretsRequest_Filters{
  134. IDPrefixes: []string{input},
  135. },
  136. })
  137. }
  138. if err != nil {
  139. return nil, err
  140. }
  141. if len(rl.Secrets) == 0 {
  142. err := fmt.Errorf("secret %s not found", input)
  143. return nil, errors.NewRequestNotFoundError(err)
  144. }
  145. if l := len(rl.Secrets); l > 1 {
  146. return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", input, l)
  147. }
  148. return rl.Secrets[0], nil
  149. }
  150. func getConfig(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Config, error) {
  151. // attempt to lookup config by full ID
  152. if rg, err := c.GetConfig(ctx, &swarmapi.GetConfigRequest{ConfigID: input}); err == nil {
  153. return rg.Config, nil
  154. }
  155. // If any error (including NotFound), ListConfigs to match via full name.
  156. rl, err := c.ListConfigs(ctx, &swarmapi.ListConfigsRequest{
  157. Filters: &swarmapi.ListConfigsRequest_Filters{
  158. Names: []string{input},
  159. },
  160. })
  161. if err != nil || len(rl.Configs) == 0 {
  162. // If any error or 0 result, ListConfigs to match via ID prefix.
  163. rl, err = c.ListConfigs(ctx, &swarmapi.ListConfigsRequest{
  164. Filters: &swarmapi.ListConfigsRequest_Filters{
  165. IDPrefixes: []string{input},
  166. },
  167. })
  168. }
  169. if err != nil {
  170. return nil, err
  171. }
  172. if len(rl.Configs) == 0 {
  173. err := fmt.Errorf("config %s not found", input)
  174. return nil, errors.NewRequestNotFoundError(err)
  175. }
  176. if l := len(rl.Configs); l > 1 {
  177. return nil, fmt.Errorf("config %s is ambiguous (%d matches found)", input, l)
  178. }
  179. return rl.Configs[0], nil
  180. }
  181. func getNetwork(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Network, error) {
  182. // GetNetwork to match via full ID.
  183. if rg, err := c.GetNetwork(ctx, &swarmapi.GetNetworkRequest{NetworkID: input}); err == nil {
  184. return rg.Network, nil
  185. }
  186. // If any error (including NotFound), ListNetworks to match via ID prefix and full name.
  187. rl, err := c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
  188. Filters: &swarmapi.ListNetworksRequest_Filters{
  189. Names: []string{input},
  190. },
  191. })
  192. if err != nil || len(rl.Networks) == 0 {
  193. rl, err = c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
  194. Filters: &swarmapi.ListNetworksRequest_Filters{
  195. IDPrefixes: []string{input},
  196. },
  197. })
  198. }
  199. if err != nil {
  200. return nil, err
  201. }
  202. if len(rl.Networks) == 0 {
  203. return nil, fmt.Errorf("network %s not found", input)
  204. }
  205. if l := len(rl.Networks); l > 1 {
  206. return nil, fmt.Errorf("network %s is ambiguous (%d matches found)", input, l)
  207. }
  208. return rl.Networks[0], nil
  209. }