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 3
/
example.rb
126 lines (105 loc) · 3.58 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
120
121
122
123
124
#!/usr/bin/env ruby
require 'azure_mgmt_resources'
require 'azure_mgmt_web'
require 'dotenv'
require 'haikunator'
Dotenv.load(File.join(__dir__, './.env'))
WEST_US = 'westus'
GROUP_NAME = 'azure-sample-group'
SERVER_FARM_NAME = 'sample-server-farm'
SITE_NAME = Haikunator.haikunate(100)
# 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)
web_client = Azure::ARM::Web::WebSiteManagementClient.new(credentials)
resource_client = Azure::ARM::Resources::ResourceManagementClient.new(credentials)
resource_client.subscription_id = web_client.subscription_id = subscription_id
#
# Create a resource group
#
resource_group_params = Azure::ARM::Resources::Models::ResourceGroup.new.tap do |rg|
rg.location = WEST_US
end
resource_group_params.class.class
puts 'Create Resource Group'
print_item resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)
#
# Create a Server Farm for your WebApp
#
puts 'Create a Server Farm for your WebApp'
server_farm_params = Azure::ARM::Web::Models::ServerFarmWithRichSku.new.tap do |sf|
sf.location = WEST_US
sf.sku = Azure::ARM::Web::Models::SkuDescription.new.tap do |sd|
sd.name = 'S1'
sd.capacity = 1
sd.tier = 'Standard'
end
end
print_item web_client.server_farms.create_or_update_server_farm(GROUP_NAME, SERVER_FARM_NAME, server_farm_params)
#
# Create a Site to be hosted in the Server Farm
#
puts 'Create a Site to be hosted in the Server Farm'
site_params = Azure::ARM::Web::Models::Site.new.tap do |site|
site.location = WEST_US
site.server_farm_id = SERVER_FARM_NAME
end
print_item web_client.sites.create_or_update_site(GROUP_NAME, SITE_NAME, site_params)
#
# List Sites by Resource Group
#
puts 'List Sites by Resource Group'
web_client.sites.get_sites(GROUP_NAME).each{ |site| print_item site }
#
# Get a single Site
#
puts 'Get a single Site'
site = web_client.sites.get_site(GROUP_NAME, SITE_NAME)
print_item site
puts "Your site and server farm have been created. You can now go and visit at 'http://#{site.default_host_name}'.\nPress enter to delete the site and server farm."
gets
#
# Delete a Site
#
puts 'Deleting the Site'
web_client.sites.delete_site(GROUP_NAME, SITE_NAME)
#
# Delete the Resource Group
#
puts 'Deleting the resource group'
resource_client.resource_groups.delete(GROUP_NAME)
end
def print_item(resource)
puts "\tName: #{resource.name}"
puts "\tId: #{resource.id}"
puts "\tLocation: #{resource.location}"
puts "\tTags: #{resource.tags}"
print_properties(resource)
end
def print_properties(resource)
puts "\tProperties:"
resource.instance_variables.sort.each do |ivar|
str = ivar.to_s.gsub /^@/, ''
if resource.respond_to? str.to_sym
puts "\t\t#{str}: #{resource.send(str.to_sym)}"
end
end
puts "\n\n"
end
if $0 == __FILE__
run_example
end