Skip to content

Commit c8a1a05

Browse files
authored
Default retention strategy for lifecycle (#945)
* Default retention strategy for lifecycle * Do not regress surface area * Update the net framework assent * ok
1 parent c1ed9fe commit c8a1a05

File tree

4 files changed

+64
-36
lines changed

4 files changed

+64
-36
lines changed

source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETCore.approved.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4988,16 +4988,28 @@ Octopus.Client.Model
49884988
class RetentionPeriod
49894989
IEquatable<RetentionPeriod>
49904990
{
4991+
.ctor(Octopus.Client.Model.RetentionPeriodStrategy, Int32, Octopus.Client.Model.RetentionUnit)
49914992
.ctor(Int32, Octopus.Client.Model.RetentionUnit)
49924993
Int32 QuantityToKeep { get; }
49934994
Boolean ShouldKeepForever { get; }
4995+
Octopus.Client.Model.RetentionPeriodStrategy Strategy { get; }
49944996
Octopus.Client.Model.RetentionUnit Unit { get; }
4997+
static Octopus.Client.Model.RetentionPeriod Default()
49954998
Boolean Equals(Octopus.Client.Model.RetentionPeriod)
49964999
Boolean Equals(Object)
49975000
Int32 GetHashCode()
49985001
static Octopus.Client.Model.RetentionPeriod KeepForever()
49995002
String ToString()
50005003
}
5004+
class RetentionPeriodStrategy
5005+
IComparable
5006+
Octopus.TinyTypes.CaseInsensitiveStringTinyType
5007+
{
5008+
static Octopus.Client.Model.RetentionPeriodStrategy Count
5009+
static Octopus.Client.Model.RetentionPeriodStrategy Default
5010+
static Octopus.Client.Model.RetentionPeriodStrategy Forever
5011+
.ctor(String)
5012+
}
50015013
class RetentionPoliciesConfigurationResource
50025014
Octopus.Client.Extensibility.IResource
50035015
Octopus.Client.Model.IAuditedResource

source/Octopus.Client.Tests/PublicSurfaceAreaFixture.ThePublicSurfaceAreaShouldNotRegress..NETFramework.approved.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5006,16 +5006,28 @@ Octopus.Client.Model
50065006
class RetentionPeriod
50075007
IEquatable<RetentionPeriod>
50085008
{
5009+
.ctor(Octopus.Client.Model.RetentionPeriodStrategy, Int32, Octopus.Client.Model.RetentionUnit)
50095010
.ctor(Int32, Octopus.Client.Model.RetentionUnit)
50105011
Int32 QuantityToKeep { get; }
50115012
Boolean ShouldKeepForever { get; }
5013+
Octopus.Client.Model.RetentionPeriodStrategy Strategy { get; }
50125014
Octopus.Client.Model.RetentionUnit Unit { get; }
5015+
static Octopus.Client.Model.RetentionPeriod Default()
50135016
Boolean Equals(Octopus.Client.Model.RetentionPeriod)
50145017
Boolean Equals(Object)
50155018
Int32 GetHashCode()
50165019
static Octopus.Client.Model.RetentionPeriod KeepForever()
50175020
String ToString()
50185021
}
5022+
class RetentionPeriodStrategy
5023+
IComparable
5024+
Octopus.TinyTypes.CaseInsensitiveStringTinyType
5025+
{
5026+
static Octopus.Client.Model.RetentionPeriodStrategy Count
5027+
static Octopus.Client.Model.RetentionPeriodStrategy Default
5028+
static Octopus.Client.Model.RetentionPeriodStrategy Forever
5029+
.ctor(String)
5030+
}
50195031
class RetentionPoliciesConfigurationResource
50205032
Octopus.Client.Extensibility.IResource
50215033
Octopus.Client.Model.IAuditedResource

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: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
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);
29+
public static RetentionPeriod KeepForever() => new(RetentionPeriodStrategy.Forever, 0, RetentionUnit.Items);
3030

31-
public static RetentionPeriod KeepForever()
31+
RetentionPeriodStrategy SelectStrategyBasedOnSettings(int quantityToKeep, RetentionUnit unit)
3232
{
33-
return new RetentionPeriod(0, RetentionUnit.Items);
33+
return quantityToKeep == 0 && unit == RetentionUnit.Items
34+
? Strategy = RetentionPeriodStrategy.Default
35+
: Strategy = RetentionPeriodStrategy.Count;
3436
}
3537

3638
public bool Equals(RetentionPeriod other)
3739
{
3840
if (ReferenceEquals(null, other)) return false;
3941
if (ReferenceEquals(this, other)) return true;
40-
return quantityToKeep == other.quantityToKeep && unit.Equals(other.unit);
42+
return Strategy.Equals(other.Strategy) && QuantityToKeep == other.QuantityToKeep && Unit.Equals(other.Unit);
4143
}
4244

4345
public override bool Equals(object obj)
@@ -52,23 +54,25 @@ public override int GetHashCode()
5254
{
5355
unchecked
5456
{
55-
return (quantityToKeep*397) ^ unit.GetHashCode();
57+
var hashCode = Strategy.GetHashCode();
58+
hashCode = (hashCode * 397) ^ (int)Unit;
59+
hashCode = (hashCode * 397) ^ QuantityToKeep;
60+
return hashCode;
5661
}
5762
}
5863

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

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

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

0 commit comments

Comments
 (0)