This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample.rb
121 lines (101 loc) · 3.85 KB
/
example.rb
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
#!/usr/bin/env ruby
require 'azure_mgmt_resources'
require 'dotenv'
Dotenv.load!(File.join(__dir__, './.env'))
WEST_US = 'westus'
GROUP_NAME = 'azure-sample-group'
# Manage resources and resource groups - create, update and delete a resource group, deploy a solution into a resource
# group, export an ARM template. Create, read, update and delete a resource
#
# This script expects that the following environment vars are set:
#
# AZURE_TENANT_ID: with your Azure Active Directory tenant id or domain
# AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID
# AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret
# AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id
#
def run_example
#
# Create the Resource Manager Client with an Application (service principal) token provider
#
subscription_id = ENV['AZURE_SUBSCRIPTION_ID'] || '11111111-1111-1111-1111-111111111111' # your Azure Subscription Id
provider = MsRestAzure::ApplicationTokenProvider.new(
ENV['AZURE_TENANT_ID'],
ENV['AZURE_CLIENT_ID'],
ENV['AZURE_CLIENT_SECRET'])
credentials = MsRest::TokenCredentials.new(provider)
options = {
credentials: credentials,
subscription_id: subscription_id
}
client = Azure::Resources::Profiles::Latest::Mgmt::Client.new(options)
#
# Managing resource groups
#
resource_group_params = client.model_classes.resource_group.new.tap do |rg|
rg.location = WEST_US
end
# List Resource Groups
puts 'List Resource Groups'
client.resource_groups.list.each{ |group| print_item(group) }
# Create Resource group
puts 'Create Resource Group'
print_item client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)
# Modify the Resource group
puts 'Modify Resource Group'
resource_group_params.tags = { hello: 'world' }
print_item client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)
# Create a Key Vault in the Resource Group
puts 'Create a Key Vault via a Generic Resource Put'
key_vault_params = client.model_classes.generic_resource.new.tap do |rg|
rg.location = WEST_US
rg.properties = {
sku: { family: 'A', name: 'standard' },
tenantId: ENV['AZURE_TENANT_ID'],
accessPolicies: [],
enabledForDeployment: true,
enabledForTemplateDeployment: true,
enabledForDiskEncryption: true
}
end
puts JSON.pretty_generate(client.resources.create_or_update(GROUP_NAME,
'Microsoft.KeyVault',
'',
'vaults',
'azureSampleVault',
'2015-06-01',
key_vault_params).properties) + "\n\n"
# List Resources within the group
puts 'List all of the resources within the group'
client.resource_groups.list.each{ |resource| print_item(resource) }
# Export the Resource group template
puts 'Export Resource Group Template'
export_params = client.model_classes.export_template_request.new.tap do |rg|
rg.resources = ['*']
end
puts JSON.pretty_generate(client.resource_groups.export_template(GROUP_NAME, export_params).template) + "\n\n"
# Delete Resource group and everything in it
puts 'Delete Resource Group'
client.resource_groups.delete(GROUP_NAME)
puts "\nDeleted: #{GROUP_NAME}"
end
def print_item(group)
puts "\tName: #{group.name}"
puts "\tId: #{group.id}"
puts "\tLocation: #{group.location}"
puts "\tTags: #{group.tags}"
print_properties(group.properties)
end
def print_properties(props)
puts "\tProperties:"
props.instance_variables.sort.each do |ivar|
str = ivar.to_s.gsub /^@/, ''
if props.respond_to? str.to_sym
puts "\t\t#{str}: #{props.send(str.to_sym)}"
end
end
puts "\n\n"
end
if $0 == __FILE__
run_example
end