Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nadim-az committed Jan 12, 2025
1 parent a63ac86 commit 184b96c
Show file tree
Hide file tree
Showing 15 changed files with 3,100 additions and 392 deletions.
86 changes: 86 additions & 0 deletions core/provider/digitalocean/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package digitalocean

import (
"context"

"github.com/digitalocean/godo"
)

// DoClient defines the interface for DigitalOcean API operations used by the provider
type DoClient interface {
// Droplet operations
CreateDroplet(ctx context.Context, req *godo.DropletCreateRequest) (*godo.Droplet, *godo.Response, error)
GetDroplet(ctx context.Context, dropletID int) (*godo.Droplet, *godo.Response, error)
DeleteDropletByTag(ctx context.Context, tag string) (*godo.Response, error)
DeleteDropletByID(ctx context.Context, id int) (*godo.Response, error)

// Firewall operations
CreateFirewall(ctx context.Context, req *godo.FirewallRequest) (*godo.Firewall, *godo.Response, error)
DeleteFirewall(ctx context.Context, firewallID string) (*godo.Response, error)

// SSH Key operations
CreateKey(ctx context.Context, req *godo.KeyCreateRequest) (*godo.Key, *godo.Response, error)
DeleteKeyByFingerprint(ctx context.Context, fingerprint string) (*godo.Response, error)
GetKeyByFingerprint(ctx context.Context, fingerprint string) (*godo.Key, *godo.Response, error)

// Tag operations
CreateTag(ctx context.Context, req *godo.TagCreateRequest) (*godo.Tag, *godo.Response, error)
DeleteTag(ctx context.Context, tag string) (*godo.Response, error)
}

// godoClient implements the DoClient interface using the actual godo.Client
type godoClient struct {
*godo.Client
}

func NewGodoClient(token string) DoClient {
return &godoClient{Client: godo.NewFromToken(token)}
}

// Droplet operations
func (c *godoClient) CreateDroplet(ctx context.Context, req *godo.DropletCreateRequest) (*godo.Droplet, *godo.Response, error) {
return c.Droplets.Create(ctx, req)
}

func (c *godoClient) GetDroplet(ctx context.Context, dropletID int) (*godo.Droplet, *godo.Response, error) {
return c.Droplets.Get(ctx, dropletID)
}

func (c *godoClient) DeleteDropletByTag(ctx context.Context, tag string) (*godo.Response, error) {
return c.Droplets.DeleteByTag(ctx, tag)
}

func (c *godoClient) DeleteDropletByID(ctx context.Context, id int) (*godo.Response, error) {
return c.Droplets.Delete(ctx, id)
}

// Firewall operations
func (c *godoClient) CreateFirewall(ctx context.Context, req *godo.FirewallRequest) (*godo.Firewall, *godo.Response, error) {
return c.Firewalls.Create(ctx, req)
}

func (c *godoClient) DeleteFirewall(ctx context.Context, firewallID string) (*godo.Response, error) {
return c.Firewalls.Delete(ctx, firewallID)
}

// SSH Key operations
func (c *godoClient) CreateKey(ctx context.Context, req *godo.KeyCreateRequest) (*godo.Key, *godo.Response, error) {
return c.Keys.Create(ctx, req)
}

func (c *godoClient) DeleteKeyByFingerprint(ctx context.Context, fingerprint string) (*godo.Response, error) {
return c.Keys.DeleteByFingerprint(ctx, fingerprint)
}

func (c *godoClient) GetKeyByFingerprint(ctx context.Context, fingerprint string) (*godo.Key, *godo.Response, error) {
return c.Keys.GetByFingerprint(ctx, fingerprint)
}

// Tag operations
func (c *godoClient) CreateTag(ctx context.Context, req *godo.TagCreateRequest) (*godo.Tag, *godo.Response, error) {
return c.Tags.Create(ctx, req)
}

func (c *godoClient) DeleteTag(ctx context.Context, tag string) (*godo.Response, error) {
return c.Tags.Delete(ctx, tag)
}
165 changes: 0 additions & 165 deletions core/provider/digitalocean/digitalocean_provider.go

This file was deleted.

94 changes: 94 additions & 0 deletions core/provider/digitalocean/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package digitalocean

import (
"context"
"io"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client"
specs "github.com/opencontainers/image-spec/specs-go/v1"
)

// DockerClient is an interface that abstracts Docker functionality
type DockerClient interface {
Ping(ctx context.Context) (types.Ping, error)
ImageInspectWithRaw(ctx context.Context, image string) (types.ImageInspect, []byte, error)
ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error)
ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *specs.Platform, containerName string) (container.CreateResponse, error)
ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error)
ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error
ContainerStop(ctx context.Context, containerID string, options container.StopOptions) error
ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error)
ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error)
ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error)
ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error)
ContainerRemove(ctx context.Context, containerID string, options container.RemoveOptions) error
Close() error
}

type defaultDockerClient struct {
client *dockerclient.Client
}

func NewDockerClient(host string) (DockerClient, error) {
client, err := dockerclient.NewClientWithOpts(dockerclient.WithHost(host))
if err != nil {
return nil, err
}
return &defaultDockerClient{client: client}, nil
}

func (d *defaultDockerClient) Ping(ctx context.Context) (types.Ping, error) {
return d.client.Ping(ctx)
}

func (d *defaultDockerClient) ImageInspectWithRaw(ctx context.Context, image string) (types.ImageInspect, []byte, error) {
return d.client.ImageInspectWithRaw(ctx, image)
}

func (d *defaultDockerClient) ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error) {
return d.client.ImagePull(ctx, ref, options)
}

func (d *defaultDockerClient) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *specs.Platform, containerName string) (container.CreateResponse, error) {
return d.client.ContainerCreate(ctx, config, hostConfig, networkingConfig, platform, containerName)
}

func (d *defaultDockerClient) ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error) {
return d.client.ContainerList(ctx, options)
}

func (d *defaultDockerClient) ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error {
return d.client.ContainerStart(ctx, containerID, options)
}

func (d *defaultDockerClient) ContainerStop(ctx context.Context, containerID string, options container.StopOptions) error {
return d.client.ContainerStop(ctx, containerID, options)
}

func (d *defaultDockerClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
return d.client.ContainerInspect(ctx, containerID)
}

func (d *defaultDockerClient) ContainerExecCreate(ctx context.Context, container string, options container.ExecOptions) (types.IDResponse, error) {
return d.client.ContainerExecCreate(ctx, container, options)
}

func (d *defaultDockerClient) ContainerExecAttach(ctx context.Context, execID string, config container.ExecAttachOptions) (types.HijackedResponse, error) {
return d.client.ContainerExecAttach(ctx, execID, config)
}

func (d *defaultDockerClient) ContainerExecInspect(ctx context.Context, execID string) (container.ExecInspect, error) {
return d.client.ContainerExecInspect(ctx, execID)
}

func (d *defaultDockerClient) ContainerRemove(ctx context.Context, containerID string, options container.RemoveOptions) error {
return d.client.ContainerRemove(ctx, containerID, options)
}

func (d *defaultDockerClient) Close() error {
return d.client.Close()
}
Loading

0 comments on commit 184b96c

Please sign in to comment.