factor common code to reduce redundancy as suggested by @dmick in https://github.com/ceph/ceph/commit/f49210f2362c991beec4a83d386cf5fe8fd38644#commitcomment-4146201