Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from onmetal/osc-main
Browse files Browse the repository at this point in the history
Full implementation
  • Loading branch information
guvenc authored Jul 3, 2023
2 parents 8b08e26 + cdcc569 commit 8e36ce4
Show file tree
Hide file tree
Showing 109 changed files with 5,706 additions and 1,485 deletions.
36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
// 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": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
//"program": "${fileDirname}",
//"args": ["add", "loadbalancer", "--id=4", "--lbports", "tcp/389", "--vip", "10.3.4.5", "--vni", "100"]
//"args": ["delete", "loadbalancer", "--id=4"]
//"args": ["add", "route", "--prefix=10.100.3.0/24", "--next-hop-vni=0", "--next-hop-ip=fc00:2::64:0:1", "--vni=100"]
//"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"]
//"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"]
//"args": ["add", "lbtarget", "--target-ip=ff80::5", "--lb-id=1"]
//"args": ["get", "lbtarget", "--lb-id=4"]
//"args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--info-type=0"]
//"args": ["get", "fwrule", "vm1", "--rule-id=4"]
//"args": ["add", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0",
// "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"]
//"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"]
//"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"]
"args": ["add", "-f", "/tmp/int.json"]
//"args": ["delete", "-f", "/tmp/vip.yaml"]
//"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=11"]
//"args": ["list", "interfaces", "--wide"]
//"args": ["get", "vni", "--vni=100", "--vni-type=0"]
//"args": ["list", "prefixes","--interface-id=vm1"]
//"args": ["list", "lbtargets","--lb-id=4"]
}
]
}
63 changes: 56 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# dpservice-cli

CLI for [net-dpservice](https://github.com/onmetal/net-dpservice).
Command-line tool for debugging over gRPC for [net-dpservice](https://github.com/onmetal/net-dpservice).

## Installation
This tool connects directly to a running dp-service and communicates with it (orchestrates it).
<br />

## Installation and developing

To build the CLI binary, run

Expand All @@ -19,24 +22,70 @@ run
make install
```

For more details about developing refer to documentation folder [docs](/docs/development/README.md)
<br />

## Autocompletion

To generate autocompletion use:

```shell
dpservice-cli completion [bash|zsh|fish|powershell]
```

Or use -h to get more info and examples for specific shell:

```shell
dpservice-cli completion -h
```
<br />

## Usage

Each command or subcommand has help that can be viewed with -h or --help flag.
```shell
dpservice-cli --help
```
```bash
Usage:
dpservice-cli [flags]
dpservice-cli [command]

Available Commands:
completion Generate the autocompletion script for the specified shell
create Creates one of [interface prefix route virtualip]
delete Deletes one of [interface prefix route virtualip]
get Gets/Lists one of [interface prefix route virtualip]
add Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule]
completion Generate completion script
delete Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule]
get Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule]
help Help about any command
init Initial set up of the DPDK app
initialized Indicates if the DPDK app has been initialized already
list Lists one of [firewallrules interfaces prefixes lbprefixes routes]

Flags:
--address string net-dpservice address. (default "localhost:1337")
--connect-timeout duration Timeout to connect to the net-dpservice. (default 4s)
-h, --help help for dpservice-cli
-o, --output string Output format. [json|yaml|table|name]
--pretty Whether to render pretty output.
-w, --wide Whether to render more info in table output.

Use "dpservice-cli [command] --help" for more information about a command.
```
```
---
Add and Delete commands also support file input with **-f, --filename** flag:
```bash
dpservice-cli [add|delete] -f /<path>/<filename>.[json|yaml]
```
Filename, directory, or URL can be used.
One file can contain multiple objects of any kind, example file:
```bash
{"kind":"VirtualIP","metadata":{"interfaceID":"vm1"},"spec":{"ip":"20.20.20.20"}}
{"kind":"VirtualIP","metadata":{"interfaceID":"vm2"},"spec":{"ip":"20.20.20.21"}}
{"kind":"Prefix","metadata":{"interfaceID":"vm3"},"spec":{"prefix":"20.20.20.0/24"}}
{"kind":"LoadBalancer","metadata":{"id":"4"},"spec":{"vni":100,"lbVipIP":"10.20.30.40","lbports":[{"protocol":6,"port":443},{"protocol":17,"port":53}]}}
{"kind":"LoadBalancerPrefix","metadata":{"interfaceID":"vm1"},"spec":{"prefix":"10.10.10.0/24"}}
```
All commands can be found in [docs](/docs/commands/dpservice-cli.md)
<br />
47 changes: 32 additions & 15 deletions cmd/create.go → cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ import (
"context"
"fmt"
"os"
"reflect"

"github.com/onmetal/dpservice-go-library/dpdk/api"
"github.com/onmetal/dpservice-go-library/dpdk/client/dynamic"
"github.com/onmetal/dpservice-go-library/sources"
"github.com/onmetal/dpservice-cli/dpdk/client/dynamic"
"github.com/onmetal/dpservice-cli/dpdk/runtime"
"github.com/onmetal/dpservice-cli/sources"
"github.com/onmetal/net-dpservice-go/errors"
"github.com/spf13/cobra"
)

func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command {
func Add(factory DPDKClientFactory) *cobra.Command {
rendererOptions := &RendererOptions{Output: "name"}
sourcesOptions := &SourcesOptions{}

cmd := &cobra.Command{
Use: "create",
Use: "add",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
return RunCreate(ctx, dpdkClientFactory, rendererOptions, sourcesOptions)
return RunAdd(ctx, factory, rendererOptions, sourcesOptions)
},
}

Expand All @@ -43,10 +45,16 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command {
sourcesOptions.AddFlags(cmd.Flags())

subcommands := []*cobra.Command{
CreateInterface(dpdkClientFactory, rendererOptions),
CreatePrefix(dpdkClientFactory, rendererOptions),
CreateRoute(dpdkClientFactory, rendererOptions),
CreateVirtualIP(dpdkClientFactory, rendererOptions),
CreateInterface(factory, rendererOptions),
AddPrefix(factory, rendererOptions),
AddRoute(factory, rendererOptions),
AddVirtualIP(factory, rendererOptions),
CreateLoadBalancer(factory, rendererOptions),
CreateLoadBalancerPrefix(factory, rendererOptions),
AddLoadBalancerTarget(factory, rendererOptions),
AddNat(factory, rendererOptions),
AddNeighborNat(factory, rendererOptions),
AddFirewallRule(factory, rendererOptions),
}

cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands))
Expand All @@ -59,7 +67,7 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command {
return cmd
}

func RunCreate(
func RunAdd(
ctx context.Context,
dpdkClientFactory DPDKClientFactory,
rendererFactory RendererFactory,
Expand Down Expand Up @@ -87,17 +95,26 @@ func RunCreate(
return fmt.Errorf("error creating sources iterator: %w", err)
}

objs, err := sources.CollectObjects(iterator, api.DefaultScheme)
objs, err := sources.CollectObjects(iterator, runtime.DefaultScheme)
if err != nil {
return fmt.Errorf("error collecting objects: %w", err)
}

for _, obj := range objs {
if err := dc.Create(ctx, obj); err != nil {
fmt.Printf("Error creating %T: %v\n", obj, err)
res, err := dc.Create(ctx, obj)
if err == errors.ErrServerError {
r := reflect.ValueOf(res)
err := reflect.Indirect(r).FieldByName("Status").FieldByName("Error")
msg := reflect.Indirect(r).FieldByName("Status").FieldByName("Message")
fmt.Printf("Error adding %T: Server error: %v %v\n", res, err, msg)
continue
}
if err != nil {
fmt.Printf("Error adding %T: %v\n", obj, err)
continue
}

if err := renderer.Render(obj); err != nil {
if err := renderer.Render(res); err != nil {
return fmt.Errorf("error rendering %T: %w", obj, err)
}
}
Expand Down
Loading

0 comments on commit 8e36ce4

Please sign in to comment.