Beispiel #1
0
def Create(taxonomy_id,
           annotation_name,
           description,
           parent_annotation=None,
           child_annotations=None):
    """Makes an API call to create an annotation in the given taxonomy.

  Args:
    taxonomy_id: Id of a taxonomy.
    annotation_name: Name of the annotation.
    description: a short description to the annotation.
    parent_annotation: Id of the parent annotation to this annotation.
    child_annotations: Ids of child annotations of this annotaiton.

  Returns:
    An Annotation message.
  """
    messages = utils.GetMessagesModule()
    return _GetService().Create(
        messages.DatapolTaxonomyStoresDataTaxonomiesAnnotationsCreateRequest(
            parent=utils.GetTaxonomyRelativeName(taxonomy_id),
            annotation=messages.Annotation(displayName=annotation_name,
                                           description=description,
                                           parentAnnotation=parent_annotation,
                                           childAnnotations=child_annotations
                                           if child_annotations else [])))
Beispiel #2
0
def Apply(data, taxonomy_id, annotation_id):
  """Applies an annotation to a data asset.

  Args:
    data: Name of the data to be annotated.
    taxonomy_id: Id of the policy taxonomy.
    annotation_id: Id of the annotation. This annotation must belong to
      the policy taxonomy specified in taxonomy_name.

  Returns:
    An AnnotationTag message.
  """
  client = utils.GetClientInstance().data_taxonomyStores_dataTaxonomies
  messages = utils.GetMessagesModule()
  name = resources.REGISTRY.Create(
      'datapol.data.taxonomy',
      data=data,
      taxonomyStoresId=utils.GetTaxonomyStoresId(),
      dataTaxonomiesId=taxonomy_id).RelativeName()
  try:
    return client.ApplyAnnotationTag(
        messages.
        DatapolDataTaxonomyStoresDataTaxonomiesApplyAnnotationTagRequest(
            name=name,
            applyAnnotationTagRequest=messages.ApplyAnnotationTagRequest(
                annotationId=annotation_id)))
  except apitools_exceptions.HttpError as e:
    raise utils.ErrorWrapper(e, name)
Beispiel #3
0
def CreateAnnotation(taxonomy_resource,
                     display_name,
                     description=None,
                     parent_annotation_id=None):
    """Creates an annotation in the specified taxonomy.

  Dy default the annotation is created as a root annotation, but an annotation
  can also be created as a child of another annotation by specifying a
  parent_annotation.

  Args:
    taxonomy_resource: The taxonomy container for the new annotation.
    display_name: The display name given to the annotation.
    description: The annotation description.
    parent_annotation_id: The parent annotation, if any.

  Returns:
    The created Annotation message.
  """
    messages = utils.GetMessagesModule()
    req = messages.CategorymanagerProjectsTaxonomiesAnnotationsCreateRequest(
        parent=taxonomy_resource.RelativeName(),
        annotation=messages.Annotation(
            displayName=display_name,
            description=description,
            parentAnnotationId=parent_annotation_id))
    return utils.GetClientInstance().projects_taxonomies_annotations.Create(
        request=req)
Beispiel #4
0
def ListDataAssets(annotations=None,
                   include_annotated_by_group=False,
                   annotatable_only=True,
                   filter_exp='',
                   limit=None):
  """Lists resource names of all data assets with the given annotations.

  Args:
    annotations: A list of annotations. Each returned data asset will be tagged
      with at one of those annotations.
    include_annotated_by_group: If true, and a given annotation has child
      annotations, also returns data assets that are annotated with those child
      annotations.
    annotatable_only: If true, only returns data assets that are annotatable by
      the caller.
    filter_exp: A expression to further filter data assets
    limit: The number of resource names to limit the resutls to.

  Returns:
    Generator that yields resource names of data assets.
  """
  request = utils.GetMessagesModule().DatapolDataAssetsListResourceNamesRequest(
      annotations=annotations or [],
      includeAnnotatedByGroup=include_annotated_by_group,
      annotatableOnly=annotatable_only,
      filter=filter_exp)
  return list_pager.YieldFromList(
      utils.GetClientInstance().dataAssets,
      request,
      limit=limit,
      method='ListResourceNames',
      field='dataAssets',
      batch_size_attribute='pageSize')
Beispiel #5
0
def Get(taxonomy_id):
    """Makes an API call to get the definition of a taxonomy.

  Args:
    taxonomy_id: Name of the taxonomy.

  Returns:
    A Taxonomy message.
  """
    return _GetService().Get(utils.GetMessagesModule(
    ).DatapolTaxonomyStoresDataTaxonomiesGetRequest(
        name=utils.GetTaxonomyRelativeName(taxonomy_id)))
Beispiel #6
0
def Get(taxonomy_id, annotation_id):
    """Makes an API call to get the definition of an annotation.

  Args:
    taxonomy_id: Id of a taxonomy.
    annotation_id: Id of the annotation.

  Returns:
    An Annotation message.
  """
    return _GetService().Get(utils.GetMessagesModule(
    ).DatapolTaxonomyStoresDataTaxonomiesAnnotationsGetRequest(
        name=utils.GetAnnotationRelativeName(taxonomy_id, annotation_id)))
Beispiel #7
0
def DeleteTaxonomy(taxonomy_resource):
    """Deletes a taxonomy resource.

  Args:
    taxonomy_resource: The resource path of the taxonomy to delete.

  Returns:
    An empty message.
  """
    messages = utils.GetMessagesModule()
    req = messages.CategorymanagerProjectsTaxonomiesDeleteRequest(
        name=taxonomy_resource.RelativeName())
    return utils.GetClientInstance().projects_taxonomies.Delete(request=req)
Beispiel #8
0
def Delete(taxonomy_id):
    """Makes an API call to delete a taxonomy.

  Args:
    taxonomy_id: Id of the taxonomy.

  Returns:
    An Operation message which can be used to check on the progress of taxonomy
    deletion.
  """
    return _GetService().Delete(utils.GetMessagesModule(
    ).DatapolTaxonomyStoresDataTaxonomiesDeleteRequest(
        name=utils.GetTaxonomyRelativeName(taxonomy_id)))
Beispiel #9
0
def ListTaxonomies(project_resource):
    """Lists all taxonomies in a project resource.

  Args:
    project_resource: The project resource container of the taxonomies to list.

  Returns:
    A list of taxonomy messages.
  """
    messages = utils.GetMessagesModule()
    req = messages.CategorymanagerProjectsTaxonomiesListRequest(
        parent=project_resource.RelativeName())
    return utils.GetClientInstance().projects_taxonomies.List(request=req)
Beispiel #10
0
def GetTaxonomyStoreFromOrgResource(org_resource):
  """Gets a taxonomy store from an organization resource.

  Args:
    org_resource: A cloudresourcemanager.organizations core.Resource object.

  Returns:
    A TaxonomyStore message.
  """
  messages = utils.GetMessagesModule()
  req = messages.CategorymanagerOrganizationsGetTaxonomyStoreRequest(
      parent=org_resource.RelativeName())
  return utils.GetClientInstance().organizations.GetTaxonomyStore(request=req)
def GetDefault(project_ref):
    """Makes an API call to the default taxonomy store for a project.

  Args:
    project_ref: A resource reference of a project. Use the current active
      project if not provided.

  Returns:
    A taxonomy store.
  """
    return _GetService().GetDefault(utils.GetMessagesModule(
    ).CategorymanagerTaxonomyStoresGetDefaultRequest(
        projectId=project_ref.projectId))
def GetIamPolicy(store_ref):
    """Gets IAM policy for a given taxonomy store.

  Args:
    store_ref: a taxonomy store resource reference.

  Returns:
    An IamPolicy message.
  """
    messages = utils.GetMessagesModule()
    return _GetService().GetIamPolicy(
        messages.CategorymanagerTaxonomyStoresGetIamPolicyRequest(
            resource=store_ref.RelativeName(),
            getIamPolicyRequest=messages.GetIamPolicyRequest()))
Beispiel #13
0
def GetIamPolicy(taxonomy_id):
    """Gets IAM policy for a given taxonomy.

  Args:
    taxonomy_id: Id of the taxonomy.

  Returns:
    An IamPolicy message.
  """
    messages = utils.GetMessagesModule()
    return _GetService().GetIamPolicy(
        messages.DatapolTaxonomyStoresDataTaxonomiesGetIamPolicyRequest(
            resource=utils.GetTaxonomyRelativeName(taxonomy_id),
            getIamPolicyRequest=messages.GetIamPolicyRequest()))
Beispiel #14
0
def ListAssetAnnotationTags(asset_resource):
    """Gets all annotation tags associated with an asset.

  Args:
    asset_resource: A category_manager.assets core.Resource asset object.

  Returns:
    A ListAnnotationTagsResponse message.
  """
    messages = utils.GetMessagesModule()
    # Set url_escape=True because the resource name of the asset must be escaped.
    req = messages.CategorymanagerAssetsAnnotationTagsListRequest(
        name=asset_resource.RelativeName(url_escape=True))
    return utils.GetClientInstance().assets_annotationTags.List(request=req)
Beispiel #15
0
def UpdateTaxonomy(taxonomy_resource, description):
  """Updates the description of a taxonomy.

  Args:
    taxonomy_resource: A category_manager.taxonomies core.Resource object.
    description: A string representing the new taxonomy description.

  Returns:
    A Taxonomy message.
  """
  messages = utils.GetMessagesModule()
  req = messages.CategorymanagerProjectsTaxonomiesPatchRequest(
      name=taxonomy_resource.RelativeName(),
      taxonomy=messages.Taxonomy(description=description))
  return utils.GetClientInstance().projects_taxonomies.Patch(request=req)
def SetIamPolicy(store_ref, policy):
    """Sets IAM policy on a taxonomy store.

  Args:
    store_ref: a taxonomy store resource reference.
    policy: An IamPolicy message.

  Returns:
    An IamPolicy message.
  """
    messages = utils.GetMessagesModule()
    return _GetService().SetIamPolicy(
        messages.CategorymanagerTaxonomyStoresSetIamPolicyRequest(
            resource=store_ref.RelativeName(),
            setIamPolicyRequest=messages.SetIamPolicyRequest(policy=policy)))
Beispiel #17
0
def GetIamPolicy(taxonomy_store_resource):
  """Gets IAM policy for a given taxonomy store.

  Args:
    taxonomy_store_resource: A categorymanager.taxonomyStores core.Resource
      object.

  Returns:
    An IamPolicy message.
  """
  messages = utils.GetMessagesModule()
  return utils.GetClientInstance().taxonomyStores.GetIamPolicy(
      messages.CategorymanagerTaxonomyStoresGetIamPolicyRequest(
          resource=taxonomy_store_resource.RelativeName(),
          getIamPolicyRequest=messages.GetIamPolicyRequest()))
Beispiel #18
0
def SetIamPolicy(taxonomy_id, policy):
    """Sets IAM policy, for a given taxonomy.

  Args:
    taxonomy_id: Id of the taxonomy.
    policy: An IamPolicy message.

  Returns:
    An IamPolicy message.
  """
    messages = utils.GetMessagesModule()
    return _GetService().SetIamPolicy(
        messages.DatapolTaxonomyStoresDataTaxonomiesSetIamPolicyRequest(
            resource=utils.GetTaxonomyRelativeName(taxonomy_id),
            setIamPolicyRequest=messages.SetIamPolicyRequest(policy=policy)))
Beispiel #19
0
def Update(taxonomy_id, annotation_id, description):
    """Makes an API call to update an annotation.

  Args:
    taxonomy_id: Id of a taxonomy.
    annotation_id: Id of the annotation.
    description: New description to be updated.

  Returns:
    An Annotation message.
  """
    messages = utils.GetMessagesModule()
    return _GetService().Patch(
        messages.DatapolTaxonomyStoresDataTaxonomiesAnnotationsPatchRequest(
            name=utils.GetAnnotationRelativeName(taxonomy_id, annotation_id),
            annotation=messages.Annotation(description=description)))
Beispiel #20
0
def SetIamPolicy(taxonomy_store_resource, policy):
  """Sets IAM policy on a taxonomy store.

  Args:
    taxonomy_store_resource: A categorymanager.taxonomyStores core.Resource
      object.
    policy: An IamPolicy message.

  Returns:
    An IamPolicy message.
  """
  messages = utils.GetMessagesModule()
  return utils.GetClientInstance().taxonomyStores.SetIamPolicy(
      messages.CategorymanagerTaxonomyStoresSetIamPolicyRequest(
          resource=taxonomy_store_resource.RelativeName(),
          setIamPolicyRequest=messages.SetIamPolicyRequest(policy=policy)))
Beispiel #21
0
def UpdateAnnotation(annotation_resource, description):
    """Updates the description of an annotation.

  Args:
    annotation_resource: A category_manager.taxonomies.annotations
      core.Resource object.
    description: A string representing the new annotation description.

  Returns:
    An Annotation message.
  """
    messages = utils.GetMessagesModule()
    req = messages.CategorymanagerProjectsTaxonomiesAnnotationsPatchRequest(
        name=annotation_resource.RelativeName(),
        annotation=messages.Annotation(description=description))
    return utils.GetClientInstance().projects_taxonomies_annotations.Patch(
        request=req)
Beispiel #22
0
def ApplyAnnotationTag(asset_ref, annotation_ref, sub_asset=None):
    """Applies an annotation tag to an asset.

  Args:
    asset_ref: An asset reference object.
    annotation_ref: An annotation reference object.
    sub_asset: A string representing the asset's sub-asset, if any.

  Returns:
    AnnotationTag response message.
  """
    messages = utils.GetMessagesModule()
    # Set url_escape=True because the resource name of the asset must be escaped.
    req = messages.CategorymanagerAssetsApplyAnnotationTagRequest(
        name=asset_ref.RelativeName(url_escape=True),
        applyAnnotationTagRequest=messages.ApplyAnnotationTagRequest(
            annotation=annotation_ref.RelativeName(), subAsset=sub_asset))
    return utils.GetClientInstance().assets.ApplyAnnotationTag(request=req)
Beispiel #23
0
def List(taxonomy_id, limit=None):
    """Makes API calls to list annotations under the given taxonomy.

  Args:
    taxonomy_id: Id of a taxonomy.
    limit: The number of taxonomies to limit the resutls to.

  Returns:
    Generator that yields taxonomies
  """
    request = utils.GetMessagesModule(
    ).DatapolTaxonomyStoresDataTaxonomiesAnnotationsListRequest(
        parent=utils.GetTaxonomyRelativeName(taxonomy_id))
    return list_pager.YieldFromList(_GetService(),
                                    request,
                                    limit=limit,
                                    field='annotations',
                                    batch_size_attribute='pageSize')
Beispiel #24
0
def CreateTaxonomy(project_resource, display_name, description=None):
    """Creates a taxonomy in the project resource container.

  Args:
    project_resource: The project resource representing the project to create
      the taxonomy in.
    display_name: The display name given to the taxonomy.
    description: The taxonomy description.

  Returns:
    The created Taxonomy message.
  """
    messages = utils.GetMessagesModule()
    req = messages.CategorymanagerProjectsTaxonomiesCreateRequest(
        parent=project_resource.RelativeName(),
        taxonomy=messages.Taxonomy(displayName=display_name,
                                   description=description))
    return utils.GetClientInstance().projects_taxonomies.Create(request=req)
Beispiel #25
0
def DeleteAnnotationTag(asset_ref, annotation_ref, sub_asset=None):
    """Delete an annotation tag on an asset.

  Args:
    asset_ref: An asset reference object.
    annotation_ref: An annotation reference object.
    sub_asset: A string representing the asset's sub-asset, if any.

  Returns:
    DeleteAnnotationTag response message.
  """
    messages = utils.GetMessagesModule()
    # Set url_escape=True because the resource name of the asset must be escaped.
    req = messages.CategorymanagerAssetsDeleteAnnotationTagRequest(
        name=DELETE_TAG_NAME_PATTERN.format(
            asset_ref.RelativeName(url_escape=True)),
        annotation=annotation_ref.RelativeName(),
        subAsset=sub_asset)
    return utils.GetClientInstance().assets.DeleteAnnotationTag(request=req)
Beispiel #26
0
def Create(taxonomy_name, description):
    """Makes an API call to create a taxonomy.

  Args:
    taxonomy_name: Name of the taxononmy. Needs to be unique within the
      organization.
    description: A short description to the taxonomy.

  Returns:
    A Taxonomy message.
  """
    messages = utils.GetMessagesModule()
    return _GetService().Create(
        messages.DatapolTaxonomyStoresDataTaxonomiesCreateRequest(
            parent=resources.REGISTRY.Create(
                'datapol.taxonomyStores',
                taxonomyStoresId=utils.GetTaxonomyStoresId()).RelativeName(),
            dataTaxonomy=messages.DataTaxonomy(displayName=taxonomy_name,
                                               description=description)))
Beispiel #27
0
def List(limit=None):
    """Makes API calls to list taxonomies under the current organization.

  Args:
    limit: The number of taxonomies to limit the resutls to.

  Returns:
    Generator that yields taxonomies
  """
    request = utils.GetMessagesModule(
    ).DatapolTaxonomyStoresDataTaxonomiesListRequest(
        parent=resources.REGISTRY.Create(
            'datapol.taxonomyStores',
            taxonomyStoresId=utils.GetTaxonomyStoresId()).RelativeName())
    return list_pager.YieldFromList(_GetService(),
                                    request,
                                    limit=limit,
                                    field='taxonomies',
                                    batch_size_attribute='pageSize')
Beispiel #28
0
def ListTags(data, limit=None):
  """Lists all annotation tags on a data asset.

  Args:
    data: Name of the data.
    limit: The number of annotation tags to limit the resutls to.

  Returns:
    Generator that yields annnotation tags.
  """
  request = utils.GetMessagesModule(
  ).DatapolDataTaxonomyStoresAnnotationTagsListRequest(
      parent='data/{data}'.format(data=data))
  return list_pager.YieldFromList(
      utils.GetClientInstance().data_taxonomyStores_annotationTags,
      request,
      limit=limit,
      field='tags',
      batch_size_attribute='pageSize')
Beispiel #29
0
def Delete(data, taxonomy_id):
  """Deletes an annotation on a data asset.

  Args:
    data: Name of the data to be annotated.
    taxonomy_id: Id of the policy taxonomy.

  Returns:
    An Empty message.
  """
  client = utils.GetClientInstance().data_taxonomyStores_dataTaxonomies
  name = resources.REGISTRY.Create(
      'datapol.data.tag',
      data=data,
      taxonomyStoresId=utils.GetTaxonomyStoresId(),
      dataTaxonomiesId=taxonomy_id).RelativeName()
  try:
    return client.DeleteAnnotationTag(
        utils.GetMessagesModule()
        .DatapolDataTaxonomyStoresDataTaxonomiesDeleteAnnotationTagRequest(
            name=name))
  except apitools_exceptions.HttpError as e:
    raise utils.ErrorWrapper(e, name)
Beispiel #30
0
def AddIamPolicyBinding(resource_resource, role, member, module):
  """Add a member to an iam role binding on resource."""
  policy = module.GetIamPolicy(resource_resource)
  iam_util.AddBindingToIamPolicy(utils.GetMessagesModule().Binding, policy,
                                 member, role)
  return module.SetIamPolicy(resource_resource, policy)