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-05-14 22:54:57 +02:00
|
|
|
|
2020-05-12 13:37:28 +02:00
|
|
|
"github.com/docker/api/context/cloud"
|
2020-04-06 17:54:01 +02:00
|
|
|
|
2020-04-29 19:57:53 +02:00
|
|
|
"github.com/docker/api/backend"
|
2020-05-05 15:37:12 +02:00
|
|
|
"github.com/docker/api/compose"
|
|
|
|
"github.com/docker/api/containers"
|
2020-04-29 16:21:46 +02:00
|
|
|
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
|
|
|
|
2020-05-28 17:37:59 +02:00
|
|
|
// New returns a backend client associated with current context
|
2020-04-29 16:21:46 +02:00
|
|
|
func New(ctx context.Context) (*Client, error) {
|
|
|
|
currentContext := apicontext.CurrentContext(ctx)
|
|
|
|
s := store.ContextStore(ctx)
|
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
cc, err := s.Get(currentContext)
|
2020-04-06 17:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-21 15:07:41 +02:00
|
|
|
|
2020-06-10 14:07:35 +02:00
|
|
|
service, err := backend.Get(ctx, cc.Type())
|
2020-04-29 19:57:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:56:46 +02:00
|
|
|
return &Client{
|
2020-06-10 14:07:35 +02:00
|
|
|
backendType: cc.Type(),
|
2020-05-05 15:37:12 +02:00
|
|
|
bs: service,
|
2020-05-01 10:56:46 +02:00
|
|
|
}, nil
|
2020-05-28 17:37:59 +02:00
|
|
|
}
|
2020-05-01 10:56:46 +02:00
|
|
|
|
2020-05-28 17:37:59 +02:00
|
|
|
// GetCloudService returns a backend CloudService (typically login, create context)
|
|
|
|
func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
|
|
|
|
return backend.GetCloudService(ctx, backendType)
|
2020-04-06 17:22:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// Client is a multi-backend client
|
2020-04-06 17:22:34 +02:00
|
|
|
type Client struct {
|
2020-04-29 16:21:46 +02:00
|
|
|
backendType string
|
2020-05-05 15:37:12 +02:00
|
|
|
bs backend.Service
|
2020-04-29 16:21:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 15:37:12 +02:00
|
|
|
// ContainerService returns the backend service for the current context
|
|
|
|
func (c *Client) ContainerService() containers.Service {
|
|
|
|
return c.bs.ContainerService()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComposeService returns the backend service for the current context
|
|
|
|
func (c *Client) ComposeService() compose.Service {
|
|
|
|
return c.bs.ComposeService()
|
2020-04-06 17:22:34 +02:00
|
|
|
}
|