11# type: ignore
22from datetime import datetime , timezone
33import json
4+
45import logging
56import mimetypes
67import os
910import requests
1011import requests .exceptions
1112
12- from labelbox import utils
1313import labelbox .exceptions
14+ from labelbox import utils
15+ from labelbox import __version__ as SDK_VERSION
1416from labelbox .orm import query
1517from labelbox .orm .db_object import DbObject
1618from labelbox .pagination import PaginatedCollection
2224from labelbox .schema .organization import Organization
2325from labelbox .schema .data_row_metadata import DataRowMetadataOntology
2426from labelbox .schema .labeling_frontend import LabelingFrontend
27+ from labelbox .schema .iam_integration import IAMIntegration
2528from labelbox .schema import role
26- from labelbox import __version__ as SDK_VERSION
2729
2830logger = logging .getLogger (__name__ )
2931
@@ -503,7 +505,7 @@ def _create(self, db_object_type, data):
503505 res = res ["create%s" % db_object_type .type_name ()]
504506 return db_object_type (self , res )
505507
506- def create_dataset (self , ** kwargs ):
508+ def create_dataset (self , iam_integration = IAMIntegration . _DEFAULT , ** kwargs ):
507509 """ Creates a Dataset object on the server.
508510
509511 Attribute values are passed as keyword arguments.
@@ -512,14 +514,52 @@ def create_dataset(self, **kwargs):
512514 >>> dataset = client.create_dataset(name="<dataset_name>", projects=project)
513515
514516 Args:
517+ iam_integration (IAMIntegration) : Uses the default integration.
518+ Optionally specify another integration or set as None to not use delegated access
515519 **kwargs: Keyword arguments with Dataset attribute values.
516520 Returns:
517521 A new Dataset object.
518522 Raises:
519523 InvalidAttributeError: If the Dataset type does not contain
520524 any of the attribute names given in kwargs.
521525 """
522- return self ._create (Dataset , kwargs )
526+ dataset = self ._create (Dataset , kwargs )
527+
528+ if iam_integration == IAMIntegration ._DEFAULT :
529+ iam_integration = self .get_organization (
530+ ).get_default_iam_integration ()
531+
532+ if iam_integration is None :
533+ return dataset
534+
535+ if not isinstance (iam_integration , IAMIntegration ):
536+ raise TypeError (
537+ f"iam integration must be a reference an `IAMIntegration` object. Found { type (iam_integration )} "
538+ )
539+
540+ if not iam_integration .valid :
541+ raise ValueError ("Integration is not valid. Please select another." )
542+ try :
543+ self .execute (
544+ """mutation setSignerForDatasetPyApi($signerId: ID!, $datasetId: ID!) {
545+ setSignerForDataset(data: { signerId: $signerId}, where: {id: $datasetId}){id}}
546+ """ , {
547+ 'signerId' : iam_integration .uid ,
548+ 'datasetId' : dataset .uid
549+ })
550+ validation_result = self .execute (
551+ """mutation validateDatasetPyApi($id: ID!){validateDataset(where: {id : $id}){
552+ valid checks{name, success}}}
553+ """ , {'id' : dataset .uid })
554+
555+ if not validation_result ['validateDataset' ]['valid' ]:
556+ raise labelbox .exceptions .LabelboxError (
557+ f"IAMIntegration { validation_result ['validateDataset' ]['checks' ]['name' ]} was not successfully added added to the project."
558+ )
559+ except Exception as e :
560+ dataset .delete ()
561+ raise e
562+ return dataset
523563
524564 def create_project (self , ** kwargs ):
525565 """ Creates a Project object on the server.
0 commit comments