From df73626725eaff2c5f47999dac4663b2da13ea36 Mon Sep 17 00:00:00 2001 From: talktovikas Date: Mon, 4 Nov 2024 16:17:57 +0530 Subject: [PATCH] adding company name and license-id from automate license. Signed-off-by: talktovikas removing trailing space. Signed-off-by: talktovikas fixing tests. Signed-off-by: talktovikas fixing license test case. Signed-off-by: talktovikas Fixing bug in condition statement Signed-off-by: talktovikas debug statements Signed-off-by: talktovikas adding the server url. Signed-off-by: talktovikas assignment in case of standalone chef-server. Signed-off-by: talktovikas adding licenseId in case of Automate-T data. Signed-off-by: talktovikas fixing T-test cases. Signed-off-by: talktovikas fixing tests. Signed-off-by: talktovikas fixing tests. Signed-off-by: talktovikas --- .expeditor/chef_server.sh | 2 + .../chef_license/src/chef_license_worker.erl | 36 ++++++----- .../test/chef_license_worker_test.erl | 10 +-- .../src/chef_telemetry_worker.erl | 64 +++++++++++++------ .../test/chef_telemetry_worker_test.erl | 25 +++++++- .../apps/oc_chef_wm/src/oc_chef_wm_base.erl | 6 +- 6 files changed, 94 insertions(+), 49 deletions(-) diff --git a/.expeditor/chef_server.sh b/.expeditor/chef_server.sh index 622f8df755..81176c2172 100755 --- a/.expeditor/chef_server.sh +++ b/.expeditor/chef_server.sh @@ -7,6 +7,8 @@ git clone https://github.com/chef/automate.git cd automate git checkout kalroy/cs_plan_changes +echo $(git branch) +echo $(git show --summary) #=============================================================================== chmod +x ./integration/tests/chef_server.sh diff --git a/src/oc_erchef/apps/chef_license/src/chef_license_worker.erl b/src/oc_erchef/apps/chef_license/src/chef_license_worker.erl index 4cf7b5b68c..361109842d 100644 --- a/src/oc_erchef/apps/chef_license/src/chef_license_worker.erl +++ b/src/oc_erchef/apps/chef_license/src/chef_license_worker.erl @@ -26,7 +26,8 @@ grace_period, expiration_date, message, - customer_name + customer_name, + license_id }). -define(DEFAULT_LICENSE_SCAN_INTERVAL, 30000). %milli seconds @@ -54,10 +55,10 @@ init(_Config) -> erlang:send_after(?DEFAULT_LICENSE_SCAN_INTERVAL, self(), check_license), {ok, State}. -handle_call(get_license, _From, #state{license_cache = undefined, license_type=Type, expiration_date=ExpDate, grace_period = GracePeriod, message = Msg, customer_name=CN}=State) -> - {reply, {valid_license, Type, GracePeriod, ExpDate, Msg, CN}, State}; -handle_call(get_license, _From, #state{license_cache = Lic, license_type=Type, expiration_date=ExpDate, grace_period = GracePeriod, message = Msg, customer_name=CN} = State) -> - {reply,{Lic, Type, GracePeriod, ExpDate, Msg, CN}, State}; +handle_call(get_license, _From, #state{license_cache = undefined, license_type=Type, expiration_date=ExpDate, grace_period = GracePeriod, message = Msg, customer_name=CN,license_id = LicenseId}=State) -> + {reply, {valid_license, Type, GracePeriod, ExpDate, Msg, CN,LicenseId}, State}; +handle_call(get_license, _From, #state{license_cache = Lic, license_type=Type, expiration_date=ExpDate, grace_period = GracePeriod, message = Msg, customer_name=CN,license_id = LicenseId} = State) -> + {reply,{Lic, Type, GracePeriod, ExpDate, Msg, CN,LicenseId}, State}; handle_call(_Message, _From, State) -> {noreply, State}. @@ -88,14 +89,14 @@ check_license(State) -> {'EXIT', _} -> <<"">> end, case process_license(JsonStr) of - {ok, valid_license, ExpDate, CustomerName} -> - State#state{license_cache=valid_license, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, customer_name=CustomerName}; - {ok, commercial_expired, ExpDate, Msg, CustomerName} -> - State#state{license_cache=commercial_expired, license_type = <<"commercial">>, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg, customer_name=CustomerName}; - {ok, commercial_grace_period, ExpDate, Msg, CustomerName} -> - State#state{license_cache=commercial_grace_period, grace_period=true, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg, customer_name=CustomerName}; - {ok, trial_expired, ExpDate, Msg, CustomerName} -> - State#state{license_cache=trial_expired_expired, license_type = <<"trial">>, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg, customer_name=CustomerName}; + {ok, valid_license, ExpDate, CustomerName,LicenseId} -> + State#state{license_cache=valid_license, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, customer_name=CustomerName,license_id = LicenseId}; + {ok, commercial_expired, ExpDate, Msg, CustomerName,LicenseId} -> + State#state{license_cache=commercial_expired, license_type = <<"commercial">>, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg, customer_name=CustomerName,license_id = LicenseId}; + {ok, commercial_grace_period, ExpDate, Msg, CustomerName,LicenseId} -> + State#state{license_cache=commercial_grace_period, grace_period=true, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg, customer_name=CustomerName,license_id = LicenseId}; + {ok, trial_expired, ExpDate, Msg, CustomerName,LicenseId} -> + State#state{license_cache=trial_expired_expired, license_type = <<"trial">>, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg, customer_name=CustomerName,license_id = LicenseId}; {error, no_license} -> State#state{license_cache=trial_expired_expired, license_type = <<"trial">>, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date="", message=get_alert_message(trial_expired, "")}; {error, _} -> State @@ -113,23 +114,24 @@ process_license(LicJson) -> case ej:get({<<"result">>}, LicJson) of {LicDetails} -> CustomerName = ej:get({<<"customer_name">>}, LicDetails), + LicenseId = ej:get({<<"license_id">>}, LicDetails), case ej:get({<<"expiration_date">>}, LicDetails) of {[{<<"seconds">>,ExpireInSeconds}]} -> ExpDate = sec_to_date(ExpireInSeconds), case os:system_time(second) < ExpireInSeconds of - true -> {ok, valid_license, ExpDate, CustomerName}; + true -> {ok, valid_license, ExpDate, CustomerName,LicenseId}; _ -> case ej:get({<<"license_type">>}, LicDetails) of <<"commercial">> -> case ej:get({<<"grace_period">>}, LicDetails) of true -> {ok, commercial_grace_period, ExpDate, - get_alert_message(commercial_grace_period, ExpDate), CustomerName}; + get_alert_message(commercial_grace_period, ExpDate), CustomerName,LicenseId}; _ -> - { ok, commercial_expired, ExpDate, get_alert_message(commercial_expired, ExpDate), CustomerName} + { ok, commercial_expired, ExpDate, get_alert_message(commercial_expired, ExpDate), CustomerName,LicenseId} end; _ -> - {ok, trial_expired, ExpDate, get_alert_message(trial_expired, ExpDate), CustomerName} + {ok, trial_expired, ExpDate, get_alert_message(trial_expired, ExpDate), CustomerName,LicenseId} end end; _ -> diff --git a/src/oc_erchef/apps/chef_license/test/chef_license_worker_test.erl b/src/oc_erchef/apps/chef_license/test/chef_license_worker_test.erl index 2a82b212ab..155b982b8a 100644 --- a/src/oc_erchef/apps/chef_license/test/chef_license_worker_test.erl +++ b/src/oc_erchef/apps/chef_license/test/chef_license_worker_test.erl @@ -28,35 +28,35 @@ license_test()-> file:write_file(?DEFAULT_FILE_PATH,get_commercial_license()), refresh_license(), timer:sleep(100), - {Result, _, _, _, _} = chef_license_worker:get_license(), + {Result, _, _, _, _,_,_} = chef_license_worker:get_license(), ?assertEqual(valid_license, Result), os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH), file:write_file(?DEFAULT_FILE_PATH,get_commercial_license_expired()), refresh_license(), timer:sleep(100), - {Result1, _, _, _, _} = chef_license_worker:get_license(), + {Result1, _, _, _, _,_,_} = chef_license_worker:get_license(), ?assertEqual(commercial_expired, Result1), os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH), file:write_file(?DEFAULT_FILE_PATH,get_commercial_grace_license()), refresh_license(), timer:sleep(100), - {Result2, _, _, _, _} = chef_license_worker:get_license(), + {Result2, _, _, _, _,_,_} = chef_license_worker:get_license(), ?assertEqual(commercial_grace_period, Result2), os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH), file:write_file(?DEFAULT_FILE_PATH,get_trail_license()), refresh_license(), timer:sleep(100), - {Result3, _, _, _, _} = chef_license_worker:get_license(), + {Result3, _, _, _, _,_,_} = chef_license_worker:get_license(), ?assertEqual(valid_license, Result3), os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH), file:write_file(?DEFAULT_FILE_PATH,get_trail_license_expired()), refresh_license(), timer:sleep(100), - {Result4,_, _, _, _ } = chef_license_worker:get_license(), + {Result4,_, _, _, _ ,_,_} = chef_license_worker:get_license(), ?assertEqual(trial_expired_expired, Result4), os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH). diff --git a/src/oc_erchef/apps/chef_telemetry/src/chef_telemetry_worker.erl b/src/oc_erchef/apps/chef_telemetry/src/chef_telemetry_worker.erl index 50b84d97c7..75175d9dec 100644 --- a/src/oc_erchef/apps/chef_telemetry/src/chef_telemetry_worker.erl +++ b/src/oc_erchef/apps/chef_telemetry/src/chef_telemetry_worker.erl @@ -199,29 +199,51 @@ solr_search(Query) -> {Error, Reason} end. +get_license_company_name()-> + {_Lic, _Type, _GracePeriod, _ExpDate, _Msg, CN,_LID} = gen_server:call(chef_license_worker, get_license), + CN. + +determine_license_id()-> + {_Lic, _Type, _GracePeriod, _ExpDate, _Msg, _CN, LicenseID} = gen_server:call(chef_license_worker, get_license), + case LicenseID of + undefined -> + <<"Infra-Server-license-Id">>; + <<"undefined">> -> + <<"Infra-Server-license-Id">>; + <<>> -> + <<"Infra-Server-license-Id">>; + _ -> + LicenseID + end. + + get_company_name(State) -> CompanyName = - case sqerl:adhoc_select([<<"email">>], <<"users">>, all) of - {ok, Ids1} -> - Ids = [Id || [{_, Id}] <- Ids1], - Fun = - fun(Email) -> - case re:run(Email, "^[^@]*@\([^.]*\)\..*$") of - {match, [_, {Pos, Len} | _]} -> - {true, binary:part(Email, Pos, Len)}; - _ -> - false - end - end, - CompanyNames = lists:filtermap(Fun, Ids), - case length(CompanyNames) == 0 of - true -> - throw("no valid Email Ids."); - _ -> - get_most_occuring(CompanyNames) + case get_license_company_name() of + CN when CN =:= undefined; CN=:= <<"">>; CN =:= "" -> + case sqerl:adhoc_select([<<"email">>], <<"users">>, all) of + {ok, Ids1} -> + Ids = [Id || [{_, Id}] <- Ids1], + Fun = + fun(Email) -> + case re:run(Email, "^[^@]*@\([^.]*\)\..*$") of + {match, [_, {Pos, Len} | _]} -> + {true, binary:part(Email, Pos, Len)}; + _ -> + false + end + end, + CompanyNames = lists:filtermap(Fun, Ids), + case length(CompanyNames) == 0 of + true -> + throw("no valid Email Ids."); + _ -> + get_most_occuring(CompanyNames) + end; + Error -> + throw(Error) end; - Error -> - throw(Error) + CN -> CN end, CurrentScan = State#state.current_scan, State#state{ @@ -276,7 +298,7 @@ get_nodes(#state{req_id = ReqId, db_context = DbContext} = State) -> generate_request(ServerVersion, State) -> CurrentScan = State#state.current_scan, Res = jiffy:encode({[ - {<<"licenseId">>, <<"Infra-Server-license-Id">>}, + {<<"licenseId">>, determine_license_id()}, {<<"customerName">>, State#state.current_scan#current_scan.company_name}, {<<"periods">>, [ {[ diff --git a/src/oc_erchef/apps/chef_telemetry/test/chef_telemetry_worker_test.erl b/src/oc_erchef/apps/chef_telemetry/test/chef_telemetry_worker_test.erl index 6914adf47f..352ecc3843 100644 --- a/src/oc_erchef/apps/chef_telemetry/test/chef_telemetry_worker_test.erl +++ b/src/oc_erchef/apps/chef_telemetry/test/chef_telemetry_worker_test.erl @@ -28,7 +28,7 @@ active_nodes = 0, fqdn = []}). -feild_value_test() -> +field_value_test() -> State = #state{fqdn_select = {ok, [[{<<"property">>, <<"FQDN:node1.domain1.com">>}], [{<<"property">>, <<"FQDN:node2.subdomain2.domain2.com">>}], [{<<"property">>, <<"FQDN:node3.subdomain3.domain3.co.uk">>}]]}, @@ -36,7 +36,13 @@ feild_value_test() -> user_emails = [[{<<"email">>, <<"test@testorg.com">>}]], nodes_count = 10 }, - Expected = #expected{company_name = <<"testorg">>, + {_Lic, _Type, _GracePeriod, _ExpDate, _Msg, CN,_} = gen_server:call(chef_license_worker, get_license), + CN1 = case CN of + CN when CN == undefined, CN== <<"">>, CN == "" -> <<"testorg">>; + CN ->CN + end, + + Expected = #expected{company_name = CN1, nodes_count = 10, active_nodes = 4, fqdn = [<<".*\.domain1.com$">>, @@ -135,12 +141,25 @@ get_message() -> throw(no_request) end. +determine_license_id()-> + {_Lic, _Type, _GracePeriod, _ExpDate, _Msg, _CN, LicenseID} = gen_server:call(chef_license_worker, get_license), + case LicenseID of + undefined -> + <<"Infra-Server-license-Id">>; + <<"undefined">> -> + <<"Infra-Server-license-Id">>; + <<>> -> + <<"Infra-Server-license-Id">>; + _ -> + LicenseID + end. + validate(Req, Expected) -> Licence = ej:get({<<"licenseId">>}, Req), TotalNodes = ej:get({<<"periods">>, 1, <<"summary">>, <<"nodes">>, <<"total">>}, Req), ActiveNodes = ej:get({<<"periods">>, 1, <<"summary">>, <<"nodes">>, <<"active">>}, Req), FQDNs = ej:get({<<"metadata">>, <<"Infra Server">>, <<"fqdn">>}, Req), - ?assertEqual(<<"Infra-Server-license-Id">>, Licence), + ?assertEqual(determine_license_id(), Licence), ?assertEqual(Expected#expected.nodes_count, TotalNodes), ?assertEqual(Expected#expected.active_nodes, ActiveNodes), ?assertEqual(true, check_fqdn(FQDNs, Expected#expected.fqdn)). diff --git a/src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl b/src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl index 52e0ba2cb0..46301e8b87 100644 --- a/src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl +++ b/src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl @@ -89,14 +89,14 @@ service_available(Req, #base_state{reqid_header_name = HeaderName} = State) -> {true, Req, State3}; _ -> case chef_license_worker:get_license() of - {valid_license, _, _, _, _, _} -> + {valid_license, _, _, _, _, _,_} -> {true, Req, State3}; - {commercial_grace_period, _, _, ExpDate, LicWarnMsg, _} -> + {commercial_grace_period, _, _, ExpDate, LicWarnMsg, _,_} -> XOps = binary_to_list(chef_json:encode({[{<<"licenseType">>, <<"commercial">>},{<<"expirationDateTime">>, list_to_binary(ExpDate)}, {<<"warningMessage">>, list_to_binary(LicWarnMsg)}, {<<"gracePeriod">>, true}]})), Req1 = wrq:set_resp_header("X-Ops-License", XOps, Req), {true, Req1, State3}; - {_, Type, _, ExpDate, LicWarnMsg, _} -> + {_, Type, _, ExpDate, LicWarnMsg, _,_} -> XOps = binary_to_list(chef_json:encode({[{<<"licenseType">>, Type},{<<"expirationDateTime">>, list_to_binary(ExpDate)}, {<<"warningMessage">>, list_to_binary(LicWarnMsg)}, {<<"gracePeriod">>, false}]})), Req1 = wrq:set_resp_header("X-Ops-License", XOps, Req),