Skip to content

Instantly share code, notes, and snippets.

@neidiom
Forked from gregohardy/cleanup.rb
Created April 16, 2018 18:58
Show Gist options
  • Save neidiom/ec82c55b6f21fb80075496b4a79b9d14 to your computer and use it in GitHub Desktop.
Save neidiom/ec82c55b6f21fb80075496b4a79b9d14 to your computer and use it in GitHub Desktop.
Azure Account Managment
require 'azure_mgmt_compute'
require 'azure_mgmt_resources'
require 'azure_mgmt_storage'
require 'azure'
include Azure
include Azure::ARM::Compute
include Azure::ARM::Compute::Models
include Azure::ARM::Resources
include Azure::ARM::Resources::Models
include Azure::ARM::Storage
include Azure::ARM::Storage::Models
tenant_id = ENV['AZURE_TENANT_ID']
client_id = ENV['AZURE_CLIENT_ID']
secret = ENV['AZURE_CLIENT_SECRET']
subscription_id = ENV['AZURE_SUBSCRIPTION_ID']
token_provider = MsRestAzure::ApplicationTokenProvider.new(tenant_id, client_id, secret)
credentials = MsRest::TokenCredentials.new(token_provider)
client = ComputeManagementClient.new(credentials)
client.subscription_id = subscription_id
storage_client = StorageManagementClient.new(credentials)
storage_client.subscription_id = subscription_id
resources_client = ResourceManagementClient.new(credentials)
resources_client.subscription_id = subscription_id
vm_management = Azure.vm_management
puts "ASM - (Classic)"
vms = vm_management.list_virtual_machines
puts "Virtual Machines :: Found #{vms.length} active vms"
cloud_service = Azure.cloud_service_management
all_cloud_services = cloud_service.list_cloud_services
services = all_cloud_services.find_all { |x| x.name =~ /CLOUD-/ }
puts "Virtual Machines :: Found #{all_cloud_services.length} should delete #{services.length}"
for service in services
puts "Deleting service #{service.name}"
cloud_service.delete_cloud_service(service.name)
end
storage_management = Azure.storage_management
all_storage_accounts = storage_management.list_storage_accounts
storage_accounts = all_storage_accounts.find_all { |x| x.label =~ /CLOUD-/ }
puts "Storage Accounts :: Found #{all_storage_accounts.length} should delete #{storage_accounts.length}"
for account in storage_accounts
puts "Deleting storage account #{account.label}"
storage_management.delete_storage_account(account.name)
end
puts "\nARM - (New)"
arm_vms = client.virtual_machines.list_all
puts "Virtual Machines :: Found #{arm_vms.value.length} active vms"
resource_groups = resources_client.resource_groups.list
cloud_rgs = resource_groups.value.find_all { |x| x.name =~ /CLOUD-(.*)-service-/ }
puts "Resource Groups :: Found #{resource_groups.value.length} should delete #{cloud_rgs.length}"
for rg in cloud_rgs
if rg.provisioning_state == 'Deleting'
puts "Deleting #{rg.name}"
else
puts "Attempting Delete of resource group #{rg.name}"
resources_client.resource_groups.begin_delete(rg.name) unless rg.provisioning_state == 'Deleting'
end
end
storage_accounts = storage_client.storage_accounts.list
cloud_storage_accounts = storage_accounts.value.find_all { |x| x.name =~ /CLOUD-/ }
puts "Storage Accounts :: Found #{storage_accounts.value.length} should delete #{cloud_storage_accounts.length}"
for account in cloud_storage_accounts
if account.provisioning_state == 'Deleting'
puts "Deleting #{account.name}"
else
puts "Attempting Delete of resource group #{account.name}"
storage_client.storage_accounts.begin_delete(account.name) unless account.provisioning_state == 'Deleting'
end
end
# Test
require 'azure_mgmt_compute'
require 'azure_mgmt_resources'
require 'azure_mgmt_storage'
require 'azure'
require 'pry'
include Azure
include Azure::ARM::Compute
include Azure::ARM::Compute::Models
include Azure::ARM::Resources
include Azure::ARM::Resources::Models
include Azure::ARM::Storage
include Azure::ARM::Storage::Models
tenant_id = ENV['AZURE_TENANT_ID']
client_id = ENV['AZURE_CLIENT_ID']
secret = ENV['AZURE_CLIENT_SECRET']
subscription_id = ENV['AZURE_SUBSCRIPTION_ID']
token_provider = MsRestAzure::ApplicationTokenProvider.new(tenant_id, client_id, secret)
credentials = MsRest::TokenCredentials.new(token_provider)
client = ComputeManagementClient.new(credentials)
client.subscription_id = subscription_id
storage_client = StorageManagementClient.new(credentials)
storage_client.subscription_id = subscription_id
resources_client = ResourceManagementClient.new(credentials)
resources_client.subscription_id = subscription_id
vm_management = Azure.vm_management
puts "ASM - (Classic)"
vms = vm_management.list_virtual_machines
puts "Virtual Machines :: Found #{vms.length} active vms"
cloud_service = Azure.cloud_service_management
all_cloud_services = cloud_service.list_cloud_services
services = all_cloud_services.find_all { |x| x.name =~ /CLOUD-/ }
puts "Virtual Machines :: Found #{all_cloud_services.length} should delete #{services.length}"
storage_management = Azure.storage_management
all_storage_accounts = storage_management.list_storage_accounts
storage_accounts = all_storage_accounts.find_all { |x| x.label =~ /CLOUD-/ }
puts "Storage Accounts :: Found #{all_storage_accounts.length} should delete #{storage_accounts.length}"
puts "\nARM - (New)"
arm_vms = client.virtual_machines.list_all
puts "Virtual Machines :: Found #{arm_vms.value.length} active vms"
resource_groups = resources_client.resource_groups.list
cloud_rgs = resource_groups.value.find_all { |x| x.name =~ /CLOUD-(.*)-service-/ }
puts "Resource Groups :: Found #{resource_groups.value.length} should delete #{cloud_rgs.length}"
storage_accounts = storage_client.storage_accounts.list
cloud_storage_accounts = storage_accounts.value.find_all { |x| x.name =~ /CLOUD-/ }
puts "Storage Accounts :: Found #{storage_accounts.value.length} should delete #{cloud_storage_accounts.length}"
https://github.com/hipchat/hipchat-cli/blob/master/tutorial/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment