Search code examples
pythongoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-monitoring

create alert policy with Cloud Function and Python - positional arguments error


I want to create a new alert policy using the monitoring v3 API. I'm following this documentation

I have this cloud function:

from google.cloud import monitoring_v3
import json

client = monitoring_v3.AlertPolicyServiceClient()

alert_policy = {
        "displayName": "This is a test",
        "combiner": "OR",
        "conditions": [
            {
                "displayName": "This is a test",
                "conditionAbsent": {
                    "duration": "3900s",
                    filter: "resource.type = \"l7_lb_rule\" AND metric.type = \"logging.googleapis.com/user/metric_name_here\""
                }
            }
        ],
    }

    
    name = 'projects/my-project'
    client.create_alert_policy(name, alert_policy)

When running the function for a test I have the log error TypeError: create_alert_policy() takes from 1 to 2 positional arguments but 3 were given

What do I'm missing here?


Solution

  • You get this error because of the changes made in 2.0 release of google-cloud-monitoring where required parameters are positional parameters. See google cloud monitoring 2.0 change log for reference. To adapt to the changes, you should define the parameters in create_alert_policy() explicitly.

    client.create_alert_policy(name=name, alert_policy=alert_policy)