-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dpvs-agent: implementation of ipset api
Signed-off-by: ywc689 <[email protected]>
- Loading branch information
Showing
10 changed files
with
1,910 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2023 IQiYi Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ipset | ||
|
||
import ( | ||
"github.com/dpvs-agent/pkg/ipc/pool" | ||
"github.com/dpvs-agent/pkg/ipc/types" | ||
api "github.com/dpvs-agent/restapi/operations/ipset" | ||
"github.com/go-openapi/runtime/middleware" | ||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
type ipsetGet struct { | ||
connPool *pool.ConnPool | ||
logger hclog.Logger | ||
} | ||
|
||
func NewIpsetGet(cp *pool.ConnPool, parentLogger hclog.Logger) *ipsetGet { | ||
logger := hclog.Default() | ||
if parentLogger != nil { | ||
logger = parentLogger.Named("ipsetGet") | ||
} | ||
return &ipsetGet{connPool: cp, logger: logger} | ||
} | ||
|
||
func (h *ipsetGet) Handle(params api.GetParams) middleware.Responder { | ||
conf := &types.IPSetParam{} | ||
|
||
conf.SetOpcode(types.IPSET_OP_LIST) | ||
conf.SetName(params.Name) | ||
infos, err, derr := conf.Get(h.connPool, h.logger) | ||
if err != nil { | ||
h.logger.Error("Ipset Get failed.", "setName", params.Name, "Reason", err.Error()) | ||
if derr == types.EDPVS_NOTEXIST { | ||
return api.NewGetNotFound().WithPayload(derr.String()) | ||
} | ||
return api.NewGetFailure().WithPayload(err.Error()) | ||
} | ||
|
||
h.logger.Info("Ipset Get succeed", "setName", params.Name) | ||
model, err := infos.Model() | ||
if err != nil { | ||
h.logger.Error("Modelling ipset Get result failed.", "setName", params.Name, "Reason", err.Error()) | ||
} | ||
|
||
resp := api.NewGetOK() | ||
if model.Count > 0 { | ||
resp.SetPayload(model.Infos[0]) | ||
} | ||
return resp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 IQiYi Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ipset | ||
|
||
import ( | ||
"github.com/dpvs-agent/pkg/ipc/pool" | ||
"github.com/dpvs-agent/pkg/ipc/types" | ||
api "github.com/dpvs-agent/restapi/operations/ipset" | ||
"github.com/go-openapi/runtime/middleware" | ||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
type ipsetGetAll struct { | ||
connPool *pool.ConnPool | ||
logger hclog.Logger | ||
} | ||
|
||
func NewIpsetGetAll(cp *pool.ConnPool, parentLogger hclog.Logger) *ipsetGetAll { | ||
logger := hclog.Default() | ||
if parentLogger != nil { | ||
logger = parentLogger.Named("ipsetGetAll") | ||
} | ||
return &ipsetGetAll{connPool: cp, logger: logger} | ||
} | ||
|
||
func (h *ipsetGetAll) Handle(params api.GetAllParams) middleware.Responder { | ||
conf := &types.IPSetParam{} | ||
|
||
conf.SetOpcode(types.IPSET_OP_LIST) | ||
infos, err, _ := conf.Get(h.connPool, h.logger) | ||
if err != nil { | ||
h.logger.Error("Ipset GetAll failed.", "Reason", err.Error()) | ||
return api.NewGetAllFailure().WithPayload(err.Error()) | ||
} | ||
|
||
h.logger.Info("Ipset GetAll succeed") | ||
model, err := infos.Model() | ||
if err != nil { | ||
h.logger.Error("Modelling ipset GetAll result failed.", "Reason", err.Error()) | ||
} | ||
return api.NewGetAllOK().WithPayload(model) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2023 IQiYi Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ipset | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/dpvs-agent/pkg/ipc/pool" | ||
"github.com/dpvs-agent/pkg/ipc/types" | ||
api "github.com/dpvs-agent/restapi/operations/ipset" | ||
"github.com/go-openapi/runtime/middleware" | ||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
type ipsetIsIn struct { | ||
connPool *pool.ConnPool | ||
logger hclog.Logger | ||
} | ||
|
||
func NewIpsetIsIn(cp *pool.ConnPool, parentLogger hclog.Logger) *ipsetIsIn { | ||
logger := hclog.Default() | ||
if parentLogger != nil { | ||
logger = parentLogger.Named("ipsetIsIn") | ||
} | ||
return &ipsetIsIn{connPool: cp, logger: logger} | ||
} | ||
|
||
func (h *ipsetIsIn) Handle(params api.IsInParams) middleware.Responder { | ||
if params.IpsetCell == nil { | ||
return api.NewIsInBadRequest().WithPayload("missing ipset entry") | ||
} | ||
|
||
conf := types.IPSetParam{} | ||
conf.SetOpcode(types.IPSET_OP_TEST) | ||
conf.SetName(params.Name) | ||
conf.SetKind(string(*params.IpsetCell.Type)) | ||
if err := conf.BuildMember(params.IpsetCell.Member); err != nil { | ||
return api.NewIsInBadRequest().WithPayload(fmt.Sprintf("invalid member: %s", err.Error())) | ||
} | ||
|
||
if err := conf.Check(); err != nil { | ||
return api.NewIsInBadRequest().WithPayload(fmt.Sprintf("invalid param: %s", err.Error())) | ||
} | ||
|
||
result, err, derr := conf.IsIn(h.connPool, h.logger) | ||
if err != nil { | ||
h.logger.Error("Ipset IsIn failed.", "setName", params.Name, "Reason", err.Error()) | ||
if derr == types.EDPVS_NOTEXIST { | ||
return api.NewIsInNotFound().WithPayload(derr.String()) | ||
} | ||
return api.NewIsInFailure().WithPayload(err.Error()) | ||
} | ||
h.logger.Info("Ipset InIn succeed.", "setName", params.Name) | ||
|
||
nomatch := "" | ||
if params.IpsetCell.Member.Options != nil && | ||
params.IpsetCell.Member.Options.NoMatch != nil && | ||
*params.IpsetCell.Member.Options.NoMatch { | ||
nomatch = " (nomatch)" | ||
} | ||
|
||
msg := "" | ||
if result { | ||
msg = fmt.Sprintf("%s%s is IN set %s", nomatch, *params.IpsetCell.Member.Entry, params.Name) | ||
} else { | ||
msg = fmt.Sprintf("%s%s is NOT IN set %s", nomatch, *params.IpsetCell.Member.Entry, params.Name) | ||
} | ||
return api.NewIsInOK().WithPayload(&api.IsInOKBody{Result: &result, Message: msg}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.