forked from GoogleCloudPlatform/dfcx-scrapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environments.py
443 lines (349 loc) · 14.7 KB
/
environments.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""CX Environments Resource functions."""
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List, Dict, Tuple
import logging
from google.oauth2 import service_account
from google.cloud.dialogflowcx_v3beta1 import services
from google.cloud.dialogflowcx_v3beta1 import types
from dfcx_scrapi.core import scrapi_base
from dfcx_scrapi.core import flows
from dfcx_scrapi.core import versions
# logging config
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
class Environments(scrapi_base.ScrapiBase):
"""Core Class for CX Environments Resource functions."""
def __init__(
self,
creds_path: str = None,
creds_dict: Dict[str, str] = None,
creds: service_account.Credentials = None,
agent_id: str = None,
):
super().__init__(
creds_path=creds_path,
creds_dict=creds_dict,
creds=creds,
)
if agent_id:
self.agent_id = agent_id
self._versions = versions.Versions(creds=self.creds)
self._flows = flows.Flows(creds=self.creds)
@staticmethod
def _get_flow_version_id(
input_tuple: Tuple[str,str],
flow_versions_list: List[types.Version]):
"""Parse Version ID based on Tuple inputs."""
for version in flow_versions_list:
if version.name.split("/")[-1] == str(input_tuple[1]):
return version.name
return print("Flow Name and Version combination not found in Agent.")
def _get_filtered_flow_map(self, agent_id, flow_tuples):
"""Get Flows and Filter based on User Input"""
flows_map = self._flows.get_flows_map(agent_id, reverse=True)
input_flows = [flow[0] for flow in flow_tuples]
filtered_map = {}
for flow in input_flows:
if flow in flows_map.keys():
filtered_map[flow] = flows_map[flow]
return filtered_map
def _get_filtered_versions(self, filtered_map):
"""Get filtered versions Dict based on filtered Flows map"""
filtered_versions = {}
for flow in filtered_map:
filtered_versions[flow] = self._versions.list_versions(
filtered_map[flow])
return filtered_versions
def _get_final_versions(self, flow_tuples, filtered_versions):
"""Determine associated Flow Version IDs based on input tuple"""
final_versions = []
for version_tuple in flow_tuples:
version_config = types.environment.Environment.VersionConfig()
version_config.version = self._get_flow_version_id(
version_tuple, filtered_versions[version_tuple[0]])
final_versions.append(version_config)
return final_versions
def get_environments_map(
self,
agent_id: str = None,
reverse: bool = False
) -> Dict[str, str]:
"""Exports Agent environment display names and UUIDs
into a user friendly dict.
Args:
agent_id: the formatted CX Agent ID to use
reverse: (Optional) Boolean flag to swap key:value -> value:key
Returns:
Dictionary containing Environment UUIDs as keys and environment
display name as values.
"""
if not agent_id:
agent_id = self.agent_id
if reverse:
environments_dict = {
environment.display_name: environment.name
for environment in self.list_environments(agent_id)
}
else:
environments_dict = {
environment.name: environment.display_name
for environment in self.list_environments(agent_id)
}
return environments_dict
@scrapi_base.api_call_counter_decorator
def list_environments(self, agent_id: str = None):
"""List all Versions for a given Flow"""
if not agent_id:
agent_id = self.agent_id
request = types.environment.ListEnvironmentsRequest()
request.parent = agent_id
client_options = self._set_region(agent_id)
client = services.environments.EnvironmentsClient(
client_options=client_options, credentials=self.creds
)
response = client.list_environments(request)
environments = []
for page in response.pages:
for environment in page.environments:
environments.append(environment)
return environments
@scrapi_base.api_call_counter_decorator
def get_environment(
self,
environment_id: str) -> types.environment.Environment:
"""Get Environment object for specified environment ID.
Args:
environment_id: Unique environment ID of the target to get. Format:
projects/<Project ID>/locations/<Location ID>/agents/
<Agent ID>/environments/<Environment ID>
Returns:
Environment object.
"""
request = types.environment.GetEnvironmentRequest(
name=environment_id)
client_options = self._set_region(environment_id)
client = services.environments.EnvironmentsClient(
client_options=client_options, credentials=self.creds
)
response = client.get_environment(request)
return response
def get_environment_by_display_name(
self,
display_name: str,
agent_id: str) -> types.environment.Environment:
"""Get Environment object for specific environment by its display name.
Args:
display_name: Human readable display name of the target to get.
agent_id: The agent for the operation. format:
projects/<Project ID>/locations/<Location ID>/agents/<Agent ID>
Returns:
Environment object.
"""
if not agent_id:
agent_id = self.agent_id
result = None
environment_list = self.list_environments(agent_id)
for environment_obj in environment_list:
if environment_obj.display_name == display_name:
result = environment_obj
return result
@scrapi_base.api_call_counter_decorator
def create_environment(
self,
environment: types.environment.Environment,
agent_id: str = None):
"""Create a new environment for a specified agent.
Args:
environment: The environment to create.
type google.cloud.dialogflowcx_v3beta1.types.Environment
agent_id: The targeted agent for the operation. format:
projects/<Project ID>/locations/<Location ID>/agents/<Agent ID>
Returns:
An object representing a long-running operation. (LRO)
"""
if not agent_id:
agent_id = self.agent_id
request = types.environment.CreateEnvironmentRequest()
request.environment = environment
request.parent = agent_id
client_options = self._set_region(agent_id)
client = services.environments.EnvironmentsClient(
credentials=self.creds, client_options=client_options
)
response = client.create_environment(request)
return response
def create_environment_by_display_name(
self,
display_name: str,
version_configs: List[Tuple[str, str]],
description: str = None,
agent_id: str = None):
"""Create a new environment for a specified agent.
Args:
display_name: The display name of the Environment to create
version_configs: A List of Tuples, containing the Flow Display
Names and Version IDs to include in the Environment creation.
Ex:
[('Default Start Flow', 2), ('Confidence Demo', 3), ('FlowC', 1)]
Internally, the create_environment method will perform a lookup and
pull the appropriate Flow Version IDs to assign to the Environment
being created.
agent_id: The targeted agent for the operation. format:
projects/<Project ID>/locations/<Location ID>/agents/<Agent ID>
Returns:
An object representing a long-running operation (LRO). View the
result by inspecting response.result()
"""
if not agent_id:
agent_id = self.agent_id
filtered_map = self._get_filtered_flow_map(agent_id, version_configs)
all_versions = self._get_filtered_versions(filtered_map)
final_versions = self._get_final_versions(version_configs, all_versions)
# Build the Environment Object
env = types.Environment()
env.display_name = display_name
env.version_configs = final_versions
env.description = description
request = types.environment.CreateEnvironmentRequest(
environment = env,
parent = agent_id
)
client_options = self._set_region(agent_id)
client = services.environments.EnvironmentsClient(
client_options=client_options, credentials=self.creds
)
response = client.create_environment(request)
return response
@scrapi_base.api_call_counter_decorator
def update_environment(
self,
environment_id: str,
environment_obj: types.Environment = None,
**kwargs):
"""Update an existing environment for a specified agent.
Args:
environment_id: The specified environment to update.
environment_obj: Optional Environment object of types.Environment
that can be provided when you are planning to replace the full
object vs. just partial updates.
Returns:
An object representing a long-running operation. (LRO)
"""
if environment_obj:
env = environment_obj
env.name = environment_id
mask = self._update_kwargs(env)
elif kwargs:
env = self.get_environment(environment_id)
mask = self._update_kwargs(env, **kwargs)
request = types.environment.UpdateEnvironmentRequest(
environment=env, update_mask=mask)
client_options = self._set_region(environment_id)
client = services.environments.EnvironmentsClient(
credentials=self.creds, client_options=client_options
)
response = client.update_environment(request)
return response
@scrapi_base.api_call_counter_decorator
def delete_environment(self, environment_id: str):
"""Delete a specified environment.
Args:
environment_id: unique ID associated with target environment. Format:
projects/<Project ID>/locations/<Location ID>/agents/
<Agent ID>/environments/<Environment ID>.
"""
request = types.environment.DeleteEnvironmentRequest()
request.name = environment_id
client_options = self._set_region(environment_id)
client = services.environments.EnvironmentsClient(
credentials=self.creds, client_options=client_options
)
client.delete_environment(request)
@scrapi_base.api_call_counter_decorator
def deploy_flow_to_environment(
self,
environment_id: str,
flow_version: str):
"""Deploys a flow to the specified environment.
Args:
environment_id: unique ID associated with target environment. Format:
projects/<Project ID>/locations/<Location ID>/agents/
<Agent ID>/environments/<Environment ID>.
flow_version: Required. The flow version to deploy. Format:
projects/<Project ID>/locations/<Location ID>/agents/
<Agent ID>/ flows/<Flow ID>/versions/<Version ID>
Returns:
An object representing a long-running operation. (LRO)
"""
request = types.environment.DeployFlowRequest()
request.environment = environment_id
request.flow_version = flow_version
client_options = self._set_region(environment_id)
client = services.environments.EnvironmentsClient(
credentials=self.creds, client_options=client_options
)
response = client.deploy_flow(request)
return response
@scrapi_base.api_call_counter_decorator
def lookup_environment_history(
self,
environment_id: str) -> List[types.Environment]:
"""Looks up the history of the specified environment.
Args:
environment_id: unique ID associated with target environment. Format:
projects/<Project ID>/locations/<Location ID>/agents/
<Agent ID>/environments/<Environment ID>.
Returns:
List of Environment objects with historical timestamps.
"""
request = types.environment.LookupEnvironmentHistoryRequest(
name = environment_id
)
client_options = self._set_region(environment_id)
client = services.environments.EnvironmentsClient(
client_options=client_options, credentials=self.creds
)
response = client.lookup_environment_history(request)
history = []
for page in response.pages:
for environment in page.environments:
history.append(environment)
return history
@scrapi_base.api_call_counter_decorator
def list_continuous_test_results(self, environment_id: str):
"""Fetches a list of continuous test results for a given environment.
Args:
environment_id: unique ID associated with target environment. Format:
projects/<Project ID>/locations/<Location ID>/agents/
<Agent ID>/environments/<Environment ID>.
Returns:
List of types.ContinuousTestResult.
"""
request = types.environment.ListContinuousTestResultsRequest(
parent = environment_id
)
client_options = self._set_region(environment_id)
client = services.environments.EnvironmentsClient(
client_options=client_options, credentials=self.creds
)
response = client.list_continuous_test_results(request)
test_results = []
for page in response.pages:
for test in page.continuous_test_results:
test_results.append(test)
return test_results