Invalid constructor input for UpdateAlertPolicyRequest

I’m trying to update an alert policy with Cloud Functions. I have the following lines of code:

filter = "resource.type = "l7_lb_rule" AND metric.type = "logging.googleapis.com/user/stuff_here"")
alert_policy = {"conditions":[{"condition_absent":{"duration": '1800s',"filter": '{}'.format(filter)}, "displayName":'test'}], "displayName":'test'), "combiner":"OR"}
policy = monitoring_v3.AlertPolicy.from_json(json.dumps(alert_policy))
client_alert.update_alert_policy(policy)

I’m running the same update with the API explorer and it’s working. However the cloud functions return me this error TypeError: Invalid constructor input for UpdateAlertPolicyRequest: display_name: "test"

I’m also wondering where I should pass the name of the alert I want to update. I tried to pass the name as a parameters or the alert_policy object but it always return me some kind of errors.

Answer

The problem is that you are passing as argument of the update_alert_policy function something that, as you can see in the source code of the library is considered an UpdateAlertPolicyRequest, the first positional argument. You need to provide your information using the alert_policy argument instead, something like:

# indicate every field you want to update and provide
# the corresponding values in the policy definition
mask = field_mask.FieldMask(paths=['display_name', 'combiner', 'conditions'])
policy = monitoring_v3.AlertPolicy(
    name='your_policy_name',
    display_name='test',
    combiner='OR',
    conditions=[
        monitoring_v3.types.AlertPolicy.Condition(
            display_name='test',
            condition_absent=monitoring_v3.types.AlertPolicy.Condition.MetricAbsence(
                duration='1800s',
                filter='resource.type = "l7_lb_rule" AND metric.type = "logging.googleapis.com/user/stuff_here"'
            )
        )
    ]
)
client.update_alert_policy(alert_policy=policy, update_mask=mask)

The library provides some snippets that may be help.