def metric_create(self, project, metric_name, filter_, description): """API call: create a metric resource. See: https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.metrics/create :type project: string :param project: ID of the project in which to create the metric. :type metric_name: string :param metric_name: the name of the metric :type filter_: string :param filter_: the advanced logs filter expression defining the entries exported by the metric. :type description: string :param description: description of the metric. """ options = None parent = 'projects/%s' % (project, ) metric_pb = LogMetric(name=metric_name, filter=filter_, description=description) try: self._gax_api.create_log_metric(parent, metric_pb, options) except GaxError as exc: if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: path = 'projects/%s/metrics/%s' % (project, metric_name) raise Conflict(path) raise
def sink_create(self, project, sink_name, filter_, destination): """API call: create a sink resource. See: https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks/create :type project: string :param project: ID of the project in which to create the sink. :type sink_name: string :param sink_name: the name of the sink :type filter_: string :param filter_: the advanced logs filter expression defining the entries exported by the sink. :type destination: string :param destination: destination URI for the entries exported by the sink. """ options = None parent = 'projects/%s' % (project, ) sink_pb = LogSink(name=sink_name, filter=filter_, destination=destination) try: self._gax_api.create_sink(parent, sink_pb, options) except GaxError as exc: if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: path = 'projects/%s/sinks/%s' % (project, sink_name) raise Conflict(path) raise
def api_request(self, **kw): from gcloud.exceptions import Conflict from gcloud.exceptions import NotFound self._called_with = kw if self._raise_conflict: raise Conflict('oops') try: response, self._responses = self._responses[0], self._responses[1:] except IndexError: raise NotFound('miss') return response
def subscription_create(self, subscription_path, topic_path, ack_deadline=None, push_endpoint=None): """API call: create a subscription See: https://cloud.google.com/pubsub/reference/rest/v1/projects.subscriptions/create :type subscription_path: string :param subscription_path: the fully-qualified path of the new subscription, in format ``projects/<PROJECT>/subscriptions/<SUB_NAME>``. :type topic_path: string :param topic_path: the fully-qualified path of the topic being subscribed, in format ``projects/<PROJECT>/topics/<TOPIC_NAME>``. :type ack_deadline: int, or ``NoneType`` :param ack_deadline: the deadline (in seconds) by which messages pulled from the back-end must be acknowledged. :type push_endpoint: string, or ``NoneType`` :param push_endpoint: URL to which messages will be pushed by the back-end. If not set, the application must pull messages. :rtype: dict :returns: ``Subscription`` resource returned from the API. """ if push_endpoint is not None: push_config = PushConfig(push_endpoint=push_endpoint) else: push_config = None if ack_deadline is None: ack_deadline = 0 try: sub_pb = self._gax_api.create_subscription(subscription_path, topic_path, push_config, ack_deadline) except GaxError as exc: if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: raise Conflict(topic_path) raise return _subscription_pb_to_mapping(sub_pb)
def commit(self, project, request_pb): """Perform a ``commit`` request. :type project: string :param project: The project to connect to. This is usually your project name in the cloud console. :type request_pb: :class:`._generated.datastore_pb2.CommitRequest` :param request_pb: The request protobuf object. :rtype: :class:`._generated.datastore_pb2.CommitResponse` :returns: The returned protobuf response object. """ request_pb.project_id = project try: return self._stub.Commit(request_pb) except _Rendezvous as exc: if exc.code() == StatusCode.ABORTED: raise Conflict(exc.details()) raise
def topic_create(self, topic_path): """API call: create a topic See: https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/create :type topic_path: string :param topic_path: fully-qualified path of the new topic, in format ``projects/<PROJECT>/topics/<TOPIC_NAME>``. :rtype: dict :returns: ``Topic`` resource returned from the API. :raises: :exc:`gcloud.exceptions.Conflict` if the topic already exists """ try: topic_pb = self._gax_api.create_topic(topic_path) except GaxError as exc: if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: raise Conflict(topic_path) raise return {'name': topic_pb.name}