Skip to content

Commit

Permalink
Change for customer Name (#3939)
Browse files Browse the repository at this point in the history
* adding company name and license-id from automate license.

Signed-off-by: talktovikas <[email protected]>

removing trailing space.

Signed-off-by: talktovikas <[email protected]>

fixing tests.

Signed-off-by: talktovikas <[email protected]>

fixing license test case.

Signed-off-by: talktovikas <[email protected]>

Fixing bug in condition statement

Signed-off-by: talktovikas <[email protected]>

debug statements

Signed-off-by: talktovikas <[email protected]>

adding the server url.

Signed-off-by: talktovikas <[email protected]>

assignment in case of standalone chef-server.

Signed-off-by: talktovikas <[email protected]>

adding licenseId in case of Automate-T data.

Signed-off-by: talktovikas <[email protected]>

fixing T-test cases.

Signed-off-by: talktovikas <[email protected]>

fixing tests.

Signed-off-by: talktovikas <[email protected]>

fixing tests.

Signed-off-by: talktovikas <[email protected]>

* remove debug logs

Signed-off-by: talktovikas <[email protected]>

* code review changes.

Signed-off-by: talktovikas <[email protected]>

---------

Signed-off-by: talktovikas <[email protected]>
  • Loading branch information
talktovikas authored and jashaik committed Nov 14, 2024
1 parent 255de1e commit 33e06e0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 49 deletions.
36 changes: 19 additions & 17 deletions src/oc_erchef/apps/chef_license/src/chef_license_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
grace_period,
expiration_date,
message,
customer_name
customer_name,
license_id
}).

-define(DEFAULT_LICENSE_SCAN_INTERVAL, 30000). %milli seconds
Expand Down Expand Up @@ -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}.

Expand Down Expand Up @@ -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
Expand All @@ -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;
_ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
64 changes: 43 additions & 21 deletions src/oc_erchef/apps/chef_telemetry/src/chef_telemetry_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,51 @@ solr_search(Query) ->
{Error, Reason}
end.

get_license_company_name()->
{_Lic, _Type, _GracePeriod, _ExpDate, _Msg, CN,_LID} = chef_license:get_license(),
CN.

determine_license_id()->
{_Lic, _Type, _GracePeriod, _ExpDate, _Msg, _CN, LicenseID} = chef_license: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{
Expand Down Expand Up @@ -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">>, [
{[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@
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">>}]]},
should_send = true,
user_emails = [[{<<"email">>, <<"[email protected]">>}]],
nodes_count = 10
},
Expected = #expected{company_name = <<"testorg">>,
{_Lic, _Type, _GracePeriod, _ExpDate, _Msg, CN,_} = chef_license: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$">>,
Expand Down Expand Up @@ -135,12 +141,25 @@ get_message() ->
throw(no_request)
end.

determine_license_id()->
{_Lic, _Type, _GracePeriod, _ExpDate, _Msg, _CN, LicenseID} = chef_license: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)).
Expand Down
6 changes: 3 additions & 3 deletions src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 33e06e0

Please sign in to comment.