Skip to content

Commit

Permalink
Implemented acl_mac and mac_comment. (#2)
Browse files Browse the repository at this point in the history
* Implemented acl_mac and mac_comment.

* isort.
  • Loading branch information
dzhuang authored Feb 14, 2024
1 parent ac6e618 commit 0adeefa
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 13 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ikuai_client = IKuaiClient(

```python

>>> ikuai_client.get_mac_groups() # get the mac_groups configured
>>> ikuai_client.list_mac_groups() # get the mac_groups configured
{'total': 2,
'data': [{'id': 1,
'comment': '',
Expand Down Expand Up @@ -58,14 +58,14 @@ ikuai_client = IKuaiClient(

The functionality related to behavioral control of devices connected, via protocol.
The implemented methods include
`add_acl_l7`, `get_acl_l7`, `edit_acl_l7`, `del_acl_l7`, `disable_acl_l7`, `enable_acl_l7`.
`add_acl_l7`, `list_acl_l7`, `edit_acl_l7`, `del_acl_l7`, `disable_acl_l7`, `enable_acl_l7`.


### domain_blacklist

The functionality related to behavioral control of devices connected, via domain blacklist.
The implemented methods include
`add_domain_blacklist`, `get_domain_blacklist`, `edit_domain_blacklist`, `del_domain_blacklist`,
`add_domain_blacklist`, `list_domain_blacklist`, `edit_domain_blacklist`, `del_domain_blacklist`,
`disable_domain_blacklist`, `enable_domain_blacklist`


Expand Down Expand Up @@ -117,5 +117,13 @@ Get the system statistics.

### monitor_lanip & monitor_lanipv6

Get the monitor list of lanip (v4) or lanipv6. For v4, use ``ikuai_client.get_monitor_lanip()``. For v6, use ``ikuai_client.get_monitor_lanip(ip_type='v6')`` .
Get the monitor list of lanip (v4) or lanipv6. For v4, use ``ikuai_client.list_monitor_lanip()``. For v6, use ``ikuai_client.list_monitor_lanip(ip_type='v6')`` .


### acl_mac

The access control of device via mac address. The implemented methods include: `add_acl_mac`, `list_acl_mac`, `edit_acl_mac`, `del_acl_mac`, `enable_acl_mac`, `disable_acl_mac`.

### mac_comment

The management of alias of devices, mapping mac addresses. The implemented methods include: `add_mac_comment`, `list_mac_comment`, `edit_mac_comment`, `del_mac_comment`.
22 changes: 21 additions & 1 deletion src/pyikuai/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ class rp_action: # noqa

class rp_func_name: # noqa
sysstat = "sysstat"

macgroup = "macgroup"
acl_l7 = "acl_l7"
mac_comment = "mac_comment"

domain_blacklist = "domain_blacklist"
monitor_lanip = "monitor_lanip"
monitor_lanipv6 = "monitor_lanipv6"

acl_l7 = "acl_l7"
acl_mac = "acl_mac"


class rp_order_param: # noqa
asc = "asc"
Expand Down Expand Up @@ -73,3 +78,18 @@ class domain_blacklist_param: # noqa
ipaddr = "ipaddr"
time = "time"
weekdays = "weekdays"


class mac_comment_param: # noqa
id = "id"
mac = "mac"
comment = "comment"


class acl_mac_param: # noqa
id = "id"
comment = "comment"
enabled = "enabled"
mac = "mac"
time = "time"
week = "week"
168 changes: 160 additions & 8 deletions src/pyikuai/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

from .constants import (JSON_RESPONSE_DATA, JSON_RESPONSE_ERRMSG,
JSON_RESPONSE_ERRMSG_SUCCESS, JSON_RESPONSE_RESULT,
acl_l7_param, acl_l7_param_action,
acl_l7_param, acl_l7_param_action, acl_mac_param,
domain_blacklist_param, json_result_code,
mac_group_param, rp_action, rp_func_name, rp_key,
rp_order_param)
mac_comment_param, mac_group_param, rp_action,
rp_func_name, rp_key, rp_order_param)
from .exceptions import (AuthenticationError, RequestError, RouterAPIError,
ValidationError)

Expand Down Expand Up @@ -100,7 +100,7 @@ def authenticate(self):
f"{content[JSON_RESPONSE_RESULT]}: {content[JSON_RESPONSE_ERRMSG]}."
)

def get_protocols_json(self):
def list_protocols_json(self):
response = self.session.get(
urljoin(self.base_url, "json/protocols_cn.json"), headers={
"Content-Type": 'application/json'
Expand Down Expand Up @@ -188,7 +188,7 @@ def add_mac_group(self, group_name, addr_pools, comments=None):
}
)

def get_mac_groups(self, **query_kwargs):
def list_mac_groups(self, **query_kwargs):
result = self.exec(
func_name=rp_func_name.macgroup,
action=rp_action.show,
Expand Down Expand Up @@ -268,7 +268,7 @@ def add_acl_l7(self, comment, src_addrs, action, dst_addrs=None,
param=param
)

def get_acl_l7(self, **query_kwargs):
def list_acl_l7(self, **query_kwargs):
result = self.exec(
func_name=rp_func_name.acl_l7,
action=rp_action.show,
Expand Down Expand Up @@ -350,7 +350,7 @@ def _get_domain_blacklist_param(
}
return param

def get_domain_blacklist(self, **query_kwargs):
def list_domain_blacklist(self, **query_kwargs):
result = self.exec(
func_name=rp_func_name.domain_blacklist,
action=rp_action.show,
Expand Down Expand Up @@ -440,7 +440,7 @@ def get_sysstat(self, param_types=None):
)
return result[JSON_RESPONSE_DATA]

def get_monitor_lanip(self, ip_type="v4", **query_kwargs):
def list_monitor_lanip(self, ip_type="v4", **query_kwargs):
assert ip_type in ["v4", "v6"], "ip_type must be 'v4' or 'v6'"

result = self.exec(
Expand All @@ -450,3 +450,155 @@ def get_monitor_lanip(self, ip_type="v4", **query_kwargs):
param=QueryRPParam(**query_kwargs).as_dict()
)
return result[JSON_RESPONSE_DATA]

# {{{ mac_comment CRUD
# 行为管控 之 终端名称管理

def list_mac_comment(self, **query_kwargs):
# mac comment will override device ip info comment
result = self.exec(
func_name=rp_func_name.mac_comment,
action=rp_action.show,
param=QueryRPParam(**query_kwargs).as_dict()
)
return result[JSON_RESPONSE_DATA]

def del_mac_comment(self, mac_comment_id):
return self.exec(
func_name=rp_func_name.mac_comment,
action=rp_action.delete,
param={
mac_comment_param.id: mac_comment_id,
}
)

def add_mac_comment(self, mac, comment):
return self.exec(
func_name=rp_func_name.mac_comment,
action=rp_action.add,
param={
mac_comment_param.mac: mac,
mac_comment_param.comment: comment
}
)

def edit_mac_comment(self, mac_comment_id, mac, comment):
return self.exec(
func_name=rp_func_name.mac_comment,
action=rp_action.edit,
param={
mac_comment_param.id: mac_comment_id,
mac_comment_param.mac: mac,
mac_comment_param.comment: comment
}
)

# }}}

# {{{ acl_mac CRUD
# 行为管控 之 MAC访问控制

def _get_acl_mac_param(
self,
mac,
enabled=True,
time="00:00-23:59",
comment=None,
week="1234567"):

self.validate_time_range(time)

enabled = "yes" if enabled else "no"
comment = comment or ""
comment = comment.replace(" ", quote(" "))

param = {
acl_mac_param.mac: mac,
acl_mac_param.comment: comment,
acl_mac_param.enabled: enabled,
acl_mac_param.time: time,
acl_mac_param.week: week
}
return param

def list_acl_mac(self, **query_kwargs):
result = self.exec(
func_name=rp_func_name.acl_mac,
action=rp_action.show,
param=QueryRPParam(**query_kwargs).as_dict()
)
return result[JSON_RESPONSE_DATA]

def add_acl_mac(
self,
mac,
enabled=True,
time="00:00-23:59",
comment=None,
week="1234567"):

param = self._get_acl_mac_param(
mac=mac,
enabled=enabled,
time=time,
comment=comment,
week=week)

return self.exec(
func_name=rp_func_name.acl_mac,
action=rp_action.add,
param=param
)

def edit_acl_mac(
self,
acl_mac_id,
mac,
enabled=True,
time="00:00-23:59",
comment=None,
week="1234567"):

param = self._get_acl_mac_param(
mac=mac,
enabled=enabled,
time=time,
comment=comment,
week=week)

param[acl_mac_param.id] = acl_mac_id

return self.exec(
func_name=rp_func_name.acl_mac,
action=rp_action.edit,
param=param
)

def del_acl_mac(self, acl_mac_id):
return self.exec(
func_name=rp_func_name.acl_mac,
action=rp_action.delete,
param={
acl_mac_param.id: acl_mac_id,
}
)

def disable_acl_mac(self, acl_mac_id):
return self.exec(
func_name=rp_func_name.acl_mac,
action=rp_action.down,
param={
acl_mac_param.id: acl_mac_id,
}
)

def enable_acl_mac(self, acl_mac_id):
return self.exec(
func_name=rp_func_name.acl_mac,
action=rp_action.up,
param={
acl_mac_param.id: acl_mac_id,
}
)

# }}}

0 comments on commit 0adeefa

Please sign in to comment.