azuremetadataazure-dns

Azure.ResourceManager.Dns and MetaData


I am migrating an app that uses Microsoft.Azure.Management.Dns to Azure.ResourceManager.Dns

Everything has been rewritten (not even close to a migration). In any event I got it all working with the exception of Metadata. The following code works well in the deprecated SDK.

RecordSet recordSetParams; is where I add Metdata when using the deprecated SDK. Here's a sample:

        // Add metadata to the record set.  Similar to Azure Resource Manager tags, this is optional and you can add multiple metadata name/value pairs
        recordSetParams.Metadata = new Dictionary<string, string>
            {
                { OUR_HOST_KEY, Environment.MachineName },
                { "Requestor-IPaddress", requestSourceIP },
                { "datetime", $"{DateTime.Now.ToShortDateString()}-{DateTime.Now.ToShortTimeString()}" }
            };
...
                recordSetParams.ARecords.Add(new ARecord(address));
...        }
        return recordSetParams;
    }

I am unable to find an object reference I can use to store our Metadata in the new SDK. I checked every class I can find; several expose Metadata but they only have a getter, no setter. DnsARecordCollection would seem to be the place to add the Dictionary but I can't find it. GitHub Copilot is three years behind so it was little help. It always falls back to the old SDK.

I'm probably just looking in the wrong places. Any help you can provide will be greatly appreciated.


Solution

  • You can refer this Github repository code to configure DNS Recordset metadata with Azure.ResourceManager.Dns

    Sample Code1:-

    using System;
    using System.Collections.Generic;
    using Azure;
    using Azure.ResourceManager.Dns;
    using Azure.ResourceManager.Dns.Models;
    using Azure.ResourceManager.Models;
    
    class Program
    {
        static void Main(string[] args)
        {
            // Define metadata
            var metadata = new Dictionary<string, string>
            {
                { "OUR_HOST_KEY", Environment.MachineName },
                { "Requestor-IPaddress", "<request-source-ip>" },
                { "datetime", $"{DateTime.Now.ToShortDateString()}-{DateTime.Now.ToShortTimeString()}" }
            };
    
            // Call the factory methods to create record data objects with metadata
            var dnsAaaaRecordData = ArmDnsModelFactory.DnsAaaaRecordData(metadata: metadata);
            var dnsARecordData = ArmDnsModelFactory.DnsARecordData(metadata: metadata);
    
            // Use the created record data objects as needed
            // For example:
            Console.WriteLine($"DnsAaaaRecordData Metadata Count: {dnsAaaaRecordData.Metadata.Count}");
            Console.WriteLine($"DnsARecordData Metadata Count: {dnsARecordData.Metadata.Count}");
        }
    }
    

    Output:-

    enter image description here

    Alternatively you can directly use Azure Rest API create/update operation and access properties.metadata.