cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Python Data Layer scopes

Python Data Layer scopes

criss07
New Poster

Hi all.

In this example is shown how to create a Data Layer scope for a specific node written in C++: How-to-provide-API-in-the-Data-Layer-for-your-app .

Is there a way to do the exact thing but in Python? I could not find the CreateScopeDirect method in python.

Thank you,

Criss.

3 REPLIES 3

nickH
Community Moderator
Community Moderator

Hi Criss, 

Yes. You can do the same thing also in Python. Therefor import the "Scope.py" from ctrlx-automation-sdk/src/python/comm/datalayer/Scope.py. 

Working with Flatbuffers in Python is a little bit different to C++. If you are struggling with that, I can recommend you to take a look at the documentaion from google

Best regards,

Nick

Thank you for the hint. I was able to write down some code but it still does not work.

 

    builder = flatbuffers.Builder(0)
    permissionsRW = builder.CreateString("test-node/**")
    description = builder.CreateString("Allow to read and write")
    name = builder.CreateString("Test-node RW permission")
    identifier = builder.CreateString("datalayer.test-node.rw")
    Scope.ScopeStart(builder)
    Scope.AddPermissionsRw(builder, permissionsRW)
    Scope.AddDescription(builder, description)
    Scope.AddName(builder, name)
    Scope.AddIdentifier(builder, identifier)
    scope = Scope.ScopeEnd(builder)
    builder.Finish(scope)
    variant = Variant()
    variant_result = variant.set_flatbuffers(builder.Output())
    if variant_result is not Result.OK:
        print("ERROR: creating variant flatbuffer: ", variant_result, flush=True)
    result, var= client.create_sync("datalayer/security/scopes", variant)
    if result is not Result.OK:
        print("ERROR: create scope failed: ", result, flush=True)

 

I receive the error : ERROR: create scope failed: Result.TYPE_MISMATCH. It seems like the variant type is wrong maybe? I don't know why.

nickH
Community Moderator
Community Moderator

Hi Chriss, 

If you take a closer look into the Scope.py you can see, that the permissions in this Flatbuffer are not Strings but Vector of Strings. Here is how I got it working. 

            builder = flatbuffers.Builder(1024)
            permissionsRW = builder.CreateString('test-node/**')
            Scope.StartPermissionsRwVector(builder, 1)
            builder.PrependSOffsetTRelative(permissionsRW)
            permissionsRWVector = builder.EndVector()
            description = builder.CreateString("Allow to read and write")
            name = builder.CreateString("Test-node RW permission")
            identifier = builder.CreateString("datalayer.test-node.rw")

            Scope.ScopeStart(builder)
            Scope.ScopeAddIdentifier(builder, identifier)
            Scope.ScopeAddName(builder, name)
            Scope.ScopeAddDescription(builder, description)
            Scope.ScopeAddPermissionsRw(builder, permissionsRWVector)
   
            scope = Scope.ScopeEnd(builder)
            builder.Finish(scope)

            variant = Variant()
            variant_result = variant.set_flatbuffers(builder.Output())
            if variant_result is not Result.OK:
                print("ERROR: creating variant flatbuffer: ", variant_result, flush=True)
            result, var= datalayer_client.create_sync("datalayer/security/scopes", variant)
            if result is not Result.OK:
                print("ERROR: create scope failed: ", result, flush=True)

 

Best regards, 

Nick

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist