Skip to content

Commit

Permalink
fix network API
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Nov 7, 2024
1 parent dfe0fb5 commit b27388b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 200 deletions.
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"request": "launch",
"mode": "exec",
"program": "/usr/bin/calaos-container",
"args": [ ],
"substitutePath": [],
"port": 8888,
"host": "192.168.30.162"
}
]
}
30 changes: 27 additions & 3 deletions cmd/calaos-os/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type apiOptions struct {
transport func(*http.Transport)
}

type ApiResponse struct {
Error bool `json:"error"`
Msg string `json:"msg"`
Output json.RawMessage `json:"output"`
}

// Optional parameter, used to configure timeouts on API calls.
func SetTimeout(timeout time.Duration) func(*apiOptions) {
return func(opts *apiOptions) {
Expand Down Expand Up @@ -273,13 +279,22 @@ func (capi *CalaosApi) UpgradeStatus(token string) (status *structs.Status, err

// GetNetworkInterfaces returns a list of network interfaces with their status
func (capi *CalaosApi) GetNetworkInterfaces(token string) (netf *[]structs.NetInterface, err error) {
_, body, err := capi.callWithToken("GET", "/network/list", token, nil, nil)
_, body, err := capi.callWithToken("GET", "/api/network/list", token, nil, nil)
if err != nil {
return
}

var initial ApiResponse
if err = json.Unmarshal(body, &initial); err != nil {
return nil, fmt.Errorf("GetNetworkInterfaces failed: %v", err)
}

if initial.Error {
return nil, fmt.Errorf("GetNetworkInterfaces failed: %v", initial.Msg)
}

netf = &[]structs.NetInterface{}
if err = json.Unmarshal(body, netf); err != nil {
if err = json.Unmarshal(initial.Output, netf); err != nil {
return nil, fmt.Errorf("GetNetworkInterfaces failed: %v", err)
}

Expand All @@ -292,10 +307,19 @@ func (capi *CalaosApi) ConfigureNetworkInterface(token string, intf string, conf
"intf": []string{intf},
}

_, _, err = capi.callWithToken("POST", "/network/"+intf, token, params, config)
_, body, err := capi.callWithToken("POST", "/api/network/"+intf, token, params, config)
if err != nil {
return
}

var initial ApiResponse
if err = json.Unmarshal(body, &initial); err != nil {
return fmt.Errorf("ConfigureNetworkInterface failed: %v", err)
}

if initial.Error {
return fmt.Errorf("ConfigureNetworkInterface failed: %v", initial.Msg)
}

return
}
Binary file added cmd/calaos-os/calaos-os
Binary file not shown.
21 changes: 11 additions & 10 deletions cmd/calaos-os/calaos-os.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,13 @@ func cmdNetList(cmd *cli.Cmd) {

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Interface", "IP", "Netmask", "Gateway", "MAC", "State", "DHCP"})
t.AppendHeader(table.Row{"Interface", "IP", "Gateway", "MAC", "State", "DHCP"})

for _, e := range *nets {
if e.IsLoopback {
continue
}

t.AppendRow(table.Row{
e.Name,
e.IPv4,
Expand All @@ -247,7 +251,7 @@ func cmdNetList(cmd *cli.Cmd) {
}

func cmdNetConfigureStatic(cmd *cli.Cmd) {
cmd.Spec = "INTERFACE IPV4 NETMASK GATEWAY DNS..."
cmd.Spec = "INTERFACE IPV4 NETMASK [GATEWAY] [DNS...]"
intf := cmd.StringArg("INTERFACE", "", "Interface to configure")
ip := cmd.StringArg("IPV4", "", "IPv4 address")
netmask := cmd.StringArg("NETMASK", "", "Netmask")
Expand All @@ -264,17 +268,14 @@ func cmdNetConfigureStatic(cmd *cli.Cmd) {
exit(err, 1)
}

dnsconf := &structs.DNSConfig{}
dnsconf.DNSServers = append(dnsconf.DNSServers, *dns...)

ipCIDR, _ := toCIDR(*ip, *netmask)
config := &structs.NetInterface{
Name: *intf,
IPv4: ipCIDR,
Gateway: *gateway,
DHCP: false,
DNSConfig: dnsconf,
Name: *intf,
IPv4: ipCIDR,
Gateway: *gateway,
DHCP: false,
}
config.DNSServers = append(config.DNSServers, *dns...)

err = a.ConfigureNetworkInterface(token, *intf, config)
if err != nil {
Expand Down
Loading

0 comments on commit b27388b

Please sign in to comment.