2020-04-06 17:22:34 +02:00
|
|
|
/*
|
2020-06-18 16:13:24 +02:00
|
|
|
Copyright 2020 Docker, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2020-04-06 17:22:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
}
|