Skip to content

Commit

Permalink
[CE-649] Add user list for node
Browse files Browse the repository at this point in the history
For ca node, can list all user registerd in it.
Add more fields for node list api.
Change network type enums value to string.

Change-Id: I4d8541fe017fe55308e245094075f89dc65a0ef7
Signed-off-by: Haitao Yue <[email protected]>
  • Loading branch information
hightall committed Jul 25, 2019
1 parent baab627 commit c7af890
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 44 deletions.
4 changes: 3 additions & 1 deletion src/api-engine/api/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ChannelType(ExtraEnum):

@unique
class NetworkType(ExtraEnum):
Fabric = 0
Fabric = "fabric"


@unique
Expand Down Expand Up @@ -155,6 +155,7 @@ class NodeStatus(ExtraEnum):
class FabricCAUserType(ExtraEnum):
Peer = "peer"
Orderer = "orderer"
User = "user"


@unique
Expand Down Expand Up @@ -204,6 +205,7 @@ class AgentOperation(ExtraEnum):
Update = "update"
Delete = "delete"
FabricCARegister = "fabric:ca:register"
NewNetwork = "new:network"


class EnumWithDisplayMeta(EnumMeta):
Expand Down
12 changes: 10 additions & 2 deletions src/api-engine/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ class Network(models.Model):
govern = models.ForeignKey(
Govern, help_text="Govern of node", null=True, on_delete=models.CASCADE
)
type = models.CharField(
help_text="Type of network, %s" % NetworkType.values(),
max_length=64,
default=NetworkType.Fabric.value,
)
version = models.CharField(
help_text="""
Version of network.
Expand Down Expand Up @@ -360,8 +365,8 @@ class Node(models.Model):
name = models.CharField(help_text="Node name", max_length=64, default="")
network_type = models.CharField(
help_text="Network type of node",
choices=NetworkType.to_choices(True),
default=NetworkType.Fabric.name.lower(),
choices=NetworkType.to_choices(),
default=NetworkType.Fabric.value,
max_length=64,
)
network_version = models.CharField(
Expand Down Expand Up @@ -508,6 +513,9 @@ class NodeUser(models.Model):
help_text="Attributes of node user", default="", max_length=512
)

class Meta:
ordering = ("id",)


class Port(models.Model):
node = models.ForeignKey(
Expand Down
4 changes: 2 additions & 2 deletions src/api-engine/api/routes/cluster/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ClusterQuery(PageQuerySerializer):
required=False,
allow_null=True,
help_text=NetworkType.get_info("Network Types:", list_str=True),
choices=NetworkType.to_choices(True),
choices=NetworkType.to_choices(),
)
size = serializers.IntegerField(
required=False,
Expand All @@ -58,7 +58,7 @@ class ClusterCreateBody(serializers.Serializer):
host_id = serializers.CharField(help_text="Host ID")
network_type = serializers.ChoiceField(
help_text=NetworkType.get_info("Network Types:", list_str=True),
choices=NetworkType.to_choices(True),
choices=NetworkType.to_choices(),
)
size = serializers.IntegerField(
min_value=SIZE_MIN_VALUE,
Expand Down
13 changes: 11 additions & 2 deletions src/api-engine/api/routes/network/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
FabricNodeType,
NetworkCreateType,
)
from api.models import Network


CHANNEL_NAME_MIN_LEN = 4
Expand Down Expand Up @@ -54,14 +55,22 @@ class NetworkMemberSerializer(serializers.Serializer):
url = serializers.CharField(help_text="URL of member")


class NetworkCreateBody(serializers.Serializer):
create_type = serializers.ChoiceField(
class NetworkCreateBody(serializers.ModelSerializer):
method = serializers.ChoiceField(
help_text=NetworkCreateType.get_info(
"Network Create Types:", list_str=True
),
choices=NetworkCreateType.to_choices(True),
)

class Meta:
model = Network
fields = ("type", "version", "method")
extra_kwargs = {
"type": {"required": True},
"version": {"required": True},
}


class NetworkMemberResponse(serializers.Serializer):
data = NetworkMemberSerializer(many=True)
Expand Down
54 changes: 40 additions & 14 deletions src/api-engine/api/routes/node/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,29 @@ class NodeIDSerializer(serializers.Serializer):
id = serializers.UUIDField(help_text="ID of node")


class FabricCASerializer(serializers.ModelSerializer):
hosts = serializers.ListField(
help_text="Hosts for ca support",
child=serializers.CharField(help_text="Host name", max_length=64),
required=False,
allow_empty=True,
)

class Meta:
model = FabricCA
fields = ("admin_name", "admin_password", "hosts", "type")


class NodeInListSerializer(NodeIDSerializer, serializers.ModelSerializer):
agent_id = serializers.UUIDField(
help_text="Agent ID", required=False, allow_null=True
)
network_id = serializers.UUIDField(
help_text="Network ID", required=False, allow_null=True
)
ca = FabricCASerializer(
help_text="CA configuration for node", required=False, allow_null=True
)

class Meta:
model = Node
Expand All @@ -64,10 +80,12 @@ class Meta:
"agent_id",
"network_id",
"status",
"ca",
)
extra_kwargs = {
"id": {"required": True, "read_only": False},
"created_at": {"required": True, "read_only": False},
"ca": {"required": False, "allow_null": True},
}


Expand All @@ -78,19 +96,6 @@ class NodeListSerializer(serializers.Serializer):
)


class FabricCASerializer(serializers.ModelSerializer):
hosts = serializers.ListField(
help_text="Hosts for ca support",
child=serializers.CharField(help_text="Host name", max_length=64),
required=False,
allow_empty=True,
)

class Meta:
model = FabricCA
fields = ("admin_name", "admin_password", "hosts", "type")


class NodeInfoSerializer(NodeIDSerializer, serializers.ModelSerializer):
ca = FabricCASerializer(
help_text="CA configuration for node", required=False, allow_null=True
Expand Down Expand Up @@ -150,7 +155,7 @@ def validate(self, attrs):
network_version = attrs.get("network_version")
agent_type = attrs.get("agent_type")
agent = attrs.get("agent")
if network_type == NetworkType.Fabric.name.lower():
if network_type == NetworkType.Fabric.value:
if network_version not in FabricVersions.values():
raise serializers.ValidationError("Not valid fabric version")
if node_type not in FabricNodeType.names():
Expand Down Expand Up @@ -230,6 +235,27 @@ class Meta:
}


class NodeUserQuerySerializer(
PageQuerySerializer, serializers.ModelSerializer
):
class Meta:
model = NodeUser
fields = ("name", "user_type", "page", "per_page", "status")


class UserInListSerializer(serializers.ModelSerializer):
class Meta:
model = NodeUser
fields = ("id", "name", "user_type", "status")


class NodeUserListSerializer(serializers.Serializer):
data = UserInListSerializer(many=True, help_text="Users list")
total = serializers.IntegerField(
help_text="Total number of node", min_value=0
)


class NodeUserIDSerializer(serializers.ModelSerializer):
class Meta:
model = NodeUser
Expand Down
82 changes: 60 additions & 22 deletions src/api-engine/api/routes/node/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
NodeUserCreateSerializer,
NodeUserIDSerializer,
NodeUserPatchSerializer,
NodeUserQuerySerializer,
NodeUserListSerializer,
)
from api.tasks import operate_node
from api.utils.common import with_common_response
Expand Down Expand Up @@ -110,15 +112,9 @@ def list(self, request, *args, **kwargs):
nodes = Node.objects.filter(**query_filter)
p = Paginator(nodes, per_page)
nodes = p.page(page)
nodes = [node.__dict__ for node in nodes]

response = NodeListSerializer(
data={"total": p.count, "data": nodes}
)
if response.is_valid(raise_exception=True):
return Response(
data=response.validated_data, status=status.HTTP_200_OK
)
response = NodeListSerializer({"total": p.count, "data": nodes})
return Response(data=response.data, status=status.HTTP_200_OK)

@swagger_auto_schema(
request_body=NodeCreateBody,
Expand Down Expand Up @@ -366,20 +362,7 @@ def retrieve(self, request, pk=None):
response = NodeInfoSerializer(node)
return Response(data=response.data, status=status.HTTP_200_OK)

@swagger_auto_schema(
methods=["post"],
request_body=NodeUserCreateSerializer,
responses=with_common_response(
{status.HTTP_201_CREATED: NodeUserIDSerializer}
),
)
@action(methods=["post"], detail=True, url_path="users", url_name="users")
def users(self, request, pk=None):
"""
Register user to node
Register user to node
"""
def _register_user(self, request, pk=None):
serializer = NodeUserCreateSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
name = serializer.validated_data.get("name")
Expand Down Expand Up @@ -432,6 +415,61 @@ def users(self, request, pk=None):
response = NodeUserIDSerializer(node_user)
return Response(data=response.data, status=status.HTTP_201_CREATED)

def _list_user(self, request, pk=None):
serializer = NodeUserQuerySerializer(data=request.GET)
if serializer.is_valid(raise_exception=True):
page = serializer.validated_data.get("page")
per_page = serializer.validated_data.get("per_page")
name = serializer.validated_data.get("name")
user_type = serializer.validated_data.get("user_type")
user_status = serializer.validated_data.get("status")
query_param = {"node__id": pk}
if name is not None:
query_param.update({"name__icontains": name})
if user_type is not None:
query_param.update({"user_type": user_type})
if user_status is not None:
query_param.update({"status": user_status})

users = NodeUser.objects.filter(**query_param)
p = Paginator(users, per_page)
users = p.page(page)

response = NodeUserListSerializer(
{"data": users, "total": p.count}
)
return Response(response.data, status=status.HTTP_200_OK)

@swagger_auto_schema(
methods=["post"],
operation_description="Register user to node",
operation_summary="Register user to node",
request_body=NodeUserCreateSerializer,
responses=with_common_response(
{status.HTTP_201_CREATED: NodeUserIDSerializer}
),
)
@swagger_auto_schema(
methods=["get"],
operation_description="List user of node",
operation_summary="List user of node",
query_serializer=NodeUserQuerySerializer,
responses=with_common_response(
{status.HTTP_200_OK: NodeUserListSerializer}
),
)
@action(
methods=["post", "get"],
detail=True,
url_path="users",
url_name="users",
)
def users(self, request, pk=None):
if request.method == "POST":
return self._register_user(request, pk)
elif request.method == "GET":
return self._list_user(request, pk)

@swagger_auto_schema(
methods=["patch"],
request_body=NodeUserPatchSerializer,
Expand Down
2 changes: 1 addition & 1 deletion src/api-engine/api/routes/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NodeQuery(PageQuerySerializer):
class NodeCreateBody(serializers.Serializer):
network_type = serializers.ChoiceField(
help_text=NetworkType.get_info("Network types:", list_str=True),
choices=NetworkType.to_choices(True),
choices=NetworkType.to_choices(),
)
type = serializers.ChoiceField(
help_text=FabricNodeType.get_info("Node Types:", list_str=True),
Expand Down

0 comments on commit c7af890

Please sign in to comment.