This repository was archived by the owner on Sep 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 304
Add backup_job_crud Sample Script #86
Open
kmruddy
wants to merge
1
commit into
vmware-archive:master
Choose a base branch
from
kmruddy:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| """ | ||
| * ******************************************************* | ||
| * Copyright (c) VMware, Inc. 2017. All Rights Reserved. | ||
| * SPDX-License-Identifier: MIT | ||
| * ******************************************************* | ||
| * | ||
| * DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, | ||
| * EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED | ||
| * WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, | ||
| * NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| """ | ||
|
|
||
| __author__ = 'VMware, Inc.' | ||
| __copyright__ = 'Copyright 2017 VMware, Inc. All rights reserved.' | ||
| __vcenter_version__ = '6.7+' | ||
|
|
||
| import argparse | ||
| from tabulate import tabulate | ||
| from samples.vsphere.common import vapiconnect | ||
| from com.vmware.appliance.recovery.backup_client import Job | ||
|
|
||
|
|
||
| class BackupJob(object): | ||
| """ | ||
| Demonstrates backup job operations | ||
|
|
||
| Retrieves backup job details from vCenter and prints the data in | ||
| tabular format | ||
|
|
||
| Prerequisites: | ||
| - vCenter | ||
| - Backup operation is performed on the vCenter either manually or | ||
| by scheduled backups | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self.stub_config = None | ||
|
|
||
| def setup(self): | ||
| parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
|
|
||
| parser.add_argument('--server', | ||
| required=True, | ||
| action='store', | ||
| help='FQDN of the VCSA') | ||
|
|
||
| parser.add_argument('--server-user', | ||
| required=True, | ||
| action='store', | ||
| help='Username for the VCSA') | ||
|
|
||
| parser.add_argument('--server-password', | ||
| required=True, | ||
| action='store', | ||
| help='Password for the VCSA') | ||
|
|
||
| parser.add_argument('-v', '--skipverification', | ||
| required=False, | ||
| action='store_true', | ||
| help='Skip SSL Verification') | ||
|
|
||
| parser.add_argument('--location', | ||
| required=False, | ||
| action='store', | ||
| help='URL of the backup location') | ||
|
|
||
| parser.add_argument('--location-user', | ||
| required=False, | ||
| action='store', | ||
| help='Username for the given location') | ||
|
|
||
| parser.add_argument('--location-password', | ||
| required=False, | ||
| action='store', | ||
| help='Password for the given location') | ||
|
|
||
| parser.add_argument('--backup-password', | ||
| required=False, | ||
| action='store', | ||
| help='Password for the backup') | ||
|
|
||
| parser.add_argument('--backup-comment', | ||
| required=False, | ||
| action='store', | ||
| help='Comment for the backup') | ||
|
|
||
| parser.add_argument('-lb', '--list-backup', | ||
| required=False, | ||
| action='store_true', | ||
| help='Switch to list backup jobs') | ||
|
|
||
| parser.add_argument('-gb', '--get-backup', | ||
| required=False, | ||
| action='store', | ||
| help='Backup Job ID to Get Information About') | ||
|
|
||
| parser.add_argument('-nb', '--create-backup', | ||
| required=False, | ||
| action='store_true', | ||
| help='Switch to create backup job') | ||
|
|
||
| parser.add_argument('-cb', '--cancel-backup', | ||
| required=False, | ||
| action='store', | ||
| help='Backup Job ID to Cancel Job') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| self.server = args.server | ||
| self.server_user = args.server_user | ||
| self.server_password = args.server_password | ||
| self.location = args.location | ||
| self.location_user = args.location_user | ||
| self.location_password = args.location_password | ||
| self.backup_password = args.backup_password | ||
| self.backup_comment = args.backup_comment | ||
| self.list_backup = args.list_backup | ||
| self.get_backup = args.get_backup | ||
| self.create_backup = args.create_backup | ||
| self.cancel_backup = args.cancel_backup | ||
|
|
||
| # Connect to vAPI services | ||
| self.stub_config = vapiconnect.connect( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you try to use the new vsphere_client. e.g: |
||
| host=self.server, | ||
| user=self.server_user, | ||
| pwd=self.server_password, | ||
| skip_verification="TRUE") | ||
|
|
||
| self.job_client = Job(self.stub_config) | ||
|
|
||
| def list_backup_job(self): | ||
| job_list = self.job_client.list() | ||
| self.print_output(job_list) | ||
|
|
||
| def get_backup_job(self): | ||
| self.print_output(self.get_backup) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the API call is missing here. |
||
|
|
||
| def create_backup_job(self): | ||
| backup_type = (self.location.split(':')[0]).upper() | ||
| piece = self.job_client.BackupRequest( | ||
| parts = ["common"], | ||
| backup_password = self.backup_password, | ||
| location_type = backup_type, | ||
| location = self.location, | ||
| location_user = self.location_user, | ||
| location_password = self.location_password, | ||
| comment = self.backup_comment) | ||
|
|
||
| job_create = self.job_client.create(piece) | ||
| self.print_output(job_create.id) | ||
|
|
||
|
|
||
| def cancel_backup_job(self): | ||
| job_cancel = self.job_client.cancel(self.cancel_backup) | ||
| self.print_output(self.cancel_backup) | ||
|
|
||
| def print_output(self, job): | ||
| table = [] | ||
|
|
||
| if type(job) is list: | ||
| for bkupjob in job: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have some duplicated code here. Maybe we can extract the common code between if/else to a separate method? |
||
| info = self.job_client.get(bkupjob) | ||
| if info.end_time is None: | ||
| duration = None | ||
| else: | ||
| duration = info.end_time - info.start_time | ||
| row = [info.id, | ||
| info.state.capitalize(), | ||
| info.start_time.strftime("%b %d %Y %H:%M"), | ||
| duration] | ||
| table.append(row) | ||
| else: | ||
| info = self.job_client.get(job) | ||
| if info.end_time is None: | ||
| duration = None | ||
| else: | ||
| duration = info.end_time - info.start_time | ||
| row = [info.id, | ||
| info.state.capitalize(), | ||
| info.start_time.strftime("%b %d %Y %H:%M"), | ||
| duration] | ||
| table.append(row) | ||
|
|
||
| headers = ["ID", "Status", "Start time", "Duration"] | ||
| print(tabulate(table, headers)) | ||
|
|
||
| def main(): | ||
| backup_job = BackupJob() | ||
| backup_job.setup() | ||
| if backup_job.list_backup: | ||
| backup_job.list_backup_job() | ||
| if backup_job.get_backup: | ||
| backup_job.get_backup_job() | ||
| if backup_job.create_backup: | ||
| backup_job.create_backup_job() | ||
| if backup_job.cancel_backup: | ||
| backup_job.cancel_backup_job() | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed as we have a copyright header above. We should change the above to 2018.