cohesity / management-sdk-python

Cohesity Management SDK for Python.
Apache License 2.0
21 stars 20 forks source link

Unable to update the protection job #79

Closed anusajith closed 1 year ago

anusajith commented 1 year ago

🐛 Bug Report

Hey team,

I am trying to update an existing Cohesity view under Data protection--> Protection, by adding one more view to it as shown below.

image When I am doing though the api , it just throws this error message:

cohesity_management_sdk.exceptions.api_exception.APIException: Response status code: 500, Response message: Could not list all the views requested for backup. Either some do not exist, or the user does not have access to it, or all views are not in same view box.

Even tried to update just the description of the protection job, then also it threw the same error.

Steps to Reproduce:

  1. Created a view under SmartFiles--> Views (using cohesity_client.views.create_view(view_body)) eg: test_view
  2. Protected the view by creating a Remote Adapter (using cohesity_client.protection_jobs.create_protection_job(body)) eg: test_remote_adapter
  3. Trying to add the newly created view to an existing Cohesity View in Data protection --> Protection (using cohesity_client.protection_jobs.update_protection_job(body, job_id))

Regarding the protection job:

  1. Got the specific protection job from this list using name:- cohesity_client.protection_jobs.get_protection_jobs()
  2. Just tried to update description/ soure_ids of the protection job
  3. Getting error while updating it

When we are updating through UI , it works. But it doesn't work through the api. Can you please let me know what am I doing wrong here?

naveena-maplelabs commented 1 year ago

@anusajith Can you please share the error details.

import random
from cohesity_management_sdk.cohesity_client import CohesityClient
from cohesity_management_sdk.models.protection_job_request_body import (
    ProtectionJobRequestBody,
)
from cohesity_management_sdk.models.create_view_request import CreateViewRequest

client = CohesityClient("x.x.x.x", admin, admin)
view_name = "View" + str(random.randint(1, 1000))
job_name = "Job" + str(random.randint(1, 1000))
view_box_id = client.view_boxes.get_view_boxes()[0].id
policy_id = client.protection_policies.get_protection_policies()[0].id
view_body = CreateViewRequest(name=view_name, view_box_id=view_box_id)
resp = client.views.create_view(view_body)
view_id = resp.view_id
print(policy_id, view_box_id)

job_body = ProtectionJobRequestBody(
    name=job_name,
    policy_id=policy_id,
    view_box_id=view_box_id,
    source_ids=[view_id],
    environment="kView",
    timezone="Asia/Kolkata"
)
job_resp = client.protection_jobs.create_protection_job(job_body)
print(job_resp.id)
job_id = job_resp.id
job_body.description = "Updated views"
job_body.id = job_id
job_body.source_ids.append(4874)

print(client.protection_jobs.update_protection_job(job_body, job_id))

I'm using the above code, was able to create job and update the same.

anusajith commented 1 year ago

Hi @naveena-maplelabs , Here are the code snippets which I am trying :

We have an already existing protection job with name "test_view" .

from cohesity_management_sdk.models.protection_job_request_body import ProtectionJobRequestBody

1) Getting the "test_cohesity_view" protection job by name. protection_job_list = cohesity_client.protection_jobs.get_protection_jobs() for job in protection_job_list: if job.name == "test_cohesity_view": protected_view_job=job

2) Getting the view_id which has to added to "test_cohesity_view" view_name="test_smart_view" view_id=cohesity_client.views.get_view_by_name(view_name).view_id

3)Update the protection job with new description and view: job_body = ProtectionJobRequestBody( name=protected_view_job.name, policy_id=protected_view_job.policy_id, view_box_id=protected_view_job.view_box_id, source_ids=protected_view_job.source_ids, source_special_parameters=[], environment="kView", ) job_body.description = "test update view" job_body.id = protected_view_job.id job_body.source_ids.append(view_id)

cohesity_client.protection_jobs.update_protection_job(job_body, view_job.id)

Traceback (most recent call last): File "", line 1, in return self._client.protection_jobs.update_protection_job(job_body, job_id) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\cohesity_management_sdk\controllers\protection_jobs_controller.py", line 644, in update_protection_job self.validate_response(_context) File "C:\Python311\Lib\site-packages\cohesity_management_sdk\controllers\base_controller.py", line 119, in validate_response raise raise_except cohesity_management_sdk.exceptions.api_exception.APIException: Response status code: 500, Response message: Could not list all the views requested for backup. Either some do not exist, or the user does not have access to it, or all views are not in same view box.

This is the response we are getting. Please guide me where I am going wrong here.

naveena-maplelabs commented 1 year ago

@anusajith Can you please make sure the view you are trying to add and the job are created using same storage domain?

anusajith commented 1 year ago

@naveena-maplelabs Yes , both are in the same storage domain. This operation is working through UI. No errors thrown. Only issue when we are trying using API.

naveena-maplelabs commented 1 year ago

Both views belong to same storage domain?

anusajith commented 1 year ago

Yes!!

Protection job View:

image

View to be added to protection job: image

naveena-maplelabs commented 1 year ago

Are you using the same login credentials for both UI and SDK?

anusajith commented 1 year ago

Yes I am !!

naveena-maplelabs commented 1 year ago

Thats strange, Can you please try the same API using postman instead of sdk and share the response here?

POST /irisservices/api/v1/public/protectionJobs Payload { "name": protected_view_job.name, "policyId": protected_view_job.policy_id, "viewBoxId" : protected_view_job.view_box_id, "sourceIds":protected_view_job.source_ids, "environment":"kView", )

naveena-maplelabs commented 1 year ago

To update PUT /public/protectionJobs/{id}

anusajith commented 1 year ago

It is working now with the following work around:

When fetching a Protection Job , source Ids are not same as the View Ids. So When we are trying to update a protection job with 100+ views in it, we have to explicitly convert those source ids to respective View Ids and then run the update method.

Some fields are not mentioned as mandatory fields. But then when we try to PUT/POST, it throws error saying its required. eg: timezone.

naveena-maplelabs commented 1 year ago

Okay,