Skip to content

Commit

Permalink
Fixed a bug in AZD integration code (#46791)
Browse files Browse the repository at this point in the history
* improved code that rewrites appsettings.json

* removed unused namespaces
  • Loading branch information
KrzysztofCwalina authored Oct 23, 2024
1 parent f8154ee commit 9ad8747
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Provisioning.CloudMac
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Provisioning.CloudMachine.Tests", "tests\Azure.Provisioning.CloudMachine.Tests.csproj", "{46DCEF27-4157-4FB6-A283-B8484EC45665}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Provisioning", "..\Azure.Provisioning\src\Azure.Provisioning.csproj", "{2F492C07-8182-41E2-8C68-27D353C9AB74}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Provisioning.KeyVault", "..\Azure.Provisioning.KeyVault\src\Azure.Provisioning.KeyVault.csproj", "{DA43BF21-558B-438B-9DBF-1A88130C944A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Provisioning.ServiceBus", "..\Azure.Provisioning.ServiceBus\src\Azure.Provisioning.ServiceBus.csproj", "{48D33345-B5A8-4F94-84D6-A61F8E16271D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Provisioning.EventGrid", "..\Azure.Provisioning.EventGrid\src\Azure.Provisioning.EventGrid.csproj", "{BCBAF45F-E078-4230-9BDC-4DE9F2FBEE9A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Provisioning.CognitiveServices", "..\Azure.Provisioning.CognitiveServices\src\Azure.Provisioning.CognitiveServices.csproj", "{987279D2-202C-488A-BA47-37FCEEA51536}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Provisioning.Storage", "..\Azure.Provisioning.Storage\src\Azure.Provisioning.Storage.csproj", "{813E7228-2B59-41D2-9AFC-BF4092EC7090}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +33,30 @@ Global
{46DCEF27-4157-4FB6-A283-B8484EC45665}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46DCEF27-4157-4FB6-A283-B8484EC45665}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46DCEF27-4157-4FB6-A283-B8484EC45665}.Release|Any CPU.Build.0 = Release|Any CPU
{2F492C07-8182-41E2-8C68-27D353C9AB74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2F492C07-8182-41E2-8C68-27D353C9AB74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F492C07-8182-41E2-8C68-27D353C9AB74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F492C07-8182-41E2-8C68-27D353C9AB74}.Release|Any CPU.Build.0 = Release|Any CPU
{DA43BF21-558B-438B-9DBF-1A88130C944A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA43BF21-558B-438B-9DBF-1A88130C944A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA43BF21-558B-438B-9DBF-1A88130C944A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA43BF21-558B-438B-9DBF-1A88130C944A}.Release|Any CPU.Build.0 = Release|Any CPU
{48D33345-B5A8-4F94-84D6-A61F8E16271D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48D33345-B5A8-4F94-84D6-A61F8E16271D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48D33345-B5A8-4F94-84D6-A61F8E16271D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48D33345-B5A8-4F94-84D6-A61F8E16271D}.Release|Any CPU.Build.0 = Release|Any CPU
{BCBAF45F-E078-4230-9BDC-4DE9F2FBEE9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCBAF45F-E078-4230-9BDC-4DE9F2FBEE9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCBAF45F-E078-4230-9BDC-4DE9F2FBEE9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCBAF45F-E078-4230-9BDC-4DE9F2FBEE9A}.Release|Any CPU.Build.0 = Release|Any CPU
{987279D2-202C-488A-BA47-37FCEEA51536}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{987279D2-202C-488A-BA47-37FCEEA51536}.Debug|Any CPU.Build.0 = Debug|Any CPU
{987279D2-202C-488A-BA47-37FCEEA51536}.Release|Any CPU.ActiveCfg = Release|Any CPU
{987279D2-202C-488A-BA47-37FCEEA51536}.Release|Any CPU.Build.0 = Release|Any CPU
{813E7228-2B59-41D2-9AFC-BF4092EC7090}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{813E7228-2B59-41D2-9AFC-BF4092EC7090}.Debug|Any CPU.Build.0 = Debug|Any CPU
{813E7228-2B59-41D2-9AFC-BF4092EC7090}.Release|Any CPU.ActiveCfg = Release|Any CPU
{813E7228-2B59-41D2-9AFC-BF4092EC7090}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Azure.Provisioning.CognitiveServices\src\Azure.Provisioning.CognitiveServices.csproj" />
<ProjectReference Include="..\..\Azure.Provisioning.EventGrid\src\Azure.Provisioning.EventGrid.csproj" />
<ProjectReference Include="..\..\Azure.Provisioning.KeyVault\src\Azure.Provisioning.KeyVault.csproj" />
<ProjectReference Include="..\..\Azure.Provisioning.ServiceBus\src\Azure.Provisioning.ServiceBus.csproj" />
<ProjectReference Include="..\..\Azure.Provisioning.Storage\src\Azure.Provisioning.Storage.csproj" />
<ProjectReference Include="..\..\Azure.Provisioning.KeyVault\src\Azure.Provisioning.KeyVault.csproj" />
<ProjectReference Include="..\..\Azure.Provisioning.CognitiveServices\src\Azure.Provisioning.CognitiveServices.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Azure.CloudMachine;

internal static class Azd
internal static class AzdHelpers
{
private const string MainBicepName = "main";
private const string ResourceGroupVersion = "2024-03-01";
Expand Down Expand Up @@ -115,18 +115,29 @@ internal static string ReadOrCreateCmid()
return cmid;
}
else
{ // add CM configuration to existing file
JsonNode? root = JsonValue.Parse(appsettings);
if (root is null)
throw new NotImplementedException();

if (root is not JsonObject obj)
throw new NotImplementedException();
{ // add CM configuration to existing file
json.Seek(0, SeekOrigin.Begin);
JsonNode? root = JsonNode.Parse(json);
json.Close();
if (root is null || root is not JsonObject obj) throw new InvalidOperationException("Existing appsettings.json is not a valid JSON object");

var cmProperties = new JsonObject();
cmid = GenerateCloudMachineId();
cmProperties.Add("ID", cmid);
obj.Add("CloudMachine", cmProperties);

using FileStream file = File.OpenWrite(appsettings);
JsonWriterOptions writerOptions = new()
{
Indented = true,
};
Utf8JsonWriter writer = new(file, writerOptions);
JsonSerializerOptions options = new()
{
WriteIndented = true,
};
root.WriteTo(writer, options);
writer.Flush();
}

return cmid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static bool Configure(string[] args, Action<CloudMachineInfrastructure>?
return false;
}

string cmid = Azd.ReadOrCreateCmid();
string cmid = AzdHelpers.ReadOrCreateCmid();

CloudMachineInfrastructure cmi = new(cmid);
if (configure != default)
Expand All @@ -286,7 +286,7 @@ public static bool Configure(string[] args, Action<CloudMachineInfrastructure>?
}

string infraDirectory = Path.Combine(".", "infra");
Azd.Init(infraDirectory, cmi);
AzdHelpers.Init(infraDirectory, cmi);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CloudMachineWorkspace(TokenCredential? credential = default, IConfigurati
string? cmid;
if (configuration == default)
{
cmid = Azd.ReadOrCreateCmid();
cmid = AzdHelpers.ReadOrCreateCmid();
}
else
{
Expand Down

0 comments on commit 9ad8747

Please sign in to comment.