Skip to content

Commit

Permalink
Extend port group API to support: (#129)
Browse files Browse the repository at this point in the history
- PortGroupAddPort() - Add a port to a port group
- PortGroupRemovePort() - Remove a port from a port group

Signed-off-by: Andre Fredette <[email protected]>
  • Loading branch information
anfredette authored Feb 3, 2021
1 parent b96f6c1 commit 8bb6a08
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
14 changes: 13 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,14 @@ type Client interface {
// Get SB_Global table options
SBGlobalGetOptions() (map[string]string, error)

// Creates a new port group in the Port_Group table named "group" with optional "ports" added to the group.
// Creates a new port group in the Port_Group table named "group" with optional "ports" and "external_ids".
PortGroupAdd(group string, ports []string, external_ids map[string]string) (*OvnCommand, error)
// Sets "ports" and/or "external_ids" on the port group named "group". It is an error if group does not exist.
PortGroupUpdate(group string, ports []string, external_ids map[string]string) (*OvnCommand, error)
// Add port to port group.
PortGroupAddPort(group string, port string) (*OvnCommand, error)
// Remove port from port group.
PortGroupRemovePort(group string, port string) (*OvnCommand, error)
// Deletes port group "group". It is an error if "group" does not exist.
PortGroupDel(group string) (*OvnCommand, error)
// Get PortGroup data structure if it exists
Expand Down Expand Up @@ -710,6 +714,14 @@ func (c *ovndb) PortGroupUpdate(group string, ports []string, external_ids map[s
return c.pgUpdateImp(group, ports, external_ids)
}

func (c *ovndb) PortGroupAddPort(group string, port string) (*OvnCommand, error) {
return c.pgAddPortImp(group, port)
}

func (c *ovndb) PortGroupRemovePort(group string, port string) (*OvnCommand, error) {
return c.pgRemovePortImp(group, port)
}

func (c *ovndb) PortGroupDel(group string) (*OvnCommand, error) {
return c.pgDelImp(group)
}
Expand Down
2 changes: 2 additions & 0 deletions ovnimp.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var (
ErrorExist = errors.New("object exist")
// ErrorNoChanges used when function called, but no changes
ErrorNoChanges = errors.New("no changes requested")
// ErrorDuplicateName used when multiple rows are found when searching by name
ErrorDuplicateName = errors.New("duplicate name")
)

// OVNRow ovn nb/sb row
Expand Down
71 changes: 70 additions & 1 deletion port_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,76 @@ func (odbi *ovndb) pgUpdateImp(group string, ports []string, external_ids map[st
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}

func (odbi *ovndb) pgAddPortImp(group, port string) (*OvnCommand, error) {
if pg, err := odbi.pgGetImp(group); err == nil {
for _, p := range pg.Ports {
if p == port {
return nil, ErrorExist
}
}
} else {
return nil, err
}

mutateUUID := []libovsdb.UUID{stringToGoUUID(port)}
mutateSet, err := libovsdb.NewOvsSet(mutateUUID)
if err != nil {
return nil, err
}

mutation := libovsdb.NewMutation("ports", opInsert, mutateSet)
condition := libovsdb.NewCondition("name", "==", group)

mutateOp := libovsdb.Operation{
Op: opMutate,
Table: TablePortGroup,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{mutateOp}
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}

func (odbi *ovndb) pgRemovePortImp(group string, port string) (*OvnCommand, error) {
if pg, err := odbi.pgGetImp(group); err == nil {
portFound := false
for _, p := range pg.Ports {
if p == port {
portFound = true
break
}
}
if portFound != true {
return nil, ErrorNotFound
}
} else {
return nil, err
}

mutateUUID := []libovsdb.UUID{stringToGoUUID(port)}
mutateSet, err := libovsdb.NewOvsSet(mutateUUID)
if err != nil {
return nil, err
}

mutation := libovsdb.NewMutation("ports", opDelete, mutateSet)
condition := libovsdb.NewCondition("name", "==", group)

mutateOp := libovsdb.Operation{
Op: opMutate,
Table: TablePortGroup,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{mutateOp}
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}

func (odbi *ovndb) pgDelImp(group string) (*OvnCommand, error) {
if _, err := odbi.pgGetImp(group); err != nil {
return nil, err
}

condition := libovsdb.NewCondition("name", "==", group)
deleteOp := libovsdb.Operation{
Op: opDelete,
Expand Down Expand Up @@ -147,7 +216,7 @@ func (odbi *ovndb) pgGetImp(pg string) (*PortGroup, error) {
if len(pgList) == 0 {
return nil, ErrorNotFound
} else if len(pgList) != 1 {
return nil, ErrorSchema
return nil, ErrorDuplicateName
} else {
return pgList[0], nil
}
Expand Down
57 changes: 57 additions & 0 deletions port_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,63 @@ func TestPortGroupAPI(t *testing.T) {
assert.Nil(err)
})

t.Run("add/delete ports to/from port group", func(t *testing.T) {
ports := []string{lsp1UUID}

// Add the port group
cmd, err = ovndbapi.PortGroupAdd(PG_TEST_PG1, ports, nil)
assert.Nil(err)
err = ovndbapi.Execute(cmd)
assert.Nil(err)

cmd, err = ovndbapi.PortGroupAddPort(PG_TEST_PG1, lsp2UUID)
assert.Nil(err)
err = ovndbapi.Execute(cmd)
assert.Nil(err)

// Validate port group
ports = append(ports, lsp2UUID)
pg, err := ovndbapi.PortGroupGet(PG_TEST_PG1)
assert.Nil(err)
assert.NotNil(pg)
sort.Strings(ports)
sort.Strings(pg.Ports)
assert.True(reflect.DeepEqual(ports, pg.Ports))

// Add duplicate port
cmd, err = ovndbapi.PortGroupAddPort(PG_TEST_PG1, lsp2UUID)
assert.Equal(ErrorExist, err)

// Add port to non-existent port group
cmd, err = ovndbapi.PortGroupAddPort(PG_TEST_PG2, lsp1UUID)
assert.Equal(ErrorNotFound, err)

// Remove lsp2 from port group
cmd, err = ovndbapi.PortGroupRemovePort(PG_TEST_PG1, lsp2UUID)
assert.Nil(err)
err = ovndbapi.Execute(cmd)
assert.Nil(err)
pg, err = ovndbapi.PortGroupGet(PG_TEST_PG1)
assert.Nil(err)
ports = []string{lsp1UUID}
assert.True(reflect.DeepEqual(ports, pg.Ports))

// Remove lsp1 from port group
cmd, err = ovndbapi.PortGroupRemovePort(PG_TEST_PG1, lsp1UUID)
assert.Nil(err)
err = ovndbapi.Execute(cmd)
assert.Nil(err)
pg, err = ovndbapi.PortGroupGet(PG_TEST_PG1)
assert.Nil(err)
assert.True(len(pg.Ports) == 0)

// Delete the port group
cmd, err = ovndbapi.PortGroupDel(PG_TEST_PG1)
assert.Nil(err)
err = ovndbapi.Execute(cmd)
assert.Nil(err)
})

t.Run("set port group that doesn't exist", func(t *testing.T) {
cmd, err = ovndbapi.PortGroupUpdate(PG_TEST_PG1, []string{lsp1UUID, lsp2UUID}, nil)
assert.NotNil(err)
Expand Down

0 comments on commit 8bb6a08

Please sign in to comment.