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 simple storage examples #41956

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading