Skip to content

Commit

Permalink
Initial version of cloning of datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbouetsmith committed Jun 8, 2024
1 parent e60a38d commit 1b50fd7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Source/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<ItemGroup>
<PackageReference Include="Npam" Version="1.0.2" />
<PackageReference Include="ROOT.Zfs.Core" Version="1.1.9" />
<PackageReference Include="ROOT.Zfs.Core" Version="1.1.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>
74 changes: 74 additions & 0 deletions Source/Api/Controllers/ClonesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Api.Core;
using Api.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ROOT.Zfs.Public;
using ROOT.Zfs.Public.Arguments.Dataset;
using ROOT.Zfs.Public.Data;
using System.Collections.Generic;
using ROOT.Zfs.Public.Data.Datasets;
using System.Linq;
using ROOT.Zfs.Public.Arguments.Snapshots;

namespace Api.Controllers
{
/// <summary>
/// Contains methods for manipulating clones of snapshots, i.e.
/// datasets that is clones of a snapshot
/// </summary>
[Authorize]
[ApiController]
public class ClonesController : ApiControllerBase
{
private readonly IZfs _zfs;

/// <inheritdoc />
public ClonesController(IZfsAccessor zfsAccessor)
{
_zfs = zfsAccessor.Zfs;
}

/// <summary>
/// Lists datasets that are clones of snapshots
/// </summary>
/// <returns>A list of datasets that are clones</returns>
[HttpGet("/api/zfs/clones")]
public Response<IEnumerable<Dataset>> ListClones()
{
DatasetListArgs args = new DatasetListArgs
{
DatasetTypes = DatasetTypes.Filesystem | DatasetTypes.Volume
};
var datasets = _zfs.Datasets.List(args);
var clones = datasets.Where(d => d.IsClone);
return new Response<IEnumerable<Dataset>> { Data = clones };
}

[HttpPost("/api/zfs/clones/{dataset}/{snapshot}")]
public Response CreateClone(string dataset, string snapshot, [FromBody] CloneRequest request)

Check warning on line 48 in Source/Api/Controllers/ClonesController.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClonesController.CreateClone(string, string, CloneRequest)'
{
var args = new SnapshotCloneArgs { Dataset = dataset, Snapshot = snapshot, TargetDataset = request.Dataset };
if (request.Properties != null && request.Properties.Length > 0)
{
var properties = request.Properties.Select(p => new PropertyValue { Property = p.Name, Value = p.Value }).ToList();
args.Properties = properties;
}
_zfs.Snapshots.Clone(args);
return new Response();
}

/// <summary>
/// Promotes a clone into an independent dataset.
/// When you promote a clone, it reverses the relationship between the original dataset and the clone.
/// So the clone becomes the "master" and the original becomes a dependent dataset
/// </summary>
/// <param name="name">The name of the dataset that should be promoted from a clone to its own dataset</param>
[HttpPut("/api/zfs/clones/{name}")]
public Response PromoteClone(string name)
{
var args = new PromoteArgs { Name = name };
_zfs.Datasets.Promote(args);
return new Response();
}
}
}
19 changes: 19 additions & 0 deletions Source/Api/Models/CloneRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Api.Models
{
/// <summary>
/// Encapsulates the data required to clone a dataset to be independent
/// </summary>
public class CloneRequest
{
/// <summary>
/// Name of the dataset that should be promoted
/// </summary>
public string Dataset { get; set; }

/// <summary>
/// Optional properties to set on the promoted dataset, if they should be different from original
/// </summary>
public PropertyData[] Properties { get; set; }

}
}

0 comments on commit 1b50fd7

Please sign in to comment.