Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from fubarhouse/isue/88
#88: Open up Docker Network API to user/config
- Loading branch information
Showing
7 changed files
with
133 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package library | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
"github.com/fubarhouse/pygmy-go/service/interface" | ||
) | ||
|
||
// NetworkCreate is part of a centralised abstraction of the Docker API | ||
// and will create a Docker network with a specified configuration. | ||
func NetworkCreate(c Config, name string) error { | ||
return model.DockerNetworkCreate(name, c.Networks[name].Config) | ||
} | ||
|
||
// NetworkConnect is part of a centralised abstraction of the Docker API | ||
// and will connect a created container to a docker network with a | ||
// specified name. | ||
func NetworkConnect(c Config, name string, containerName string) error { | ||
return model.DockerNetworkConnect(name, containerName) | ||
} | ||
|
||
// NetworkStatus will check the state of a Docker network to test if it has | ||
// been created, and will return false if the network can not be found. | ||
func NetworkStatus(network string) (bool, error) { | ||
ctx := context.Background() | ||
cli, err := client.NewEnvClient() | ||
if err != nil { | ||
return false, err | ||
} | ||
networkResources, err := cli.NetworkList(ctx, types.NetworkListOptions{}) | ||
for _, Network := range networkResources { | ||
if Network.Name == network { | ||
return true, nil | ||
} | ||
} | ||
return false, errors.New(fmt.Sprintf("network %v not found\n", network)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,26 @@ | ||
package network | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
"github.com/fubarhouse/pygmy-go/service/interface" | ||
"github.com/docker/docker/api/types/network" | ||
model "github.com/fubarhouse/pygmy-go/service/interface" | ||
) | ||
|
||
func Create(network string) error { | ||
return model.DockerNetworkCreate(network) | ||
} | ||
|
||
func Connect(containerName string, network string) error { | ||
return model.DockerNetworkConnect(network, containerName) | ||
} | ||
|
||
func Status(network string) (bool, error) { | ||
ctx := context.Background() | ||
cli, err := client.NewEnvClient() | ||
if err != nil { | ||
return false, err | ||
} | ||
networkResources, err := cli.NetworkList(ctx, types.NetworkListOptions{}) | ||
for _, Network := range networkResources { | ||
if Network.Name == network { | ||
return true, nil | ||
} | ||
// New will generate the defaults for the Docker network. | ||
// If configuration is provided this will not be used at all. | ||
func New() model.Network { | ||
return model.Network{ | ||
Name: "amazeeio-network", | ||
Containers: []string{"amazeeio-haproxy"}, | ||
Config: types.NetworkCreate{ | ||
IPAM: &network.IPAM{ | ||
Config: []network.IPAMConfig{ | ||
{ | ||
Subnet: "10.99.99.0/24", | ||
Gateway: "10.99.99.1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
return false, errors.New(fmt.Sprintf("network %v not found\n", network)) | ||
} |