2424namespace Google \Cloud \Samples \Monitoring ;
2525
2626// [START monitoring_alert_enable_policies]
27- use Google \Cloud \Monitoring \V3 \AlertPolicyServiceClient ;
27+ use Google \Cloud \Monitoring \V3 \Client \AlertPolicyServiceClient ;
28+ use Google \Cloud \Monitoring \V3 \ListAlertPoliciesRequest ;
29+ use Google \Cloud \Monitoring \V3 \UpdateAlertPolicyRequest ;
2830use Google \Protobuf \FieldMask ;
2931
3032/**
@@ -40,11 +42,12 @@ function alert_enable_policies($projectId, $enable = true, $filter = null)
4042 $ alertClient = new AlertPolicyServiceClient ([
4143 'projectId ' => $ projectId ,
4244 ]);
43- $ projectName = $ alertClient ->projectName ($ projectId );
45+ $ projectName = 'projects/ ' . $ projectId ;
46+ $ listAlertPoliciesRequest = (new ListAlertPoliciesRequest ())
47+ ->setName ($ projectName )
48+ ->setFilter ($ filter );
4449
45- $ policies = $ alertClient ->listAlertPolicies ($ projectName , [
46- 'filter ' => $ filter
47- ]);
50+ $ policies = $ alertClient ->listAlertPolicies ($ listAlertPoliciesRequest );
4851 foreach ($ policies ->iterateAllElements () as $ policy ) {
4952 $ isEnabled = $ policy ->getEnabled ()->getValue ();
5053 if ($ enable == $ isEnabled ) {
@@ -56,9 +59,10 @@ function alert_enable_policies($projectId, $enable = true, $filter = null)
5659 $ policy ->getEnabled ()->setValue ((bool ) $ enable );
5760 $ mask = new FieldMask ();
5861 $ mask ->setPaths (['enabled ' ]);
59- $ alertClient ->updateAlertPolicy ($ policy , [
60- 'updateMask ' => $ mask
61- ]);
62+ $ updateAlertPolicyRequest = (new UpdateAlertPolicyRequest ())
63+ ->setAlertPolicy ($ policy )
64+ ->setUpdateMask ($ mask );
65+ $ alertClient ->updateAlertPolicy ($ updateAlertPolicyRequest );
6266 printf ('%s %s ' . PHP_EOL ,
6367 $ enable ? 'Enabled ' : 'Disabled ' ,
6468 $ policy ->getName ()
0 commit comments