-
Notifications
You must be signed in to change notification settings - Fork 1
/
synchronize.py
206 lines (150 loc) · 10.4 KB
/
synchronize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'''
Copyright 2020 Flexera Software LLC
See LICENSE.TXT for full license text
SPDX-License-Identifier: MIT
Created on Sep 14, 2019
@author: SGeary
'''
import sys
import logging
###################################################################################
# Test the version of python to make sure it's at least the version the script
# was tested on, otherwise there could be unexpected results
if sys.version_info <= (3, 5):
raise "Script created/tested against python version 3.5"
else:
pass
###################################################################################
# Set up logging handler to allow for different levels of logging to be capture
logging.basicConfig(format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', datefmt='%Y-%m-%d:%H:%M:%S',
filename="_workflow.log", filemode='w',level=logging.DEBUG)
logger = logging.getLogger(__name__)
import config
import FNCI.v7.projects.getProjects
import FNCI.v6.project.getProjectID
import FNCI.v7.users.searchUsers
import FNCI.v7.inventories.getInventoryItemDetails
import FNCI.v7.inventories.updateInventory
import v7_Data.getTaskData
import v7_Data.getInventoryData
import v7_Data.update_guidance_data
import v6_Data.manage_custom_data
import v6_Data.create_project
import v6_Data.get_obligation_data
import workflow.create_request
import workflow.update_request
#-------------------------------------------------#
def main():
authToken = config.AUTHTOKEN
admin_authToken = config.ADMIN_AUTHTOKEN
v6_authToken = config.v6_AUTHTOKEN
print("###############################################################################")
# Start to cycle through projects in v7 looking for open tasks
PROJECTS = FNCI.v7.projects.getProjects.get_project_list(admin_authToken)
# Cycle through each project
for project in PROJECTS:
projectID = project["id"]
projectName = project["name"]
projectStatus = project["status"]
projectOwner = project["owner"]
v6ProjectName = projectName + " (" + str(projectID) + ")"
# Only check for tasks/requests if the project is not "complete"
if projectStatus != "Project Completed":
print("\n")
print("Examining project %s for active manual review tasks." %projectName, flush=True)
# Get project Tasks and the inventory item they are associated with
PROJECTTASKDATA = v7_Data.getTaskData.get_v7_task_data(projectID)
# see if there are any tasks to worry about
if PROJECTTASKDATA:
print(" - There are %s active manual review tasks for this project" %(len(PROJECTTASKDATA)), flush=True)
# See if there is a matching v6 project
v6_projectID = FNCI.v6.project.getProjectID.get_project_id(config.v6_teamName, v6ProjectName, v6_authToken)
if v6_projectID == "No Matching Project Found":
# Since there is no project in v6 one needs to be created
v6_projectID = v6_Data.create_project.create_v6_project(v6ProjectName, projectOwner)
else:
print(" - There is a corresponding project of name %s in v6" %v6ProjectName, flush=True)
print(" -- v6 ProjectID is %s" %v6_projectID, flush=True)
# Cycle though the tasks and get the inventory information for each one
for taskId in PROJECTTASKDATA:
logger.debug("Check inventory for task with id %s" %taskId)
inventoryId = PROJECTTASKDATA[taskId][0]
logger.debug(" Inventory ID %s" %inventoryId)
# See if there is an existing workflowURL for the inventory item associated to this task
workflowURL = FNCI.v7.inventories.getInventoryItemDetails.get_inventory_item_workflowURL_by_id(inventoryId, authToken)
# Is there a workflow request already in play?
if workflowURL == "None":
# There is no workflow item so create it
# Get the component details from the inventory Item
INVENTORYDATA = v7_Data.getInventoryData.get_v7_inventory_item_data(inventoryId, authToken)
if INVENTORYDATA != None and len(INVENTORYDATA) > 0:
# This was disclosed so create the request
# No current mapping so create a new request
print(" - No previous mapping for task with ID: %s" %taskId, flush=True)
# What is being requested for use?
componentId = INVENTORYDATA["componentId"]
componentVersionId = INVENTORYDATA["componentVersionId"]
componentLicenseId = INVENTORYDATA["selectedLicenseId"]
# See if there is any custom data within the request
if (componentId > 1000000000) or (componentVersionId > 1000000000) or (componentLicenseId > 1000000000):
# At least one item is custom
INVENTORYITEMDATA = v6_Data.manage_custom_data.determine_custom_data(INVENTORYDATA)
else:
# This is all stock data from the PDL
INVENTORYITEMDATA = [componentId, componentVersionId, componentLicenseId]
# Users IDs associated with the task
ownerId = PROJECTTASKDATA[taskId][1]
createdById = PROJECTTASKDATA[taskId][2]
# Use the ID within the task data to get the user's email address which is needed
# on the v6 side to create the request
projectOwnerEmail = FNCI.v7.users.searchUsers.get_user_email_by_id(ownerId, admin_authToken)
requesterEmail = FNCI.v7.users.searchUsers.get_user_email_by_id(createdById, admin_authToken)
logger.debug("Project owner eMail Address is %s" %projectOwnerEmail)
logger.debug("Requester eMail Address is %s" %requesterEmail)
v6RequestID = workflow.create_request.create_new_request(v6_projectID, taskId, projectOwnerEmail, requesterEmail, INVENTORYITEMDATA)
print(" - Task with ID taskId %s now has v6 requestId %s associated with it " %(taskId, v6RequestID), flush=True)
# Update Inventory Item with URL
requestURL = config.v6_REQUESTURL + str(v6RequestID) + "&projectId=" + str(v6_projectID) + "&from=requests"
FNCI.v7.inventories.updateInventory.update_inventory_item_workflowURL(inventoryId, requestURL, authToken)
'''
print(" - Updating Usage Guidance for inventory item with ID of %s" %(inventoryId))
# Update v7 Inventory Item with License Obligation Data from v6
componentLicenseId = INVENTORYITEMDATA[2]
licenseName, OBLIGATIONDATA = v6_Data.get_obligation_data.get_v6_license_obligation_data(componentLicenseId, v6_authToken)
v7_Data.update_guidance_data.update_v7_guidance_notes(inventoryId, licenseName, OBLIGATIONDATA, authToken)
'''
# Update Task with info
workflow.update_request.get_update_for_existing_request(v6_projectID, inventoryId, taskId, v6RequestID, requestURL)
else:
logger.debug("No Inventory Data")
else:
# This is an existing request so update the task with the latest information
# Get the v6RequestID from the workflowURL
v6RequestID = workflowURL.split("=")[1].split("&")[0]
print(" - Task with ID %s already has a v6 requestId of %s associated with it." %(taskId, v6RequestID), flush=True)
logger.debug("taskId %s already has a requestId %s associated with it." %(taskId, v6RequestID))
workflow.update_request.get_update_for_existing_request(v6_projectID, inventoryId, taskId, v6RequestID, workflowURL)
else:
print(" - There are no tasks for this project", flush=True)
print("")
print("###############################################################################")
else:
# The project is marked as completed
print("\n")
print("Project %s has a status of %s so no action taken" %(projectName, projectStatus))
print("")
print("###############################################################################")
print("\n")
print("Script Completed")
if __name__ == "__main__":
if int(config.FNCI_PORT) > 65535:
print("The port specified for FNCI v7 (%s) is greater than 65535." %config.FNCI_PORT)
print("Please update config.py with correct port for FNCI_PORT")
print("Exiting Script")
sys.exit()
if int(config.v6_FNCI_PORT) > 65535:
print("The port specified for FNCI v6 (%s) is greater than 65535." %config.v6_FNCI_PORT)
print("Please update config.py with correct port for v6_FNCI_PORT")
print("Exiting Script")
sys.exit()
main()