Skip to content

Commit

Permalink
CA-399629: make daily-license-check aware of never (#6106)
Browse files Browse the repository at this point in the history
  • Loading branch information
psafont authored Nov 7, 2024
2 parents e23d3b6 + 8451cb9 commit 2364109
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
37 changes: 22 additions & 15 deletions ocaml/license/daily_license_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,35 @@ let seconds_per_30_days = 30. *. seconds_per_day
let days_to_expiry now expiry =
(expiry /. seconds_per_day) -. (now /. seconds_per_day)

let get_expiry_date license =
List.assoc_opt "expiry" license
|> Fun.flip Option.bind (fun e -> if e = "never" then None else Some e)
|> Option.map Xapi_stdext_date.Date.of_string
|> Option.map Xapi_stdext_date.Date.to_float

let get_hosts all_license_params threshold =
List.fold_left
(fun acc (name_label, license_params) ->
let expiry = List.assoc "expiry" license_params in
let expiry = Xapi_stdext_date.Date.(to_float (of_string expiry)) in
if expiry < threshold then
name_label :: acc
else
acc
match get_expiry_date license_params with
| Some expiry when expiry < threshold ->
name_label :: acc
| _ ->
acc
)
[] all_license_params

let check_license now pool_license_state all_license_params =
let expiry = List.assoc "expiry" pool_license_state in
let expiry = Xapi_stdext_date.Date.(to_float (of_string expiry)) in
let days = days_to_expiry now expiry in
if days <= 0. then
Expired (get_hosts all_license_params now)
else if days <= 30. then
Expiring (get_hosts all_license_params (now +. seconds_per_30_days))
else
Good
match get_expiry_date pool_license_state with
| Some expiry ->
let days = days_to_expiry now expiry in
if days <= 0. then
Expired (get_hosts all_license_params now)
else if days <= 30. then
Expiring (get_hosts all_license_params (now +. seconds_per_30_days))
else
Good
| None ->
Good

let get_info_from_db rpc session =
let pool = List.hd (XenAPI.Pool.get_all rpc session) in
Expand Down
1 change: 1 addition & 0 deletions ocaml/tests/alerts/test_daily_license_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ let expiry_samples =
[
(([("expiry", "20170101T00:00:00Z")], []), Good)
; (([("expiry", "20160701T04:01:00Z")], []), Good)
; (([("expiry", "never")], []), Good)
; (([("expiry", "20160701T04:00:00Z")], []), Expiring [])
; (([("expiry", "20160616T00:00:00Z")], []), Expiring [])
; (([("expiry", "20160601T04:00:01Z")], []), Expiring [])
Expand Down

0 comments on commit 2364109

Please sign in to comment.