Open
Description
While attempting to model an existing resource I was trying to set the scope property of an existing resource but it turns out we don't expose it. I wanted to poke through the strongly typed API to a weakly typed api to set properties that weren't exposed on the top-level model. With DefineProperty being protected, there's a bias towards inheritance over composition which is less than ideal. Ideally there would be a low level way to access these properties without making a derived class.
This is what I ended up with in an attempt to model above:
using Azure.Provisioning;
using Azure.Provisioning.Expressions;
using Azure.Provisioning.Resources;
using Azure.Provisioning.Storage;
var infra = new Infrastructure("infra");
var storage = ExternalStorageAccount.FromExisting("storage");
storage.Name = "myStorage";
storage.Scope = BicepFunction2.GetResourceGroup("external");
infra.Add(storage);
Console.WriteLine(infra.Build().Compile().First().Value);
class BicepFunction2
{
public static BicepValue<ResourceGroup> GetResourceGroup(string name) =>
new FunctionCallExpression(new IdentifierExpression("resourceGroup"), new StringLiteralExpression(name));
}
class ExternalStorageAccount(string bicepIdentifier) : StorageAccount(bicepIdentifier)
{
public static ExternalStorageAccount FromExisting(string bicepIdentifier) =>
new(bicepIdentifier) { IsExistingResource = true };
public BicepValue<ResourceGroup> Scope
{
get
{
Initialize();
return _scope!;
}
set
{
Initialize();
// This runs after initialization since IsExistingResource marks all properties as readonly
_scope ??= DefineProperty<ResourceGroup>("scope", ["scope"], isOutput: false, isRequired: false);
_scope!.Assign(value);
}
}
private BicepValue<ResourceGroup>? _scope;
}