def test_update_alert_policy(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" expected_response = {"name": name, "display_name": display_name} expected_response = alert_pb2.AlertPolicy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.AlertPolicyServiceClient() # Setup Request alert_policy = {} response = client.update_alert_policy(alert_policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = alert_service_pb2.UpdateAlertPolicyRequest( alert_policy=alert_policy ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_alert_policy(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' expected_response = {'name': name, 'display_name': display_name} expected_response = alert_pb2.AlertPolicy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = monitoring_v3.AlertPolicyServiceClient(channel=channel) # Setup Request alert_policy = {} response = client.update_alert_policy(alert_policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = alert_service_pb2.UpdateAlertPolicyRequest( alert_policy=alert_policy) actual_request = channel.requests[0][1] assert expected_request == actual_request
def update_alert_policy( self, alert_policy, update_mask=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Updates an alerting policy. You can either replace the entire policy with a new one or replace only certain fields in the current alerting policy by specifying the fields to be updated via ``updateMask``. Returns the updated alerting policy. Example: >>> from google.cloud import monitoring_v3 >>> >>> client = monitoring_v3.AlertPolicyServiceClient() >>> >>> # TODO: Initialize `alert_policy`: >>> alert_policy = {} >>> >>> response = client.update_alert_policy(alert_policy) Args: alert_policy (Union[dict, ~google.cloud.monitoring_v3.types.AlertPolicy]): Required. The updated alerting policy or the updated values for the fields listed in ``update_mask``. If ``update_mask`` is not empty, any fields in this policy that are not in ``update_mask`` are ignored. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.monitoring_v3.types.AlertPolicy` update_mask (Union[dict, ~google.cloud.monitoring_v3.types.FieldMask]): Optional. A list of alerting policy field names. If this field is not empty, each listed field in the existing alerting policy is set to the value of the corresponding field in the supplied policy (``alert_policy``), or to the field's default value if the field is not in the supplied alerting policy. Fields not listed retain their previous value. Examples of valid field masks include ``display_name``, ``documentation``, ``documentation.content``, ``documentation.mime_type``, ``user_labels``, ``user_label.nameofkey``, ``enabled``, ``conditions``, ``combiner``, etc. If this field is empty, then the supplied alerting policy replaces the existing policy. It is the same as deleting the existing policy and adding the supplied policy, except for the following: - The new policy will have the same ``[ALERT_POLICY_ID]`` as the former policy. This gives you continuity with the former policy in your notifications and incidents. - Conditions in the new policy will keep their former ``[CONDITION_ID]`` if the supplied condition includes the ``name`` field with that ``[CONDITION_ID]``. If the supplied condition omits the ``name`` field, then a new ``[CONDITION_ID]`` is created. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.monitoring_v3.types.FieldMask` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.monitoring_v3.types.AlertPolicy` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ if metadata is None: metadata = [] metadata = list(metadata) # Wrap the transport method to add retry and timeout logic. if "update_alert_policy" not in self._inner_api_calls: self._inner_api_calls[ "update_alert_policy"] = google.api_core.gapic_v1.method.wrap_method( self.transport.update_alert_policy, default_retry=self._method_configs["UpdateAlertPolicy"]. retry, default_timeout=self._method_configs["UpdateAlertPolicy"]. timeout, client_info=self._client_info, ) request = alert_service_pb2.UpdateAlertPolicyRequest( alert_policy=alert_policy, update_mask=update_mask) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("alert_policy.name", alert_policy.name)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header) metadata.append(routing_metadata) return self._inner_api_calls["update_alert_policy"](request, retry=retry, timeout=timeout, metadata=metadata)