Skip to content

Commit fdc25e0

Browse files
authored
Change for customer Name (#3939)
* 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]>
1 parent bfa89ec commit fdc25e0

File tree

5 files changed

+92
-49
lines changed

5 files changed

+92
-49
lines changed

src/oc_erchef/apps/chef_license/src/chef_license_worker.erl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
grace_period,
2727
expiration_date,
2828
message,
29-
customer_name
29+
customer_name,
30+
license_id
3031
}).
3132

3233
-define(DEFAULT_LICENSE_SCAN_INTERVAL, 30000). %milli seconds
@@ -54,10 +55,10 @@ init(_Config) ->
5455
erlang:send_after(?DEFAULT_LICENSE_SCAN_INTERVAL, self(), check_license),
5556
{ok, State}.
5657

57-
handle_call(get_license, _From, #state{license_cache = undefined, license_type=Type, expiration_date=ExpDate, grace_period = GracePeriod, message = Msg, customer_name=CN}=State) ->
58-
{reply, {valid_license, Type, GracePeriod, ExpDate, Msg, CN}, State};
59-
handle_call(get_license, _From, #state{license_cache = Lic, license_type=Type, expiration_date=ExpDate, grace_period = GracePeriod, message = Msg, customer_name=CN} = State) ->
60-
{reply,{Lic, Type, GracePeriod, ExpDate, Msg, CN}, State};
58+
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) ->
59+
{reply, {valid_license, Type, GracePeriod, ExpDate, Msg, CN,LicenseId}, State};
60+
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) ->
61+
{reply,{Lic, Type, GracePeriod, ExpDate, Msg, CN,LicenseId}, State};
6162
handle_call(_Message, _From, State) ->
6263
{noreply, State}.
6364

@@ -88,14 +89,14 @@ check_license(State) ->
8889
{'EXIT', _} -> <<"">>
8990
end,
9091
case process_license(JsonStr) of
91-
{ok, valid_license, ExpDate, CustomerName} ->
92-
State#state{license_cache=valid_license, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, customer_name=CustomerName};
93-
{ok, commercial_expired, ExpDate, Msg, CustomerName} ->
94-
State#state{license_cache=commercial_expired, license_type = <<"commercial">>, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg, customer_name=CustomerName};
95-
{ok, commercial_grace_period, ExpDate, Msg, CustomerName} ->
96-
State#state{license_cache=commercial_grace_period, grace_period=true, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg, customer_name=CustomerName};
97-
{ok, trial_expired, ExpDate, Msg, CustomerName} ->
98-
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};
92+
{ok, valid_license, ExpDate, CustomerName,LicenseId} ->
93+
State#state{license_cache=valid_license, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, customer_name=CustomerName,license_id = LicenseId};
94+
{ok, commercial_expired, ExpDate, Msg, CustomerName,LicenseId} ->
95+
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};
96+
{ok, commercial_grace_period, ExpDate, Msg, CustomerName,LicenseId} ->
97+
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};
98+
{ok, trial_expired, ExpDate, Msg, CustomerName,LicenseId} ->
99+
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};
99100
{error, no_license} ->
100101
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, "")};
101102
{error, _} -> State
@@ -113,23 +114,24 @@ process_license(LicJson) ->
113114
case ej:get({<<"result">>}, LicJson) of
114115
{LicDetails} ->
115116
CustomerName = ej:get({<<"customer_name">>}, LicDetails),
117+
LicenseId = ej:get({<<"license_id">>}, LicDetails),
116118
case ej:get({<<"expiration_date">>}, LicDetails) of
117119
{[{<<"seconds">>,ExpireInSeconds}]} ->
118120
ExpDate = sec_to_date(ExpireInSeconds),
119121
case os:system_time(second) < ExpireInSeconds of
120-
true -> {ok, valid_license, ExpDate, CustomerName};
122+
true -> {ok, valid_license, ExpDate, CustomerName,LicenseId};
121123
_ ->
122124
case ej:get({<<"license_type">>}, LicDetails) of
123125
<<"commercial">> ->
124126
case ej:get({<<"grace_period">>}, LicDetails) of
125127
true ->
126128
{ok, commercial_grace_period, ExpDate,
127-
get_alert_message(commercial_grace_period, ExpDate), CustomerName};
129+
get_alert_message(commercial_grace_period, ExpDate), CustomerName,LicenseId};
128130
_ ->
129-
{ ok, commercial_expired, ExpDate, get_alert_message(commercial_expired, ExpDate), CustomerName}
131+
{ ok, commercial_expired, ExpDate, get_alert_message(commercial_expired, ExpDate), CustomerName,LicenseId}
130132
end;
131133
_ ->
132-
{ok, trial_expired, ExpDate, get_alert_message(trial_expired, ExpDate), CustomerName}
134+
{ok, trial_expired, ExpDate, get_alert_message(trial_expired, ExpDate), CustomerName,LicenseId}
133135
end
134136
end;
135137
_ ->

src/oc_erchef/apps/chef_license/test/chef_license_worker_test.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,35 @@ license_test()->
2828
file:write_file(?DEFAULT_FILE_PATH,get_commercial_license()),
2929
refresh_license(),
3030
timer:sleep(100),
31-
{Result, _, _, _, _} = chef_license_worker:get_license(),
31+
{Result, _, _, _, _,_,_} = chef_license_worker:get_license(),
3232
?assertEqual(valid_license, Result),
3333
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),
3434

3535
file:write_file(?DEFAULT_FILE_PATH,get_commercial_license_expired()),
3636
refresh_license(),
3737
timer:sleep(100),
38-
{Result1, _, _, _, _} = chef_license_worker:get_license(),
38+
{Result1, _, _, _, _,_,_} = chef_license_worker:get_license(),
3939
?assertEqual(commercial_expired, Result1),
4040
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),
4141

4242
file:write_file(?DEFAULT_FILE_PATH,get_commercial_grace_license()),
4343
refresh_license(),
4444
timer:sleep(100),
45-
{Result2, _, _, _, _} = chef_license_worker:get_license(),
45+
{Result2, _, _, _, _,_,_} = chef_license_worker:get_license(),
4646
?assertEqual(commercial_grace_period, Result2),
4747
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),
4848

4949
file:write_file(?DEFAULT_FILE_PATH,get_trail_license()),
5050
refresh_license(),
5151
timer:sleep(100),
52-
{Result3, _, _, _, _} = chef_license_worker:get_license(),
52+
{Result3, _, _, _, _,_,_} = chef_license_worker:get_license(),
5353
?assertEqual(valid_license, Result3),
5454
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),
5555

5656
file:write_file(?DEFAULT_FILE_PATH,get_trail_license_expired()),
5757
refresh_license(),
5858
timer:sleep(100),
59-
{Result4,_, _, _, _ } = chef_license_worker:get_license(),
59+
{Result4,_, _, _, _ ,_,_} = chef_license_worker:get_license(),
6060
?assertEqual(trial_expired_expired, Result4),
6161
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH).
6262

src/oc_erchef/apps/chef_telemetry/src/chef_telemetry_worker.erl

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -199,29 +199,51 @@ solr_search(Query) ->
199199
{Error, Reason}
200200
end.
201201

202+
get_license_company_name()->
203+
{_Lic, _Type, _GracePeriod, _ExpDate, _Msg, CN,_LID} = chef_license:get_license(),
204+
CN.
205+
206+
determine_license_id()->
207+
{_Lic, _Type, _GracePeriod, _ExpDate, _Msg, _CN, LicenseID} = chef_license:get_license(),
208+
case LicenseID of
209+
undefined ->
210+
<<"Infra-Server-license-Id">>;
211+
<<"undefined">> ->
212+
<<"Infra-Server-license-Id">>;
213+
<<>> ->
214+
<<"Infra-Server-license-Id">>;
215+
_ ->
216+
LicenseID
217+
end.
218+
219+
202220
get_company_name(State) ->
203221
CompanyName =
204-
case sqerl:adhoc_select([<<"email">>], <<"users">>, all) of
205-
{ok, Ids1} ->
206-
Ids = [Id || [{_, Id}] <- Ids1],
207-
Fun =
208-
fun(Email) ->
209-
case re:run(Email, "^[^@]*@\([^.]*\)\..*$") of
210-
{match, [_, {Pos, Len} | _]} ->
211-
{true, binary:part(Email, Pos, Len)};
212-
_ ->
213-
false
214-
end
215-
end,
216-
CompanyNames = lists:filtermap(Fun, Ids),
217-
case length(CompanyNames) == 0 of
218-
true ->
219-
throw("no valid Email Ids.");
220-
_ ->
221-
get_most_occuring(CompanyNames)
222+
case get_license_company_name() of
223+
CN when CN =:= undefined; CN=:= <<"">>; CN =:= "" ->
224+
case sqerl:adhoc_select([<<"email">>], <<"users">>, all) of
225+
{ok, Ids1} ->
226+
Ids = [Id || [{_, Id}] <- Ids1],
227+
Fun =
228+
fun(Email) ->
229+
case re:run(Email, "^[^@]*@\([^.]*\)\..*$") of
230+
{match, [_, {Pos, Len} | _]} ->
231+
{true, binary:part(Email, Pos, Len)};
232+
_ ->
233+
false
234+
end
235+
end,
236+
CompanyNames = lists:filtermap(Fun, Ids),
237+
case length(CompanyNames) == 0 of
238+
true ->
239+
throw("no valid Email Ids.");
240+
_ ->
241+
get_most_occuring(CompanyNames)
242+
end;
243+
Error ->
244+
throw(Error)
222245
end;
223-
Error ->
224-
throw(Error)
246+
CN -> CN
225247
end,
226248
CurrentScan = State#state.current_scan,
227249
State#state{
@@ -276,7 +298,7 @@ get_nodes(#state{req_id = ReqId, db_context = DbContext} = State) ->
276298
generate_request(ServerVersion, State) ->
277299
CurrentScan = State#state.current_scan,
278300
Res = jiffy:encode({[
279-
{<<"licenseId">>, <<"Infra-Server-license-Id">>},
301+
{<<"licenseId">>, determine_license_id()},
280302
{<<"customerName">>, State#state.current_scan#current_scan.company_name},
281303
{<<"periods">>, [
282304
{[

src/oc_erchef/apps/chef_telemetry/test/chef_telemetry_worker_test.erl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@
2828
active_nodes = 0,
2929
fqdn = []}).
3030

31-
feild_value_test() ->
31+
field_value_test() ->
3232
State = #state{fqdn_select = {ok, [[{<<"property">>, <<"FQDN:node1.domain1.com">>}],
3333
[{<<"property">>, <<"FQDN:node2.subdomain2.domain2.com">>}],
3434
[{<<"property">>, <<"FQDN:node3.subdomain3.domain3.co.uk">>}]]},
3535
should_send = true,
3636
user_emails = [[{<<"email">>, <<"[email protected]">>}]],
3737
nodes_count = 10
3838
},
39-
Expected = #expected{company_name = <<"testorg">>,
39+
{_Lic, _Type, _GracePeriod, _ExpDate, _Msg, CN,_} = chef_license:get_license(),
40+
CN1 = case CN of
41+
CN when CN == undefined, CN== <<"">>, CN == "" -> <<"testorg">>;
42+
CN ->CN
43+
end,
44+
45+
Expected = #expected{company_name = CN1,
4046
nodes_count = 10,
4147
active_nodes = 4,
4248
fqdn = [<<".*\.domain1.com$">>,
@@ -135,12 +141,25 @@ get_message() ->
135141
throw(no_request)
136142
end.
137143

144+
determine_license_id()->
145+
{_Lic, _Type, _GracePeriod, _ExpDate, _Msg, _CN, LicenseID} = chef_license:get_license(),
146+
case LicenseID of
147+
undefined ->
148+
<<"Infra-Server-license-Id">>;
149+
<<"undefined">> ->
150+
<<"Infra-Server-license-Id">>;
151+
<<>> ->
152+
<<"Infra-Server-license-Id">>;
153+
_ ->
154+
LicenseID
155+
end.
156+
138157
validate(Req, Expected) ->
139158
Licence = ej:get({<<"licenseId">>}, Req),
140159
TotalNodes = ej:get({<<"periods">>, 1, <<"summary">>, <<"nodes">>, <<"total">>}, Req),
141160
ActiveNodes = ej:get({<<"periods">>, 1, <<"summary">>, <<"nodes">>, <<"active">>}, Req),
142161
FQDNs = ej:get({<<"metadata">>, <<"Infra Server">>, <<"fqdn">>}, Req),
143-
?assertEqual(<<"Infra-Server-license-Id">>, Licence),
162+
?assertEqual(determine_license_id(), Licence),
144163
?assertEqual(Expected#expected.nodes_count, TotalNodes),
145164
?assertEqual(Expected#expected.active_nodes, ActiveNodes),
146165
?assertEqual(true, check_fqdn(FQDNs, Expected#expected.fqdn)).

src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ service_available(Req, #base_state{reqid_header_name = HeaderName} = State) ->
8989
{true, Req, State3};
9090
_ ->
9191
case chef_license_worker:get_license() of
92-
{valid_license, _, _, _, _, _} ->
92+
{valid_license, _, _, _, _, _,_} ->
9393
{true, Req, State3};
94-
{commercial_grace_period, _, _, ExpDate, LicWarnMsg, _} ->
94+
{commercial_grace_period, _, _, ExpDate, LicWarnMsg, _,_} ->
9595
XOps = binary_to_list(chef_json:encode({[{<<"licenseType">>, <<"commercial">>},{<<"expirationDateTime">>, list_to_binary(ExpDate)},
9696
{<<"warningMessage">>, list_to_binary(LicWarnMsg)}, {<<"gracePeriod">>, true}]})),
9797
Req1 = wrq:set_resp_header("X-Ops-License", XOps, Req),
9898
{true, Req1, State3};
99-
{_, Type, _, ExpDate, LicWarnMsg, _} ->
99+
{_, Type, _, ExpDate, LicWarnMsg, _,_} ->
100100
XOps = binary_to_list(chef_json:encode({[{<<"licenseType">>, Type},{<<"expirationDateTime">>, list_to_binary(ExpDate)},
101101
{<<"warningMessage">>, list_to_binary(LicWarnMsg)}, {<<"gracePeriod">>, false}]})),
102102
Req1 = wrq:set_resp_header("X-Ops-License", XOps, Req),

0 commit comments

Comments
 (0)