diff --git a/.github/scripts/delete-aws-resources.sh b/.github/scripts/delete-aws-resources.sh index 610347aa..af25ed02 100644 --- a/.github/scripts/delete-aws-resources.sh +++ b/.github/scripts/delete-aws-resources.sh @@ -1,11 +1,42 @@ - for RESOURCE_TYPE in ${{ env.RESOURCE_TYPES }}; do - aws cloudcontrol list-resources --region ${{ env.AWS_REGION }} --type-name $RESOURCE_TYPE --query 'ResourceDescriptions[].Identifier' --output text > resources.txt - while read -r line; do - aws cloudcontrol delete-resource --region ${{ env.AWS_REGION }} --type $RESOURCE_TYPE --identifier $line - done < resources.txt - done - aws cloudcontrol list-resources --region ${{ env.AWS_REGION }} --type-name ${{ env.RESOURCE_TYPES }} --query 'ResourceDescriptions[].Identifier' --output text > resources.txt - # delete resource - while read -r line; do - aws cloudcontrol delete-resource --region ${{ env.AWS_REGION }} --type ${{ env.RESOURCE_TYPES }} --identifier $line - done < resources.txt \ No newline at end of file +#!/bin/bash + +# ------------------------------------------------------------ +# Copyright 2023 The Radius Authors. +# +# 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 +# +# http://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. +# ------------------------------------------------------------ + +APP_NAME=$1 +APP_LABEL='RadiusApplication' +RESOURCE_TYPES='AWS::RDS::DBInstance,AWS::RDS::DBSubnetGroup,AWS::MemoryDB::Cluster,AWS::MemoryDB::SubnetGroup' + +for resource_type in ${RESOURCE_TYPES//,/ } +do + aws cloudcontrol list-resources --type-name "$resource_type" --query "ResourceDescriptions[].Identifier" --output text | tr '\t' '\n' | while read identifier + do + aws cloudcontrol get-resource --type-name "$resource_type" --identifier "$identifier" --query "ResourceDescription.Properties" --output text | while read resource + do + resource_tags=$(jq -c -r .Tags <<< "$resource") + for tag in $(jq -c -r '.[]' <<< "$resource_tags") + do + key=$(jq -r '.Key' <<< "$tag") + value=$(jq -r '.Value' <<< "$tag") + if [[ "$key" == "$APP_LABEL" && "$value" == "$APP_NAME" ]] + then + echo "Deleting resource of type: $resource_type with identifier: $identifier" + aws cloudcontrol delete-resource --type-name "$resource_type" --identifier "$identifier" + fi + done + done + done +done diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0ef65f5f..d7f897b1 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -306,25 +306,19 @@ jobs: --subscription $SUBSCRIPTION_ID \ --name $RESOURCE_GROUP \ --yes - - name: Delete AWS Resources + - name: Delete EKS Cluster if: always() && matrix.credential == 'aws' env: EKS_CLUSTER_NAME: ${{ steps.gen-id.outputs.TEST_EKS_CLUSTER_NAME }} - UNIQUE_APP_NAME: ${{ steps.gen-id.outputs.TEST_UNIQUE_APP_NAME }} run: | # Uninstall Radius from EKS cluster rad uninstall kubernetes # Delete EKS cluster echo "Deleting EKS cluster: $EKS_CLUSTER_NAME" eksctl delete cluster --name $EKS_CLUSTER_NAME --region ${{ env.AWS_REGION }} --wait - # Delete AWS resources - # get the arn list of all resources tagged by the appname: $UNIQUE_APP_NAME - arn_list=$(aws resourcegroupstaggingapi get-resources --tag-filters Key=application,Values=$UNIQUE_APP_NAME --query 'ResourceTagMappingList[*].ResourceARN' --output text) - # delete all resources tagged by appname: $UNIQUE_APP_NAME - # tag the resource - # get the list of all resources in account - - # list all memorydb clusters + - name: Delete AWS Resources + run: | + ./.github/scripts/delete-aws-resources.sh ${{ steps.gen-id.outputs.TEST_UNIQUE_APP_NAME }} - name: Create GitHub issue on failure if: failure() && github.event_name != 'pull_request' run: gh issue create --title "Samples deployment failed for ${{ matrix.app }}" --body "Test failed on ${{ github.repository }}. See [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details." --repo ${{ github.repository }} diff --git a/reference-apps/eshop/iac/eshop.bicep b/reference-apps/eshop/iac/eshop.bicep index aec6fb5b..fb2233bc 100644 --- a/reference-apps/eshop/iac/eshop.bicep +++ b/reference-apps/eshop/iac/eshop.bicep @@ -94,6 +94,7 @@ module aws 'infra/aws.bicep' = if (platform == 'aws') { environment: environment adminLogin: adminLogin adminPassword: adminPassword + applicationName: appName } } diff --git a/reference-apps/eshop/iac/infra/aws.bicep b/reference-apps/eshop/iac/infra/aws.bicep index bf4474ed..3e2f4132 100644 --- a/reference-apps/eshop/iac/infra/aws.bicep +++ b/reference-apps/eshop/iac/infra/aws.bicep @@ -8,7 +8,7 @@ param environment string param application string @description('Radius application name') -param appname string +param applicationName string @description('SQL administrator username') param adminLogin string @@ -39,7 +39,7 @@ resource sqlSubnetGroup 'AWS.RDS/DBSubnetGroup@default' = { Tags: [ { Key: 'RadiusApplication' - Value: application + Value: applicationName } ] } @@ -68,7 +68,7 @@ resource identityDb 'AWS.RDS/DBInstance@default' = { Tags: [ { Key: 'RadiusApplication' - Value: application + Value: applicationName } ] } @@ -97,7 +97,7 @@ resource catalogDb 'AWS.RDS/DBInstance@default' = { Tags: [ { Key: 'RadiusApplication' - Value: application + Value: applicationName } ] } @@ -126,7 +126,7 @@ resource orderingDb 'AWS.RDS/DBInstance@default' = { Tags: [ { Key: 'RadiusApplication' - Value: application + Value: applicationName } ] } @@ -155,7 +155,7 @@ resource webhooksDb 'AWS.RDS/DBInstance@default' = { Tags: [ { Key: 'RadiusApplication' - Value: application + Value: applicationName } ] } @@ -170,7 +170,7 @@ resource redisSubnetGroup 'AWS.MemoryDB/SubnetGroup@default' = { Tags: [ { Key: 'RadiusApplication' - Value: application + Value: applicationName } ] } @@ -189,7 +189,7 @@ resource keystoreCache 'AWS.MemoryDB/Cluster@default' = { Tags: [ { Key: 'RadiusApplication' - Value: application + Value: applicationName } ] } @@ -208,7 +208,7 @@ resource basketCache 'AWS.MemoryDB/Cluster@default' = { Tags: [ { Key: 'RadiusApplication' - Value: application + Value: applicationName } ] }