2020-04-06 17:22:34 +02:00
|
|
|
/*
|
2020-04-24 14:39:49 +02:00
|
|
|
Copyright (c) 2020 Docker Inc.
|
2020-04-06 17:22:34 +02:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
|
|
obtaining a copy of this software and associated documentation
|
|
|
|
files (the "Software"), to deal in the Software without
|
|
|
|
restriction, including without limitation the rights to use, copy,
|
|
|
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED,
|
|
|
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY,
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
TORT OR OTHERWISE,
|
|
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
|
|
|
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
2020-04-06 17:54:01 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2020-04-29 19:57:53 +02:00
|
|
|
"errors"
|
2020-04-06 17:54:01 +02:00
|
|
|
|
2020-04-29 19:57:53 +02:00
|
|
|
"github.com/docker/api/backend"
|
2020-04-24 11:50:30 +02:00
|
|
|
v1 "github.com/docker/api/backend/v1"
|
2020-04-29 16:21:46 +02:00
|
|
|
"github.com/docker/api/containers"
|
|
|
|
apicontext "github.com/docker/api/context"
|
|
|
|
"github.com/docker/api/context/store"
|
2020-04-24 11:50:30 +02:00
|
|
|
)
|
2020-04-06 17:22:34 +02:00
|
|
|
|
|
|
|
// New returns a GRPC client
|
2020-04-29 16:21:46 +02:00
|
|
|
func New(ctx context.Context) (*Client, error) {
|
|
|
|
currentContext := apicontext.CurrentContext(ctx)
|
|
|
|
s := store.ContextStore(ctx)
|
|
|
|
|
|
|
|
cc, err := s.Get(currentContext, nil)
|
2020-04-06 17:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-29 16:21:46 +02:00
|
|
|
contextType := s.GetType(cc)
|
2020-04-21 15:07:41 +02:00
|
|
|
|
2020-04-29 19:57:53 +02:00
|
|
|
b, err := backend.Get(ctx, contextType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ba, ok := b.(containers.ContainerService); ok {
|
|
|
|
return &Client{
|
|
|
|
backendType: contextType,
|
|
|
|
cc: ba,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("backend not found")
|
2020-04-06 17:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2020-04-06 17:54:01 +02:00
|
|
|
v1.BackendClient
|
2020-04-29 16:21:46 +02:00
|
|
|
backendType string
|
2020-04-29 19:57:53 +02:00
|
|
|
cc containers.ContainerService
|
2020-04-29 16:21:46 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 23:10:06 +02:00
|
|
|
func (c *Client) ContainerService() containers.ContainerService {
|
2020-04-29 19:57:53 +02:00
|
|
|
return c.cc
|
2020-04-06 17:22:34 +02:00
|
|
|
}
|