Skip to content

Commit 039da8d

Browse files
authored
CLOUDP-303120: fix: Don't panic on deep copy of backup compliance. (#2162)
* fix() Don't panic on deep copy of backup compliance. The pointers can be nil in which case it will panic. Use the getter to ensure it's always initialized. * Add unit test * Tweak naming/description of test.
1 parent 8f5ef01 commit 039da8d

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

api/v1/atlasbackupcompliancepolicy_types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ func NewBCPFromAtlas(in *admin.DataProtectionSettings20231001) *AtlasBackupCompl
117117
PITEnabled: admin.GetOrDefault(in.PitEnabled, false),
118118
RestoreWindowDays: admin.GetOrDefault(in.RestoreWindowDays, 0),
119119
OnDemandPolicy: AtlasOnDemandPolicy{
120-
RetentionUnit: in.OnDemandPolicyItem.RetentionUnit,
121-
RetentionValue: in.OnDemandPolicyItem.RetentionValue,
120+
RetentionUnit: in.GetOnDemandPolicyItem().RetentionUnit,
121+
RetentionValue: in.GetOnDemandPolicyItem().RetentionValue,
122122
},
123123
}
124124

api/v1/atlasbackupcompliancepolicy_types_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,50 @@ func TestBackupCompliancePolicyFromAtlas(t *testing.T) {
129129
assert.True(t, reflect.DeepEqual(*out, want), cmp.Diff(*out, want))
130130
})
131131
}
132+
133+
func TestBackupCompliancePolicyNilOndemandPolicy(t *testing.T) {
134+
t.Run("Can convert when OndemandPolicyItem is nil", func(t *testing.T) {
135+
in := &admin.DataProtectionSettings20231001{
136+
AuthorizedEmail: "[email protected]",
137+
AuthorizedUserFirstName: "James",
138+
AuthorizedUserLastName: "Bond",
139+
CopyProtectionEnabled: pointer.MakePtr(true),
140+
EncryptionAtRestEnabled: pointer.MakePtr(false),
141+
PitEnabled: pointer.MakePtr(true),
142+
ProjectId: pointer.MakePtr("testProjectID"),
143+
RestoreWindowDays: pointer.MakePtr(24),
144+
ScheduledPolicyItems: &[]admin.BackupComplianceScheduledPolicyItem{
145+
{
146+
FrequencyType: "monthly",
147+
FrequencyInterval: 2,
148+
RetentionUnit: "months",
149+
RetentionValue: 4,
150+
},
151+
},
152+
OnDemandPolicyItem: nil,
153+
}
154+
155+
out := NewBCPFromAtlas(in)
156+
157+
want := AtlasBackupCompliancePolicySpec{
158+
AuthorizedEmail: "[email protected]",
159+
AuthorizedUserFirstName: "James",
160+
AuthorizedUserLastName: "Bond",
161+
CopyProtectionEnabled: true,
162+
EncryptionAtRestEnabled: false,
163+
PITEnabled: true,
164+
RestoreWindowDays: 24,
165+
ScheduledPolicyItems: []AtlasBackupPolicyItem{
166+
{
167+
FrequencyType: "monthly",
168+
FrequencyInterval: 2,
169+
RetentionUnit: "months",
170+
RetentionValue: 4,
171+
},
172+
},
173+
OnDemandPolicy: AtlasOnDemandPolicy{},
174+
}
175+
176+
assert.True(t, reflect.DeepEqual(*out, want), cmp.Diff(*out, want))
177+
})
178+
}

0 commit comments

Comments
 (0)