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

Azure Provisioning Integration #2429

Merged
merged 17 commits into from
Mar 1, 2024
Merged

Azure Provisioning Integration #2429

merged 17 commits into from
Mar 1, 2024

Conversation

mitchdenny
Copy link
Member

@mitchdenny mitchdenny commented Feb 26, 2024

Contributes to #2423

This PR introduces a new extension method called AddAzureConstruct(...) which allows developers to make use of the Azure CDK (Azure.Provisioning) to define Azure resources. This initial version provides basic integration for accepting parameters from the Aspire app model and using outputs.

The usage is as follows:

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Azure.Provisioning.Storage;
using Azure.ResourceManager.Storage.Models;

var builder = DistributedApplication.CreateBuilder(args);
builder.AddAzureProvisioning();

var sku = builder.AddParameter("storagesku");

var construct1 = builder.AddAzureConstruct("construct1", (construct) =>
{
    var account = construct.AddStorageAccount(
        name: "bob",
        kind: StorageKind.BlobStorage,
        sku: StorageSkuName.StandardLrs
        );
    account.AssignParameter(a => a.Sku.Name, construct.AddParameter(sku));

    account.AddOutput(data => data.PrimaryEndpoints.TableUri, "tableUri", isSecure: true);
});

builder.AddProject<Projects.CdkSample_ApiService>("api")
       .WithEnvironment("TABLE_URI", construct1.GetOutput("tableUri"));

// This project is only added in playground projects to support development/debugging
// of the dashboard. It is not required in end developer code. Comment out this code
// to test end developer dashboard launch experience. Refer to Directory.Build.props
// for the path to the dashboard binary (defaults to the Aspire.Dashboard bin output
// in the artifacts dir).
builder.AddProject<Projects.Aspire_Dashboard>(KnownResourceNames.AspireDashboard);

builder.Build().Run();

This would result in the following manifest:

{
  "resources": {
    "storagesku": {
      "type": "parameter.v0",
      "value": "{storagesku.inputs.value}",
      "inputs": {
        "value": {
          "type": "string"
        }
      }
    },
    "construct1": {
      "type": "azure.bicep.v0",
      "path": "construct1.module.bicep",
      "params": {
        "storagesku": "{storagesku.value}"
      }
    },
    "api": {
      "type": "project.v0",
      "path": "../CdkSample.ApiService/CdkSample.ApiService.csproj",
      "env": {
        "OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES": "true",
        "OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
        "TABLE_URI": "{construct1.outputs.tableUri}"
      },
      "bindings": {
        "http": {
          "scheme": "http",
          "protocol": "tcp",
          "transport": "http"
        },
        "https": {
          "scheme": "https",
          "protocol": "tcp",
          "transport": "http"
        }
      }
    }
  }
}

And the construct1.module.bicep file would look like the following:

targetScope = 'resourceGroup'

@description('')
param location string = resourceGroup().location

@description('')
param storagesku string


resource storageAccount_unUi1Obb4 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: toLower(take(concat('bob', uniqueString(resourceGroup().id)), 24))
  location: location
  sku: {
    name: storagesku
  }
  kind: 'StorageV2'
  properties: {
  }
}

output tableUri string = storageAccount_unUi1Obb4.properties.primaryEndpoints.table

You can see that the Aspire parameter has been bound into the generated Bicep as a parameter (we rely on AZD and/or Aspire.Hosting.Azure.Provisioning to pipe these values through.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Feb 26, 2024
@mitchdenny mitchdenny self-assigned this Feb 26, 2024
@mitchdenny mitchdenny added this to the preview 5 (Apr) milestone Feb 26, 2024
@mitchdenny mitchdenny marked this pull request as ready for review February 29, 2024 23:39
@mitchdenny mitchdenny changed the title DRAFT: Azure Provisioning Integration Azure Provisioning Integration Feb 29, 2024
@mitchdenny mitchdenny requested a review from davidfowl February 29, 2024 23:45
@mitchdenny mitchdenny enabled auto-merge (squash) March 1, 2024 02:41
@mitchdenny mitchdenny merged commit 1b31789 into main Mar 1, 2024
8 checks passed
@mitchdenny mitchdenny deleted the mitchdenny/add-cdk-resource branch March 1, 2024 02:43
@github-actions github-actions bot locked and limited conversation to collaborators Apr 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants