Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 53b8235

Browse files
authored
Make backup/restore file name a parameter [(#2248)](GoogleCloudPlatform/python-docs-samples#2248)
* Make backup/restore file a parameter Previously was hard coded as 'backup.json' * Added parameter to test function calls
1 parent 8fb8e61 commit 53b8235

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

samples/snippets/v3/alerts-client/snippets.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,17 @@ def delete_notification_channels(project_name, channel_ids, force=None):
108108

109109

110110
# [START monitoring_alert_backup_policies]
111-
def backup(project_name):
111+
def backup(project_name, backup_filename):
112112
alert_client = monitoring_v3.AlertPolicyServiceClient()
113113
channel_client = monitoring_v3.NotificationChannelServiceClient()
114114
record = {'project_name': project_name,
115115
'policies': list(alert_client.list_alert_policies(project_name)),
116116
'channels': list(channel_client.list_notification_channels(
117117
project_name))}
118-
json.dump(record, open('backup.json', 'wt'), cls=ProtoEncoder, indent=2)
119-
print('Backed up alert policies and notification channels to backup.json.')
118+
json.dump(record, open(backup_filename, 'wt'), cls=ProtoEncoder, indent=2)
119+
print('Backed up alert policies and notification channels to {}.'.format(
120+
backup_filename)
121+
)
120122

121123

122124
class ProtoEncoder(json.JSONEncoder):
@@ -136,9 +138,11 @@ def default(self, obj):
136138
# [START monitoring_alert_create_channel]
137139
# [START monitoring_alert_update_channel]
138140
# [START monitoring_alert_enable_channel]
139-
def restore(project_name):
140-
print('Loading alert policies and notification channels from backup.json.')
141-
record = json.load(open('backup.json', 'rt'))
141+
def restore(project_name, backup_filename):
142+
print('Loading alert policies and notification channels from {}.'.format(
143+
backup_filename)
144+
)
145+
record = json.load(open(backup_filename, 'rt'))
142146
is_same_project = project_name == record['project_name']
143147
# Convert dicts to AlertPolicies.
144148
policies_json = [json.dumps(policy) for policy in record['policies']]
@@ -299,11 +303,19 @@ def project_name():
299303
'backup',
300304
help=backup.__doc__
301305
)
306+
backup_parser.add_argument(
307+
'--backup_to_filename',
308+
required=True
309+
)
302310

303311
restore_parser = subparsers.add_parser(
304312
'restore',
305313
help=restore.__doc__
306314
)
315+
restore_parser.add_argument(
316+
'--restore_from_filename',
317+
required=True
318+
)
307319

308320
args = parser.parse_args()
309321

@@ -325,7 +337,7 @@ def project_name():
325337
args.notification_channel_id)
326338

327339
elif args.command == 'backup':
328-
backup(project_name())
340+
backup(project_name(), args.backup_to_filename)
329341

330342
elif args.command == 'restore':
331-
restore(project_name())
343+
restore(project_name(), args.restore_from_filename)

samples/snippets/v3/alerts-client/snippets_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ def test_replace_channels(capsys, pochan):
107107

108108

109109
def test_backup_and_restore(capsys, pochan):
110-
snippets.backup(pochan.project_name)
110+
snippets.backup(pochan.project_name, 'backup.json')
111111
out, _ = capsys.readouterr()
112112

113-
snippets.restore(pochan.project_name)
113+
snippets.restore(pochan.project_name, 'backup.json')
114114
out, _ = capsys.readouterr()
115115
assert "Updated {0}".format(pochan.alert_policy.name) in out
116116
assert "Updating channel {0}".format(

0 commit comments

Comments
 (0)