Last active
November 3, 2023 14:41
-
-
Save anthony-c-martin/aa377090d074a820c77b3c7095de5eef to your computer and use it in GitHub Desktop.
Random C# Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Bicep.Core.Diagnostics; | |
using Bicep.Core.Parsing; | |
using Bicep.Core.PrettyPrint; | |
using Bicep.Core.PrettyPrint.Options; | |
using Bicep.Core.Syntax; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var param = new ParameterDeclarationSyntax( | |
Enumerable.Empty<SyntaxBase>(), | |
SyntaxFactory.CreateToken(TokenType.Identifier, "param"), | |
SyntaxFactory.CreateIdentifier("accName"), | |
SyntaxFactory.CreateIdentifier("string"), | |
null); | |
var resource = new ResourceDeclarationSyntax( | |
Enumerable.Empty<SyntaxBase>(), | |
SyntaxFactory.CreateToken(TokenType.Identifier, "resource"), | |
SyntaxFactory.CreateIdentifier("stgAcc"), | |
SyntaxFactory.CreateStringLiteral("Microsoft.Storage/storageAccounts@2022-09-01"), | |
null, | |
SyntaxFactory.AssignmentToken, | |
SyntaxFactory.CreateObject(new[] { | |
SyntaxFactory.CreateObjectProperty("name", SyntaxFactory.CreateIdentifier("accName")), | |
SyntaxFactory.CreateObjectProperty("location", SyntaxFactory.CreatePropertyAccess(SyntaxFactory.CreateFunctionCall("resourceGroup"), "location")), | |
SyntaxFactory.CreateObjectProperty("sku", SyntaxFactory.CreateObject(new [] { | |
SyntaxFactory.CreateObjectProperty("name", SyntaxFactory.CreateStringLiteral("Standard_LRS")), | |
})), | |
SyntaxFactory.CreateObjectProperty("kind", SyntaxFactory.CreateStringLiteral("StorageV2")), | |
})); | |
var program = new ProgramSyntax(new SyntaxBase[] { | |
param, | |
SyntaxFactory.NewlineToken, | |
SyntaxFactory.NewlineToken, | |
resource, | |
}, SyntaxFactory.CreateToken(TokenType.EndOfFile, ""), Enumerable.Empty<IDiagnostic>()); | |
var output = PrettyPrinter.PrintProgram(program, new PrettyPrintOptions(NewlineOption.Auto, IndentKindOption.Space, 2, true)); | |
Console.Write(output); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Azure.Bicep.Types.Concrete; | |
using Azure.Bicep.Types.Az; | |
using Newtonsoft.Json.Linq; | |
using System.Runtime.CompilerServices; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var loader = new AzTypeLoader(); | |
var index = loader.LoadTypeIndex(); | |
var allTypes = index.Resources; | |
var vm = loader.LoadResourceType(allTypes["Microsoft.KeyVault/managedHSMs@2023-07-01"]); | |
var vmProps = (vm.Body.Type as ObjectType).Properties; | |
Console.WriteLine(ToJsonSchemaRecursive(vm.Body.Type).ToString()); | |
} | |
private static JObject ToJsonSchemaRecursive(TypeBase type) | |
{ | |
// TODO handle cycles! | |
RuntimeHelpers.EnsureSufficientExecutionStack(); | |
switch (type) | |
{ | |
case StringLiteralType _: | |
case StringType _: | |
case UnionType _: | |
return new JObject | |
{ | |
["type"] = "string" | |
}; | |
case IntegerType _: | |
return new JObject | |
{ | |
["type"] = "number" | |
}; | |
case BooleanType _: | |
return new JObject | |
{ | |
["type"] = "boolean" | |
}; | |
case ArrayType arrayType: | |
return new JObject | |
{ | |
["type"] = "array", | |
["items"] = ToJsonSchemaRecursive(arrayType.ItemType.Type) | |
}; | |
case ObjectType objectType: | |
var writableProps = objectType.Properties.Where(x => !x.Value.Flags.HasFlag(ObjectTypePropertyFlags.ReadOnly)); | |
var requiredProps = writableProps.Where(x => x.Value.Flags.HasFlag(ObjectTypePropertyFlags.Required)); | |
var properties = writableProps.Select(x => new JProperty(x.Key, ToJsonSchemaRecursive(x.Value.Type.Type))); | |
return new JObject | |
{ | |
["type"] = "object", | |
["properties"] = new JObject(properties), | |
["required"] = new JArray(requiredProps.Select(x => x.Key)), | |
}; | |
default: | |
// TODO discriminated object support | |
throw new NotImplementedException($"{type.GetType()}"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Net.Http; | |
using Microsoft.Rest; | |
using Microsoft.Rest.Azure; | |
using Microsoft.Azure.Management.ResourceManager; | |
using Microsoft.Azure.Management.ResourceManager.Models; | |
#nullable enable | |
public class CorrelationIdHandler : DelegatingHandler | |
{ | |
private readonly string? correlationId; | |
public CorrelationIdHandler(string? correlationId) | |
{ | |
this.correlationId = correlationId; | |
} | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
if (correlationId is not null) | |
{ | |
request.Headers.TryAddWithoutValidation("x-ms-correlation-request-id", correlationId); | |
} | |
return base.SendAsync(request, cancellationToken); | |
} | |
} | |
public class ArmClientFactory | |
{ | |
private readonly ServiceClientCredentials credentials; | |
public ArmClientFactory(ServiceClientCredentials credentials) | |
{ | |
this.credentials = credentials; | |
} | |
public IResourceManagementClient Create(string? correlationId) | |
=> new ResourceManagementClient(credentials, new DelegatingHandler[] { | |
new CorrelationIdHandler(correlationId), | |
}); | |
} | |
public class ArmManagementGroupScopeOperations | |
{ | |
private readonly ArmClientFactory clientFactory; | |
private readonly string managementGroupId; | |
private readonly string location; | |
public ArmManagementGroupScopeOperations(ArmClientFactory clientFactory, string managementGroupId, string location) | |
{ | |
this.clientFactory = clientFactory; | |
this.managementGroupId = managementGroupId; | |
this.location = location; | |
} | |
public Func<string, Task<DeploymentValidateResult>> ValidateFunc( | |
string deploymentName, | |
DeploymentProperties deploymentProperties, | |
CancellationToken cancellationToken) | |
{ | |
var deployment = new ScopedDeployment | |
{ | |
Properties = deploymentProperties, | |
Location = this.location, | |
}; | |
return (correlationId) => clientFactory.Create(correlationId).Deployments.ValidateAtManagementGroupScopeAsync( | |
this.managementGroupId, | |
deploymentName, | |
deployment, | |
cancellationToken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment