1
0
Fork 0

proto/infra -> dc/proto

This cleans up proto/!

Change-Id: I3e280c16deef7ad0b01e6e409062831581ec863e
master
q3k 2019-07-21 16:07:49 +02:00
parent 1446e98beb
commit 5b5b7c3d86
8 changed files with 52 additions and 51 deletions

View File

@ -10,8 +10,8 @@ go_library(
visibility = ["//visibility:private"],
deps = [
"//dc/m6220-proxy/proto:go_default_library",
"//dc/proto:go_default_library",
"//go/mirko:go_default_library",
"//proto/infra:go_default_library",
"@com_github_golang_glog//:go_default_library",
"@com_github_ziutek_telnet//:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",

View File

@ -15,7 +15,7 @@ import (
"google.golang.org/grpc/status"
pb "code.hackerspace.pl/hscloud/dc/m6220-proxy/proto"
ipb "code.hackerspace.pl/hscloud/proto/infra"
dpb "code.hackerspace.pl/hscloud/dc/proto"
)
var (
@ -70,7 +70,7 @@ func (s *service) RunCommand(ctx context.Context, req *pb.RunCommandRequest) (*p
return res, nil
}
func (s *service) parseInterfaceStatus(res *ipb.GetPortsResponse, lines []string) error {
func (s *service) parseInterfaceStatus(res *dpb.GetPortsResponse, lines []string) error {
if len(lines) < 4 {
return fmt.Errorf("need at least 4 lines of output, got %d", len(lines))
}
@ -104,20 +104,20 @@ func (s *service) parseInterfaceStatus(res *ipb.GetPortsResponse, lines []string
speedStr := parts[len(parts)-4]
stateStr := parts[len(parts)-2]
port := &ipb.SwitchPort{
port := &dpb.SwitchPort{
Name: portName,
}
if speedStr == "100" {
port.Speed = ipb.SwitchPort_SPEED_100M
port.Speed = dpb.SwitchPort_SPEED_100M
} else if speedStr == "1000" {
port.Speed = ipb.SwitchPort_SPEED_1G
port.Speed = dpb.SwitchPort_SPEED_1G
} else if speedStr == "10000" {
port.Speed = ipb.SwitchPort_SPEED_10G
port.Speed = dpb.SwitchPort_SPEED_10G
}
if stateStr == "Up" {
port.LinkState = ipb.SwitchPort_LINKSTATE_UP
port.LinkState = dpb.SwitchPort_LINKSTATE_UP
} else if stateStr == "Down" {
port.LinkState = ipb.SwitchPort_LINKSTATE_DOWN
port.LinkState = dpb.SwitchPort_LINKSTATE_DOWN
}
res.Ports = append(res.Ports, port)
@ -126,7 +126,7 @@ func (s *service) parseInterfaceStatus(res *ipb.GetPortsResponse, lines []string
return nil
}
func (s *service) parseInterfaceConfig(port *ipb.SwitchPort, lines []string) error {
func (s *service) parseInterfaceConfig(port *dpb.SwitchPort, lines []string) error {
glog.Infof("%+v", port)
for _, line := range lines {
glog.Infof("%s: %q", port.Name, line)
@ -137,23 +137,23 @@ func (s *service) parseInterfaceConfig(port *ipb.SwitchPort, lines []string) err
if len(parts) >= 2 && parts[0] == "switchport" {
if parts[1] == "mode" {
if port.PortMode != ipb.SwitchPort_PORTMODE_INVALID {
if port.PortMode != dpb.SwitchPort_PORTMODE_INVALID {
return fmt.Errorf("redefinition of switchport mode")
}
if parts[2] == "access" {
port.PortMode = ipb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
} else if parts[2] == "trunk" {
port.PortMode = ipb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED
port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED
} else if parts[2] == "general" {
port.PortMode = ipb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC
port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC
} else {
port.PortMode = ipb.SwitchPort_PORTMODE_MANGLED
port.PortMode = dpb.SwitchPort_PORTMODE_MANGLED
}
}
if parts[1] == "access" {
if port.PortMode == ipb.SwitchPort_PORTMODE_INVALID {
port.PortMode = ipb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
if port.PortMode == dpb.SwitchPort_PORTMODE_INVALID {
port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
}
if len(parts) > 3 && parts[2] == "vlan" {
vlan, err := strconv.Atoi(parts[3])
@ -183,34 +183,34 @@ func (s *service) parseInterfaceConfig(port *ipb.SwitchPort, lines []string) err
}
port.Mtu = int32(mtu)
} else if len(parts) >= 2 && parts[0] == "spanning-tree" && parts[1] == "portfast" {
port.SpanningTreeMode = ipb.SwitchPort_SPANNING_TREE_MODE_PORTFAST
port.SpanningTreeMode = dpb.SwitchPort_SPANNING_TREE_MODE_PORTFAST
}
}
// no mode -> access
if port.PortMode == ipb.SwitchPort_PORTMODE_INVALID {
port.PortMode = ipb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
if port.PortMode == dpb.SwitchPort_PORTMODE_INVALID {
port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
}
// apply defaults
if port.Mtu == 0 {
port.Mtu = 1500
}
if port.SpanningTreeMode == ipb.SwitchPort_SPANNING_TREE_MODE_INVALID {
port.SpanningTreeMode = ipb.SwitchPort_SPANNING_TREE_MODE_AUTO_PORTFAST
if port.SpanningTreeMode == dpb.SwitchPort_SPANNING_TREE_MODE_INVALID {
port.SpanningTreeMode = dpb.SwitchPort_SPANNING_TREE_MODE_AUTO_PORTFAST
}
// sanitize
if port.PortMode == ipb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED {
if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED {
port.VlanTagged = []int32{}
port.Prefixes = []string{}
if port.VlanNative == 0 {
port.VlanNative = 1
}
} else if port.PortMode == ipb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED {
} else if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED {
port.VlanNative = 0
port.Prefixes = []string{}
} else if port.PortMode == ipb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC {
} else if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC {
port.Prefixes = []string{}
if port.VlanNative == 0 {
port.VlanNative = 1
@ -219,13 +219,13 @@ func (s *service) parseInterfaceConfig(port *ipb.SwitchPort, lines []string) err
return nil
}
func (s *service) GetPorts(ctx context.Context, req *ipb.GetPortsRequest) (*ipb.GetPortsResponse, error) {
func (s *service) GetPorts(ctx context.Context, req *dpb.GetPortsRequest) (*dpb.GetPortsResponse, error) {
cli, err := s.connect()
if err != nil {
return nil, status.Error(codes.Unavailable, "could not connect to switch")
}
defer s.disconnect()
res := &ipb.GetPortsResponse{}
res := &dpb.GetPortsResponse{}
statusLines, _, err := cli.runCommand(ctx, "show interface status")
if err != nil {
@ -267,7 +267,7 @@ func main() {
}
pb.RegisterM6220ProxyServer(m.GRPC(), s)
ipb.RegisterSwitchControlServer(m.GRPC(), s)
dpb.RegisterSwitchControlServer(m.GRPC(), s)
if err := m.Serve(); err != nil {
glog.Exitf("Serve(): %v", err)

View File

@ -2,22 +2,22 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
proto_library(
name = "infra_proto",
srcs = ["control.proto"],
name = "proto_proto",
srcs = ["dc.proto"],
visibility = ["//visibility:public"],
)
go_proto_library(
name = "infra_go_proto",
name = "proto_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "code.hackerspace.pl/hscloud/proto/infra",
proto = ":infra_proto",
importpath = "code.hackerspace.pl/hscloud/dc/proto",
proto = ":proto_proto",
visibility = ["//visibility:public"],
)
go_library(
name = "go_default_library",
embed = [":infra_go_proto"],
importpath = "code.hackerspace.pl/hscloud/proto/infra",
embed = [":proto_go_proto"],
importpath = "code.hackerspace.pl/hscloud/dc/proto",
visibility = ["//visibility:public"],
)

View File

@ -1,5 +1,6 @@
syntax = "proto3";
package infra;
package dc;
option go_package = "code.hackerspace.pl/hscloud/dc/proto";
message GetPortsRequest {
};

View File

@ -9,13 +9,13 @@ go_library(
importpath = "code.hackerspace.pl/hscloud/dc/topo",
visibility = ["//visibility:private"],
deps = [
"//dc/proto:go_default_library",
"//dc/topo/assets:go_default_library",
"//dc/topo/graph:go_default_library",
"//dc/topo/proto:go_default_library",
"//dc/topo/state:go_default_library",
"//go/mirko:go_default_library",
"//go/statusz:go_default_library",
"//proto/infra:go_default_library",
"@com_github_digitalocean_go_netbox//netbox:go_default_library",
"@com_github_digitalocean_go_netbox//netbox/client:go_default_library",
"@com_github_golang_glog//:go_default_library",

View File

@ -11,8 +11,8 @@ import (
"code.hackerspace.pl/hscloud/go/mirko"
"code.hackerspace.pl/hscloud/go/statusz"
ipb "code.hackerspace.pl/hscloud/proto/infra"
dpb "code.hackerspace.pl/hscloud/dc/proto"
"code.hackerspace.pl/hscloud/dc/topo/assets"
"code.hackerspace.pl/hscloud/dc/topo/graph"
"code.hackerspace.pl/hscloud/dc/topo/state"
@ -152,22 +152,22 @@ func (s *Service) statusHandleSwitchports(ctx context.Context) interface{} {
for _, po := range sw.Ports {
state := "INVALID"
switch po.Proto.LinkState {
case ipb.SwitchPort_LINKSTATE_DOWN:
case dpb.SwitchPort_LINKSTATE_DOWN:
state = "DOWN"
case ipb.SwitchPort_LINKSTATE_UP:
case dpb.SwitchPort_LINKSTATE_UP:
state = "UP"
}
mode := "INVALID"
switch po.Proto.PortMode {
case ipb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED:
case dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED:
mode = fmt.Sprintf("UNTAGGED (%d)", po.Proto.VlanNative)
case ipb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED:
case dpb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED:
mode = fmt.Sprintf("TAGGED (%v)", po.Proto.VlanTagged)
case ipb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC:
case dpb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC:
mode = "GENERIC"
case ipb.SwitchPort_PORTMODE_ROUTED:
case dpb.SwitchPort_PORTMODE_ROUTED:
mode = "ROUTED"
case ipb.SwitchPort_PORTMODE_MANGLED:
case dpb.SwitchPort_PORTMODE_MANGLED:
mode = "MANGLED"
}

View File

@ -6,9 +6,9 @@ go_library(
importpath = "code.hackerspace.pl/hscloud/dc/topo/state",
visibility = ["//visibility:public"],
deps = [
"//dc/proto:go_default_library",
"//dc/topo/proto:go_default_library",
"//go/pki:go_default_library",
"//proto/infra:go_default_library",
"@org_golang_google_grpc//:go_default_library",
],
)

View File

@ -7,24 +7,24 @@ import (
"google.golang.org/grpc"
dpb "code.hackerspace.pl/hscloud/dc/proto"
"code.hackerspace.pl/hscloud/go/pki"
ipb "code.hackerspace.pl/hscloud/proto/infra"
pb "code.hackerspace.pl/hscloud/dc/topo/proto"
)
type SwitchportState struct {
Proto *ipb.SwitchPort
Proto *dpb.SwitchPort
}
type SwitchState struct {
Name string
Ports []*SwitchportState
Stub ipb.SwitchControlClient
Stub dpb.SwitchControlClient
}
func (s *SwitchState) Fetch(ctx context.Context) error {
req := ipb.GetPortsRequest{}
req := dpb.GetPortsRequest{}
res, err := s.Stub.GetPorts(ctx, &req)
if err != nil {
return fmt.Errorf("GetPorts: %v", err)
@ -65,7 +65,7 @@ func (s *StateManager) FetchState(ctx context.Context, conf *pb.Config) error {
s.Switches[sw.Name] = &SwitchState{
Name: sw.Name,
Stub: ipb.NewSwitchControlClient(conn),
Stub: dpb.NewSwitchControlClient(conn),
}
err := s.Switches[sw.Name].Fetch(ctx)
if err != nil {