class Network(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of network", default=make_uuid, editable=True, ) govern = models.ForeignKey( Govern, help_text="Govern of node", null=True, on_delete=models.CASCADE ) version = models.CharField( help_text=""" Version of network. Fabric supported versions: %s """ % (FabricVersions.values()), max_length=64, default="", ) created_at = models.DateTimeField( help_text="Create time of network", auto_now_add=True ) class Meta: ordering = ("-created_at",)
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
class Network(models.Model): id = models.UUIDField( primary_key=True, help_text="ID of network", default=make_uuid, editable=True, ) name = models.CharField( help_text="network name, can be generated automatically.", max_length=64, default=random_name("netowrk"), ) 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. Fabric supported versions: %s """ % (FabricVersions.values()), max_length=64, default="", ) created_at = models.DateTimeField(help_text="Create time of network", auto_now_add=True) consensus = models.CharField( help_text="Consensus of network", max_length=128, default="raft", ) organizations = ArrayField(models.CharField(max_length=128, blank=True), help_text="organizations of network", default=list, null=True) genesisblock = models.TextField( help_text="genesis block", null=True, ) database = models.CharField( help_text="database of network", max_length=128, default="leveldb", ) class Meta: ordering = ("-created_at", )
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)