Skip to content

Commit

Permalink
add simple storage examples (#41956)
Browse files Browse the repository at this point in the history
* add simple storage examples

* update api
  • Loading branch information
m-nash authored Feb 14, 2024
1 parent 81f7d3c commit 38ceece
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,21 @@ public partial class SqlServer : Azure.Provisioning.Resource<Azure.ResourceManag
protected override Azure.Provisioning.Resource? FindParentInScope(Azure.Provisioning.IConstruct scope) { throw null; }
}
}
namespace Azure.Provisioning.Storage
{
public partial class BlobService : Azure.Provisioning.Resource<Azure.ResourceManager.Storage.BlobServiceData>
{
public BlobService(Azure.Provisioning.IConstruct scope, string name = "blob") : base (default(Azure.Provisioning.IConstruct), default(Azure.Provisioning.Resource), default(string), default(Azure.Core.ResourceType), default(string), default(Azure.ResourceManager.Storage.BlobServiceData)) { }
protected override Azure.Provisioning.Resource? FindParentInScope(Azure.Provisioning.IConstruct scope) { throw null; }
}
public partial class StorageAccount : Azure.Provisioning.Resource<Azure.ResourceManager.Storage.StorageAccountData>
{
public StorageAccount(Azure.Provisioning.IConstruct scope, Azure.ResourceManager.Storage.Models.StorageKind kind, Azure.ResourceManager.Storage.Models.StorageSkuName sku, Azure.Provisioning.ResourceManager.ResourceGroup? parent = null, string name = "sa") : base (default(Azure.Provisioning.IConstruct), default(Azure.Provisioning.Resource), default(string), default(Azure.Core.ResourceType), default(string), default(Azure.ResourceManager.Storage.StorageAccountData)) { }
protected override Azure.Provisioning.Resource? FindParentInScope(Azure.Provisioning.IConstruct scope) { throw null; }
}
public static partial class StorageExtensions
{
public static Azure.Provisioning.Storage.BlobService AddBlobService(this Azure.Provisioning.IConstruct scope, string name = "blob") { throw null; }
public static Azure.Provisioning.Storage.StorageAccount AddStorageAccount(this Azure.Provisioning.IConstruct scope, Azure.ResourceManager.Storage.Models.StorageKind kind, Azure.ResourceManager.Storage.Models.StorageSkuName sku, Azure.Provisioning.ResourceManager.ResourceGroup? parent = null, string name = "sa") { throw null; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,21 @@ public partial class SqlServer : Azure.Provisioning.Resource<Azure.ResourceManag
protected override Azure.Provisioning.Resource? FindParentInScope(Azure.Provisioning.IConstruct scope) { throw null; }
}
}
namespace Azure.Provisioning.Storage
{
public partial class BlobService : Azure.Provisioning.Resource<Azure.ResourceManager.Storage.BlobServiceData>
{
public BlobService(Azure.Provisioning.IConstruct scope, string name = "blob") : base (default(Azure.Provisioning.IConstruct), default(Azure.Provisioning.Resource), default(string), default(Azure.Core.ResourceType), default(string), default(Azure.ResourceManager.Storage.BlobServiceData)) { }
protected override Azure.Provisioning.Resource? FindParentInScope(Azure.Provisioning.IConstruct scope) { throw null; }
}
public partial class StorageAccount : Azure.Provisioning.Resource<Azure.ResourceManager.Storage.StorageAccountData>
{
public StorageAccount(Azure.Provisioning.IConstruct scope, Azure.ResourceManager.Storage.Models.StorageKind kind, Azure.ResourceManager.Storage.Models.StorageSkuName sku, Azure.Provisioning.ResourceManager.ResourceGroup? parent = null, string name = "sa") : base (default(Azure.Provisioning.IConstruct), default(Azure.Provisioning.Resource), default(string), default(Azure.Core.ResourceType), default(string), default(Azure.ResourceManager.Storage.StorageAccountData)) { }
protected override Azure.Provisioning.Resource? FindParentInScope(Azure.Provisioning.IConstruct scope) { throw null; }
}
public static partial class StorageExtensions
{
public static Azure.Provisioning.Storage.BlobService AddBlobService(this Azure.Provisioning.IConstruct scope, string name = "blob") { throw null; }
public static Azure.Provisioning.Storage.StorageAccount AddStorageAccount(this Azure.Provisioning.IConstruct scope, Azure.ResourceManager.Storage.Models.StorageKind kind, Azure.ResourceManager.Storage.Models.StorageSkuName sku, Azure.Provisioning.ResourceManager.ResourceGroup? parent = null, string name = "sa") { throw null; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Azure.ResourceManager.KeyVault" />
<PackageReference Include="Azure.ResourceManager.Sql" />
<PackageReference Include="Azure.ResourceManager.AppService" />
<PackageReference Include="Azure.ResourceManager.Storage" />
<PackageReference Include="System.ClientModel" />
</ItemGroup>

Expand Down
44 changes: 44 additions & 0 deletions sdk/provisioning/Azure.Provisioning/src/storage/BlobService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

namespace Azure.Provisioning.Storage
{
/// <summary>
/// Represents a blob service.
/// </summary>
public class BlobService : Resource<BlobServiceData>
{
private const string ResourceTypeName = "Microsoft.Storage/storageAccounts/blobServices";

private static string GetName(IConstruct scope, string? name)
{
return $"{name}-{scope.EnvironmentName}";
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobService"/>.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The name.</param>
public BlobService(IConstruct scope, string name = "blob")
: base(scope, null, GetName(scope, name), ResourceTypeName, "2022-09-01", ArmStorageModelFactory.BlobServiceData(
name: GetName(scope, name),
resourceType: ResourceTypeName))
{
}

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
var result = base.FindParentInScope(scope);
if (result is null)
{
result = scope.GetSingleResource<StorageAccount>() ?? new StorageAccount(scope, StorageKind.BlockBlobStorage, StorageSkuName.PremiumLrs);
}
return result;
}
}
}
54 changes: 54 additions & 0 deletions sdk/provisioning/Azure.Provisioning/src/storage/StorageAccount.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

namespace Azure.Provisioning.Storage
{
/// <summary>
/// Represents a storage account.
/// </summary>
public class StorageAccount : Resource<StorageAccountData>
{
private const string ResourceTypeName = "Microsoft.Storage/storageAccounts";

private static string GetName(IConstruct scope, string name)
{
var result = $"{name}-{Guid.NewGuid()}";
return result.Substring(0, Math.Min(result.Length, 24));
}

/// <summary>
/// Initializes a new instance of the <see cref="StorageAccount"/>.
/// </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>
public StorageAccount(IConstruct scope, StorageKind kind, StorageSkuName sku, ResourceGroup? parent = null, string name = "sa")
: base(scope, parent, GetName(scope, name), ResourceTypeName, "2022-09-01", ArmStorageModelFactory.StorageAccountData(
name: GetName(scope, name),
resourceType: ResourceTypeName,
location: Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
sku: new StorageSku(sku),
kind: StorageKind.StorageV2))
{
}

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
var result = base.FindParentInScope(scope);
if (result is null)
{
result = scope.GetOrAddResourceGroup();
}
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

namespace Azure.Provisioning.Storage
{
/// <summary>
/// Extension methods for <see cref="IConstruct"/>.
/// </summary>
public static class StorageExtensions
{
/// <summary>
/// Adds a <see cref="StorageAccount"/> to the construct.
/// </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>
/// <returns></returns>
public static StorageAccount AddStorageAccount(this IConstruct scope, StorageKind kind, StorageSkuName sku, ResourceGroup? parent = null, string name = "sa")
{
return new StorageAccount(scope, kind, sku, parent, name);
}

/// <summary>
/// Adds a <see cref="BlobService"/> to the construct.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public static BlobService AddBlobService(this IConstruct scope, string name = "blob")
{
return new BlobService(scope, name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
targetScope = subscription


resource resourceGroup_I6QNkoPsb 'Microsoft.Resources/resourceGroups@2023-07-01' = {
name: 'rg-TEST'
location: 'westus'
tags: {
azd-env-name: 'TEST'
}
}

resource storageAccount_k7HxHnTvM 'Microsoft.Storage/storageAccounts@2022-09-01' = {
scope: resourceGroup_I6QNkoPsb
name: 'photoAcct-bda380cf-8cdb-'
location: 'westus'
sku: {
name: 'Premium_LRS'
}
kind: 'StorageV2'
properties: {
}
}

resource blobService_BvgLmmmbK 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_k7HxHnTvM
name: 'photos-TEST'
properties: {
cors: {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
targetScope = subscription


resource resourceGroup_I6QNkoPsb 'Microsoft.Resources/resourceGroups@2023-07-01' = {
name: 'rg-TEST'
location: 'westus'
tags: {
azd-env-name: 'TEST'
}
}

resource storageAccount_9gvCV8M9t 'Microsoft.Storage/storageAccounts@2022-09-01' = {
scope: resourceGroup_I6QNkoPsb
name: 'photoAcct-464eb449-f1cd-'
location: 'westus'
sku: {
name: 'Premium_LRS'
}
kind: 'StorageV2'
properties: {
}
}

resource blobService_8xlkwSZNm 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_9gvCV8M9t
name: 'photos-TEST'
properties: {
cors: {
}
deleteRetentionPolicy: {
enabled: true
}
}
}
24 changes: 24 additions & 0 deletions sdk/provisioning/Azure.Provisioning/tests/ProvisioningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Azure.Provisioning.KeyVaults;
using Azure.Provisioning.Sql;
using Azure.Provisioning.Resources;
using Azure.Provisioning.Storage;
using Azure.ResourceManager.Storage.Models;

namespace Azure.Provisioning.Tests
{
Expand Down Expand Up @@ -93,6 +95,28 @@ public void WebSiteUsingL3()
infra.Build(GetOutputPath());
}

[Test]
public void StorageBlobDefaults()
{
var infra = new TestInfrastructure();
infra.AddStorageAccount(name: "photoAcct", sku: StorageSkuName.PremiumLrs, kind: StorageKind.BlockBlobStorage);
infra.AddBlobService(name: "photos");
infra.Build(GetOutputPath());
}

[Test]
public void StorageBlobDropDown()
{
var infra = new TestInfrastructure();
infra.AddStorageAccount(name: "photoAcct", sku: StorageSkuName.PremiumLrs, kind: StorageKind.BlockBlobStorage);
var blob = infra.AddBlobService(name: "photos");
blob.Properties.DeleteRetentionPolicy = new DeleteRetentionPolicy()
{
IsEnabled = true
};
infra.Build(GetOutputPath());
}

private static string GetGitRoot()
{
ProcessStartInfo startInfo = new ProcessStartInfo
Expand Down

0 comments on commit 38ceece

Please sign in to comment.