Přeskočit navigaci

Manage Azure DNS with Ruby

Poslední aktualizace: 27.09.2016
Upravit na GitHubu

This sample demonstrates how to manage your Azure DNS using a ruby client.

On this page

1. If you don't already have it, install Ruby and the Ruby DevKit.

  1. If you don't have bundler, install it.

    gem install bundler
  2. Clone the repository.

    git clone https://github.com/Azure-Samples/dns-ruby-zones-and-recordsets.git
  3. Install the dependencies using bundle.

    cd dns-ruby-zones-and-recordsets
    bundle install
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. 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 of export.

  6. Run the sample.

    bundle exec ruby example.rb

What does example.rb do?

The sample creates a DNS Management client and creates a DNS zone. 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(
credentials = MsRest::TokenCredentials.new(provider)

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.

resource_group_params = Azure::ARM::Resources::Models::ResourceGroup.new.tap do |rg|
  rg.location = WEST_US


resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)

Create a DNS record

record = Azure::ARM::Dns::Models::RecordSet.new.tap do |r|
    arecord1 = Azure::ARM::Dns::Models::ARecord.new.tap do |a|
        a.ipv4address = ""
    arecord2 = Azure::ARM::Dns::Models::ARecord.new.tap do |a|
        a.ipv4address = ""
    r.arecords = [arecord1 , arecord2]
record_params = Azure::ARM::Dns::Models::RecordSetUpdateParameters.new.tap do |r|
    r.record_set = record

dns_client.record_sets.create_or_update(GROUP_NAME, ZONE_NAME, "www", Azure::ARM::Dns::Models::RecordType::A, record_params)

Delete a zone

dns_client.zones.delete(GROUP_NAME, ZONE_NAME)

More information

Please refer to Azure SDK for Ruby for more information.

Další ukázky od: Veronica Giaudrone