How to change AWS SDK region in code? - c#

It is necessary to define
<add key="AWSRegion" value="us-east-1"/>
application setting in App.config to specify region to use.
I need to change that programmatically
var creds = new BasicAWSCredentials(key, token);
using (var routes = AWSClientFactory.CreateAmazonRoute53Client(creds)){}
how to specify the region in code?

It is the best practice to use more configurable version (i.e. endpoint configured from web.config/app.config). For EC2 client, you can do it by the following way:
var region = RegionEndpoint.GetBySystemName("ap-northeast-1");
var awsEC2Client = new AmazonEC2Client(region);
For others reason, you can specify from here
Resource Link:
How to set the EndPoint / Region for the C# .NET SDK : EC2Client?
How to start an Amazon EC2 instance programmatically in .NET

Related

C# AWS - How to determine AWS Region Endpoint dynamically in c#

I am connecting to AWS SecretManager, in which my code connects to specific region endpoint(below code)
AmazonSecretsManagerConfig config = new AmazonSecretsManagerConfig { RegionEndpoint = RegionEndpoint.USEast1 };
Now, I want to add one more new region RegionEndpoint.USEast2
I want this RegionEndpoint selection dynamically, based on the specific region request. Kindly help, How can I configure Multi Region in my C# Code?
Note: My app is running on Fargate and not on EC2 instance, so below solutions won't work:
client = Amazon.Util.EC2InstanceMetadata.Region.SystemName;
or
client = new Amazon&&&&Client(credentials, RegionEndpoint.GetBySystemName("us-east-1"));
You should be able to get the current region from reading the AWS_REGION environment variable as mentioned in this post: How can we determine the current region with AWS Fargate.

Configure a Virtual Network in Azure DevTest Labs - programmatically (c#)

I'm trying to create a brand new network with two address spaces (10.0.0.0/20 and 10.0.16.0/20) and, most importantly, set useInVmCreationPermission and usePublicIpAddressPermission to Allow.
I have the lab already created and I'm able to create a network but I'm unable to relate them with each other. I'm not sure if it's possible through the REST API but if the portal does it, I'm pretty sure it can be achieved (even if I had to use HttpClient). The way it's done in the Azure Portal, is described here: https://learn.microsoft.com/en-us/azure/devtest-labs/devtest-lab-configure-vnet .
This is my code:
private async Task Create(IAzure azureClient, DbLab dbLab)
{
var virtualNetworkId = Guid.NewGuid().ToString();
var virtualNetwork = await azureClient
.Networks
.Define(virtualNetworkId)
.WithRegion(dbLab.Region.Value)
.WithExistingResourceGroup(dbLab.ResourceGroupName)
.WithAddressSpace(Consts.IPv4DynamicAddressSpace)
.WithAddressSpace(Consts.IPv4StaticAddressSpace)
.WithSubnet($"{virtualNetworkId}_dynamic", Consts.IPv4DynamicAddressSpace)
.WithSubnet($"{virtualNetworkId}_static", Consts.IPv4StaticAddressSpace)
.CreateAsync();
var fragment = new VirtualNetworkFragment(
subnetOverrides: virtualNetwork.Subnets.Select(s =>
new SubnetOverrideFragment(
resourceId: s.Value.Name,
useInVmCreationPermission: "Allow",
usePublicIpAddressPermission: "Allow")).ToList());
// fails because vnet is not related to that lab
var vnet = await BaseClientFactory.CreateDevTestLabClient(azureClient)
.VirtualNetworks
.UpdateAsync(dbLab.ResourceGroupName, dbLab.LabId.ToString(), virtualNetworkId, fragment);
}
How do I attach a lab to the vnet programmatically in c#?
Yes, you can configure your existing virtual network to a lab's Virtual Network settings via both the REST API and the SDK as well.
REST API: Virtual Networks - Create Or Update
PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/virtualnetworks/{name}?api-version=2018-09-15
Note that resourceGroupName is the name of the resource group where the DevTest Lab exists, and not that of the VNet.
Here is the equivalent method in the .Net SDK: CreateOrUpdateWithHttpMessagesAsync

need to create subdomains in Azure DNS from ASP.NET Core dynamically

I need to create subdomains in Azure DNS from ASP.NET Core dynamically so that the users who needs to create their own subdomains can do it as we see in most of the web apps out there like wix, etc..
Could anyone please detail the right steps well explained?
Thanks.
There is a management SDK for Azure, which you can use inside your ASP.NET Core application.
Create DNS zones and record sets using the .NET SDK - this is detailed full tutorial for this.
Code will be similar to this, though you will probably use CNAMEs instead of A records
// Create record set parameters
var recordSetParams = new RecordSet();
recordSetParams.TTL = 3600;
// Add records to the record set parameter object. In this case, we'll add a record of type 'A'
recordSetParams.ARecords = new List<ARecord>();
recordSetParams.ARecords.Add(new ARecord("1.2.3.4"));
// 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>();
recordSetParams.Metadata.Add("user", "Mary");
// Create the actual record set in Azure DNS
// Note: no ETAG checks specified, will overwrite existing record set if one exists
var recordSet = await dnsClient.RecordSets.CreateOrUpdateAsync(resourceGroupName, zoneName, recordSetName, RecordType.A, recordSetParams);

TFS configuration policy creation using API in C#

I'm trying to create a new branch policy using Microsoft.TeamFoundation.Policy.WebApi Library.
my Code is:
variable info:
json - contains the setting in the policy configuration
connection - is the VSSConnection to our TFS server
TFSProject - is the project name in the TFS
the error i get the is not showing anything in my searches, i would appreciate some examples for how to create a new policy in TFS
code:
var json = "{\"statusName\": \"" + StatusNameForBlock + "\",\"statusGenre\": \"ci\",\"authorId\": null,\"invalidateOnSourceUpdate\": false,\"policyApplicability\": null,\"scope\": [{\"refName\": \"refs/heads/master\",\"matchKind\": \"Exact\"}]}";
JObject jToken = new JObject(JObject.Parse(json));
var newPolicy = new PolicyConfiguration();
var policyType = new PolicyTypeRef();
policyType.Id = Guid.NewGuid();
newPolicy.Type = policyType;
newPolicy.Settings = jToken;
var gitPolicyHttpClient = connection.GetClient<PolicyHttpClient>();
var policyCreated = gitPolicyHttpClient.CreatePolicyConfigurationAsync(newPolicy, TFSProject).Result;
exception: VssServiceException: Type with id '98813712-70a4-4937-b139-9a3654c9795f' does not exist
You could use Rest API to create a new branch policy.
POST https://{instance}/{collection}/{project}/_apis/policy/configurations/{configurationId}?api-version=5.0
Use these APIs to define policies for your projects. Configurations associate a type, such as "Required reviewers", with specific settings, such as "For pull requests with files named *.dll targeting the master branch in the xxx Git repository, add the Source-Controlled Binaries Team as a required reviewer".
Policy Examples for your reference.
For more details, you could also take a look at this blog-- Configuring standard policies for all repositories in Azure Repos

Dynamically creating endpoints for Magento in C#

I need to dynamically set the endpoint for my Magento implementation using C# but can't override C#'s default check of the endpoint path and credentials in the web.config.
Does anyone know how to do this?
My service currently looks like this:
using (Mage_Api_Model_Server_V2_HandlerPortTypeClient proxy = new Mage_Api_Model_Server_V2_HandlerPortTypeClient("NameOfEndpoint", ConnectionCurrent.WsdlPath))
{
string sessionKey = proxy.startSession();
string loginSession = proxy.login(ConnectionCurrent.UserName, ConnectionCurrent.Password);
...
At Login, it then says that I have two endpoints configured.
I've looked everywhere but can't find a solution.
Thanks!!
This is using WCF but it is similarly done with the older web services implementation:
EndpointAddress endPoint = new EndpointAddress("http://some.endpoint.addr");
Binding binding = new WSHttpBinding(SecurityMode.None);
var service = new Mage_Api_Model_Server_V2_HandlerPortTypeClient(binding, endpoint);

Categories