Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davidfowl opened this issue Nov 22, 2024 · 0 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. Provisioning

Comments

@davidfowl
Copy link
Member

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;
}
@github-actions github-actions bot added the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Nov 22, 2024
@davidfowl davidfowl changed the title Expose a weakly typed API to get an set resource properties that are not yet in any CDK Consider exposing a weakly typed API to get an set resource properties that are not yet in any CDK Nov 22, 2024
@jsquire jsquire added Client This issue points to a problem in the data-plane of the library. Provisioning and removed needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. labels Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. Provisioning
Projects
None yet
Development

No branches or pull requests

3 participants