Skip to content

Commit 38f149a

Browse files
authored
automate creation of azure.yaml (Azure#47404)
1 parent 874f44b commit 38f149a

File tree

10 files changed

+123
-11
lines changed

10 files changed

+123
-11
lines changed

sdk/cloudmachine/Azure.Provisioning.CloudMachine/api/Azure.Provisioning.CloudMachine.net8.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public static partial class Azd
44
{
55
public static void Init(Azure.CloudMachine.CloudMachineClient client, string? infraDirectory = null) { }
66
public static void Init(Azure.CloudMachine.CloudMachineInfrastructure infra, string? infraDirectory = null) { }
7+
public static void InitDeployment(Azure.CloudMachine.CloudMachineInfrastructure infra, string? webProjectName) { }
78
}
89
public static partial class CloudMachineClientExtensions
910
{

sdk/cloudmachine/Azure.Provisioning.CloudMachine/api/Azure.Provisioning.CloudMachine.netstandard2.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public static partial class Azd
44
{
55
public static void Init(Azure.CloudMachine.CloudMachineClient client, string? infraDirectory = null) { }
66
public static void Init(Azure.CloudMachine.CloudMachineInfrastructure infra, string? infraDirectory = null) { }
7+
public static void InitDeployment(Azure.CloudMachine.CloudMachineInfrastructure infra, string? webProjectName) { }
78
}
89
public static partial class CloudMachineClientExtensions
910
{

sdk/cloudmachine/Azure.Provisioning.CloudMachine/src/CloudMachineInfrastructure/CloudMachineInfrastructure.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public CloudMachineInfrastructure(string? cmId = default)
6262
AddFeature(new ServiceBusSubscriptionFeature("cm_servicebus_subscription_private", sbTopicPrivate)); // TODO: should private connections not be in the Connections collection?
6363
AddFeature(new ServiceBusSubscriptionFeature("cm_servicebus_subscription_default", sbTopicDefault));
6464
var systemTopic = AddFeature(new EventGridSystemTopicFeature(Id, storage, "Microsoft.Storage.StorageAccounts"));
65-
AddFeature(new SystemTopicEventSubscriptionFeature("cm_eventgrid_subscription_blob", systemTopic, sbTopicPrivate, sbNamespace));
65+
AddFeature(new SystemTopicEventSubscriptionFeature("cm-eventgrid-subscription-blob", systemTopic, sbTopicPrivate, sbNamespace));
6666
}
6767

6868
public T AddFeature<T>(T feature) where T: CloudMachineFeature

sdk/cloudmachine/Azure.Provisioning.CloudMachine/src/Tooling/Azd.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Azure.Provisioning.Primitives;
77
using Azure.Provisioning.Resources;
88
using Azure.Provisioning.Expressions;
9+
using System.Linq;
910

1011
namespace Azure.CloudMachine;
1112

@@ -27,7 +28,8 @@ public static void Init(CloudMachineClient client, string? infraDirectory = defa
2728

2829
public static void Init(CloudMachineInfrastructure infra, string? infraDirectory = default)
2930
{
30-
if (infraDirectory == default) infraDirectory = Path.Combine(".", "infra");
31+
if (infraDirectory == default)
32+
infraDirectory = Path.Combine(".", "infra");
3133

3234
Directory.CreateDirectory(infraDirectory);
3335

@@ -64,6 +66,28 @@ public static void Init(CloudMachineInfrastructure infra, string? infraDirectory
6466

6567
WriteMainParametersFile(infraDirectory);
6668
}
69+
70+
public static void InitDeployment(CloudMachineInfrastructure infra, string? webProjectName)
71+
{
72+
var webCsproj = webProjectName switch
73+
{
74+
null => ".",
75+
_ => webProjectName.EndsWith(".csproj") ? webProjectName : $"{webProjectName}.csproj"
76+
};
77+
var webProjDirectory = webCsproj switch
78+
{
79+
"." => null,
80+
_ => Directory
81+
.GetFiles(Directory.GetCurrentDirectory(), "*" + webCsproj, SearchOption.AllDirectories)
82+
.SingleOrDefault()
83+
};
84+
if (webProjDirectory != null)
85+
{
86+
webProjDirectory = Path.GetDirectoryName(webProjDirectory)?.Replace(Directory.GetCurrentDirectory(), ".");
87+
}
88+
WriteAzureYamlFile(webProjDirectory ?? "./", infra.Id);
89+
}
90+
6791
private static void WriteMainParametersFile(string infraDirectory)
6892
{
6993
File.WriteAllText(Path.Combine(infraDirectory, $"{MainBicepName}.parameters.json"),
@@ -85,4 +109,23 @@ private static void WriteMainParametersFile(string infraDirectory)
85109
}
86110
""");
87111
}
112+
113+
private static void WriteAzureYamlFile(string webProjDirectory, string cmId, string? hostType = "appservice")
114+
{
115+
if (webProjDirectory == ".")
116+
{
117+
webProjDirectory = "./";
118+
}
119+
120+
File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "azure.yaml"),
121+
$"""
122+
name: {cmId}
123+
resourceGroup: {cmId}
124+
services:
125+
{cmId}:
126+
project: {webProjDirectory} # path to your web project
127+
language: csharp # one of the supported languages
128+
host: {hostType} # one of the supported Azure services
129+
""");
130+
}
88131
}

sdk/cloudmachine/Azure.Provisioning.CloudMachine/src/Tooling/CloudMachineCommands.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ public static bool Execute(string[] args, Action<CloudMachineInfrastructure>? co
3333
return Handled(exitProcessIfHandled);
3434
}
3535

36+
if (args[0] == "-init")
37+
{
38+
Azd.Init(cmi);
39+
40+
string? projName = default;
41+
if (args.Length > 1)
42+
{
43+
projName = args[1];
44+
}
45+
Azd.InitDeployment(cmi, projName);
46+
return Handled(exitProcessIfHandled);
47+
}
48+
3649
if (args[0] == "-tsp")
3750
{
3851
GenerateTsp(cmi.Endpoints);

sdk/cloudmachine/Azure.Provisioning.CloudMachine/tests/CommandsTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ public void ListModels()
1313
{
1414
CloudMachineCommands.Execute(["-ai", "chat"], exitProcessIfHandled: false);
1515
}
16+
17+
[Test]
18+
public void DoInit()
19+
{
20+
CloudMachineCommands.Execute(["-init", "demo.csproj"], exitProcessIfHandled: false);
21+
}
1622
}

sdk/cloudmachine/Azure.Provisioning.CloudMachine/tests/Data/GenerateBicep.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ resource cm_eventgrid_topic 'Microsoft.EventGrid/systemTopics@2022-06-15' = {
193193
}
194194

195195
resource cm_eventgrid_subscription_blob 'Microsoft.EventGrid/systemTopics/eventSubscriptions@2022-06-15' = {
196-
name: 'cm_eventgrid_subscription_blob'
196+
name: 'cm-eventgrid-subscription-blob'
197197
properties: {
198198
deliveryWithResourceIdentity: {
199199
identity: {

sdk/cloudmachine/Azure.Provisioning.CloudMachine/tests/Data/JustCloudMachine.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ resource cm_eventgrid_topic 'Microsoft.EventGrid/systemTopics@2022-06-15' = {
193193
}
194194

195195
resource cm_eventgrid_subscription_blob 'Microsoft.EventGrid/systemTopics/eventSubscriptions@2022-06-15' = {
196-
name: 'cm_eventgrid_subscription_blob'
196+
name: 'cm-eventgrid-subscription-blob'
197197
properties: {
198198
deliveryWithResourceIdentity: {
199199
identity: {

sdk/cloudmachine/Azure.Provisioning.CloudMachine/tests/Data/OpenAI.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ resource cm_eventgrid_topic 'Microsoft.EventGrid/systemTopics@2022-06-15' = {
193193
}
194194

195195
resource cm_eventgrid_subscription_blob 'Microsoft.EventGrid/systemTopics/eventSubscriptions@2022-06-15' = {
196-
name: 'cm_eventgrid_subscription_blob'
196+
name: 'cm-eventgrid-subscription-blob'
197197
properties: {
198198
deliveryWithResourceIdentity: {
199199
identity: {

sdk/cloudmachine/README.MD renamed to sdk/cloudmachine/README.md

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
# Azure CloudMachine
1+
# Azure CloudMachine client library for .NETyy
22

33
Write Azure apps in 5 minutes
44

55
## Getting started
66

7+
### Install the package
8+
9+
Install the client library for .NET with [NuGet](https://www.nuget.org/ ):
10+
11+
```dotnetcli
12+
dotnet add package Azure.CloudMachine.All --prerelease
13+
```
14+
15+
### Authenticate the Client
16+
717
### Prerequisites
818

919
* You must have an [Azure subscription](https://azure.microsoft.com/free/dotnet/).
@@ -13,6 +23,10 @@ Write Azure apps in 5 minutes
1323
* You must have npm installed
1424
* You must be logged into Azure CLI and Azure Developer CLI
1525

26+
## Key concepts
27+
28+
Write Azure apps in 5 minutes using simplified opinionated APIs and declarative resource provisioning.
29+
1630
### Walkthrough
1731

1832
#### Create Server Project
@@ -38,7 +52,7 @@ using Azure.CloudMachine;
3852

3953
if (CloudMachineInfrastructure.Configure(args)) return;
4054
```
41-
The `CloudMachineInfrastructure.Configure` call allows running the app with a `-bicep` switch, which will generate bicep files required to provision CloudMachine resources in Azure. Let's generate these bicep files now.
55+
The `CloudMachineInfrastructure.Configure` call allows running the app with a `-bicep` switch, which will generate bicep files required to provision CloudMachine resources in Azure. Let's generate these bicep files now.
4256
```dotnetcli
4357
dotnet run -bicep
4458
```
@@ -56,7 +70,7 @@ When provisioning finishes, you should see something like the following in the c
5670
```dotnetcli
5771
(✓) Done: Resource group: cm125957681369428 (627ms)
5872
```
59-
And if you go to your Azure portal, or execute the following az command, you can see the resource group created. The resource group will contain resources such as Storage, ServiceBus, and EventGrid.
73+
And if you go to your Azure portal, or execute the following az command, you can see the resource group created. The resource group will contain resources such as Storage, ServiceBus, and EventGrid.
6074
```dotnetcli
6175
az resource list --resource-group <resource_group_from_command_line> --output table
6276
```
@@ -92,7 +106,7 @@ class AssistantService {
92106
var client = cm.GetOpenAIChatClient();
93107
ChatCompletion completion = await client.CompleteChatAsync(message);
94108
return completion.Content[0].Text;
95-
}
109+
}
96110
}
97111
```
98112
Lastly, create an instance of the service and call the `Chat` method when the user navigates to the root URL:
@@ -123,7 +137,7 @@ class AssistantService {
123137
var client = cm.GetOpenAIChatClient();
124138
ChatCompletion completion = await client.CompleteChatAsync(message);
125139
return completion.Content[0].Text;
126-
}
140+
}
127141

128142
internal static void Configure(CloudMachineInfrastructure cm) {
129143
cm.AddFeature(new OpenAIFeature() {
@@ -181,7 +195,7 @@ class AssistantService : IAssistantService {
181195
var client = cm.GetOpenAIChatClient();
182196
ChatCompletion completion = await client.CompleteChatAsync(message);
183197
return completion.Content[0].Text;
184-
}
198+
}
185199

186200
internal static void Configure(CloudMachineInfrastructure cm) {
187201
cm.AddFeature(new OpenAIFeature() {
@@ -270,3 +284,37 @@ Go back to the client project command window and start the client:
270284
dotnet run
271285
```
272286
You can use the simple command line app to ask Azure OpenAI some questions.
287+
288+
## Examples
289+
290+
## Troubleshooting
291+
292+
- File an issue via [GitHub Issues](https://github.com/Azure/azure-sdk-for-net/issues).
293+
- Check [previous questions](https://stackoverflow.com/questions/tagged/azure+.net) or ask new ones on Stack Overflow using Azure and .NET tags.
294+
295+
## Next steps
296+
297+
## Contributing
298+
299+
For details on contributing to this repository, see the [contributing
300+
guide][cg].
301+
302+
This project welcomes contributions and suggestions. Most contributions
303+
require you to agree to a Contributor License Agreement (CLA) declaring
304+
that you have the right to, and actually do, grant us the rights to use
305+
your contribution. For details, visit <https://cla.microsoft.com>.
306+
307+
When you submit a pull request, a CLA-bot will automatically determine
308+
whether you need to provide a CLA and decorate the PR appropriately
309+
(for example, label, comment). Follow the instructions provided by the
310+
bot. You'll only need to do this action once across all repositories
311+
using our CLA.
312+
313+
This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For
314+
more information, see the [Code of Conduct FAQ][coc_faq] or contact
315+
<[email protected]> with any other questions or comments.
316+
317+
<!-- LINKS -->
318+
[cg]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/CONTRIBUTING.md
319+
[coc]: https://opensource.microsoft.com/codeofconduct/
320+
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/

0 commit comments

Comments
 (0)