Example #1
0
def GetLabelValueFromDisplayName(display_name, label_key):
    """Returns the LabelValue with display_name under label_key if it exists.

  Args:
    display_name: String, display name of the LabelValue
    label_key: String, resource name of the parent of the LabelKey

  Raises:
    InvalidInputError: if the specified display_name does not exist under the
    label_key

  Returns:
    The resource name of the LabelValue associated with the display_name
  """
    labelvalues_service = labelmanager.LabelValuesService()
    labelmanager_messages = labelmanager.LabelManagerMessages()

    list_request = labelmanager_messages.LabelmanagerLabelValuesListRequest(
        parent=label_key)
    response = labelvalues_service.List(list_request)

    for value in response.values:
        if value.displayName == display_name:
            return value.name

    raise InvalidInputError(
        'Invalid display_name for LabelValue [{}] in parent [{}]'.format(
            display_name, label_key))
Example #2
0
def GetLabelKeyFromDisplayName(display_name, label_parent):
    """Returns the LabelKey with display_name under label_parent if it exists.

  Args:
    display_name: String, display name of the LabelKey
    label_parent: String, resource name of the parent of the LabelKey

  Raises:
    InvalidInputError: if the specified display_name does not exist under the
    label_parent

  Returns:
    The resource name of the LabelKey associated with the display_name
  """
    labelkeys_service = labelmanager.LabelKeysService()
    labelmanager_messages = labelmanager.LabelManagerMessages()

    list_request = labelmanager_messages.LabelmanagerLabelKeysListRequest(
        parent=label_parent)
    response = labelkeys_service.List(list_request)

    for key in response.keys:
        if key.displayName == display_name:
            return key.name

    raise InvalidInputError(
        'Invalid display_name for LabelKey [{}] in parent [{}]'.format(
            display_name, label_parent))
Example #3
0
def GetLabelBindingNameFromLabelValueAndResource(label_value, resource):
    """Returns the LabelBinding name for the LabelValue and resource if it exists.

  Args:
    label_value: String, numeric id of the LabelValue
    resource: String, full resource name of the resource

  Raises:
    InvalidInputError: if the specified LabelValue and resource are not bound.

  Returns:
    The LabelBinding name of the LabelValue bound to the resource.
  """
    labelbindings_service = labelmanager.LabelBindingsService()
    labelmanager_messages = labelmanager.LabelManagerMessages()

    list_request = (labelmanager_messages.LabelmanagerLabelBindingsListRequest(
        filter='labelValue:' + label_value))
    response = labelbindings_service.List(list_request)

    for binding in response.bindings:
        if (binding.labelValue == label_value
                and binding.resource == resource):
            return binding.name

    raise InvalidInputError(
        'Invalid LabelBinding for LabelValue [{}] and resource [{}]'.format(
            label_value, resource))
Example #4
0
    def Run(self, args):
        labelkeys_service = labelmanager.LabelKeysService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        if args.IsSpecified('label_parent'):
            label_key = utils.GetLabelKeyFromDisplayName(
                args.LABEL_KEY_ID, args.label_parent)
        else:
            label_key = args.LABEL_KEY_ID

        get_iam_policy_req = (
            labelmanager_messages.LabelmanagerLabelKeysGetIamPolicyRequest(
                resource=label_key))
        policy = labelkeys_service.GetIamPolicy(get_iam_policy_req)
        condition = iam_util.ValidateAndExtractConditionMutexRole(args)
        iam_util.RemoveBindingFromIamPolicyWithCondition(
            policy, args.member, args.role, condition, args.all)

        set_iam_policy_request = labelmanager_messages.SetIamPolicyRequest(
            policy=policy)
        request = labelmanager_messages.LabelmanagerLabelKeysSetIamPolicyRequest(
            resource=label_key, setIamPolicyRequest=set_iam_policy_request)
        result = labelkeys_service.SetIamPolicy(request)
        iam_util.LogSetIamPolicy(label_key, 'LabelKey')
        return result
    def Run(self, args):
        operations_service = labelmanager.OperationsService()
        labelmanager_messages = labelmanager.LabelManagerMessages()
        operation_name = args.OPERATION_NAME

        get_request = labelmanager_messages.LabelmanagerOperationsGetRequest(
            name=operation_name)
        return operations_service.Get(get_request)
Example #6
0
    def Run(self, args):
        labelvalues_service = labelmanager.LabelValuesService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_value = utils.GetLabelValueIfArgsAreValid(args)

        get_request = labelmanager_messages.LabelmanagerLabelValuesGetRequest(
            name=label_value)
        return labelvalues_service.Get(get_request)
Example #7
0
    def Run(self, args):
        labelbindings_service = labelmanager.LabelBindingsService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_value = utils.GetLabelValueIfArgsAreValid(args)

        binding = labelmanager_messages.LabelBinding(labelValue=label_value,
                                                     resource=args.resource)

        return labelbindings_service.Create(binding)
Example #8
0
  def Run(self, args):
    labelkeys_service = labelmanager.LabelKeysService()
    labelmanager_messages = labelmanager.LabelManagerMessages()

    label_parent = args.label_parent

    list_request = labelmanager_messages.LabelmanagerLabelKeysListRequest(
        parent=label_parent)
    response = labelkeys_service.List(list_request)
    return response.keys
Example #9
0
    def Run(self, args):
        labelbindings_service = labelmanager.LabelBindingsService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_value = utils.GetLabelValueIfArgsAreValid(args)
        label_binding_name = utils.GetLabelBindingNameFromLabelValueAndResource(
            label_value, args.resource)

        request = labelmanager_messages.LabelmanagerLabelBindingsDeleteRequest(
            name=label_binding_name)

        return labelbindings_service.Delete(request)
Example #10
0
    def Run(self, args):
        labelkeys_service = labelmanager.LabelKeysService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        if args.IsSpecified('label_parent'):
            label_key = utils.GetLabelKeyFromDisplayName(
                args.LABEL_KEY_ID, args.label_parent)
        else:
            label_key = args.LABEL_KEY_ID

        request = labelmanager_messages.LabelmanagerLabelKeysGetIamPolicyRequest(
            resource=label_key)
        return labelkeys_service.GetIamPolicy(request)
Example #11
0
    def Run(self, args):
        labelbindings_service = labelmanager.LabelBindingsService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        if args.IsSpecified('resource'):
            request = (
                labelmanager_messages.LabelmanagerLabelBindingsListRequest(
                    filter='resource:' + args.resource))
        else:
            label_value = utils.GetLabelValueIfArgsAreValid(args)
            request = (
                labelmanager_messages.LabelmanagerLabelBindingsListRequest(
                    filter='labelValue:' + label_value))
        return labelbindings_service.List(request)
    def Run(self, args):
        labelbindings_service = labelmanager.LabelBindingsService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_value = utils.GetLabelValueIfArgsAreValid(args)
        label_value_id = utils.GetIdFromResourceName(label_value)

        request = labelmanager_messages.LabelmanagerLabelValuesLabelBindingsDeleteRequest(
            labelBinding_labelValue=label_value,
            labelBinding_resource=args.resource,
            labelValuesId=label_value_id,
            labelBindingsId=args.resource)

        return labelbindings_service.Delete(request)
Example #13
0
    def Run(self, args):
        labelkeys_service = labelmanager.LabelKeysService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_key_id = args.LABEL_KEY_ID

        if args.IsSpecified('label_parent'):
            label_key = utils.GetLabelKeyFromDisplayName(
                label_key_id, args.label_parent)
        else:
            label_key = label_key_id

        get_request = labelmanager_messages.LabelmanagerLabelKeysGetRequest(
            name=label_key)
        return labelkeys_service.Get(get_request)
Example #14
0
    def Run(self, args):
        labelvalues_service = labelmanager.LabelValuesService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_key_id = args.label_key

        if args.IsSpecified('label_parent'):
            label_key = utils.GetLabelKeyFromDisplayName(
                label_key_id, args.label_parent)
        else:
            label_key = label_key_id

        list_request = labelmanager_messages.LabelmanagerLabelValuesListRequest(
            parent=label_key)
        response = labelvalues_service.List(list_request)
        return response.values
    def Run(self, args):
        labelbindings_service = labelmanager.LabelBindingsService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_value = utils.GetLabelValueIfArgsAreValid(args)
        label_value_id = utils.GetIdFromResourceName(label_value)

        binding = labelmanager_messages.LabelBinding(labelValue=label_value,
                                                     resource=args.resource)
        create_request = labelmanager_messages.CreateLabelBindingRequest(
            labelBinding=binding)

        request = labelmanager_messages.LabelmanagerLabelValuesLabelBindingsCreateRequest(
            createLabelBindingRequest=create_request,
            labelValuesId=label_value_id)
        return labelbindings_service.Create(request)
Example #16
0
    def Run(self, args):
        labelbindings_service = labelmanager.LabelBindingsService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_value = utils.GetLabelValueIfArgsAreValid(args)
        label_binding_name = utils.GetLabelBindingNameFromLabelValueAndResource(
            label_value, args.resource)

        request = labelmanager_messages.LabelmanagerLabelBindingsDeleteRequest(
            name=label_binding_name)

        op = labelbindings_service.Delete(request)
        if op.response is not None:
            return {'response': op.response}
        else:
            raise operations.OperationError(op.error.message)
    def Run(self, args):
        labelvalues_service = labelmanager.LabelValuesService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_value = utils.GetLabelValueIfArgsAreValid(args)

        delete_request = labelmanager_messages.LabelmanagerLabelValuesDeleteRequest(
            name=label_value)
        op = labelvalues_service.Delete(delete_request)

        if op.response is not None:
            response_dict = encoding.MessageToPyValue(op.response)
            del response_dict['@type']
            return response_dict
        else:
            raise operations.OperationError(op.error.message)
  def Run(self, args):
    labelbindings_service = labelmanager.LabelBindingsService()
    labelmanager_messages = labelmanager.LabelManagerMessages()

    label_value = utils.GetLabelValueIfArgsAreValid(args)

    binding = labelmanager_messages.LabelBinding(
        labelValue=label_value, resource=args.resource)

    op = labelbindings_service.Create(binding)

    if op.response is not None:
      response_dict = encoding.MessageToPyValue(op.response)
      del response_dict['@type']
      return response_dict
    else:
      raise operations.OperationError(op.error.message)
    def Run(self, args):
        labelvalues_service = labelmanager.LabelValuesService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        label_value = utils.GetLabelValueIfArgsAreValid(args)

        delete_request = labelmanager_messages.LabelmanagerLabelValuesDeleteRequest(
            name=label_value)
        op = labelvalues_service.Delete(delete_request)

        if args.async_:
            return op
        else:
            return operations.WaitForOperation(
                op,
                'Waiting for LabelValue [{}] to be deleted with [{}]'.format(
                    args.LABEL_VALUE_ID, op.name), labelvalues_service)
Example #20
0
  def Run(self, args):
    labelkeys_service = labelmanager.LabelKeysService()
    labelmanager_messages = labelmanager.LabelManagerMessages()

    display_name = args.DISPLAY_NAME
    label_parent = args.label_parent
    description = args.description

    create_request = labelmanager_messages.LabelKey(
        displayName=display_name, parent=label_parent, description=description)
    op = labelkeys_service.Create(create_request)

    if args.async_:
      return op
    else:
      done_op = operations.WaitForOperation(
          op, 'Waiting for label [{}] to be created with [{}]'.format(
              display_name, op.name))
      return done_op
    def Run(self, args):
        labelkeys_service = labelmanager.LabelKeysService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        if args.IsSpecified('label_parent'):
            label_key = utils.GetLabelKeyFromDisplayName(
                args.LABEL_KEY_ID, args.label_parent)
        else:
            label_key = args.LABEL_KEY_ID

        delete_request = labelmanager_messages.LabelmanagerLabelKeysDeleteRequest(
            name=label_key)
        op = labelkeys_service.Delete(delete_request)

        if op.response is not None:
            response_dict = encoding.MessageToPyValue(op.response)
            del response_dict['@type']
            return response_dict
        else:
            raise operations.OperationError(op.error.message)
Example #22
0
  def Run(self, args):
    labelkeys_service = labelmanager.LabelKeysService()
    labelmanager_messages = labelmanager.LabelManagerMessages()

    if args.IsSpecified('label_parent'):
      label_key = utils.GetLabelKeyFromDisplayName(args.LABEL_KEY_ID,
                                                   args.label_parent)
    else:
      label_key = args.LABEL_KEY_ID

    delete_request = labelmanager_messages.LabelmanagerLabelKeysDeleteRequest(
        name=label_key)
    op = labelkeys_service.Delete(delete_request)

    if args.async_:
      return op
    else:
      done_op = operations.WaitForOperation(
          op, 'Waiting for label [{}] to be deleted with [{}]'.format(
              args.LABEL_KEY_ID, op.name), labelkeys_service)
      return done_op
    def Run(self, args):
        labelkeys_service = labelmanager.LabelKeysService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        if args.IsSpecified('label_parent'):
            label_key = utils.GetLabelKeyFromDisplayName(
                args.LABEL_KEY_ID, args.label_parent)
        else:
            label_key = args.LABEL_KEY_ID

        policy = iam_util.ParsePolicyFile(args.POLICY_FILE,
                                          labelmanager_messages.Policy)
        policy.version = iam_util.MAX_LIBRARY_IAM_SUPPORTED_VERSION

        set_iam_policy_request = labelmanager_messages.SetIamPolicyRequest(
            policy=policy)

        request = labelmanager_messages.LabelmanagerLabelKeysSetIamPolicyRequest(
            resource=label_key, setIamPolicyRequest=set_iam_policy_request)
        result = labelkeys_service.SetIamPolicy(request)
        iam_util.LogSetIamPolicy(label_key, 'LabelKey')
        return result
Example #24
0
  def Run(self, args):
    labelbindings_service = labelmanager.LabelBindingsService()
    labelmanager_messages = labelmanager.LabelManagerMessages()

    if args.IsSpecified('label_key'):
      label_key = args.label_key
      if args.IsSpecified('label_parent'):
        label_key = utils.GetLabelKeyFromDisplayName(args.label_key,
                                                     args.label_parent)
      label_value = utils.GetLabelValueFromDisplayName(args.LABEL_VALUE_ID,
                                                       label_key)
    else:
      label_value = args.LABEL_VALUE_ID

    binding = labelmanager_messages.LabelBinding(
        labelValue=label_value, resource=args.resource)
    create_request = labelmanager_messages.CreateLabelBindingRequest(
        labelBinding=binding)

    request = labelmanager_messages.LabelmanagerLabelValuesLabelBindingsCreateRequest(
        createLabelBindingRequest=create_request, labelValuesId=label_value)
    return labelbindings_service.Create(request)
    def Run(self, args):
        labelvalues_service = labelmanager.LabelValuesService()
        labelmanager_messages = labelmanager.LabelManagerMessages()

        if args.IsSpecified('label_parent'):
            label_key = utils.GetLabelKeyFromDisplayName(
                args.label_key, args.label_parent)
        else:
            label_key = args.label_key

        create_request = labelmanager_messages.LabelValue(
            displayName=args.DISPLAY_NAME,
            parent=label_key,
            description=args.description)
        op = labelvalues_service.Create(create_request)

        if args.async_:
            return op
        else:
            return operations.WaitForOperation(
                op,
                'Waiting for label value [{}] to be created with [{}]'.format(
                    args.DISPLAY_NAME, op.name),
                service=labelvalues_service)