Skip to content

Commit 443736d

Browse files
committed
Fix backup compliance policy without onDemandPolicy
This addresses a bug where the `onDemandPolicy` can be missing in the CRD but is still uploaded to Atlas, creating an error.
1 parent a9ac619 commit 443736d

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

api/v1/atlasbackupcompliancepolicy_types.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,14 @@ func (b *AtlasBackupCompliancePolicy) ToAtlas(projectID string) *admin.DataProte
9999
RestoreWindowDays: pointer.MakePtr(b.Spec.RestoreWindowDays),
100100
}
101101

102-
result.OnDemandPolicyItem = &admin.BackupComplianceOnDemandPolicyItem{
103-
FrequencyInterval: 0,
104-
FrequencyType: "ondemand",
105-
RetentionValue: b.Spec.OnDemandPolicy.RetentionValue,
106-
RetentionUnit: strings.ToLower(b.Spec.OnDemandPolicy.RetentionUnit),
102+
var emptyPolicy AtlasOnDemandPolicy
103+
if b.Spec.OnDemandPolicy != emptyPolicy {
104+
result.OnDemandPolicyItem = &admin.BackupComplianceOnDemandPolicyItem{
105+
FrequencyInterval: 0,
106+
FrequencyType: "ondemand",
107+
RetentionValue: b.Spec.OnDemandPolicy.RetentionValue,
108+
RetentionUnit: strings.ToLower(b.Spec.OnDemandPolicy.RetentionUnit),
109+
}
107110
}
108111

109112
temp := make([]admin.BackupComplianceScheduledPolicyItem, len(b.Spec.ScheduledPolicyItems))

api/v1/atlasbackupcompliancepolicy_types_test.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ func TestBackupCompliancePolicyFromAtlas(t *testing.T) {
144144
})
145145
}
146146

147-
func TestBackupCompliancePolicyNilOndemandPolicy(t *testing.T) {
148-
t.Run("Can convert when OndemandPolicyItem is nil", func(t *testing.T) {
147+
func TestBackupCompliancePolicyFromAtlasNilOndemandPolicy(t *testing.T) {
148+
t.Run("Can convert from Atlas when OndemandPolicyItem is nil", func(t *testing.T) {
149149
in := &admin.DataProtectionSettings20231001{
150150
AuthorizedEmail: "[email protected]",
151151
AuthorizedUserFirstName: "James",
@@ -190,3 +190,57 @@ func TestBackupCompliancePolicyNilOndemandPolicy(t *testing.T) {
190190
assert.True(t, reflect.DeepEqual(*out, want), cmp.Diff(*out, want))
191191
})
192192
}
193+
194+
func TestBackupCompliancePolicyToAtlasNilOndemandPolicy(t *testing.T) {
195+
t.Run("Can convert to Atlas when OndemandPolicyItem is nil", func(t *testing.T) {
196+
in := AtlasBackupCompliancePolicy{
197+
ObjectMeta: metav1.ObjectMeta{
198+
Name: "my-bcp",
199+
Namespace: "test-ns",
200+
Labels: map[string]string{
201+
"test": "label",
202+
},
203+
},
204+
Spec: AtlasBackupCompliancePolicySpec{
205+
AuthorizedEmail: "[email protected]",
206+
AuthorizedUserFirstName: "James",
207+
AuthorizedUserLastName: "Bond",
208+
CopyProtectionEnabled: true,
209+
EncryptionAtRestEnabled: false,
210+
PITEnabled: true,
211+
RestoreWindowDays: 24,
212+
ScheduledPolicyItems: []AtlasBackupPolicyItem{
213+
{
214+
FrequencyType: "monthly",
215+
FrequencyInterval: 2,
216+
RetentionUnit: "months",
217+
RetentionValue: 4,
218+
},
219+
},
220+
},
221+
}
222+
out := in.ToAtlas("testProjectID")
223+
224+
want := admin.DataProtectionSettings20231001{
225+
AuthorizedEmail: "[email protected]",
226+
AuthorizedUserFirstName: "James",
227+
AuthorizedUserLastName: "Bond",
228+
CopyProtectionEnabled: pointer.MakePtr(true),
229+
EncryptionAtRestEnabled: pointer.MakePtr(false),
230+
PitEnabled: pointer.MakePtr(true),
231+
ProjectId: pointer.MakePtr("testProjectID"),
232+
RestoreWindowDays: pointer.MakePtr(24),
233+
ScheduledPolicyItems: &[]admin.BackupComplianceScheduledPolicyItem{
234+
{
235+
FrequencyType: "monthly",
236+
FrequencyInterval: 2,
237+
RetentionUnit: "months",
238+
RetentionValue: 4,
239+
},
240+
},
241+
OnDemandPolicyItem: nil,
242+
}
243+
244+
assert.True(t, reflect.DeepEqual(*out, want), cmp.Diff(*out, want))
245+
})
246+
}

0 commit comments

Comments
 (0)