Skip to content

Commit 9b622ec

Browse files
authored
Merge pull request #2219 from crossplane-contrib/backport-2218-to-release-0.52
[Backport release-0.52] feat(transfer): add homedirectorymapping uptodate check
2 parents c05f50d + b522a9e commit 9b622ec

File tree

2 files changed

+174
-1
lines changed

2 files changed

+174
-1
lines changed

pkg/controller/transfer/user/setup.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (h *hooks) isUpToDate(_ context.Context, cr *svcapitypes.User, obj *svcsdk.
167167
var areTagsUpToDate bool
168168
areTagsUpToDate, h.cache.tagsToAdd, h.cache.tagsToDelete = utils.DiffTags(cr.Spec.ForProvider.Tags, obj.User.Tags)
169169

170-
return areKeysUpToDate && areTagsUpToDate, "", nil
170+
return areKeysUpToDate && areTagsUpToDate && isMappingsUpToDate(cr, obj), "", nil
171171
}
172172

173173
func isSSHPublicKeysUpToDate(cr *svcapitypes.User, obj *svcsdk.DescribeUserOutput) (isUpToDate bool, toAdd, toRemove []string) {
@@ -199,6 +199,34 @@ func isSSHPublicKeysUpToDate(cr *svcapitypes.User, obj *svcsdk.DescribeUserOutpu
199199
return len(toRemove) == 0 && len(toAdd) == 0, toAdd, toRemove
200200
}
201201

202+
func isMappingsUpToDate(cr *svcapitypes.User, obj *svcsdk.DescribeUserOutput) bool {
203+
specMap := make(map[string]string, len(cr.Spec.ForProvider.HomeDirectoryMappings))
204+
for _, k := range cr.Spec.ForProvider.HomeDirectoryMappings {
205+
specMap[*k.Entry] = *k.Target
206+
}
207+
208+
curMap := make(map[string]string, len(obj.User.HomeDirectoryMappings))
209+
210+
for _, mapping := range obj.User.HomeDirectoryMappings {
211+
body := ptr.Deref(mapping.Entry, "")
212+
curMap[body] = ptr.Deref(mapping.Target, "")
213+
214+
if _, exists := specMap[body]; !exists {
215+
return false
216+
}
217+
}
218+
219+
for key, value := range specMap {
220+
if val, exists := curMap[key]; !exists {
221+
return false
222+
} else if value != val {
223+
return false
224+
}
225+
}
226+
227+
return true
228+
}
229+
202230
func preCreate(_ context.Context, cr *svcapitypes.User, obj *svcsdk.CreateUserInput) error {
203231
obj.ServerId = cr.Spec.ForProvider.ServerID
204232
obj.Role = cr.Spec.ForProvider.Role

pkg/controller/transfer/user/setup_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,3 +1277,148 @@ func TestIsSSHPublicKeysUpToDate(t *testing.T) {
12771277
})
12781278
}
12791279
}
1280+
1281+
func TestIsMappingUpToDate(t *testing.T) {
1282+
type args struct {
1283+
cr *svcapitypes.User
1284+
obj *svcsdk.DescribeUserOutput
1285+
}
1286+
1287+
type want struct {
1288+
isUpToDate bool
1289+
}
1290+
1291+
cases := map[string]struct {
1292+
args
1293+
want
1294+
}{
1295+
"NeedsUpdate": {
1296+
args: args{
1297+
cr: user(
1298+
withExternalName("test"),
1299+
withSpec(svcapitypes.UserParameters{
1300+
Region: "us-east-1",
1301+
HomeDirectoryMappings: []*svcapitypes.HomeDirectoryMapEntry{
1302+
{
1303+
Entry: ptr.To("entry"),
1304+
Target: ptr.To("target"),
1305+
},
1306+
},
1307+
CustomUserParameters: svcapitypes.CustomUserParameters{
1308+
ServerID: ptr.To("server"),
1309+
Role: ptr.To("role"),
1310+
SSHPublicKeys: []svcapitypes.SSHPublicKeySpec{
1311+
{
1312+
Body: "some-public-key",
1313+
},
1314+
},
1315+
},
1316+
}),
1317+
withStatus(svcapitypes.UserObservation{
1318+
ARN: ptr.To("ARN"),
1319+
}),
1320+
),
1321+
obj: &svcsdk.DescribeUserOutput{
1322+
User: &svcsdk.DescribedUser{
1323+
HomeDirectoryMappings: []*svcsdk.HomeDirectoryMapEntry{},
1324+
},
1325+
},
1326+
},
1327+
want: want{
1328+
isUpToDate: false,
1329+
},
1330+
},
1331+
"NeedsUpdateNewTarget": {
1332+
args: args{
1333+
cr: user(
1334+
withExternalName("test"),
1335+
withSpec(svcapitypes.UserParameters{
1336+
Region: "us-east-1",
1337+
HomeDirectoryMappings: []*svcapitypes.HomeDirectoryMapEntry{
1338+
{
1339+
Entry: ptr.To("entry"),
1340+
Target: ptr.To("targetnew"),
1341+
},
1342+
},
1343+
CustomUserParameters: svcapitypes.CustomUserParameters{
1344+
ServerID: ptr.To("server"),
1345+
Role: ptr.To("role"),
1346+
SSHPublicKeys: []svcapitypes.SSHPublicKeySpec{
1347+
{
1348+
Body: "some-public-key",
1349+
},
1350+
},
1351+
},
1352+
}),
1353+
withStatus(svcapitypes.UserObservation{
1354+
ARN: ptr.To("ARN"),
1355+
}),
1356+
),
1357+
obj: &svcsdk.DescribeUserOutput{
1358+
User: &svcsdk.DescribedUser{
1359+
HomeDirectoryMappings: []*svcsdk.HomeDirectoryMapEntry{
1360+
{
1361+
Entry: ptr.To("entry"),
1362+
Target: ptr.To("target"),
1363+
},
1364+
},
1365+
},
1366+
},
1367+
},
1368+
want: want{
1369+
isUpToDate: false,
1370+
},
1371+
},
1372+
"IsUpToDate": {
1373+
args: args{
1374+
cr: user(
1375+
withExternalName("test"),
1376+
withSpec(svcapitypes.UserParameters{
1377+
Region: "us-east-1",
1378+
HomeDirectoryMappings: []*svcapitypes.HomeDirectoryMapEntry{
1379+
{
1380+
Entry: ptr.To("entry"),
1381+
Target: ptr.To("target"),
1382+
},
1383+
},
1384+
CustomUserParameters: svcapitypes.CustomUserParameters{
1385+
ServerID: ptr.To("server"),
1386+
Role: ptr.To("role"),
1387+
SSHPublicKeys: []svcapitypes.SSHPublicKeySpec{
1388+
{
1389+
Body: "some-public-key",
1390+
},
1391+
},
1392+
},
1393+
}),
1394+
withStatus(svcapitypes.UserObservation{
1395+
ARN: ptr.To("ARN"),
1396+
}),
1397+
),
1398+
obj: &svcsdk.DescribeUserOutput{
1399+
User: &svcsdk.DescribedUser{
1400+
HomeDirectoryMappings: []*svcsdk.HomeDirectoryMapEntry{
1401+
{
1402+
Entry: ptr.To("entry"),
1403+
Target: ptr.To("target"),
1404+
},
1405+
},
1406+
},
1407+
},
1408+
},
1409+
want: want{
1410+
isUpToDate: true,
1411+
},
1412+
},
1413+
}
1414+
1415+
for name, tc := range cases {
1416+
t.Run(name, func(t *testing.T) {
1417+
isUpToDate := isMappingsUpToDate(tc.args.cr, tc.args.obj)
1418+
1419+
if diff := cmp.Diff(tc.want.isUpToDate, isUpToDate, test.EquateErrors()); diff != "" {
1420+
t.Errorf("isUpToDate: -want, +got:\n%s", diff)
1421+
}
1422+
})
1423+
}
1424+
}

0 commit comments

Comments
 (0)