mirror of
https://github.com/vmware/vsphere-automation-sdk-python.git
synced 2024-11-22 01:39:58 -05:00
Samples for VMC M9 release
Signed-off-by: Kunal Singh <singhk@vmware.com>
This commit is contained in:
parent
0f9cd1607c
commit
1807e8793f
111
samples/vmc/draas/activate_srm_ops.py
Normal file
111
samples/vmc/draas/activate_srm_ops.py
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
* *******************************************************
|
||||||
|
* Copyright (c) VMware, Inc. 2019. 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.'
|
||||||
|
|
||||||
|
import time
|
||||||
|
from samples.vmc.helpers.sample_cli import parser, optional_args
|
||||||
|
|
||||||
|
from vmware.vapi.vmc.vmc_draas_client import create_vmc_draas_client
|
||||||
|
from vmware.vapi.vmc.client import create_vmc_client
|
||||||
|
|
||||||
|
|
||||||
|
class SrmActivationOperations(object):
|
||||||
|
"""
|
||||||
|
Demonstrates VMware Cloud Disaster Recovery As a Service (DRaaS)
|
||||||
|
Site Recovery Manager (SRM) Activation Operations
|
||||||
|
|
||||||
|
Sample Prerequisites:
|
||||||
|
- An organization associated with the calling user.
|
||||||
|
- A SDDC in the organization with SRM Addon activated.
|
||||||
|
- Refresh Token
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
optional_args.add_argument('-c', '--cleardata',
|
||||||
|
action='store_true',
|
||||||
|
help='Clean up after sample run')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
self.org_id = args.org_id
|
||||||
|
self.sddc_id = args.sddc_id
|
||||||
|
self.query_wait_time = 100
|
||||||
|
self.max_wait_time = 900
|
||||||
|
|
||||||
|
self.cleanup = args.cleardata
|
||||||
|
self.vmc_client = create_vmc_client(refresh_token=args.refresh_token)
|
||||||
|
self.draas_client = create_vmc_draas_client(refresh_token=args.refresh_token)
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
# Check if the organization exists
|
||||||
|
orgs = self.vmc_client.Orgs.list()
|
||||||
|
if self.org_id not in [org.id for org in orgs]:
|
||||||
|
raise ValueError("Org with ID {} doesn't exist".format(self.org_id))
|
||||||
|
|
||||||
|
# Check if the SDDC exists
|
||||||
|
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
|
||||||
|
if self.sddc_id not in [sddc.id for sddc in sddcs]:
|
||||||
|
raise ValueError("SDDC with ID {} doesn't exist in org {}".
|
||||||
|
format(self.sddc_id, self.org_id))
|
||||||
|
|
||||||
|
# Activate SRM Addon in a SDDC
|
||||||
|
def activate_srm(self):
|
||||||
|
if self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id).site_recovery_state != "ACTIVATED":
|
||||||
|
srm_activation = self.draas_client.SiteRecovery.post(self.org_id,
|
||||||
|
self.sddc_id,
|
||||||
|
activate_site_recovery_config=None)
|
||||||
|
print("Activation of SRM {} : {}".format(srm_activation.status,
|
||||||
|
srm_activation.start_time))
|
||||||
|
self.query_activation_status()
|
||||||
|
else:
|
||||||
|
print("SRM already activated in {}".format(self.sddc_id))
|
||||||
|
|
||||||
|
'''
|
||||||
|
Note: There is no Task API to query activation status, though there is a task structure
|
||||||
|
Hence querying the SRM activation status with resource_id & state for the status.
|
||||||
|
'''
|
||||||
|
def query_activation_status(self):
|
||||||
|
timeout = time.time() + self.max_wait_time
|
||||||
|
while time.time() < timeout:
|
||||||
|
time.sleep(self.query_wait_time)
|
||||||
|
status = self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id)
|
||||||
|
if status.site_recovery_state in ['ACTIVATED', 'DEACTIVATED', 'CANCELLED', 'FAILED']:
|
||||||
|
print("Site Recovery (DRaaS) Activation Status in {} : {}"
|
||||||
|
.format(status.updated, status.site_recovery_state))
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print("Site Recovery (DRaaS) Activation Status in {} : {}"
|
||||||
|
.format(status.updated, status.site_recovery_state))
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
raise Exception("Max time out reached {}".format(self.max_wait_time))
|
||||||
|
|
||||||
|
# De-activate SRM Addon in a SDDC. This is a forceful operation as force=True
|
||||||
|
def deactivate_srm(self):
|
||||||
|
if self.cleanup:
|
||||||
|
print("Deactivating SRM")
|
||||||
|
self.draas_client.SiteRecovery.delete(self.org_id,
|
||||||
|
self.sddc_id,
|
||||||
|
force=True)
|
||||||
|
self.query_activation_status()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
srm_activation_ops = SrmActivationOperations()
|
||||||
|
srm_activation_ops.activate_srm()
|
||||||
|
srm_activation_ops.deactivate_srm()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
123
samples/vmc/draas/deploy_additional_node.py
Normal file
123
samples/vmc/draas/deploy_additional_node.py
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
* *******************************************************
|
||||||
|
* Copyright (c) VMware, Inc. 2019. 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.'
|
||||||
|
|
||||||
|
import time
|
||||||
|
from samples.vmc.helpers.sample_cli import parser, optional_args
|
||||||
|
|
||||||
|
from vmware.vapi.vmc.vmc_draas_client import create_vmc_draas_client
|
||||||
|
from vmware.vapi.vmc.client import create_vmc_client
|
||||||
|
from com.vmware.vmc.draas.model_client import ProvisionSrmConfig
|
||||||
|
|
||||||
|
|
||||||
|
class DeployAdditionalNode(object):
|
||||||
|
"""
|
||||||
|
Demonstrates VMware Cloud Disaster Recovery As a Service (DRaaS)
|
||||||
|
Additional Site Recovery Manager (SRM) Node Deployment operations
|
||||||
|
|
||||||
|
Sample Prerequisites:
|
||||||
|
- An organization associated with the calling user.
|
||||||
|
- A SDDC in the organization with SRM Addon activated
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
optional_args.add_argument('-c', '--cleardata',
|
||||||
|
action='store_true',
|
||||||
|
help='Clean up after sample run')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
self.org_id = args.org_id
|
||||||
|
self.sddc_id = args.sddc_id
|
||||||
|
self.wait_time = 100
|
||||||
|
self.max_wait_time = 900
|
||||||
|
self.node_extension_id = 'com.vcDr1'
|
||||||
|
|
||||||
|
self.cleanup = args.cleardata
|
||||||
|
self.vmc_client = create_vmc_client(refresh_token=args.refresh_token)
|
||||||
|
self.draas_client = create_vmc_draas_client(refresh_token=args.refresh_token)
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
# Check if the organization exists
|
||||||
|
orgs = self.vmc_client.Orgs.list()
|
||||||
|
if self.org_id not in [org.id for org in orgs]:
|
||||||
|
raise ValueError("Org with ID {} doesn't exist".format(self.org_id))
|
||||||
|
|
||||||
|
# Check if the SDDC exists
|
||||||
|
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
|
||||||
|
if self.sddc_id not in [sddc.id for sddc in sddcs]:
|
||||||
|
raise ValueError("SDDC with ID {} doesn't exist in org {}".
|
||||||
|
format(self.sddc_id, self.org_id))
|
||||||
|
|
||||||
|
# Check if the SRM Add-on is activated in VMC
|
||||||
|
if "ACTIVATED" != self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id).site_recovery_state:
|
||||||
|
raise ValueError("DRaaS is not activated in SDDC with ID {} & org with ID {}".
|
||||||
|
format(self.sddc_id, self.org_id))
|
||||||
|
|
||||||
|
# Deploy Additional SRM Node
|
||||||
|
def deploy_srm(self):
|
||||||
|
deploy_srm = self.draas_client.SiteRecoverySrmNodes.post(
|
||||||
|
self.org_id,
|
||||||
|
self.sddc_id,
|
||||||
|
ProvisionSrmConfig(srm_extension_key_suffix=self.node_extension_id))
|
||||||
|
print('Srm Additional Node Deployment Started {}'.format(deploy_srm.start_time))
|
||||||
|
return deploy_srm.resource_id
|
||||||
|
|
||||||
|
'''
|
||||||
|
Note: There is no Task API to query activation status, though there is a task structure.
|
||||||
|
Hence querying the SRM activation status with resource_id and state for the status.
|
||||||
|
'''
|
||||||
|
def query_deployment(self, deployed_node_id):
|
||||||
|
srm_node_details = self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id).srm_nodes
|
||||||
|
for node_index in range(len(srm_node_details)):
|
||||||
|
if deployed_node_id == srm_node_details[node_index].id:
|
||||||
|
timeout = time.time() + self.max_wait_time
|
||||||
|
while time.time() < timeout:
|
||||||
|
node_details = self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id)
|
||||||
|
time.sleep(self.wait_time)
|
||||||
|
if node_details.srm_nodes[node_index].state in ['READY', 'DELETING', 'CANCELLED', 'FAILED']:
|
||||||
|
print("Site Recovery (DRaaS) Additonal Node Deployment Status {} : {}"
|
||||||
|
.format(node_details.updated,
|
||||||
|
node_details.srm_nodes[node_index].state))
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print("Site Recovery (DRaaS) Additonal Node Deployment Status {} : {}"
|
||||||
|
.format(node_details.updated,
|
||||||
|
node_details.srm_nodes[node_index].state))
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
raise Exception("Max time out reached {}".format(self.max_wait_time))
|
||||||
|
node_index += 1
|
||||||
|
|
||||||
|
# Deleting the additional node if with --cleardata flag
|
||||||
|
def delete_node(self, node_id):
|
||||||
|
if self.cleanup:
|
||||||
|
print("Removing the Additional Node")
|
||||||
|
self.draas_client.SiteRecoverySrmNodes.delete(
|
||||||
|
self.org_id,
|
||||||
|
self.sddc_id,
|
||||||
|
node_id)
|
||||||
|
self.query_deployment(node_id)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
deploy_addtional_nodes = DeployAdditionalNode()
|
||||||
|
deploy_addtional_nodes.setup()
|
||||||
|
srm_node_id = deploy_addtional_nodes.deploy_srm()
|
||||||
|
deploy_addtional_nodes.query_deployment(srm_node_id)
|
||||||
|
deploy_addtional_nodes.delete_node(srm_node_id)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
30
samples/vmc/draas/get_srm_info.py
Normal file
30
samples/vmc/draas/get_srm_info.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import argparse
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from vmware.vapi.vmc.vmc_draas_client import create_vmc_draas_client
|
||||||
|
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument(
|
||||||
|
'--refresh_token',
|
||||||
|
required=True,
|
||||||
|
help='VMware Cloud API refresh token')
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--org_id',
|
||||||
|
required=True,
|
||||||
|
help='Organization identifier.')
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--sddc_id',
|
||||||
|
required=True,
|
||||||
|
help='Sddc Identifier.')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
refresh_token = args.refresh_token
|
||||||
|
org_id = args.org_id
|
||||||
|
sddc_id = args.sddc_id
|
||||||
|
|
||||||
|
client = create_vmc_draas_client(refresh_token)
|
||||||
|
site_recovery_activation_task = client.SiteRecovery.get(org_id, sddc_id)
|
||||||
|
print(site_recovery_activation_task)
|
76
samples/vsphere/appliances/get_service.py
Normal file
76
samples/vsphere/appliances/get_service.py
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
"""
|
||||||
|
* *******************************************************
|
||||||
|
* Copyright (c) VMware, Inc. 2019. 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.'
|
||||||
|
__vcenter_version__ = '6.7+'
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from vmware.vapi.vsphere.client import create_vsphere_client
|
||||||
|
|
||||||
|
from samples.vsphere.common import (sample_cli, sample_util)
|
||||||
|
from samples.vsphere.common.ssl_helper import get_unverified_session
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Description: Demonstrates services api workflow
|
||||||
|
1.Stop a running service
|
||||||
|
2.Get details of stopped service
|
||||||
|
3.Start the service stopped in step 2
|
||||||
|
4.Get details of service
|
||||||
|
5.Restart the service
|
||||||
|
6.Get details of service
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
parser = sample_cli.build_arg_parser()
|
||||||
|
parser.add_argument(
|
||||||
|
'--service_name',
|
||||||
|
action='store',
|
||||||
|
required=True,
|
||||||
|
help='Specify servicename for all stop/start/restart operations')
|
||||||
|
args = sample_util.process_cli_args(parser.parse_args())
|
||||||
|
service_name = args.service_name
|
||||||
|
session = get_unverified_session() if args.skipverification else None
|
||||||
|
client = create_vsphere_client(server=args.server,
|
||||||
|
username=args.username,
|
||||||
|
password=args.password,
|
||||||
|
session=session)
|
||||||
|
|
||||||
|
appliance_service = client.appliance.Services
|
||||||
|
service_list = appliance_service.list()
|
||||||
|
|
||||||
|
|
||||||
|
def ouput_display(info, service_name):
|
||||||
|
print("Service : {}".format(service_name))
|
||||||
|
print("Description : {}".format(info.description))
|
||||||
|
print("state : {}".format(info.state))
|
||||||
|
print("-----------------------------------")
|
||||||
|
|
||||||
|
|
||||||
|
if service_name not in service_list:
|
||||||
|
raise ValueError('Service with service name {} does not exists'.format(service_name))
|
||||||
|
|
||||||
|
print("Example: Stopping service : {}\n".format(service_name))
|
||||||
|
appliance_service.stop(service_name)
|
||||||
|
service_state = appliance_service.get(service_name)
|
||||||
|
ouput_display(service_state, service_name)
|
||||||
|
print("Example: Starting service : {}\n".format(service_name))
|
||||||
|
appliance_service.start(service_name)
|
||||||
|
service_state = appliance_service.get(service_name)
|
||||||
|
ouput_display(service_state, service_name)
|
||||||
|
print("Example: Restarting service : {}\n" .format(service_name))
|
||||||
|
appliance_service.restart(service_name)
|
||||||
|
print("Example: Getting service : {}\n".format(service_name))
|
||||||
|
service_state = appliance_service.get(service_name)
|
||||||
|
ouput_display(service_state, service_name)
|
45
samples/vsphere/appliances/list_service.py
Normal file
45
samples/vsphere/appliances/list_service.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
|
* *******************************************************
|
||||||
|
* Copyright (c) VMware, Inc. 2019. 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.'
|
||||||
|
__vcenter_version__ = '6.7+'
|
||||||
|
|
||||||
|
from vmware.vapi.vsphere.client import create_vsphere_client
|
||||||
|
|
||||||
|
from samples.vsphere.common import (sample_cli, sample_util)
|
||||||
|
from samples.vsphere.common.ssl_helper import get_unverified_session
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Description: Demonstrates services api workflow
|
||||||
|
1.List all services
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
parser = sample_cli.build_arg_parser()
|
||||||
|
args = sample_util.process_cli_args(parser.parse_args())
|
||||||
|
session = get_unverified_session() if args.skipverification else None
|
||||||
|
client = create_vsphere_client(server=args.server,
|
||||||
|
username=args.username,
|
||||||
|
password=args.password,
|
||||||
|
session=session)
|
||||||
|
|
||||||
|
service_list = client.appliance.Services.list()
|
||||||
|
|
||||||
|
print("Example: List Appliance Services:")
|
||||||
|
print("-------------------\n")
|
||||||
|
for key, values in service_list.items():
|
||||||
|
print("Service Name : {} ".format(key))
|
||||||
|
print("value : {}".format(values.description))
|
||||||
|
print("State: {} \n".format(values.state))
|
Loading…
Reference in New Issue
Block a user