Skip to content

Commit 9237661

Browse files
committed
Default retention strategy for lifecycle
1 parent 9bebbde commit 9237661

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

source/Octopus.Server.Client/Model/LifecycleResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class LifecycleResource : Resource, INamedResource, IHaveSpaceResource, I
1212
public LifecycleResource()
1313
{
1414
Phases = new List<PhaseResource>();
15-
ReleaseRetentionPolicy = RetentionPeriod.KeepForever();
16-
TentacleRetentionPolicy = RetentionPeriod.KeepForever();
15+
ReleaseRetentionPolicy = RetentionPeriod.Default();
16+
TentacleRetentionPolicy = RetentionPeriod.Default();
1717
}
1818

1919
[Writeable]
Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
using System;
2+
using Newtonsoft.Json;
3+
using Octopus.TinyTypes;
24

35
namespace Octopus.Client.Model
46
{
57
public class RetentionPeriod : IEquatable<RetentionPeriod>
68
{
7-
readonly int quantityToKeep;
8-
readonly RetentionUnit unit;
9-
10-
public RetentionPeriod(int quantityToKeep, RetentionUnit unit)
9+
[JsonConstructor]
10+
public RetentionPeriod(RetentionPeriodStrategy strategy, int quantityToKeep, RetentionUnit unit)
1111
{
12-
this.quantityToKeep = quantityToKeep;
13-
this.unit = unit;
12+
Strategy = strategy ?? SelectStrategyBasedOnSettings(quantityToKeep, unit);
13+
QuantityToKeep = quantityToKeep;
14+
Unit = unit;
1415
}
1516

16-
public RetentionUnit Unit
17-
{
18-
get { return unit; }
19-
}
17+
public RetentionPeriod(int quantityToKeep, RetentionUnit unit) : this(null, quantityToKeep, unit)
18+
{}
2019

21-
public int QuantityToKeep
22-
{
23-
get { return quantityToKeep; }
24-
}
20+
public RetentionPeriodStrategy Strategy { get; protected set; }
2521

26-
public bool ShouldKeepForever
27-
{
28-
get { return QuantityToKeep == 0; }
29-
}
22+
public RetentionUnit Unit { get; protected set; }
23+
24+
public int QuantityToKeep { get; protected set; }
25+
26+
public bool ShouldKeepForever => QuantityToKeep == 0;
27+
28+
public static RetentionPeriod Default() => new(RetentionPeriodStrategy.Default, 0, RetentionUnit.Items);
3029

31-
public static RetentionPeriod KeepForever()
30+
RetentionPeriodStrategy SelectStrategyBasedOnSettings(int quantityToKeep, RetentionUnit unit)
3231
{
33-
return new RetentionPeriod(0, RetentionUnit.Items);
32+
return quantityToKeep == 0 && unit == RetentionUnit.Items
33+
? Strategy = RetentionPeriodStrategy.Default
34+
: Strategy = RetentionPeriodStrategy.Count;
3435
}
3536

3637
public bool Equals(RetentionPeriod other)
3738
{
3839
if (ReferenceEquals(null, other)) return false;
3940
if (ReferenceEquals(this, other)) return true;
40-
return quantityToKeep == other.quantityToKeep && unit.Equals(other.unit);
41+
return Strategy.Equals(other.Strategy) && QuantityToKeep == other.QuantityToKeep && Unit.Equals(other.Unit);
4142
}
4243

4344
public override bool Equals(object obj)
@@ -52,23 +53,24 @@ public override int GetHashCode()
5253
{
5354
unchecked
5455
{
55-
return (quantityToKeep*397) ^ unit.GetHashCode();
56+
var hashCode = Strategy.GetHashCode();
57+
hashCode = (hashCode * 397) ^ (int)Unit;
58+
hashCode = (hashCode * 397) ^ QuantityToKeep;
59+
return hashCode;
5660
}
5761
}
5862

59-
public static bool operator ==(RetentionPeriod left, RetentionPeriod right)
60-
{
61-
return Equals(left, right);
62-
}
63+
public static bool operator ==(RetentionPeriod left, RetentionPeriod right) => Equals(left, right);
6364

64-
public static bool operator !=(RetentionPeriod left, RetentionPeriod right)
65-
{
66-
return !Equals(left, right);
67-
}
65+
public static bool operator !=(RetentionPeriod left, RetentionPeriod right) => !Equals(left, right);
6866

69-
public override string ToString()
70-
{
71-
return ShouldKeepForever ? "Forever" : "Last " + quantityToKeep + " " + (unit == RetentionUnit.Days ? "day" + (quantityToKeep == 1 ? "" : "s") : "item" + (quantityToKeep == 1 ? "" : "s"));
72-
}
67+
public override string ToString() => ShouldKeepForever ? "Forever" : "Last " + QuantityToKeep + " " + (Unit == RetentionUnit.Days ? "day" + (QuantityToKeep == 1 ? "" : "s") : "item" + (QuantityToKeep == 1 ? "" : "s"));
68+
69+
}
70+
71+
public class RetentionPeriodStrategy(string value) : CaseInsensitiveStringTinyType(value)
72+
{
73+
public static readonly RetentionPeriodStrategy Default = new("Default");
74+
public static readonly RetentionPeriodStrategy Count = new("Count");
7375
}
7476
}

0 commit comments

Comments
 (0)