aristanetworks / cvprac

Other
46 stars 47 forks source link

change_control_available_tasks() lists tasks that are already assigned to Change Controls #186

Open colinmacgiolla opened 2 years ago

colinmacgiolla commented 2 years ago

cvprac version: v1.0.7 cvp version tested: 2021.3.0

When calling api.change_control_available_tasks() I would expect that only unassigned tasks are returned. Instead it looks like any tasks that are in the workOrderUserDefinedStatus: Pending state are returned.

It looks like none of the ccId values are updated when a task is assigned to a CC e.g (taskId 20 is assigned to a CC).

{'ccId': '',
 'ccIdV2': '',
 'completedOnInLongFormat': 1644867921463,
 'createdBy': 'cvpadmin',
 'createdOnInLongFormat': 1644867915204,
 'currentTaskName': 'Submit',
 'currentTaskType': 'User Task',
 'data': {'APP_SESSION_ID': '',
          'ERROR_IN_CAPTURING_DESIGN_CONFIG': '',
          'ERROR_IN_CAPTURING_RUNNING_CONFIG': '',
          'INCORRECT_CONFIG_IN_CAPTURING_DESIGN_CONFIG': '',
          'INCORRECT_CONFIG_IN_CAPTURING_DESIGN_CONFIG_OUTPUT_INDEX': '',
          'IS_ADD_OR_MOVE_FLOW': False,
          'IS_AUTO_GENERATED_IN_CVP': False,
          'IS_CONFIG_PUSH_NEEDED': 'no',
          'NETELEMENT_ID': '50:00:00:72:8b:31',
          'VIEW': 'IMAGE',
          'WORKFLOW_ACTION': 'Image Push',
          'ZERO_TOUCH_REPLACEMENT': '',
          'ccExecutingNode': '',
          'ccId': '',
          'commandUsedInMgmtIpVal': '',
          'config': [],
          'configExistInCVP': False,
          'configSnapshots': [],
          'configletList': [],
          'currentparentContainerId': 'container_a4002992-a276-468c-9494-18c4f159e8f3',
          'designedConfig': '',
          'designedConfigOutputIndex': '',
          'extensionsRequireReboot': [],
          'ignoreConfigletList': [],
          'image': '',
          'imageBundleId': '',
          'imageId': [],
          'imageIdList': [],
          'imageToBePushedToDevice': '',
          'imagebundle': {'appliedContainersCount': 0,
                          'appliedDevicesCount': 1,
                          'id': 'imagebundle_1635538536083800899',
                          'images': [{'imageBundleKeys': [],
                                      'imageSize': '931.9 MB',
                                      'isHotFix': 'false',
                                      'isRebootRequired': 'true',
                                      'name': 'EOS-4.25.4M.swi',
                                      'sha512': '54e6874984a3a46b1371bd6c53196bbd622c922606b65d59ed3fa23e918a43d174d468ac9179146a4d1b00e7094c4755ea90c2df4ab94c562e745c14a402b491',
                                      'swiMaxHwepoch': '2',
                                      'swiVariant': 'US',
                                      'uploadedDate': 1635538512660,
                                      'user': 'cvp system',
                                      'version': '4.25.4M-22402993.4254M'},
                                     {'imageBundleKeys': [],
                                      'imageSize': '6.9 MB',
                                      'isHotFix': 'true',
                                      'isRebootRequired': 'false',
                                      'name': 'TerminAttr-1.17.0-1.swix',
                                      'sha512': 'e90934f925930192a4d0cbc997d12cdfb07bdd57b846097d23d52e80a4516c3879cf140de1cedf2b6f898a8524b64f4213748820cfc775c049655d51d3302334',
                                      'swiMaxHwepoch': '0',
                                      'swiVariant': '',
                                      'uploadedDate': 1638905792504,
                                      'user': 'cvpadmin',
                                      'version': 'v1.17.0-1'}],
                          'isCertifiedImage': 'true',
                          'name': 'EOS-4.25.4M',
                          'updatedTimeInLongFormat': 1638973990960,
                          'uploadedBy': 'cvpadmin'},
          'isDCAEnabled': False,
          'isRollbackFromSnapshotFlow': False,
          'isRollbackTask': False,
          'newparentContainerId': 'container_a4002992-a276-468c-9494-18c4f159e8f3',
          'noOfRe-Tries': 0,
          'preRollbackImage': '',
          'presentImageInDevice': '',
          'runningConfig': '',
          'sessionUsedInMgmtIpVal': '',
          'targetIpAddress': '',
          'user': ''},
 'description': 'Image Bundle Assign: DC1-LEAF1A',
 'dualSupervisor': False,
 'executedBy': '',
 'executedOnInLongFormat': 0,
 'name': '',
 'netElementId': '50:00:00:72:8b:31',
 'newParentContainerId': 'container_a4002992-a276-468c-9494-18c4f159e8f3',
 'newParentContainerName': 'DC1_LEAF1',
 'note': '',
 'stageId': '',
 'taskStatus': 'ACTIVE',
 'taskStatusBeforeCancel': '',
 'templateId': 'ztp',
 'workFlowDetailsId': '',
 'workOrderDetails': {'ipAddress': '172.168.1.102',
                      'netElementHostName': 'DC1-LEAF1A',
                      'netElementId': '50:00:00:72:8b:31',
                      'serialNumber': '9EA47405A05A81505EFA36B21CE57AED',
                      'workOrderDetailsId': '',
                      'workOrderId': ''},
 'workOrderId': '20',
 'workOrderState': 'ACTIVE',
 'workOrderUserDefinedStatus': 'Pending'}

But at the same time:

{'result': {'time': '2022-02-14T20:12:50.342470291Z',
            'type': 'INITIAL',
            'value': {'change': {'name': 'Image CC',
                                 'notes': '',
                                 'rootStageId': '6Sp6WIrWot',
                                 'stages': {'values': {'6Sp6WIrWot': {'name': 'Change '
                                                                              '20220214_194524 '
                                                                              'Root',
                                                                      'rows': {'values': [{'values': ['g4dZ1P2MOe']}]}},
                                                       'g4dZ1P2MOe': {'action': {'args': {'values': {'TaskID': '20'}},
                                                                                 'name': 'task',
                                                                                 'timeout': 3000},
                                                                      'name': 'Upgrade '
                                                                              'Image'}}},
                                 'time': '2022-02-14T20:12:50.342470291Z',
                                 'user': 'cvpadmin'},
                      'key': {'id': 'x3DZlJltf'}}}}
noredistribution commented 2 years ago

yes, unfortunately this is a big caveat with the Task API, which is one other reason why it is going away in the near future. There's no easy way to get the assignable tasks, without a lot of work, basically you'd have to get all the CCs and go through each stage and check if any stage has any taskIDs that match your pending Task list, but not sure if that's worth the effort; the UI unfortunately is reading this directly from the db, but does a similar yet less expensive operation to figure this out

I think a better solution would be to create/execute/schedule change controls from the tasks you generate with your scripts, as you will always get the taskIds for each of your actions like adding configlets, moving devices, etc.

phil-dileo commented 2 years ago

The UI performs the following check:

  1. Get pending tasks from workOrderUserDefinedStatus: Pending | Failed (since failed tasks can be re-run)
  2. Go to cvp:/changecontrol/taskMap to see which CC a particular task is/was assigned to. If the CC is pending then this task is not assignable. If the CC has status:failed then the task is assignable.

  if (status === TASK_STATUS.PENDING) {
    if (_.isEmpty(cc)) {
      return true;
    }
    return (
      cc.status === CHANGE_CONTROL_STATUS.COMPLETED || cc.status === CHANGE_CONTROL_STATUS.FAILED
    );
  }
  if (status === TASK_STATUS.FAILED) {
    if (!_.isEmpty(cc)) {
      return (
        cc.status === CHANGE_CONTROL_STATUS.COMPLETED || cc.status === CHANGE_CONTROL_STATUS.FAILED
      );
    }
    return true;
  }
  return false;
}