|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2018 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Cloud\Samples\Monitoring; |
| 19 | + |
| 20 | +use Symfony\Component\Console\Application; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Input\InputArgument; |
| 23 | +use Symfony\Component\Console\Input\InputDefinition; |
| 24 | + |
| 25 | +# Includes the autoloader for libraries installed with composer |
| 26 | +require __DIR__ . '/vendor/autoload.php'; |
| 27 | + |
| 28 | +$application = new Application('Stackdriver Monitoring Alerts'); |
| 29 | + |
| 30 | +$inputDefinition = new InputDefinition([ |
| 31 | + new InputArgument('project_id', InputArgument::REQUIRED, 'The project id'), |
| 32 | +]); |
| 33 | + |
| 34 | +$application->add(new Command('backup-policies')) |
| 35 | + ->setDefinition($inputDefinition) |
| 36 | + ->setDescription('Back up alert policies.') |
| 37 | + ->setCode(function ($input, $output) { |
| 38 | + alert_backup_policies( |
| 39 | + $input->getArgument('project_id') |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | +$application->add(new Command('create-channel')) |
| 44 | + ->setDefinition($inputDefinition) |
| 45 | + ->setDescription('Create a notification channel.') |
| 46 | + ->setCode(function ($input, $output) { |
| 47 | + alert_create_channel( |
| 48 | + $input->getArgument('project_id') |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | +$application->add(new Command('create-policy')) |
| 53 | + ->setDefinition($inputDefinition) |
| 54 | + ->setDescription('Create an alert policy.') |
| 55 | + ->setCode(function ($input, $output) { |
| 56 | + alert_create_policy( |
| 57 | + $input->getArgument('project_id') |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | +$application->add(new Command('delete-channel')) |
| 62 | + ->setDefinition(clone $inputDefinition) |
| 63 | + ->addArgument('channel_id', InputArgument::REQUIRED, 'The notification channel ID to delete') |
| 64 | + ->setDescription('Delete a notification channel.') |
| 65 | + ->setCode(function ($input, $output) { |
| 66 | + alert_delete_channel( |
| 67 | + $input->getArgument('project_id'), |
| 68 | + $input->getArgument('channel_id') |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | +$application->add(new Command('enable-policies')) |
| 73 | + ->setDefinition(clone $inputDefinition) |
| 74 | + ->addArgument('enable', InputArgument::OPTIONAL, 'Enable or disable the polcies', true) |
| 75 | + ->addArgument('filter', InputArgument::OPTIONAL, 'Only enable/disable alert policies that match a filter.') |
| 76 | + ->setDescription('Enable or disable alert policies in a project.') |
| 77 | + ->setCode(function ($input, $output) { |
| 78 | + alert_enable_policies( |
| 79 | + $input->getArgument('project_id'), |
| 80 | + $input->getArgument('enable'), |
| 81 | + $input->getArgument('filter') |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | +$application->add(new Command('restore-policies')) |
| 86 | + ->setDefinition($inputDefinition) |
| 87 | + ->setDescription('Restore alert policies from a backup.') |
| 88 | + ->setCode(function ($input, $output) { |
| 89 | + alert_restore_policies( |
| 90 | + $input->getArgument('project_id') |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | +$application->add(new Command('list-policies')) |
| 95 | + ->setDefinition($inputDefinition) |
| 96 | + ->setDescription('List alert policies.') |
| 97 | + ->setCode(function ($input, $output) { |
| 98 | + alert_list_policies( |
| 99 | + $input->getArgument('project_id') |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | +$application->add(new Command('list-channels')) |
| 104 | + ->setDefinition($inputDefinition) |
| 105 | + ->setDescription('List alert channels.') |
| 106 | + ->setCode(function ($input, $output) { |
| 107 | + alert_list_channels( |
| 108 | + $input->getArgument('project_id') |
| 109 | + ); |
| 110 | + }); |
| 111 | +$application->add(new Command('replace-channels')) |
| 112 | + ->setDefinition(clone $inputDefinition) |
| 113 | + ->addArgument('policy_id', InputArgument::REQUIRED, 'The policy id') |
| 114 | + ->addArgument('channel_id', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'list of channel ids') |
| 115 | + ->setDescription('Replace alert channels.') |
| 116 | + ->setCode(function ($input, $output) { |
| 117 | + alert_replace_channels( |
| 118 | + $input->getArgument('project_id'), |
| 119 | + $input->getArgument('policy_id'), |
| 120 | + (array) $input->getArgument('channel_id') |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | +// for testing |
| 125 | +if (getenv('PHPUNIT_TESTS') === '1') { |
| 126 | + return $application; |
| 127 | +} |
| 128 | + |
| 129 | +$application->run(); |
0 commit comments