finish work
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"fmt"
|
||||
"github.com/go-jose/go-jose/v3/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -29,11 +32,26 @@ func DisConnect(ns string, id string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func Connect(ns string, id string) (err error) {
|
||||
reader := strings.NewReader(fmt.Sprintf(`{
|
||||
"container":"%s"
|
||||
}`, id))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/"+ns+"/connect", reader)
|
||||
type ConnectInfo struct {
|
||||
ContainerID string `json:"container"`
|
||||
Aliases []string `json:"aliases,aliases"`
|
||||
StaticIPs []string `json:"static_ips,omitempty"`
|
||||
StaticMac string `json:"static_mac,omitempty"`
|
||||
}
|
||||
|
||||
func Connect(ns string, r model.ConnectRequest) (err error) {
|
||||
var con ConnectInfo
|
||||
con.ContainerID = r.Id
|
||||
if r.Ip != "" {
|
||||
con.StaticIPs = strings.Split(r.Ip, ",")
|
||||
}
|
||||
if r.Alias != "" {
|
||||
con.Aliases = strings.Split(r.Alias, ",")
|
||||
}
|
||||
con.StaticMac = r.Mac
|
||||
b, _ := json.Marshal(&con)
|
||||
fmt.Println(string(b))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/"+ns+"/connect", bytes.NewReader(b))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("request for network list error:", err)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Create(r model.CreateNetworkRequest) error {
|
||||
var create CreatNet
|
||||
create.DnsEnabled = r.EnableDns
|
||||
create.Driver = "bridge"
|
||||
create.Name = r.Name
|
||||
if r.Subnet != "" {
|
||||
create.Subnets = []SubNet{
|
||||
{
|
||||
Subnet: r.Subnet,
|
||||
},
|
||||
}
|
||||
}
|
||||
b, _ := json.Marshal(&create)
|
||||
log.Println(string(b))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/create", bytes.NewReader(b))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
var e model.ErrInfo
|
||||
json.NewDecoder(res.Body).Decode(&e)
|
||||
if e.Response > 400 {
|
||||
return errors.New(e.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CreatNet struct {
|
||||
Name string `json:"name,omitzero"`
|
||||
Id string `json:"id,omitzero"`
|
||||
Driver string `json:"driver,omitzero"`
|
||||
NetworkInterface string `json:"network_interface,omitzero"`
|
||||
Created time.Time `json:"created,omitzero"`
|
||||
Subnets []SubNet `json:"subnets,omitzero"`
|
||||
Ipv6Enabled bool `json:"ipv6_enabled,omitzero"`
|
||||
Internal bool `json:"internal,omitzero"`
|
||||
DnsEnabled bool `json:"dns_enabled,omitzero"`
|
||||
IpamOptions struct {
|
||||
Driver string `json:"driver,omitzero"`
|
||||
} `json:"ipam_options,omitzero"`
|
||||
}
|
||||
type SubNet struct {
|
||||
Subnet string `json:"subnet"`
|
||||
Gateway string `json:"gateway"`
|
||||
}
|
||||
|
||||
func MakerNet(name string, id []string) (err error) {
|
||||
var create CreatNet
|
||||
create.DnsEnabled = true
|
||||
create.Driver = "bridge"
|
||||
create.Name = name
|
||||
b, _ := json.Marshal(&create)
|
||||
log.Println(string(b))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/create", bytes.NewReader(b))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("request for network list error:", err)
|
||||
return
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
str, _ := io.ReadAll(res.Body)
|
||||
log.Println(string(str))
|
||||
}
|
||||
log.Println(id)
|
||||
var c ConnectInfo
|
||||
for _, v := range id {
|
||||
c.ContainerID = v
|
||||
b, _ = json.Marshal(&c)
|
||||
req, _ = http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/"+name+"/connect", bytes.NewReader(b))
|
||||
_, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Delete(ns string) error {
|
||||
req, _ := http.NewRequest(http.MethodDelete, config.Entrypoint+"/v4.0.0/libpod/networks/"+ns, nil)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
}
|
||||
defer res.Body.Close()
|
||||
var e model.ErrInfo
|
||||
json.NewDecoder(res.Body).Decode(&e)
|
||||
if e.Response > 400 {
|
||||
return errors.New(e.Message)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user