68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package network
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"floares/config"
|
|
"floares/lib/model"
|
|
"fmt"
|
|
"github.com/go-jose/go-jose/v3/json"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func DisConnect(ns string, id string) (err error) {
|
|
reader := strings.NewReader(fmt.Sprintf(`{
|
|
"container":"%s"
|
|
}`, id))
|
|
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/networks/"+ns+"/disconnect", reader)
|
|
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))
|
|
err = errors.New(res.Status)
|
|
|
|
}
|
|
return
|
|
}
|
|
|
|
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)
|
|
return
|
|
}
|
|
if res.StatusCode != 200 {
|
|
str, _ := io.ReadAll(res.Body)
|
|
log.Println(string(str))
|
|
err = errors.New(res.Status)
|
|
|
|
}
|
|
return
|
|
}
|