Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: abstract common api client #740

Merged
merged 1 commit into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Option struct {
type Cli struct {
Option
rootCmd *cobra.Command
APIClient *client.APIClient
APIClient client.CommonAPIClient
padding int
}

Expand Down Expand Up @@ -69,7 +69,7 @@ func (c *Cli) NewAPIClient() {
}

// Client returns API client torwards daemon.
func (c *Cli) Client() *client.APIClient {
func (c *Cli) Client() client.CommonAPIClient {
return c.APIClient
}

Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type APIClient struct {
}

// NewAPIClient initializes a new API client for the given host
func NewAPIClient(host string, tls utils.TLSConfig) (*APIClient, error) {
func NewAPIClient(host string, tls utils.TLSConfig) (CommonAPIClient, error) {
if host == "" {
host = defaultHost
}
Expand Down
15 changes: 0 additions & 15 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ import (
"github.com/alibaba/pouch/apis/types"
)

// ContainerAPIClient defines methods of Container client.
type ContainerAPIClient interface {
ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error)
ContainerStart(ctx context.Context, name, detachKeys string) error
ContainerStop(ctx context.Context, name, timeout string) error
ContainerRemove(ctx context.Context, name string, force bool) error
ContainerList(ctx context.Context, all bool) ([]*types.Container, error)
ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error)
ContainerCreateExec(ctx context.Context, name string, config *types.ExecCreateConfig) (*types.ExecCreateResp, error)
ContainerStartExec(ctx context.Context, execid string, config *types.ExecStartConfig) (net.Conn, *bufio.Reader, error)
ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error)
ContainerRename(ctx context.Context, id string, name string) error
ContainerPause(ctx context.Context, name string) error
}

// ContainerCreate creates a new container based in the given configuration.
func (client *APIClient) ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkingConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error) {
createConfig := types.ContainerCreateConfig{
Expand Down
8 changes: 0 additions & 8 deletions client/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"github.com/alibaba/pouch/apis/types"
)

// ImageAPIClient defines methods of Image client.
type ImageAPIClient interface {
ImageList(ctx context.Context) ([]types.ImageInfo, error)
ImageInspect(ctx context.Context, name string) (types.ImageInfo, error)
ImagePull(ctx context.Context, name, tag string) (io.ReadCloser, error)
ImageRemove(ctx context.Context, name string, force bool) error
}

// ImageInspect requests daemon to inspect an image.
func (client *APIClient) ImageInspect(ctx context.Context, name string) (types.ImageInfo, error) {
image := types.ImageInfo{}
Expand Down
66 changes: 66 additions & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package client

import (
"bufio"
"context"
"io"
"net"

"github.com/alibaba/pouch/apis/types"
)

// CommonAPIClient defines common methods of api client
type CommonAPIClient interface {
ContainerAPIClient
ImageAPIClient
VolumeAPIClient
SystemAPIClient
NetworkAPIClient
}

// ContainerAPIClient defines methods of Container client.
type ContainerAPIClient interface {
ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error)
ContainerStart(ctx context.Context, name, detachKeys string) error
ContainerStop(ctx context.Context, name, timeout string) error
ContainerRemove(ctx context.Context, name string, force bool) error
ContainerList(ctx context.Context, all bool) ([]*types.Container, error)
ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error)
ContainerCreateExec(ctx context.Context, name string, config *types.ExecCreateConfig) (*types.ExecCreateResp, error)
ContainerStartExec(ctx context.Context, execid string, config *types.ExecStartConfig) (net.Conn, *bufio.Reader, error)
ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error)
ContainerRename(ctx context.Context, id string, name string) error
ContainerPause(ctx context.Context, name string) error
ContainerUnpause(ctx context.Context, name string) error
}

// ImageAPIClient defines methods of Image client.
type ImageAPIClient interface {
ImageList(ctx context.Context) ([]types.ImageInfo, error)
ImageInspect(ctx context.Context, name string) (types.ImageInfo, error)
ImagePull(ctx context.Context, name, tag string) (io.ReadCloser, error)
ImageRemove(ctx context.Context, name string, force bool) error
}

// VolumeAPIClient defines methods of Volume client.
type VolumeAPIClient interface {
VolumeCreate(ctx context.Context, config *types.VolumeCreateConfig) (*types.VolumeInfo, error)
VolumeRemove(ctx context.Context, name string) error
VolumeInspect(ctx context.Context, name string) (*types.VolumeInfo, error)
VolumeList(ctx context.Context) (*types.VolumeListResp, error)
}

// SystemAPIClient defines methods of System client.
type SystemAPIClient interface {
SystemPing(ctx context.Context) (string, error)
SystemVersion(ctx context.Context) (*types.SystemVersion, error)
SystemInfo(ctx context.Context) (*types.SystemInfo, error)
}

// NetworkAPIClient defines methods of Network client.
type NetworkAPIClient interface {
NetworkCreate(ctx context.Context, req *types.NetworkCreateConfig) (*types.NetworkCreateResp, error)
NetworkRemove(ctx context.Context, networkID string) error
NetworkInspect(ctx context.Context, networkID string) (*types.NetworkInspectResp, error)
NetworkList(ctx context.Context) (*types.NetworkListResp, error)
}
3 changes: 2 additions & 1 deletion test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ var (
func TestMain(m *testing.M) {
var err error

apiClient, err = client.NewAPIClient(environment.PouchdAddress, environment.TLSConfig)
commonAPIClient, err := client.NewAPIClient(environment.PouchdAddress, environment.TLSConfig)
if err != nil {
fmt.Printf("fail to initializes pouch API client: %s", err.Error())
os.Exit(1)
}
apiClient = commonAPIClient.(*client.APIClient)

os.Exit(m.Run())
}
Expand Down
7 changes: 6 additions & 1 deletion test/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ func Post(endpoint string, opts ...Option) (*http.Response, error) {
//
// FIXME: Could we make some functions exported in alibaba/pouch/client?
func newAPIClient(host string, tls utils.TLSConfig) (*client.APIClient, error) {
return client.NewAPIClient(host, tls)
commonAPIClient, err := client.NewAPIClient(host, tls)
if err != nil {
return nil, err
}
apiClient := commonAPIClient.(*client.APIClient)
return apiClient, nil
}

// newRequest creates request targeting on specific host/path by method.
Expand Down