Skip to content

Consider exposing a weakly typed API to get an set resource properties that are not yet in any CDK #47291

Open
@davidfowl

Description

@davidfowl

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;
}

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Provisioning

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions