def create(self, request): """ Create Network :param request: create parameter :return: organization ID :rtype: uuid """ serializer = NetworkCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): name = serializer.validated_data.get("name") consensus = serializer.validated_data.get("consensus") organizations = serializer.validated_data.get("organizations") database = serializer.validated_data.get("database") try: Network.objects.get(name=name) except ObjectDoesNotExist: pass orderers = [] peers = [] i = 0 for organization in organizations: org = Organization.objects.get(pk=organization) orderers.append({"name": org.name, "hosts": []}) peers.append({"name": org.name, "hosts": []}) nodes = Node.objects.filter(org=org) for node in nodes: if node.type == "peer": peers[i]["hosts"].append({"name": node.name}) elif node.type == "orderer": orderers[i]["hosts"].append({"name": node.name}) i = i + 1 ConfigTX(name).create(consensus=consensus, orderers=orderers, peers=peers) ConfigTxGen(name).genesis() block = self._genesis2base64(name) network = Network(name=name, consensus=consensus, organizations=organizations, genesisblock=block) network.save() for organization in organizations: Organization.objects.filter(pk=organization).update(network=network) org = Organization.objects.get(pk=organization) nodes = Node.objects.filter(org=org) for node in nodes: try: self._start_node(node.id) except Exception as e: print("err:", e) pass response = NetworkIDSerializer(data=network.__dict__) if response.is_valid(raise_exception=True): return Response( response.validated_data, status=status.HTTP_201_CREATED )
def create(self, request): """ Create Network :param request: create parameter :return: organization ID :rtype: uuid """ try: serializer = NetworkCreateBody(data=request.data) if serializer.is_valid(raise_exception=True): name = serializer.validated_data.get("name") consensus = serializer.validated_data.get("consensus") database = serializer.validated_data.get("database") try: if Network.objects.get(name=name): raise ResourceExists except ObjectDoesNotExist: pass org = request.user.organization if org.network: raise ResourceExists orderers = [] peers = [] orderers.append({"name": org.name, "hosts": []}) peers.append({"name": org.name, "hosts": []}) nodes = Node.objects.filter(organization=org) for node in nodes: if node.type == "peer": peers[0]["hosts"].append({"name": node.name}) elif node.type == "orderer": orderers[0]["hosts"].append({"name": node.name}) ConfigTX(name).create(consensus=consensus, orderers=orderers, peers=peers) ConfigTxGen(name).genesis() block = self._genesis2base64(name) network = Network(name=name, consensus=consensus, genesisblock=block) network.save() org.network = network org.save() nodes = Node.objects.filter(organization=org) for node in nodes: try: threading.Thread(target=self._start_node, args=(node.id, )).start() except Exception as e: raise e response = NetworkIDSerializer(data=network.__dict__) if response.is_valid(raise_exception=True): return Response(ok(response.validated_data), status=status.HTTP_201_CREATED) except ResourceExists as e: raise e except Exception as e: return Response(err(e.args), status=status.HTTP_400_BAD_REQUEST)