blob: 5328cbec9a40c808c6d97579f952f0786e349dbf [file] [log] [blame]
aguilard8c0a0542023-11-13 13:16:32 +00001#!/usr/bin/env bash
2#######################################################################################
3# Copyright ETSI Contributors and Others.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#######################################################################################
18
19# Delete a VM and its resources (nic, disk, nsg, public IP) on Azure.
20# To do this it reads the following environment variables:
21# - VM_NAME: name of the VM
22# - RESOURCE_GROUP: name of the resource-group where the VM is
23function delete_azure_vm {
24 set -eux
25 INTERFACE_ID=$(az vm show --resource-group ${RESOURCE_GROUP} --name ${VM_NAME} --query networkProfile.networkInterfaces[0].id)
26 INTERFACE_ID=${INTERFACE_ID:1:-1}
27 OS_DISK_ID=$(az vm show --resource-group ${RESOURCE_GROUP} --name ${VM_NAME} --query storageProfile.osDisk.managedDisk.id)
28 OS_DISK_ID=${OS_DISK_ID:1:-1}
29 SECURITY_GROUP_ID=$(az network nic show --id ${INTERFACE_ID} --query networkSecurityGroup.id)
30 SECURITY_GROUP_ID=${SECURITY_GROUP_ID:1:-1}
31 PUBLIC_IP_ID=$(az network nic show --id ${INTERFACE_ID} --query ipConfigurations[0].publicIpAddress.id)
32 PUBLIC_IP_ID=${PUBLIC_IP_ID:1:-1}
33 az vm delete --resource-group ${RESOURCE_GROUP} --name ${VM_NAME} --yes
34 az network nic delete --id ${INTERFACE_ID}
35 az disk delete --id ${OS_DISK_ID} --yes
36 az network nsg delete --id ${SECURITY_GROUP_ID}
37 if [ -n "${PUBLIC_IP_ID}" ]; then
38 az network public-ip delete --id ${PUBLIC_IP_ID}
39 fi
40}
41
42# Delete a VM on GCP.
43# To do this it reads the following environment variables:
44# - VM_NAME: name of the VM
45# - GCP_PROJECT: name of project where the VM is
46# - GCP_ZONE: name of the zone
47function delete_gcp_vm {
48 gcloud compute instances delete "${VM_NAME}" --project="${GCP_PROJECT}" --zone="${GCP_ZONE}" --delete-disks all -q
49}
50
51if [ -n "${1:-}" ]; then # If there is an argument, it must be the hostname
52 VM_NAME=$1
53else
54 VM_NAME="${OSM_IMAGE_NAME}"
55fi
56
57# Branch by CLOUD_TYPE value ("azure", "gcp")
58if [ "${CLOUD_TYPE}" == "azure" ]; then
59 # Azure VIM
60 delete_azure_vm
61elif [ "${CLOUD_TYPE}" == "gcp" ]; then
62 # GCP VIM
63 delete_gcp_vm
64else
65 echo "Invalid cloud type: ${CLOUD_TYPE}"
66fi