Azure Resource Manager DNS: Sample code to create a DNS record - azure-resource-manager

I'm currently trying to move out from using old Microsoft.Azure.Management.Dns package to the new Azure.ResourceManager.Dns.
However I've been having issues in our code that creates Dns records such as an Arecord.
I've tried to go through the official documentation https://learn.microsoft.com/en-us/dotnet/api/azure.resourcemanager.dns.dnsarecordcollection.createorupdate?view=azure-dotnet
But the classes that represent an Arecord are either read only or private so I have no idea how to update this simple lines:
RecordSet set = DnsManagementClient.client.RecordSets.Get(resourceGroupName, zone, recordSetName, RecordType.A);
set.ARecords = set.ARecords ?? new List<ARecord>();
set.ARecords.Add(new ARecord(ipAddress));
DnsManagementClient.client.RecordSets.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, zone, recordSetName, RecordType.A, set, ifNoneMatch: "*");
Currently documentation only talks about Zones, can an example be added to the official documentation on how to add or update a DNS record (A,CNAME,etc..)
https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/dns/Azure.ResourceManager.Dns
I'm expecting a method to create an A record that let's you specify an IP address, and currently all the classes that potentially can be used to do that are either read-only or internal.

DnsARecordData has an internal list of Arecords, DnsARecordData.DnsARecords is where we can invoke the Add method to create the record. The reason DnsARecordData doesn't have a setter method is due to the .Net framework design guideline..
An example of how to create an A record using Azure.Resourcemanager.Dns can be found here:
// Create or update A record
string myARecordName = "myrecord";
DnsARecordData dnsARecordData = new() {TtlInSeconds = (long)TimeSpan.FromHours(1).TotalSeconds};
dnsARecordData.DnsARecords.Add(new DnsARecordInfo { IPv4Address = IPAddress.Parse("127.0.0.1") });
DnsARecordCollection dnsARecordCollection1 = dnsZoneResource.GetDnsARecords();
dnsARecordCollection1.CreateOrUpdate(WaitUntil.Completed, myARecordName, dnsARecordData);
// Create or update CName pointing to A record
string myCnameName = "mycname";
DnsCnameRecordData dnsCnameRecordData = new() { Cname = $"{myARecordName}.{DnsZone}", TtlInSeconds = (long)TimeSpan.FromMinutes(10).TotalSeconds, };
DnsCnameRecordCollection cnameRecordCollection = dnsZoneResource.GetDnsCnameRecords();
cnameRecordCollection.CreateOrUpdate(WaitUntil.Completed, myCnameName, dnsCnameRecordData);

I tried in my environment and got below results:
You can create A record set using Azure.ResourceManager.Dns package. The version of NuGet package is beta-1.
NuGet Package:
Azure.ResourceManager.Dns 1.0.0 beta-1
Code:
using Azure;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Dns;
using Azure.ResourceManager.Resources;
using System.Net;
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
// first we need to get the resource group
string rgName = "rg-name";
ResourceGroupResource resourceGroup = await subscription.GetResourceGroups().GetAsync(rgName);
string dnsZoneName = "dns name";
DnsZoneCollection dnsZoneCollection = resourceGroup.GetDnsZones();
DnsZoneData data1 = new DnsZoneData("Global")
{
};
ArmOperation<DnsZoneResource> lro = await dnsZoneCollection.CreateOrUpdateAsync(WaitUntil.Completed, dnsZoneName, data1);
DnsZoneResource dnsZone = lro.Value;
RecordSetACollection recordSetACollection = dnsZone.GetRecordSetAs();
string name = "cname1";
var parm = new ARecordSetData();
parm.TTL =600;
parm.ARecords = new List<ARecord>();
parm.ARecords.Add(new ARecord("1.2.3.4"));
ArmOperation<RecordSetAResource> recordSetAResource = recordSetACollection.CreateOrUpdate(WaitUntil.Completed, name,parm);
RecordSetAResource recordSetAs = recordSetAResource.Value;
Console:
Portal:
For more reference:
azure-sdk-for-net/Sample2_ManagingRecordSetPtrs.md at dvbb-mgmt-track2-dns-2 ยท dvbb/azure-sdk-for-net (github.com)

Related

Read AzureDevOps Pipeline Variables in C#

I need develop a C# .NetCore Console Application to access my DevOps account, select a release and read all variables in Pipeline Variables[
Anybody can give me an example?
Found a solution do read variables from Azure DevOps with REST API.
https://learn.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-6.1
Here I have a rough draft to read the variables from a release definition. You can create the .NET helper objects by creating helper classes from the JSON. There are sites on the internet for this.
Main() {
var pat = Environment.GetEnvironmentVariable("????");
var baseUrl = "https://dev.azure.com/{organization}";
var apiversion = "api-version=6.0";
var project = "????";
var search = "Releasename";
var actUrl = string.Format("{0}/{1}/_apis/release/definitions?searchText={2}&{3}",baseUrl,project,search,apiversion);
var json = await GetJsonFromUrlPAT(actUrl, pat);
cReleases varReleases = JsonConvert.DeserializeObject<cReleases>(json);
int ReleaseId=0;
if (varReleases.count == 1)
{
ReleaseId = varReleases.values[0].id;
actUrl = string.Format("{0}/{1}/_apis/release/definitions/{2}?{3}",baseUrl,project,ReleaseId,apiversion);
var json2 = await GetJsonFromUrlPAT(actUrl, pat);
cReleasedefinition varReleaseDef = JsonConvert.DeserializeObject<cReleasedefinition>(json2);
string jsonstring = JsonConvert.SerializeObject(varReleaseDef);
foreach (KeyValuePair<String,JToken> element in varReleaseDef.variables)
{
string varname = element.Key;
string varvalue = ((JObject)(element.Value)).GetValue("value").ToString();
}
foreach (var element in varReleaseDef.environments)
{
string environmentname = element.name;
foreach (KeyValuePair<String, JToken> item in element.variables)
{
string varname = item.Key;
string varvalue = ((JObject)(item.Value)).GetValue("value").ToString();
}
}
}
}
No worries!
To get your pipeline variables applied to your application configuration, you're going to want to use the File Transform task within your pipeline and configure it to point to your configuration JSON file within the Target Files
File Transform Task: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/file-transform?view=azure-devops
What this task is going to do is use the variables you've associated with your pipeline to replace values within your JSON configuration.
Some things to note when using File Transform:
It can only perform replace operations, it cannot add to your configuration. So, be sure to have placeholders for every value you are looking to replace within your JSON file.
To access a nested value within JSON, you want to use a period within your naming conventions. In your case for example, you'll want to change ConnectionStrings:StorageAccountLogs to **ConnectionStrings.StorageAccountLogs" ** in order to correctly perform the variable substitution.

FluentMigrator create password protected SqlLite DB

I use FluentMigrator to create a SqlLite DB in C# using FluentMigrator.Runner.MigrationRunner. I wonder is there any way to use the SetPassword command o the SqlConnection only when the DB needs to be created ? There's a SqLiteRunnerContextFactory object but it don't seem to be a property that I can use to specify password.
public MigrationRunner CreateMigrationRunner(string connectionString, string[] migrationTargets, Assembly assembly, long version)
{
var announcer = new TextWriterAnnouncer(Console.WriteLine) { ShowSql = true };
var options = new ProcessorOptions { PreviewOnly = false, Timeout = 60 };
var runnerContext = new SqLiteRunnerContextFactory().CreateRunnerContext(connectionString, migrationTargets, version, announcer);
var sqlLiteConnection = new SQLiteConnection(connectionString);
//If the DB has already been created, it crashes later on if I specify this
sqlLiteConnection.SetPassword("ban4an4");
return new MigrationRunner(assembly,
runnerContext,
new SQLiteProcessor(sqlLiteConnection,
new SQLiteGenerator(),
announcer,
options,
new SQLiteDbFactory()));
}
I would like to avoid having to look if the file exists before setting password on connection.
Well, finally the code below works perfectly by using SetPassword everytime you create de runner. No need to check if the file exists or not. First time it creates it with the password and second time it opens it with it seems to use it to open DB. Which is exactly what I was looking for.

Query through SPListCollection

I wonder if it's somehow possible to query a SPListCollection object using SPQuery, as with SPListItemCollection. Imagine that you want to find out which lists were created by a given Author or visible for a given user, for example.
No, this is not possible with SPQuery! But i would prefer you using KeywordQuery:
using (SPSite siteCollection = new SPSite("http://server/sitecollection"))
{
KeywordQuery keywordQuery = new KeywordQuery(siteCollection);
keywordQuery.QueryText = "SharePoint";
SearchExecutor searchExecutor = new SearchExecutor();
ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
var resultTable = resultTables.FirstOrDefault();
DataTable dataTable = resultTable.Table;
}
Within the Keywordquery you could use for example contentclass STSList to retrieve only lists. And in this case when only using contentclass:"STSList" then you would get all lists where the executor has permissions. You can narrow down by adding additional query parameters. SharePoint search is what you are looking for.

MSDeploy API - Deleting a remote file through code

I am looking to remotely fire a delete command using the MSDeploy API through c# code.
I want to achieve the following command:
msdeploy.exe -verb:delete -dest:contentPath="/folderName/filename.txt"
instead of through running an unmanaged external executable, I want to execute this using the MSDeploy .Net API.
Assuming you're trying to delete an absolute filepath (rather than a file in a website), you're looking for something like this:
DeploymentObject destObject = DeploymentManager.CreateObject(
DeploymentWellKnownProvider.FilePath, "/foldername/filename.txt");
DeploymentObject sourceObject = DeploymentManager.CreateObject("auto", "");
DeploymentBaseOptions baseOptions = new DeploymentBaseOptions();
DeploymentSyncOptions syncOptions = new DeploymentSyncOptions
{
DeleteDestination = true;
};
DeploymentChangeSummary results = sourceObject.SyncTo(
destObject, baseOptions, syncOptions);
// results.ObjectsDeleted == 1
I've found the answer thanks to Richard Szalay's leading and i've used the ContentPath provider as this is a common provider used by VS Publishing so the chances of having permissions is high:
var deployBaseOptions = new DeploymentBaseOptions
{
ComputerName = "https://mywebserver.com:8172/msdeploy.axd?sitename=yourIISWebsiteName",
UserName = "username",
Password = "password",
UseDelegation = true,
AuthenticationType = "Basic"
};
var syncOptions = new DeploymentSyncOptions
{
DeleteDestination = true
};
var deploymentObject = DeploymentManager.CreateObject(DeploymentWellKnownProvider.ContentPath,
"yourIISWebsiteName" + "/fileToDelete.txt",
destBaseOptions);
var results = deploymentObject.SyncTo(deployBaseOptions, syncOptions);
The weird thing is that results always shows 3 files deleted even when there is only one...?!

How to create a Structure Group in SDL Tridion 2011 using core services?

I want to create a Structure Group in Tridion 2011 using core services
Any idea?
This is the code:
var structureGroup = ClientAdmin.GetDefaultData(ItemType.StructureGroup, "tcm:0-2-1");
structureGroup.Title = "SG";
structureGroup.Directiry = structureGroup.Title.Replace(" ", ""),
structureGroup = (StructureGroupData) ClientAdmin.Create(structureGroup, new ReadOptions());
I don't think that any explanations are needed here, if you want to know more - check StructureGroupData class in CoreService API docs
The code above doesn't seem to be an standard Core Services API. Check this one.
CoreServiceClient channel = new CoreServiceClient("basicHttp_2011");
string organizationalItemId = "tcm:6-3-4";
StructureGroupData sg = new StructureGroupData();
sg.Id = "tcm:0-0-0";
sg.Title = "NewSG";
sg.LocationInfo = new LocationInfo() { OrganizationalItem = new LinkToOrganizationalItemData() { IdRef = organizationalItemId } };
sg.Directory = "NewSG";
sg = (StructureGroupData)channel.Save(sg, new ReadOptions());
You might note there that the Directory property cannot contain blank spaces. That is because it is validated by a regular expression. You can change it in the file cm_xml_usr.xsd located at [TRIDION_HOME]\bin.

Resources