client.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "context"
  16. "github.com/docker/api/context/cloud"
  17. "github.com/docker/api/backend"
  18. "github.com/docker/api/compose"
  19. "github.com/docker/api/containers"
  20. apicontext "github.com/docker/api/context"
  21. "github.com/docker/api/context/store"
  22. )
  23. // New returns a backend client associated with current context
  24. func New(ctx context.Context) (*Client, error) {
  25. currentContext := apicontext.CurrentContext(ctx)
  26. s := store.ContextStore(ctx)
  27. cc, err := s.Get(currentContext)
  28. if err != nil {
  29. return nil, err
  30. }
  31. service, err := backend.Get(ctx, cc.Type())
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &Client{
  36. backendType: cc.Type(),
  37. bs: service,
  38. }, nil
  39. }
  40. // GetCloudService returns a backend CloudService (typically login, create context)
  41. func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
  42. return backend.GetCloudService(ctx, backendType)
  43. }
  44. // Client is a multi-backend client
  45. type Client struct {
  46. backendType string
  47. bs backend.Service
  48. }
  49. // ContainerService returns the backend service for the current context
  50. func (c *Client) ContainerService() containers.Service {
  51. return c.bs.ContainerService()
  52. }
  53. // ComposeService returns the backend service for the current context
  54. func (c *Client) ComposeService() compose.Service {
  55. return c.bs.ComposeService()
  56. }