Skip to content

Commit a1383eb

Browse files
author
Satish Ramachandran
committed
Incorporate review comments.
1 parent 42005d3 commit a1383eb

File tree

4 files changed

+64
-53
lines changed

4 files changed

+64
-53
lines changed

netctl/commands.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,21 @@ var Commands = []cli.Command{
272272
Name: "ls",
273273
Aliases: []string{"list"},
274274
Usage: "List external contracts",
275-
Flags: []cli.Flag{quietFlag, jsonFlag},
275+
Flags: []cli.Flag{quietFlag, jsonFlag, tenantFlag},
276276
Action: listExternalContracts,
277277
},
278278
{
279279
Name: "rm",
280280
Aliases: []string{"delete"},
281281
Usage: "Delete external contracts",
282+
Flags: []cli.Flag{tenantFlag},
282283
Action: deleteExternalContracts,
283284
},
284285
{
285286
Name: "create",
286287
Usage: "Create external contracts",
287288
Flags: []cli.Flag{
289+
tenantFlag,
288290
cli.BoolFlag{
289291
Name: "consumed, c",
290292
Usage: "External contracts type - consumed",

netctl/netctl.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,8 @@ func listExternalContracts(ctx *cli.Context) {
819819
extContractsGroups, err := getClient(ctx).ExtContractsGroupList()
820820
errCheck(ctx, err)
821821

822+
tenant := ctx.String("tenant")
823+
822824
if ctx.Bool("json") {
823825
dumpJSONList(ctx, extContractsGroups)
824826
} else if ctx.Bool("quiet") {
@@ -831,6 +833,9 @@ func listExternalContracts(ctx *cli.Context) {
831833
writer := tabwriter.NewWriter(os.Stdout, 0, 2, 2, ' ', 0)
832834
defer writer.Flush()
833835
for _, extContractsGroup := range *extContractsGroups {
836+
if extContractsGroup.TenantName != tenant {
837+
continue
838+
}
834839
writer.Write([]byte(fmt.Sprintf(" Name: %s\t\tType: %s\n", extContractsGroup.ContractsGroupName, extContractsGroup.ContractsType)))
835840
writer.Write([]byte(fmt.Sprintf(" Contracts:\n")))
836841
for _, contract := range extContractsGroup.Contracts {
@@ -843,9 +848,10 @@ func listExternalContracts(ctx *cli.Context) {
843848
func deleteExternalContracts(ctx *cli.Context) {
844849
argCheck(1, ctx)
845850
contractsGroupName := ctx.Args()[0]
851+
tenant := ctx.String("tenant")
846852

847-
logrus.Infof("Deleting external contracts group %s", contractsGroupName)
848-
errCheck(ctx, getClient(ctx).ExtContractsGroupDelete(contractsGroupName))
853+
logrus.Infof("Deleting external contracts group %s in tenant %s", contractsGroupName, tenant)
854+
errCheck(ctx, getClient(ctx).ExtContractsGroupDelete(tenant, contractsGroupName))
849855

850856
}
851857

@@ -862,6 +868,8 @@ func createExternalContracts(ctx *cli.Context) {
862868
} else {
863869
errExit(ctx, exitHelp, "Either provided or consumed must be specified", false)
864870
}
871+
872+
tenant := ctx.String("tenant")
865873

866874
contracts := strings.Split(ctx.String("contracts"), ",")
867875
if ctx.String("contracts") == "" {
@@ -871,6 +879,7 @@ func createExternalContracts(ctx *cli.Context) {
871879
contractsGroupName := ctx.Args()[0]
872880

873881
errCheck(ctx, getClient(ctx).ExtContractsGroupPost(&contivClient.ExtContractsGroup{
882+
TenantName: tenant,
874883
ContractsGroupName: contractsGroupName,
875884
ContractsType: contractsType,
876885
Contracts: contracts,

netmaster/objApi/apiController.go

+4-42
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/contiv/netplugin/netmaster/intent"
2323
"github.com/contiv/netplugin/netmaster/master"
2424
"github.com/contiv/netplugin/utils"
25-
"github.com/contiv/netplugin/utils/extContracts"
2625
"github.com/contiv/objdb/modeldb"
2726
"strconv"
2827
"strings"
@@ -344,7 +343,7 @@ func endpointGroupCleanup(endpointGroup *contivModel.EndpointGroup) {
344343
}
345344

346345
// Cleanup any external contracts
347-
err = extContracts.CleanupExternalContracts(endpointGroup)
346+
err = cleanupExternalContracts(endpointGroup)
348347
if err != nil {
349348
log.Errorf("Error cleaning up external contracts for epg %s", endpointGroup.Key)
350349
}
@@ -429,7 +428,7 @@ func (ac *APIController) EndpointGroupCreate(endpointGroup *contivModel.Endpoint
429428
}
430429

431430
// Setup external contracts this EPG might have.
432-
err = extContracts.SetupExternalContracts(endpointGroup, endpointGroup.ConsExtContractsGrps, endpointGroup.ProvExtContractsGrps)
431+
err = setupExternalContracts(endpointGroup, endpointGroup.ConsExtContractsGrps, endpointGroup.ProvExtContractsGrps)
433432
if err != nil {
434433
log.Errorf("Error setting up external contracts for epg %s", endpointGroup.Key)
435434
endpointGroupCleanup(endpointGroup)
@@ -532,13 +531,13 @@ func (ac *APIController) EndpointGroupUpdate(endpointGroup, params *contivModel.
532531
// For the external contracts, we can keep the update simple. Remove
533532
// all that we have now, and update the epg with the new list.
534533
// Step 1: Cleanup existing external contracts.
535-
err := extContracts.CleanupExternalContracts(endpointGroup)
534+
err := cleanupExternalContracts(endpointGroup)
536535
if err != nil {
537536
return err
538537
}
539538
// Step 2: Add contracts from the update.
540539
// Consumed contracts
541-
err = extContracts.SetupExternalContracts(endpointGroup, params.ConsExtContractsGrps, params.ProvExtContractsGrps)
540+
err = setupExternalContracts(endpointGroup, params.ConsExtContractsGrps, params.ProvExtContractsGrps)
542541
if err != nil {
543542
return err
544543
}
@@ -1166,40 +1165,3 @@ func validatePorts(ports []string) bool {
11661165
}
11671166
return true
11681167
}
1169-
1170-
func isExtContractsGroupUsed(contractsGroup *contivModel.ExtContractsGroup) bool {
1171-
if len(contractsGroup.LinkSets.EndpointGroups) > 0 {
1172-
return true
1173-
}
1174-
1175-
return false
1176-
}
1177-
1178-
// ExtContractsGroupCreate creates a new group of external contracts
1179-
func (ac *APIController) ExtContractsGroupCreate(contractsGroup *contivModel.ExtContractsGroup) error {
1180-
log.Infof("Received ExtContractsGroupCreate: %+v", contractsGroup)
1181-
1182-
return nil
1183-
}
1184-
1185-
// ExtContractsGroupUpdate updates an existing group of contract sets
1186-
func (ac *APIController) ExtContractsGroupUpdate(contractsGroup, params *contivModel.ExtContractsGroup) error {
1187-
log.Infof("Received ExtContractsGroupUpdate: %+v, params: %+v", contractsGroup, params)
1188-
log.Errorf("Error: external contracts update not supported: %s", contractsGroup.ContractsGroupName)
1189-
1190-
return core.Errorf("external contracts update not supported")
1191-
}
1192-
1193-
// ExtContractsGroupDelete deletes an existing external contracts group
1194-
func (ac *APIController) ExtContractsGroupDelete(contractsGroup *contivModel.ExtContractsGroup) error {
1195-
log.Infof("Received ExtContractsGroupDelete: %+v", contractsGroup)
1196-
1197-
// At this moment, we let the external contracts to be deleted only
1198-
// if there are no consumers of this external contracts group
1199-
if extContracts.IsExtContractsGroupUsed(contractsGroup) == true {
1200-
log.Errorf("Error: External contracts groups is being used: %s", contractsGroup.ContractsGroupName)
1201-
return core.Errorf("External contracts group is in-use")
1202-
}
1203-
1204-
return nil
1205-
}

utils/extContracts/extContracts.go netmaster/objApi/extContracts.go

+46-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
*/
1515

16-
package extContracts
16+
package objApi
1717

1818
import (
1919
"fmt"
@@ -86,10 +86,12 @@ func extContractsGrpValidateAndRegister(epg *contivModel.EndpointGroup,
8686
}
8787

8888
// Cleanup external contracts from an epg.
89-
func CleanupExternalContracts(endpointGroup *contivModel.EndpointGroup) error {
89+
func cleanupExternalContracts(endpointGroup *contivModel.EndpointGroup) error {
9090
// Cleanup consumed external contracts
91+
tenant := endpointGroup.TenantName
9192
for _, consExtContractsGrp := range endpointGroup.ConsExtContractsGrps {
92-
contractsGrp := contivModel.FindExtContractsGroup(consExtContractsGrp)
93+
contractsGrpKey := tenant + ":" + consExtContractsGrp
94+
contractsGrp := contivModel.FindExtContractsGroup(contractsGrpKey)
9395
err := extContractsGrpDeregister(endpointGroup, contractsGrp, "consumed")
9496
if err != nil {
9597
if contractsGrp != nil {
@@ -101,7 +103,8 @@ func CleanupExternalContracts(endpointGroup *contivModel.EndpointGroup) error {
101103

102104
// Cleanup provided external contracts
103105
for _, provExtContractsGrp := range endpointGroup.ProvExtContractsGrps {
104-
contractsGrp := contivModel.FindExtContractsGroup(provExtContractsGrp)
106+
contractsGrpKey := tenant + ":" + provExtContractsGrp
107+
contractsGrp := contivModel.FindExtContractsGroup(contractsGrpKey)
105108
err := extContractsGrpDeregister(endpointGroup, contractsGrp, "provided")
106109
if err != nil {
107110
if contractsGrp != nil {
@@ -115,11 +118,13 @@ func CleanupExternalContracts(endpointGroup *contivModel.EndpointGroup) error {
115118
}
116119

117120
// Setup external contracts for an epg.
118-
func SetupExternalContracts(endpointGroup *contivModel.EndpointGroup,
121+
func setupExternalContracts(endpointGroup *contivModel.EndpointGroup,
119122
consContractsGrps, provContractsGrps []string) error {
120123
// Validate presence and register consumed external contracts
124+
tenant := endpointGroup.TenantName
121125
for _, consExtContractsGrp := range consContractsGrps {
122-
contractsGrp := contivModel.FindExtContractsGroup(consExtContractsGrp)
126+
contractsGrpKey := tenant + ":" + consExtContractsGrp
127+
contractsGrp := contivModel.FindExtContractsGroup(contractsGrpKey)
123128
err := extContractsGrpValidateAndRegister(endpointGroup, contractsGrp, "consumed")
124129
if err != nil {
125130
return err
@@ -128,7 +133,8 @@ func SetupExternalContracts(endpointGroup *contivModel.EndpointGroup,
128133

129134
// Validate presence and register provided external contracts
130135
for _, provExtContractsGrp := range provContractsGrps {
131-
contractsGrp := contivModel.FindExtContractsGroup(provExtContractsGrp)
136+
contractsGrpKey := tenant + ":" + provExtContractsGrp
137+
contractsGrp := contivModel.FindExtContractsGroup(contractsGrpKey)
132138
err := extContractsGrpValidateAndRegister(endpointGroup, contractsGrp, "provided")
133139
if err != nil {
134140
return err
@@ -139,10 +145,42 @@ func SetupExternalContracts(endpointGroup *contivModel.EndpointGroup,
139145
}
140146

141147
// Check if the external contracts are being used by any of the EPGs.
142-
func IsExtContractsGroupUsed(contractsGroup *contivModel.ExtContractsGroup) bool {
148+
func isExtContractsGroupUsed(contractsGroup *contivModel.ExtContractsGroup) bool {
143149
if len(contractsGroup.LinkSets.EndpointGroups) > 0 {
144150
return true
145151
}
146152

147153
return false
148154
}
155+
156+
// ExtContractsGroupCreate creates a new group of external contracts
157+
func (ac *APIController) ExtContractsGroupCreate(contractsGroup *contivModel.ExtContractsGroup) error {
158+
log.Infof("Received ExtContractsGroupCreate: %+v", contractsGroup)
159+
160+
// NOTE: Nothing more needs to be done here. This object
161+
// need not be created in the masterCfg.
162+
163+
return nil
164+
}
165+
166+
// ExtContractsGroupUpdate updates an existing group of contract sets
167+
func (ac *APIController) ExtContractsGroupUpdate(contractsGroup, params *contivModel.ExtContractsGroup) error {
168+
log.Infof("Received ExtContractsGroupUpdate: %+v, params: %+v", contractsGroup, params)
169+
log.Errorf("Error: external contracts update not supported: %s", contractsGroup.ContractsGroupName)
170+
171+
return core.Errorf("external contracts update not supported")
172+
}
173+
174+
// ExtContractsGroupDelete deletes an existing external contracts group
175+
func (ac *APIController) ExtContractsGroupDelete(contractsGroup *contivModel.ExtContractsGroup) error {
176+
log.Infof("Received ExtContractsGroupDelete: %+v", contractsGroup)
177+
178+
// At this moment, we let the external contracts to be deleted only
179+
// if there are no consumers of this external contracts group
180+
if isExtContractsGroupUsed(contractsGroup) == true {
181+
log.Errorf("Error: External contracts groups is being used: %s", contractsGroup.ContractsGroupName)
182+
return core.Errorf("External contracts group is in-use")
183+
}
184+
185+
return nil
186+
}

0 commit comments

Comments
 (0)