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-393] implement lifecycle_approve_for_my_org function #394

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/api-engine/api/routes/chaincode/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def md5_for_file(chunks):
md5.update(data)
return md5.hexdigest()

class ChainCodeApproveForMyOrgBody(serializers.Serializer):
channel_name = serializers.CharField(max_length=128, required=True)
chaincode_name = serializers.CharField(max_length=128, required=True)
chaincode_version = serializers.CharField(max_length=128, required=True)
policy = serializers.CharField(max_length=128, required=True)
orderer_url = serializers.CharField(max_length=128, required=True)
sequence = serializers.IntegerField(min_value=1, required=True)


class ChainCodeNetworkSerializer(serializers.Serializer):
id = serializers.UUIDField(help_text="Network ID")
Expand Down
51 changes: 49 additions & 2 deletions src/api-engine/api/routes/chaincode/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

from api.routes.chaincode.serializers import (
ChainCodePackageBody,
ChainCodeIDSerializer
ChainCodeIDSerializer,
ChainCodeApproveForMyOrgBody
)
from api.common import ok, err

Expand Down Expand Up @@ -110,6 +111,7 @@ def package(self, request):
return Response(
ok("success"), status=status.HTTP_200_OK
)

@swagger_auto_schema(
method="post",
responses=with_common_response(
Expand Down Expand Up @@ -168,7 +170,6 @@ def query_installed(self, request):
ok(installed_chaincodes), status=status.HTTP_200_OK
)


@swagger_auto_schema(
method="get",
responses=with_common_response(
Expand Down Expand Up @@ -196,6 +197,52 @@ def get_installed_package(self, request):
ok("success"), 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 approve_for_my_org(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])
orderer_tls_root_cert = ""
for _, _, files in os.walk(orderer_tls_dir):
orderer_tls_root_cert = orderer_tls_dir + "/" + files[0]
break

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_approve_for_my_org(orderer_url, orderer_tls_root_cert, channel_name,
chaincode_name, chaincode_version, policy, sequence)
if code != 0:
return Response(err(" lifecycle_approve_for_my_org failed. err: "+content), status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
return Response(
err(e.args), status=status.HTTP_400_BAD_REQUEST
)
return Response(
ok("success"), status=status.HTTP_200_OK
)


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