class NetworkMemberSerializer(serializers.Serializer): id = serializers.CharField(help_text="Network member id") type = serializers.ChoiceField( help_text=FabricNodeType.get_info("Node Types:", list_str=True), choices=FabricNodeType.to_choices(True), ) url = serializers.CharField(help_text="URL of member")
class NodeCreateBody(serializers.Serializer): network_type = serializers.ChoiceField( help_text=NetworkType.get_info("Network types:", list_str=True), choices=NetworkType.to_choices(True), ) type = serializers.ChoiceField( help_text=FabricNodeType.get_info("Node Types:", list_str=True), choices=FabricNodeType.to_choices(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") return attrs
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") if network_type == NetworkType.Fabric.name.lower(): 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 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
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") if network_type == NetworkType.Fabric.name.lower(): 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 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
class Node(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of node", default=make_uuid, editable=True, ) 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(), max_length=64, ) network_version = models.CharField( help_text=""" Version of network for node. Fabric supported versions: %s """ % (FabricVersions.values()), max_length=64, default="", ) type = models.CharField( help_text=""" Node type defined for network. Fabric available types: %s """ % (FabricNodeType.names()), max_length=64, ) urls = JSONField( help_text="URL configurations for node", null=True, blank=True, default=dict, ) user = models.OneToOneField( UserProfile, help_text="User of node", null=True, on_delete=models.CASCADE, ) govern = models.ForeignKey( Govern, help_text="Govern of node", null=True, on_delete=models.CASCADE ) organization = models.ForeignKey( Organization, help_text="Organization of node", null=True, on_delete=models.CASCADE, ) agent = models.ForeignKey( Agent, help_text="Agent of node", null=True, on_delete=models.CASCADE ) network = models.ForeignKey( Network, help_text="Network which node joined.", on_delete=models.CASCADE, null=True, ) created_at = models.DateTimeField( help_text="Create time of network", auto_now_add=True ) status = models.CharField( help_text="Status of node", choices=NodeStatus.to_choices(True), max_length=64, default=NodeStatus.Deploying.name.lower(), ) compose_file = models.FileField( help_text="Compose file for node, if agent type is docker.", max_length=256, upload_to=get_compose_file_path, blank=True, null=True, ) class Meta: ordering = ("-created_at",) def get_compose_file_path(self): return "%s/org/%s/agent/docker/compose_files/%s/docker-compose.yml" % ( MEDIA_ROOT, str(self.organization.id), str(self.id), ) def save( self, force_insert=False, force_update=False, using=None, update_fields=None, ): if self.name == "": self.name = random_name(self.type) super(Node, self).save( force_insert, force_update, using, update_fields ) def delete(self, using=None, keep_parents=False): if self.compose_file: compose_file_path = Path(self.compose_file.path) if os.path.isdir(os.path.dirname(compose_file_path)): shutil.rmtree(os.path.dirname(compose_file_path)) super(Node, self).delete(using, keep_parents)
class Node(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of node", default=make_uuid, editable=True, ) name = models.CharField(help_text="Node name", max_length=64, default="") type = models.CharField( help_text=""" Node type defined for network. Fabric available types: %s """ % (FabricNodeType.names()), max_length=64, ) urls = JSONField( help_text="URL configurations for node", null=True, blank=True, default=dict, ) user = models.ForeignKey( UserProfile, help_text="User of node", null=True, on_delete=models.CASCADE, ) org = models.ForeignKey( Organization, help_text="Organization of node", null=True, related_name="org", on_delete=models.CASCADE, ) # agent = models.ForeignKey( # Agent, # help_text="Agent of node", # null=True, # related_name="network", # on_delete=models.CASCADE # ) # network = models.ForeignKey( # Network, # help_text="Network which node joined.", # on_delete=models.CASCADE, # null=True, # ) created_at = models.DateTimeField(help_text="Create time of network", auto_now_add=True) status = models.CharField( help_text="Status of node", choices=NodeStatus.to_choices(True), max_length=64, default=NodeStatus.Deploying.name.lower(), ) config_file = models.TextField( help_text="Config file of node", max_length=256, blank=True, null=True, ) msp = models.TextField( help_text="msp of node", null=True, ) tls = models.TextField( help_text="tls of node", null=True, ) class Meta: ordering = ("-created_at", ) def get_compose_file_path(self): return "%s/org/%s/agent/docker/compose_files/%s/docker-compose.yml" % ( MEDIA_ROOT, str(self.organization.id), str(self.id), ) def save( self, force_insert=False, force_update=False, using=None, update_fields=None, ): if self.name == "": self.name = random_name(self.type) super(Node, self).save(force_insert, force_update, using, update_fields)