Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#issue-395] implement query_approved and check_commit_readiness func… #396

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/api-engine/api/routes/chaincode/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,77 @@ def approve_for_my_org(self, request):
ok("success"), status=status.HTTP_200_OK
)

@swagger_auto_schema(
method="get",
responses=with_common_response(
{status.HTTP_201_CREATED: ChainCodeIDSerializer}
),
)
@action(detail=False, methods=['get'])
def query_approved(self, request):
try:
org = request.user.organization
peer_node = Node.objects.get(type="peer", organization=org.id)
envs = init_env_vars(peer_node, org)

channel_name = request.data.get("channel_name")
cc_name = request.data.get("cc_name")

peer_channel_cli = PeerChainCode("v2.2.0", **envs)
code, content = peer_channel_cli.lifecycle_query_approved(channel_name, cc_name)
if code != 0:
return Response(err("query_approved failed."), status=status.HTTP_400_BAD_REQUEST)

except Exception as e:
return Response(
err(e.args), status=status.HTTP_400_BAD_REQUEST
)
return Response(
ok(content), status=status.HTTP_200_OK
)

@swagger_auto_schema(
method="post",
responses=with_common_response(
{status.HTTP_201_CREATED: ChainCodeIDSerializer}
),
)
@action(detail=False, methods=['post'])
def check_commit_readiness(self, request):
serializer = ChainCodeApproveForMyOrgBody(data=request.data)
if serializer.is_valid(raise_exception=True):
try:
channel_name = serializer.validated_data.get("channel_name")
chaincode_name = serializer.validated_data.get("chaincode_name")
chaincode_version = serializer.validated_data.get("chaincode_version")
policy = serializer.validated_data.get("policy")
# Perhaps the orderer's port is best stored in the database
orderer_url = serializer.validated_data.get("orderer_url")
sequence = serializer.validated_data.get("sequence")
org = request.user.organization
orderer_node = Node.objects.get(type="orderer", organization=org.id)

orderer_tls_dir = "{}/{}/crypto-config/ordererOrganizations/{}/orderers/{}/msp/tlscacerts" \
.format(CELLO_HOME, org.name, org.name.split(".", 1)[1], orderer_node.name + "." +
org.name.split(".", 1)[1])

peer_node = Node.objects.get(type="peer", organization=org.id)
envs = init_env_vars(peer_node, org)

peer_channel_cli = PeerChainCode("v2.2.0", **envs)
code, content = peer_channel_cli.lifecycle_check_commit_readiness(orderer_url, orderer_tls_dir, channel_name, chaincode_name, chaincode_version,
policy, sequence)
if code != 0:
return Response(err("check_commit_readiness failed."), status=status.HTTP_400_BAD_REQUEST)

except Exception as e:
return Response(
err(e.args), status=status.HTTP_400_BAD_REQUEST
)
return Response(
ok(content), status=status.HTTP_200_OK
)


def init_env_vars(node, org):
"""
Expand Down