@@ -263,6 +263,25 @@ public ListResponse<WebhookResponse> listWebhooks(ListWebhooksCmd cmd) {
263263 final String name = cmd .getName ();
264264 final String keyword = cmd .getKeyword ();
265265 final String scopeStr = cmd .getScope ();
266+ Webhook .Scope scope = null ;
267+ if (StringUtils .isNotEmpty (scopeStr )) {
268+ scope = EnumUtils .getEnumIgnoreCase (Webhook .Scope .class , scopeStr );
269+ if (scope == null ) {
270+ throw new InvalidParameterValueException ("Invalid scope specified" );
271+ }
272+ }
273+ if ((Webhook .Scope .Global .equals (scope ) && !Account .Type .ADMIN .equals (caller .getType ())) ||
274+ (Webhook .Scope .Domain .equals (scope ) &&
275+ !List .of (Account .Type .ADMIN , Account .Type .DOMAIN_ADMIN ).contains (caller .getType ()))) {
276+ throw new InvalidParameterValueException (String .format ("Scope %s can not be specified" , scope ));
277+ }
278+ Webhook .State state = null ;
279+ if (StringUtils .isNotEmpty (stateStr )) {
280+ state = EnumUtils .getEnumIgnoreCase (Webhook .State .class , stateStr );
281+ if (state == null ) {
282+ throw new InvalidParameterValueException ("Invalid state specified" );
283+ }
284+ }
266285 List <WebhookResponse > responsesList = new ArrayList <>();
267286 List <Long > permittedAccounts = new ArrayList <>();
268287 Ternary <Long , Boolean , Project .ListProjectResourcesCriteria > domainIdRecursiveListProject =
@@ -287,27 +306,6 @@ public ListResponse<WebhookResponse> listWebhooks(ListWebhooksCmd cmd) {
287306 SearchCriteria <WebhookJoinVO > sc = sb .create ();
288307 accountManager .buildACLSearchCriteria (sc , domainId , isRecursive , permittedAccounts ,
289308 listProjectResourcesCriteria );
290- Webhook .Scope scope = null ;
291- if (StringUtils .isNotEmpty (scopeStr )) {
292- try {
293- scope = Webhook .Scope .valueOf (scopeStr );
294- } catch (IllegalArgumentException iae ) {
295- throw new InvalidParameterValueException ("Invalid scope specified" );
296- }
297- }
298- if ((Webhook .Scope .Global .equals (scope ) && !Account .Type .ADMIN .equals (caller .getType ())) ||
299- (Webhook .Scope .Domain .equals (scope ) &&
300- !List .of (Account .Type .ADMIN , Account .Type .DOMAIN_ADMIN ).contains (caller .getType ()))) {
301- throw new InvalidParameterValueException (String .format ("Scope %s can not be specified" , scope ));
302- }
303- Webhook .State state = null ;
304- if (StringUtils .isNotEmpty (stateStr )) {
305- try {
306- state = Webhook .State .valueOf (stateStr );
307- } catch (IllegalArgumentException iae ) {
308- throw new InvalidParameterValueException ("Invalid state specified" );
309- }
310- }
311309 if (scope != null ) {
312310 sc .setParameters ("scope" , scope .name ());
313311 }
@@ -345,9 +343,8 @@ public WebhookResponse createWebhook(CreateWebhookCmd cmd) throws CloudRuntimeEx
345343 final String stateStr = cmd .getState ();
346344 Webhook .Scope scope = Webhook .Scope .Local ;
347345 if (StringUtils .isNotEmpty (scopeStr )) {
348- try {
349- scope = Webhook .Scope .valueOf (scopeStr );
350- } catch (IllegalArgumentException iae ) {
346+ scope = EnumUtils .getEnumIgnoreCase (Webhook .Scope .class , scopeStr );
347+ if (scope == null ) {
351348 throw new InvalidParameterValueException ("Invalid scope specified" );
352349 }
353350 }
@@ -359,9 +356,8 @@ public WebhookResponse createWebhook(CreateWebhookCmd cmd) throws CloudRuntimeEx
359356 }
360357 Webhook .State state = Webhook .State .Enabled ;
361358 if (StringUtils .isNotEmpty (stateStr )) {
362- try {
363- state = Webhook .State .valueOf (stateStr );
364- } catch (IllegalArgumentException iae ) {
359+ state = EnumUtils .getEnumIgnoreCase (Webhook .State .class , stateStr );
360+ if (state == null ) {
365361 throw new InvalidParameterValueException ("Invalid state specified" );
366362 }
367363 }
@@ -428,29 +424,27 @@ public WebhookResponse updateWebhook(UpdateWebhookCmd cmd) throws CloudRuntimeEx
428424 updateNeeded = true ;
429425 }
430426 if (StringUtils .isNotEmpty (stateStr )) {
431- try {
432- Webhook .State state = Webhook .State .valueOf (stateStr );
433- webhook .setState (state );
434- updateNeeded = true ;
435- } catch (IllegalArgumentException iae ) {
427+ Webhook .State state = EnumUtils .getEnumIgnoreCase (Webhook .State .class , stateStr );
428+ if (state == null ) {
436429 throw new InvalidParameterValueException ("Invalid state specified" );
437430 }
431+ webhook .setState (state );
432+ updateNeeded = true ;
438433 }
439434 Account owner = accountManager .getAccount (webhook .getAccountId ());
440435 if (StringUtils .isNotEmpty (scopeStr )) {
441- try {
442- Webhook .Scope scope = Webhook .Scope .valueOf (scopeStr );
443- if ((Webhook .Scope .Global .equals (scope ) && !Account .Type .ADMIN .equals (owner .getType ())) ||
444- (Webhook .Scope .Domain .equals (scope ) &&
445- !List .of (Account .Type .ADMIN , Account .Type .DOMAIN_ADMIN ).contains (owner .getType ()))) {
446- throw new InvalidParameterValueException (
447- String .format ("Scope %s can not be specified for owner %s" , scope , owner .getName ()));
448- }
449- webhook .setScope (scope );
450- updateNeeded = true ;
451- } catch (IllegalArgumentException iae ) {
436+ Webhook .Scope scope = EnumUtils .getEnumIgnoreCase (Webhook .Scope .class , scopeStr );
437+ if (scope == null ) {
452438 throw new InvalidParameterValueException ("Invalid scope specified" );
453439 }
440+ if ((Webhook .Scope .Global .equals (scope ) && !Account .Type .ADMIN .equals (owner .getType ())) ||
441+ (Webhook .Scope .Domain .equals (scope ) &&
442+ !List .of (Account .Type .ADMIN , Account .Type .DOMAIN_ADMIN ).contains (owner .getType ()))) {
443+ throw new InvalidParameterValueException (
444+ String .format ("Scope %s can not be specified for owner %s" , scope , owner .getName ()));
445+ }
446+ webhook .setScope (scope );
447+ updateNeeded = true ;
454448 }
455449 URI uri = URI .create (webhook .getPayloadUrl ());
456450 if (StringUtils .isNotEmpty (payloadUrl )) {
@@ -490,8 +484,7 @@ public WebhookResponse createWebhookResponse(long webhookId) {
490484
491485 @ Override
492486 public ListResponse <WebhookDeliveryResponse > listWebhookDeliveries (ListWebhookDeliveriesCmd cmd ) {
493- final CallContext ctx = CallContext .current ();
494- final Account caller = ctx .getCallingAccount ();
487+ final Account caller = CallContext .current ().getCallingAccount ();
495488 final Long id = cmd .getId ();
496489 final Long webhookId = cmd .getWebhookId ();
497490 final Long managementServerId = cmd .getManagementServerId ();
@@ -549,15 +542,15 @@ public WebhookDeliveryResponse executeWebhookDelivery(ExecuteWebhookDeliveryCmd
549542 final String secretKey = cmd .getSecretKey ();
550543 final Boolean sslVerification = cmd .isSslVerification ();
551544 final String payload = cmd .getPayload ();
552- final Account owner = accountManager .finalizeOwner (caller , null , null , null );
553545
554546 if (ObjectUtils .allNull (deliveryId , webhookId ) && StringUtils .isBlank (payloadUrl )) {
555547 throw new InvalidParameterValueException (String .format ("One of the %s, %s or %s must be specified" ,
556548 ApiConstants .ID , ApiConstants .WEBHOOK_ID , ApiConstants .PAYLOAD_URL ));
557549 }
558- if (deliveryId != null && webhookId != null ) {
550+ if (deliveryId != null && ( webhookId != null || StringUtils . isNotBlank ( payloadUrl )) ) {
559551 throw new InvalidParameterValueException (
560- String .format ("Only one of the %s or %s can be specified" , ApiConstants .ID , ApiConstants .WEBHOOK_ID ));
552+ String .format ("%s cannot be specified with %s or %s" , ApiConstants .ID , ApiConstants .WEBHOOK_ID ,
553+ ApiConstants .PAYLOAD_URL ));
561554 }
562555 WebhookDeliveryVO existingDelivery = null ;
563556 WebhookVO webhook = null ;
@@ -590,7 +583,7 @@ public WebhookDeliveryResponse executeWebhookDelivery(ExecuteWebhookDeliveryCmd
590583 accountManager .checkAccess (caller , SecurityChecker .AccessType .OperateEntry , false , webhook );
591584 }
592585 if (ObjectUtils .allNull (deliveryId , webhookId )) {
593- webhook = new WebhookVO (owner .getDomainId (), owner .getId (), payloadUrl , secretKey ,
586+ webhook = new WebhookVO (caller .getDomainId (), caller .getId (), payloadUrl , secretKey ,
594587 Boolean .TRUE .equals (sslVerification ));
595588 }
596589 WebhookDelivery webhookDelivery = webhookService .executeWebhookDelivery (existingDelivery , webhook , payload );
@@ -649,7 +642,14 @@ public WebhookFilterResponse addWebhookFilter(AddWebhookFilterCmd cmd) throws Cl
649642 WebhookFilterVO webhookFilter = new WebhookFilterVO (webhook .getId (), type , mode , matchType , value );
650643 List <? extends WebhookFilter > existingFilters = webhookFilterDao .listByWebhook (webhook .getId ());
651644 if (CollectionUtils .isNotEmpty (existingFilters )) {
652- webhookFilter .isConflicting (existingFilters );
645+ WebhookFilter conflicting = webhookFilter .getConflicting (existingFilters );
646+ if (conflicting != null ) {
647+ logger .error ("Conflict detected when adding WebhookFilter having type: {}, mode: {}, " +
648+ "matchtype: {}, value: {} with existing {} for {}" , type , mode , matchType , value , conflicting ,
649+ webhook );
650+ throw new InvalidParameterValueException (String .format ("Conflicting Webhook filter exists ID: %s" ,
651+ conflicting .getId ()));
652+ }
653653 }
654654 webhookFilter = webhookFilterDao .persist (webhookFilter );
655655 webhookService .invalidateWebhookFiltersCache (webhook .getId ());
0 commit comments