Skip to content

Commit 9239403

Browse files
fix: Update Watson Assistant code to allow for destruction of resource (#85)
BREAKING CHANGE: In this release, the code to manage Watson Assistant was updated to allow for deletion of the Assistant when the RAG sample DA is undeployed. As this is a different mechanism, when upgrading the DA to this or later versions, users may end up with a duplicate Assistant within their Watson Assistant instance. This should not incur additional costs as it will be unused, but we recommend that users navigate to their Watson Assistant instance after the upgrade, and delete the original Assistant from there. Prefixes for assistants are now available, so we recommend using a prefix to generate a unique name for the new assistant.
1 parent e9496fc commit 9239403

File tree

10 files changed

+214
-114
lines changed

10 files changed

+214
-114
lines changed

.secrets.baseline

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2024-04-04T16:29:33Z",
6+
"generated_at": "2024-05-31T17:16:48Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -95,12 +95,32 @@
9595
"verified_result": null
9696
}
9797
],
98-
"solutions/banking/artifacts/watsonx.Assistant/watsonx-custom-ext-api-call.openapi.json": [
98+
"solutions/banking/watson-scripts/assistant-create.sh": [
9999
{
100-
"hashed_secret": "2308d0fb5ab83a7e92e8000f5f094342d3f87bd0",
100+
"hashed_secret": "20e0438047329f2ff165b6d2a4c081e42d280a3b",
101101
"is_secret": false,
102102
"is_verified": false,
103-
"line_number": 21,
103+
"line_number": 4,
104+
"type": "Secret Keyword",
105+
"verified_result": null
106+
}
107+
],
108+
"solutions/banking/watson-scripts/assistant-destroy.sh": [
109+
{
110+
"hashed_secret": "20e0438047329f2ff165b6d2a4c081e42d280a3b",
111+
"is_secret": false,
112+
"is_verified": false,
113+
"line_number": 5,
114+
"type": "Secret Keyword",
115+
"verified_result": null
116+
}
117+
],
118+
"solutions/banking/watson-scripts/assistant-read.sh": [
119+
{
120+
"hashed_secret": "20e0438047329f2ff165b6d2a4c081e42d280a3b",
121+
"is_secret": false,
122+
"is_verified": false,
123+
"line_number": 5,
104124
"type": "Secret Keyword",
105125
"verified_result": null
106126
}

solutions/banking/main.tf

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,23 @@ resource "null_resource" "discovery_file_upload" {
183183
}
184184

185185
# assistant creation
186-
resource "null_resource" "assistant_project_creation" {
187-
triggers = {
188-
always_run = timestamp()
186+
resource "shell_script" "watson_assistant" {
187+
lifecycle_commands {
188+
create = file("${path.module}/watson-scripts/assistant-create.sh")
189+
delete = file("${path.module}/watson-scripts/assistant-destroy.sh")
190+
read = file("${path.module}/watson-scripts/assistant-read.sh")
189191
}
190192

191-
provisioner "local-exec" {
192-
command = "${path.module}/watson-scripts/assistant-project-creation.sh \"${local.watsonx_assistant_url}\""
193-
interpreter = ["/bin/bash", "-c"]
194-
quiet = true
195-
environment = {
196-
IAM_TOKEN = local.sensitive_tokendata
197-
}
193+
environment = {
194+
WATSON_ASSISTANT_API_VERSION = "2023-06-15"
195+
WATSON_ASSISTANT_DESCRIPTION = "Generative AI sample app assistant"
196+
WATSON_ASSISTANT_LANGUAGE = "en"
197+
WATSON_ASSISTANT_NAME = var.prefix != "" ? "${var.prefix}-gen-ai-rag-sample-app-assistant" : "gen-ai-rag-sample-app-assistant"
198+
WATSON_ASSISTANT_URL = local.watsonx_assistant_url
198199
}
199-
}
200200

201-
# get assistant integration ID
202-
data "external" "assistant_get_integration_id" {
203-
depends_on = [null_resource.assistant_project_creation]
204-
program = ["bash", "${path.module}/watson-scripts/assistant-get-integration-id.sh"]
205-
query = {
206-
tokendata = local.sensitive_tokendata
207-
watson_assistant_url = local.watsonx_assistant_url
201+
sensitive_environment = {
202+
IBMCLOUD_API_KEY = var.ibmcloud_api_key
208203
}
209204
}
210205

@@ -247,21 +242,21 @@ resource "ibm_cd_tekton_pipeline_property" "application_flavor_pipeline_property
247242
# Update CI pipeline with Assistant integration ID
248243
resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_integration_id_pipeline_property_ci" {
249244
provider = ibm.ibm_resources
250-
depends_on = [data.external.assistant_get_integration_id]
245+
depends_on = [shell_script.watson_assistant]
251246
name = "watsonx_assistant_integration_id"
252247
pipeline_id = var.ci_pipeline_id
253248
type = "text"
254-
value = data.external.assistant_get_integration_id.result.assistant_integration_id
249+
value = shell_script.watson_assistant.output["assistant_integration_id"]
255250
}
256251

257252
# Update CD pipeline with Assistant integration ID
258253
resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_integration_id_pipeline_property_cd" {
259254
provider = ibm.ibm_resources
260-
depends_on = [data.external.assistant_get_integration_id]
255+
depends_on = [shell_script.watson_assistant]
261256
name = "watsonx_assistant_integration_id"
262257
pipeline_id = var.cd_pipeline_id
263258
type = "text"
264-
value = data.external.assistant_get_integration_id.result.assistant_integration_id
259+
value = shell_script.watson_assistant.output["assistant_integration_id"]
265260
}
266261

267262
# Random string for webhook token
@@ -289,6 +284,7 @@ resource "ibm_cd_tekton_pipeline_trigger" "ci_pipeline_webhook" {
289284

290285
# Ensure webhook trigger runs against correct git branch
291286
resource "ibm_cd_tekton_pipeline_trigger_property" "ci_pipeline_webhook_branch_property" {
287+
provider = ibm.ibm_resources
292288
depends_on = [ibm_cd_tekton_pipeline_trigger.ci_pipeline_webhook]
293289
name = "branch"
294290
pipeline_id = var.ci_pipeline_id

solutions/banking/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ output "cos_instance_crn" {
2525

2626
output "watsonx_assistant_integration_id" {
2727
description = "WatsonX assistant integration ID."
28-
value = data.external.assistant_get_integration_id.result.assistant_integration_id
28+
value = shell_script.watson_assistant.output["assistant_integration_id"]
2929
}
3030

3131
output "watson_discovery_project_id" {

solutions/banking/version.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
terraform {
22
required_providers {
3-
external = {
4-
source = "hashicorp/external"
5-
version = ">=2.3.3"
6-
}
73
ibm = {
84
source = "IBM-Cloud/ibm"
95
version = ">= 1.66.0"
@@ -20,6 +16,10 @@ terraform {
2016
source = "Mastercard/restapi"
2117
version = ">= 1.19.1"
2218
}
19+
shell = {
20+
source = "scottwinkler/shell"
21+
version = ">= 1.7.7"
22+
}
2323
}
2424
required_version = ">= 1.3.0"
2525
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
token=$(curl -fLsS -X POST 'https://iam.cloud.ibm.com/identity/token' -H 'Content-Type: application/x-www-form-urlencoded' -d "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=$IBMCLOUD_API_KEY" | jq -r '.access_token')
5+
6+
OUTPUT=$(curl -fLsS -X POST --location "$WATSON_ASSISTANT_URL/v2/assistants?version=$WATSON_ASSISTANT_API_VERSION" \
7+
--header "Authorization: Bearer $token" \
8+
--header "Content-Type: application/json" \
9+
--data "{\"name\":\"$WATSON_ASSISTANT_NAME\",\"language\":\"$WATSON_ASSISTANT_LANGUAGE\",\"description\":\"$WATSON_ASSISTANT_DESCRIPTION\"}")
10+
11+
ASSISTANT_ID=$(echo "$OUTPUT" | jq -r '.assistant_id')
12+
ENVIRONMENT_ID=$(echo "$OUTPUT" | jq -r '.assistant_environments[] | select(.name == "draft") | .environment_id')
13+
14+
INTEGRATION_ID=$(curl -X GET --retry 3 -flsS --location "$WATSON_ASSISTANT_URL/v2/assistants/$ASSISTANT_ID/environments/$ENVIRONMENT_ID?version=2023-06-15" \
15+
--header "Authorization: Bearer $token" \
16+
--header "Content-Type: application/json" \
17+
| jq -r '.integration_references[] | select(.type == "web_chat") | .integration_id ')
18+
19+
jq -nS --arg assistant_id "$ASSISTANT_ID" --arg assistant_integration_id "$INTEGRATION_ID" '{"assistant_integration_id": $assistant_integration_id, "assistant_id": $assistant_id}'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
token=$(curl -fLsS -X POST 'https://iam.cloud.ibm.com/identity/token' -H 'Content-Type: application/x-www-form-urlencoded' -d "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=$IBMCLOUD_API_KEY" | jq -r '.access_token')
6+
7+
IN=$(cat)
8+
EXISTING_ASSISTANT_ID=$(echo "$IN" | jq -r .assistant_id)
9+
10+
OUTPUT=$(curl -flsS --retry 3 -X GET --location "$WATSON_ASSISTANT_URL/v2/assistants?version=$WATSON_ASSISTANT_API_VERSION" \
11+
--header "Authorization: Bearer $token" \
12+
--header "Content-Type: application/json" | jq -r '.assistants[] | select(.assistant_id == "'"$EXISTING_ASSISTANT_ID"'")')
13+
14+
if [[ $OUTPUT ]]; then
15+
curl -fLsS -X DELETE --location "$WATSON_ASSISTANT_URL/v2/assistants/$EXISTING_ASSISTANT_ID?version=$WATSON_ASSISTANT_API_VERSION" \
16+
--header "Authorization: Bearer $token"
17+
fi

solutions/banking/watson-scripts/assistant-get-integration-id.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

solutions/banking/watson-scripts/assistant-project-creation.sh

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
token=$(curl -fLsS -X POST 'https://iam.cloud.ibm.com/identity/token' -H 'Content-Type: application/x-www-form-urlencoded' -d "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=$IBMCLOUD_API_KEY" | jq -r '.access_token')
6+
7+
IN=$(cat)
8+
EXISTING_ASSISTANT_ID=$(echo "$IN" | jq -r .assistant_id)
9+
10+
OUTPUT=$(curl -X GET --retry 3 -fLsS --location "$WATSON_ASSISTANT_URL/v2/assistants?version=$WATSON_ASSISTANT_API_VERSION" \
11+
--header "Authorization: Bearer $token" \
12+
--header "Content-Type: application/json" | jq -r '.assistants[] | select(.assistant_id == "'"$EXISTING_ASSISTANT_ID"'")')
13+
14+
if [ -z "${OUTPUT}" ]; then
15+
exit 1
16+
fi
17+
18+
ASSISTANT_ID=$(echo "$OUTPUT" | jq -r '.assistant_id')
19+
ENVIRONMENT_ID=$(echo "$OUTPUT" | jq -r '.assistant_environments[] | select(.environment == "draft") | .environment_id')
20+
21+
INTEGRATION_ID=$(curl -X GET --retry 3 -flsS --location "$WATSON_ASSISTANT_URL/v2/assistants/$ASSISTANT_ID/environments/$ENVIRONMENT_ID?version=$WATSON_ASSISTANT_API_VERSION" \
22+
--header "Authorization: Bearer $token" \
23+
--header "Content-Type: application/json" \
24+
| jq -r '.integration_references[] | select(.type == "web_chat") | .integration_id ')
25+
26+
jq -nS --arg assistant_id "$ASSISTANT_ID" --arg assistant_integration_id "$INTEGRATION_ID" '{"assistant_integration_id": $assistant_integration_id, "assistant_id": $assistant_id}'

0 commit comments

Comments
 (0)