Skip to content

Commit

Permalink
Merge pull request #210 from XuHugo/node
Browse files Browse the repository at this point in the history
Add node
  • Loading branch information
yeasy authored Jan 25, 2021
2 parents 1f2dda3 + 22be365 commit b55865c
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 178 deletions.
93 changes: 39 additions & 54 deletions src/api-engine/api/routes/node/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class Meta:
"per_page",
"type",
"name",
"network_type",
"network_version",
"agent_id",
)
extra_kwargs = {"type": {"required": False}}
Expand Down Expand Up @@ -238,69 +236,56 @@ class Meta:


class NodeCreateBody(serializers.ModelSerializer):
agent_type = serializers.ChoiceField(
help_text="Agent type",
choices=HostType.to_choices(True),
required=False,
)
ca = FabricCASerializer(
help_text="CA configuration for node", required=False
)
peer = FabricPeerSerializer(
help_text="Peer configuration for node", required=False
)
organization = serializers.UUIDField(help_text="ID of organization")

class Meta:
model = Node
fields = (
"network_type",
"network_version",
"name",
"urls",
"type",
"agent_type",
"agent",
"ca",
"peer",
"organization",
)
extra_kwargs = {
"network_type": {"required": True},
"network_version": {"required": True},
"name": {"required": True},
"urls": {"required": True},
"type": {"required": True},
}

def validate(self, attrs):
network_type = attrs.get("network_type")
node_type = attrs.get("type")
network_version = attrs.get("network_version")
agent_type = attrs.get("agent_type")
agent = attrs.get("agent")
ca = attrs.get("ca")
peer = attrs.get("peer")
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():
raise serializers.ValidationError(
"Not valid node type for %s" % network_type
)
if node_type == FabricNodeType.Ca.name.lower() and ca is None:
raise serializers.ValidationError(
"Please input ca configuration for ca node"
)
elif (
node_type == FabricNodeType.Peer.name.lower() and peer is None
):
raise serializers.ValidationError(
"Please input peer configuration for peer node"
)

if agent_type is None and agent is None:
raise serializers.ValidationError("Please set agent_type or agent")

if agent_type and agent:
if agent_type != agent.type:
raise serializers.ValidationError(
"agent type not equal to agent"
)
# network_type = attrs.get("network_type")
# node_type = attrs.get("type")
# network_version = attrs.get("network_version")
# agent_type = attrs.get("agent_type")
# agent = attrs.get("agent")
# ca = attrs.get("ca")
# peer = attrs.get("peer")
# 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():
# raise serializers.ValidationError(
# "Not valid node type for %s" % network_type
# )
# if node_type == FabricNodeType.Ca.name.lower() and ca is None:
# raise serializers.ValidationError(
# "Please input ca configuration for ca node"
# )
# elif (
# node_type == FabricNodeType.Peer.name.lower() and peer is None
# ):
# raise serializers.ValidationError(
# "Please input peer configuration for peer node"
# )
#
# if agent_type is None and agent is None:
# raise serializers.ValidationError("Please set agent_type or agent")
#
# if agent_type and agent:
# if agent_type != agent.type:
# raise serializers.ValidationError(
# "agent type not equal to agent"
# )

return attrs

Expand Down
Loading

0 comments on commit b55865c

Please sign in to comment.