Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make backup/restore file name a parameter #2248

Merged
merged 2 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions monitoring/api/v3/alerts-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ def delete_notification_channels(project_name, channel_ids, force=None):


# [START monitoring_alert_backup_policies]
def backup(project_name):
def backup(project_name, backup_filename):
alert_client = monitoring_v3.AlertPolicyServiceClient()
channel_client = monitoring_v3.NotificationChannelServiceClient()
record = {'project_name': project_name,
'policies': list(alert_client.list_alert_policies(project_name)),
'channels': list(channel_client.list_notification_channels(
project_name))}
json.dump(record, open('backup.json', 'wt'), cls=ProtoEncoder, indent=2)
print('Backed up alert policies and notification channels to backup.json.')
json.dump(record, open(backup_filename, 'wt'), cls=ProtoEncoder, indent=2)
print('Backed up alert policies and notification channels to {}.'.format(
backup_filename)
)


class ProtoEncoder(json.JSONEncoder):
Expand All @@ -136,9 +138,11 @@ def default(self, obj):
# [START monitoring_alert_create_channel]
engelke marked this conversation as resolved.
Show resolved Hide resolved
# [START monitoring_alert_update_channel]
# [START monitoring_alert_enable_channel]
def restore(project_name):
print('Loading alert policies and notification channels from backup.json.')
record = json.load(open('backup.json', 'rt'))
def restore(project_name, backup_filename):
print('Loading alert policies and notification channels from {}.'.format(
backup_filename)
)
record = json.load(open(backup_filename, 'rt'))
is_same_project = project_name == record['project_name']
# Convert dicts to AlertPolicies.
policies_json = [json.dumps(policy) for policy in record['policies']]
Expand Down Expand Up @@ -299,11 +303,19 @@ def project_name():
'backup',
help=backup.__doc__
)
backup_parser.add_argument(
'--backup_to_filename',
required=True
)

restore_parser = subparsers.add_parser(
'restore',
help=restore.__doc__
)
restore_parser.add_argument(
'--restore_from_filename',
required=True
)

args = parser.parse_args()

Expand All @@ -325,7 +337,7 @@ def project_name():
args.notification_channel_id)

elif args.command == 'backup':
backup(project_name())
backup(project_name(), args.backup_to_filename)

elif args.command == 'restore':
restore(project_name())
restore(project_name(), args.restore_from_filename)
4 changes: 2 additions & 2 deletions monitoring/api/v3/alerts-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def test_replace_channels(capsys, pochan):


def test_backup_and_restore(capsys, pochan):
snippets.backup(pochan.project_name)
snippets.backup(pochan.project_name, 'backup.json')
out, _ = capsys.readouterr()

snippets.restore(pochan.project_name)
snippets.restore(pochan.project_name, 'backup.json')
out, _ = capsys.readouterr()
assert "Updated {0}".format(pochan.alert_policy.name) in out
assert "Updating channel {0}".format(
Expand Down