services | platforms | author |
---|---|---|
app-service |
ruby |
devigned |
This sample demonstrates how to manage your Azure websites using a ruby client.
On this page
-
If you don't already have it, install Ruby and the Ruby DevKit.
-
If you don't have bundler, install it.
gem install bundler
-
Clone the repository.
git clone https://github.com:Azure-Samples/app-service-web-ruby-manage.git
-
Install the dependencies using bundle.
cd app-service-web-ruby-manage bundle install
-
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
-
Set the following environment variables using the information from the service principle that you created.
export AZURE_TENANT_ID={your tenant id} export AZURE_CLIENT_ID={your client id} export AZURE_CLIENT_SECRET={your client secret} export AZURE_SUBSCRIPTION_ID={your subscription id}
[AZURE.NOTE] On Windows, use
set
instead ofexport
. -
Run the sample.
bundle exec ruby example.rb
The sample creates, lists and updates a website. It starts by setting up a ResourceManagementClient object using your subscription and credentials.
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
The sample then sets up a resource group in which it will create the website.
resource_group_params = Azure::ARM::Resources::Models::ResourceGroup.new.tap do |rg|
rg.location = WEST_US
end
resource_group_params.class.class
resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)
Create a server farm to host your website.
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)
site_params = Azure::ARM::Web::Models::Site.new.tap do |site|
site.location = WEST_US
site.properties = Azure::ARM::Web::Models::SiteProperties.new.tap do |props|
props.server_farm_id
end
end
web_client.sites.create_or_update_site(GROUP_NAME, SITE_NAME, site_params)
web_client.sites.get_sites(GROUP_NAME).each{ |site| print_item site }
web_client.sites.get_site(GROUP_NAME, SITE_NAME)
web_client.sites.delete_site(GROUP_NAME, SITE_NAME)
At this point, the sample also deletes the resource group that it created.
resource_client.resource_groups.delete(GROUP_NAME)
Please refer to Azure SDK for Ruby for more information.