Skip to content

Commit

Permalink
Added sample scripts using V2 Client
Browse files Browse the repository at this point in the history
  • Loading branch information
naveena-maplelabs committed Feb 5, 2024
1 parent 238de43 commit 1bf86d3
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 0 deletions.
Empty file added samples/helios/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions samples/helios/helios_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Third-party import.
from cohesity_sdk.cluster.cluster_client import ClusterClient
from cohesity_sdk.helios_mcm.v1.mcm_v1_client import McmV1Client
from cohesity_sdk.helios_mcm.v2.mcm_v2_client import McmV2Client

from config import (
helios_ip,
helios_api_key,
cluster_vip,
cluster_username,
cluster_password,
cluster_domain,
)


def helios_connector():
"""
Function to create V1 Helios client.
returns: client
"""
client = McmV1Client(cluster_vip=helios_ip, api_key=helios_api_key)
return client


def helios_v2_connector():
"""
Function to create V2 Helios client.
returns: client
"""
client = McmV2Client(
cluster_vip=helios_ip,
api_key=helios_api_key,
)
return client


def helios_v2_connector_with_cluster_id():
"""
Function to create V2 Helios client with cluster Id in headers.
returns: client
"""
# Fetch the cluster Id.
cluster_client = ClusterClient(
cluster_vip=cluster_vip,
username=cluster_username,
password=cluster_password,
domain=cluster_domain,
)
cluster_id = cluster_client.platform.get_cluster().id
client = McmV2Client(
cluster_vip=helios_ip,
api_key=helios_api_key,
access_cluster_id=cluster_id,
)
return client
51 changes: 51 additions & 0 deletions samples/helios/library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from cohesity_sdk.helios_mcm.v2.model.source_registration_request_params import (
SourceRegistrationRequestParams,
)
from cohesity_sdk.helios_mcm.v2.model.vmware_source_registration_params import (
VmwareSourceRegistrationParams,
)
from cohesity_sdk.helios_mcm.v2.model.vcenter_registration_params import (
VcenterRegistrationParams,
)
from cohesity_sdk.helios_mcm.v2.model.physical_source_registration_params import (
PhysicalSourceRegistrationParams,
)


def register_vmware_source(client, source, username, password):
"""
Function to register a VMware source
: return source Id
"""
payload = SourceRegistrationRequestParams(
environment="kVMware",
vmware_params=VmwareSourceRegistrationParams(
type="kVCenter",
v_center_params=VcenterRegistrationParams(
username=username, password=password, endpoint=source
),
),
)
response = client.source.register_protection_source(payload)
return response.id


def register_physical_source(client, source):
"""
Function to register a physical source
: return sourceId
"""
try:
payload = SourceRegistrationRequestParams(
environment="kPhysical",
physical_params=PhysicalSourceRegistrationParams(
endpoint=source,
force_register=True,
physical_type="kHost",
host_type="kLinux",
),
)
response = client.source.register_protection_source(payload)
return response.id
except Exception as err:
print(err)
142 changes: 142 additions & 0 deletions samples/helios/physical_workflow_using_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import time
import random

from cohesity_sdk.helios_mcm.v2.model.create_or_update_protection_group_request import (
CreateOrUpdateProtectionGroupRequest,
)
from cohesity_sdk.helios_mcm.v2.model.create_protection_group_run_request import (
CreateProtectionGroupRunRequest,
)
from cohesity_sdk.helios_mcm.v2.model.create_or_update_protection_group_request import (
CreateOrUpdateProtectionGroupRequest,
)
from cohesity_sdk.helios_mcm.v2.model.physical_file_protection_group_params import (
PhysicalFileProtectionGroupParams,
)
from cohesity_sdk.helios_mcm.v2.model.physical_protection_group_params import (
PhysicalProtectionGroupParams,
)
from cohesity_sdk.helios_mcm.v2.model.physical_volume_protection_group_object_params import (
PhysicalVolumeProtectionGroupObjectParams,
)
from cohesity_sdk.helios_mcm.v2.model.physical_volume_protection_group_params import (
PhysicalVolumeProtectionGroupParams,
)
from config import physical_server
from library import register_physical_source
from helios_client import helios_v2_connector_with_cluster_id

# Initialise a client.
client = helios_v2_connector_with_cluster_id()
job_name = "PhysicalJob_" + str(random.randint(1, 100))


def get_storage_domain_id():
domains = client.storage_domain.get_storage_domains().storage_domains or []
for domain in domains:
if domain.name == "DefaultStorageDomain":
return domain.id
return -1


def get_policy_id():
policies = client.policy.get_protection_policies().policies or []
for policy in policies:
if policy.name == "Bronze":
return policy.id
return -1


def create_job(job_name):
try:
source_id = None
jobs = client.protection_group.get_protection_groups(is_deleted=False)[
"protection_groups"
]
# Check the job is already available.
for job in jobs:
if job["name"] == job_name:
print("Job %s is already available, skipping creation" % job_name)
return job["id"], job

# Get storage_domain id.
print("Fetching storage domains")
domain_id = get_storage_domain_id()
if domain_id == -1:
raise Exception("Storage Domain not available.")

# Check if the source is already registered.
sources = (
client.source.mcm_get_protection_sources(environments=["kPhysical"]).sources
or []
)
for source in sources:
if source.name == physical_server:
source_id = source.source_info_list[0].source_id
if not source_id:
source_id = register_physical_source(client, physical_server)
print("Source Id %s" % str(source_id))
body = CreateOrUpdateProtectionGroupRequest(
storage_domain_id=domain_id,
policy_id=get_policy_id(),
name=job_name,
environment="kPhysical",
physical_params=PhysicalProtectionGroupParams(
protection_type="kVolume",
volume_protection_type_params=PhysicalVolumeProtectionGroupParams(
objects=[PhysicalVolumeProtectionGroupObjectParams(id=source_id)]
),
),
)
resp = client.protection_group.create_protection_group(body)
return resp
except Exception as err:
print(err)
exit()


def run_job(job_id):
# Check for job run and trigger one.
runs = client.protection_group.get_protection_group_runs(job_id)["runs"]
if len(runs) == 0 or runs[0]["local_backup_info"]["status"] not in [
"Running",
"Accepted",
]:
# Trigger a job run.
body = CreateProtectionGroupRunRequest(run_type="kRegular")
client.protection_group.create_protection_group_run(job_id, body)

count = 15
while count != 0:
runs = client.protection_group.get_protection_group_runs(job_id)["runs"]
if not runs:
time.sleep(10)
continue
if runs[0]["local_backup_info"]["status"] in ["Running", "Accepted"]:
time.sleep(30)
count -= 1
else:
return True


if __name__ == "__main__":
print("Creating job")
job = create_job(job_name)
job_id = job.id
print("Job Content", job)
print("Updating job with id %s" % job_id)
resp = client.protection_group.update_protection_group(job_id, job)
print("Trigger job run")
run = run_job(job_id)
if run:
objects = client.search.search_protected_objects(protection_group_ids=[job_id])[
"objects"
]
snapshot_id = (
objects[0].latest_snapshots_info[0].local_snapshot_info["snapshotId"]
)

# Delete the job.
print("Deleting created job '%s'" % job_name)
client.protection_group.delete_protection_group(job_id, delete_snapshots=True)
print("Successfully deleted job '%s'" % job_name)
72 changes: 72 additions & 0 deletions samples/helios/view_workflow_using_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import random

from cohesity_sdk.helios_mcm.v2.model.create_or_update_protection_group_request import (
CreateOrUpdateProtectionGroupRequest,
)

from cohesity_sdk.helios_mcm.v2.model.create_view_request import CreateViewRequest
from cohesity_sdk.helios_mcm.v2.model.view_protection_group_params import (
ViewProtectionGroupParams,
)
from cohesity_sdk.helios_mcm.v2.model.view_protection_group_object_params import (
ViewProtectionGroupObjectParams,
)
from cohesity_sdk.helios_mcm.v2.model.view_protocol import ViewProtocol
from cohesity_sdk.helios_mcm.v2.model.qo_s import QoS

from helios_client import helios_v2_connector_with_cluster_id

# Initialise a client.
client = helios_v2_connector_with_cluster_id()
name = "ViewJob_" + str(random.randint(1, 100))
policy_id = client.policy.get_protection_policies().policies[0].id
domain_id = client.storage_domain.get_storage_domains().storage_domains[0].id


def create_view():
"""
Function to create a view.
"""
try:
protocol_access = ViewProtocol(type="NFS", mode="ReadWrite")
payload = CreateViewRequest(
storage_domain_id=domain_id,
name="View_" + str(random.randint(1, 100)),
category="FileServices",
protocol_access=[protocol_access],
qos={},
)
response = client.view.create_view(payload)
print(response)
return response.view_id
except Exception as err:
print(err)


try:
# Create a view, protect the view.
views = client.view.get_views().views
if not views:
view_id = create_view()
else:
view_id = views[0].view_id
view_id = create_view()
payload = CreateOrUpdateProtectionGroupRequest(
environment="kView",
name=name,
policy_id=policy_id,
storage_domain_id=domain_id,
view_params=ViewProtectionGroupParams(
objects=[ViewProtectionGroupObjectParams(id=view_id)]
),
)
print("Creating protection job, name '%s'" % payload.name)
resp = client.protection_group.create_protection_group(payload)
job_id = resp.id
# Delete the job.
print("Deleting protection job and snapshots")
resp = client.protection_group.delete_protection_group(
job_id, delete_snapshots=True
)
except Exception as err:
print(err)
Loading

0 comments on commit 1bf86d3

Please sign in to comment.