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

Add Cognitive Services support #42477

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<PackageReference Update="Azure.ResourceManager.AppConfiguration" Version="1.3.0-alpha.20240222.1" />
<PackageReference Update="Azure.ResourceManager.AppService" Version="1.1.0-alpha.20240222.2" />
<PackageReference Update="Azure.ResourceManager.Authorization" Version="1.2.0-alpha.20240227.2" />
<PackageReference Update="Azure.ResourceManager.CognitiveServices" Version="1.4.0-alpha.20240306.1" />
<PackageReference Update="Azure.ResourceManager.CosmosDB" Version="1.4.0-alpha.20240305.4" />
<PackageReference Update="Azure.ResourceManager.KeyVault" Version="1.3.0-alpha.20240222.2" />
<PackageReference Update="Azure.ResourceManager.PostgreSql" Version="1.2.0-alpha.20240304.1" />
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Azure.ResourceManager.Redis" />
<PackageReference Include="Azure.ResourceManager.PostgreSql" />
<PackageReference Include="Azure.ResourceManager.CosmosDB" />
<PackageReference Include="Azure.ResourceManager.CognitiveServices" />
<PackageReference Include="System.ClientModel" />
</ItemGroup>

Expand Down
17 changes: 16 additions & 1 deletion sdk/provisioning/Azure.Provisioning/src/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,22 @@ protected string GetGloballyUniqueName(string resourceName)
/// <returns>The parent <see cref="Resource"/>.</returns>
protected virtual Resource? FindParentInScope(IConstruct scope)
{
return scope is Resource resource ? resource : null;
if (this is Tenant)
{
return null;
}

if (this is Subscription)
{
return scope.Root;
}

if (Parent == null)
{
return this is ResourceGroup ? scope.GetOrCreateSubscription() : scope.GetOrAddResourceGroup();
}

return Parent;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class AppConfigurationExtensions
/// <returns></returns>
public static AppConfigurationStore AddAppConfigurationStore(this IConstruct construct, string name = "store")
{
return new AppConfigurationStore(construct, name);
return new AppConfigurationStore(construct, name: name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public class AppConfigurationStore : Resource<AppConfigurationStoreData>
/// Initializes a new instance of the <see cref="AppConfigurationStore"/> class.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="parent">The parent.</param>
/// <param name="name">The name.</param>
/// <param name="version">The version.</param>
/// <param name="location">The location.</param>
public AppConfigurationStore(IConstruct scope, string name = "store", string version = "2023-03-01", AzureLocation? location = default)
: this(scope, name, version, location, null, false, (name) => ArmAppConfigurationModelFactory.AppConfigurationStoreData(
public AppConfigurationStore(IConstruct scope, ResourceGroup? parent = null, string name = "store", string version = "2023-03-01", AzureLocation? location = default)
: this(scope, parent, name, version, false, (name) => ArmAppConfigurationModelFactory.AppConfigurationStoreData(
name: name,
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
Expand All @@ -34,7 +35,7 @@ public AppConfigurationStore(IConstruct scope, string name = "store", string ver
AddOutput(store => store.Endpoint, $"{Name}_endpoint");
}

private AppConfigurationStore(IConstruct scope, string name = "store", string version = "2023-03-01", AzureLocation? location = default, ResourceGroup? parent = null, bool isExisting = false, Func<string, AppConfigurationStoreData>? creator = null)
private AppConfigurationStore(IConstruct scope, ResourceGroup? parent = null, string name = "store", string version = "2023-03-01", bool isExisting = false, Func<string, AppConfigurationStoreData>? creator = null)
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting)
{
}
Expand All @@ -47,17 +48,6 @@ private AppConfigurationStore(IConstruct scope, string name = "store", string ve
/// <param name="parent">The resource group.</param>
/// <returns>The KeyVault instance.</returns>
public static AppConfigurationStore FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
=> new AppConfigurationStore(scope, name, parent: parent, isExisting: true);

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
var result = base.FindParentInScope(scope);
if (result is null)
{
result = scope.GetOrAddResourceGroup();
}
return result;
}
=> new AppConfigurationStore(scope, parent, name, isExisting: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public RoleDefinition(string value)
/// </summary>
public static RoleDefinition KeyVaultAdministrator { get; } = new RoleDefinition("00482a5a-887f-4fb3-b363-3b7fe8e74483");

/// <summary>
/// Cognitive Services Open AI contributor role.
/// </summary>
public static RoleDefinition CognitiveServicesOpenAIContributor { get; } = new RoleDefinition("a001fd3d-188f-4b5d-821b-7da978bf7442");

/// <summary> Converts a string to a <see cref="RoleDefinition"/>. </summary>
public static implicit operator RoleDefinition(string value) => new RoleDefinition(value);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Core;
using Azure.Provisioning.ResourceManager;
using Azure.ResourceManager.CognitiveServices;
using Azure.ResourceManager.CognitiveServices.Models;

namespace Azure.Provisioning.CognitiveServices
{
/// <summary>
/// Represents a Cognitive Services account.
/// </summary>
public class CognitiveServicesAccount : Resource<CognitiveServicesAccountData>
{
private const string ResourceTypeName = "Microsoft.CognitiveServices/accounts";
private static readonly Func<string, CognitiveServicesAccountData> Empty = (name) => ArmCognitiveServicesModelFactory.CognitiveServicesAccountData();

/// <summary>
/// Creates a new instance of the <see cref="CognitiveServicesAccount"/> class.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="kind">The kind.</param>
/// <param name="sku">The sku.</param>
/// <param name="parent">The parent.</param>
/// <param name="name">The name.</param>
/// <param name="version">The version.</param>
/// <param name="location">The location.</param>
public CognitiveServicesAccount(
IConstruct scope,
string? kind = default,
CognitiveServicesSku? sku = default,
ResourceGroup? parent = default,
string name = "cs",
string version = "2023-05-01",
AzureLocation? location = default)
: this(scope, parent, name, version, false, (name) =>
ArmCognitiveServicesModelFactory.CognitiveServicesAccountData(
name: name,
sku: sku ?? new CognitiveServicesSku("S0"),
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
kind: kind ?? "OpenAI",
properties: new CognitiveServicesAccountProperties()))
{
}

private CognitiveServicesAccount(
IConstruct scope,
ResourceGroup? parent = default,
string name = "cosmosDB",
string version = "2023-04-15",
bool isExisting = false,
Func<string, CognitiveServicesAccountData>? creator = null)
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting)
{
}

/// <summary>
/// Creates a new instance of the <see cref="CognitiveServicesAccount"/> class referencing an existing instance.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The resource name.</param>
/// <param name="parent">The resource group.</param>
/// <returns>The KeyVault instance.</returns>
public static CognitiveServicesAccount FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
=> new CognitiveServicesAccount(scope, parent: parent, name: name, isExisting: true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.ResourceManager.CognitiveServices;
using Azure.ResourceManager.CognitiveServices.Models;

namespace Azure.Provisioning.CognitiveServices
{
/// <summary>
/// Cognitive Services account deployment.
/// </summary>
public class CognitiveServicesAccountDeployment : Resource<CognitiveServicesAccountDeploymentData>
{
private const string ResourceTypeName = "Microsoft.CognitiveServices/accounts/deployments";
private static readonly Func<string, CognitiveServicesAccountDeploymentData> Empty = (name) => ArmCognitiveServicesModelFactory.CognitiveServicesAccountDeploymentData();

/// <summary>
/// Creates a new instance of the <see cref="CognitiveServicesAccount"/> class.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="model">The model.</param>
/// <param name="parent">The parent.</param>
/// <param name="sku">The sku.</param>
/// <param name="name">The name.</param>
/// <param name="version">The version.</param>
public CognitiveServicesAccountDeployment(
IConstruct scope,
CognitiveServicesAccountDeploymentModel model,
CognitiveServicesAccount? parent = default,
CognitiveServicesSku? sku = default,
string name = "cs",
string version = "2023-05-01")
: this(scope, parent, name, version, false, (name) =>
ArmCognitiveServicesModelFactory.CognitiveServicesAccountDeploymentData(
name: name,
// TODO why does the deployment also have a SKU
sku: sku ?? new CognitiveServicesSku("S0"),
properties: new CognitiveServicesAccountDeploymentProperties { Model = model }))
{
}

private CognitiveServicesAccountDeployment(
IConstruct scope,
CognitiveServicesAccount? parent = default,
string name = "cosmosDB",
string version = "2023-04-15",
bool isExisting = false,
Func<string, CognitiveServicesAccountDeploymentData>? creator = null)
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting)
{
}

/// <summary>
/// Creates a new instance of the <see cref="CognitiveServicesAccount"/> class referencing an existing instance.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The resource name.</param>
/// <param name="parent">The parent.</param>
/// <returns>The KeyVault instance.</returns>
public static CognitiveServicesAccountDeployment FromExisting(IConstruct scope, string name, CognitiveServicesAccount parent)
=> new CognitiveServicesAccountDeployment(scope, parent: parent, name: name, isExisting: true);

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
return scope.GetSingleResource<CognitiveServicesAccount>() ?? new CognitiveServicesAccount(scope);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ public static CosmosDBAccount FromExisting(IConstruct scope, string name, Resour
public CosmosDBAccountConnectionString GetConnectionString(CosmosDBKey? key = default)
=> new CosmosDBAccountConnectionString(this, key ?? CosmosDBKey.PrimaryKey);

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
var result = base.FindParentInScope(scope);
if (result is null)
{
result = scope.GetOrAddResourceGroup();
}
return result;
}

/// <inheritdoc/>
protected override string GetAzureName(IConstruct scope, string resourceName) => GetGloballyUniqueName(resourceName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@ private CosmosDBSqlDatabase(
/// <returns>The KeyVault instance.</returns>
public static CosmosDBSqlDatabase FromExisting(IConstruct scope, string name, CosmosDBAccount parent)
=> new CosmosDBSqlDatabase(scope, parent: parent, name: name, isExisting: true);

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
return scope.GetSingleResource<CosmosDBAccount>() ?? new CosmosDBAccount(scope);
}
}
}
15 changes: 2 additions & 13 deletions sdk/provisioning/Azure.Provisioning/src/keyvault/KeyVault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static KeyVault FromExisting(IConstruct scope, string name, ResourceGroup
/// <param name="location">The location.</param>
/// <param name="parent"></param>
public KeyVault(IConstruct scope, ResourceGroup? parent = default, string name = "kv", string version = "2023-02-01", AzureLocation? location = default)
: this(scope, parent, name, version, location, false, (name) => ArmKeyVaultModelFactory.KeyVaultData(
: this(scope, parent, name, version, false, (name) => ArmKeyVaultModelFactory.KeyVaultData(
name: name,
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
Expand All @@ -64,7 +64,7 @@ public KeyVault(IConstruct scope, ResourceGroup? parent = default, string name =
}
}

private KeyVault(IConstruct scope, ResourceGroup? parent = default, string name = "kv", string version = "2023-02-01", AzureLocation? location = default, bool isExisting = false, Func<string, KeyVaultData>? creator = null)
private KeyVault(IConstruct scope, ResourceGroup? parent = default, string name = "kv", string version = "2023-02-01", bool isExisting = false, Func<string, KeyVaultData>? creator = null)
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting: isExisting)
{
}
Expand All @@ -78,17 +78,6 @@ public void AddAccessPolicy(Output output)
_ = new KeyVaultAddAccessPolicy(Scope, new Parameter(output), this);
}

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
var result = base.FindParentInScope(scope);
if (result is null)
{
result = scope.GetOrAddResourceGroup();
}
return result;
}

/// <inheritdoc/>
protected override string GetAzureName(IConstruct scope, string resourceName) => GetGloballyUniqueName(resourceName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ public static KeyVaultSecret FromExisting(IConstruct scope, string name, KeyVaul
/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
var result = base.FindParentInScope(scope);
if (result is null)
{
result = scope.GetSingleResource<KeyVault>() ?? new KeyVault(scope);
}
return result;
return scope.GetSingleResource<KeyVault>() ?? new KeyVault(scope);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public PostgreSqlFlexibleServer(
string name = "postgres",
string version = "2020-06-01",
AzureLocation? location = default)
: this(scope, administratorLogin, administratorPassword, sku, highAvailability, storage, backup, network, availabilityZone, parent, name, version, location, false, (name) => ArmPostgreSqlFlexibleServersModelFactory.PostgreSqlFlexibleServerData(
: this(scope, parent, name, version, false, (name) => ArmPostgreSqlFlexibleServersModelFactory.PostgreSqlFlexibleServerData(
name: name,
// create new instances so the properties can be overriden by user if needed
version: serverVersion ?? new PostgreSqlFlexibleServerVersion(),
Expand All @@ -72,35 +72,15 @@ public PostgreSqlFlexibleServer(

private PostgreSqlFlexibleServer(
IConstruct scope,
Parameter administratorLogin = default,
Parameter administratorPassword = default,
PostgreSqlFlexibleServerSku? sku = default,
PostgreSqlFlexibleServerHighAvailability? highAvailability = default,
PostgreSqlFlexibleServerStorage? storage = default,
PostgreSqlFlexibleServerBackupProperties? backup = default,
PostgreSqlFlexibleServerNetwork? network = default,
string? availabilityZone = default,
ResourceGroup? parent = default,
string name = "postgres",
string version = "2020-06-01",
AzureLocation? location = default,
bool isExisting = false,
Func<string, PostgreSqlFlexibleServerData>? creator = null)
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting)
{
}

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
var result = base.FindParentInScope(scope);
if (result is null)
{
result = scope.GetOrAddResourceGroup();
}
return result;
}

/// <summary>
/// Creates a new instance of the <see cref="PostgreSqlFlexibleServer"/> class referencing an existing instance.
/// </summary>
Expand Down
Loading