
                         L    d Z ddlZddlmZ ddlmZ d Zd Zd Zd Zd	 Z	d
 Z
y)z4Shared util methods common to Notification commands.    N)errors)utilc                     t        j                  d      }|j                  |       st        j                  d      | j                  d      d   S )Nz#(organizations|projects|folders)/.*zrWhen providing a full resource path, it must also include the pattern the organization, project, or folder prefix./notificationConfigs/r   )recompilematchr   InvalidSCCInputErrorsplit)notification_config_nameresource_patterns     Elib/googlecloudsdk/command_lib/scc/notifications/notification_util.py#GetParentFromNotificationConfigNamer      sR    ZZ EF			 8	9

%
%	7  
"	'	'(?	@	CC    c                 @    | j                  d      }|d   dz   |d   z   S )N/r      )r   )resource_namelist_organization_componentss     r   GetParentFromResourceNamer       s.    !.!4!4S!9	%a	(3	.1Ma1P	PPr   c                 8   t        j                  d      }t        j                  d      }| j                  }|j                  |      s&|j                  |      st	        j
                  d      |j                  |      r|S t        j                  |       dz   |z   S )an  Returns relative resource name for a v1 notification config.

  Validates on regexes for args containing full names or short names with
  resources. Localization is supported by the
  ValidateAndGetNotificationConfigV2Name method.

  Args:
    args: an argparse object that should contain .NOTIFICATIONCONFIGID,
      optionally 1 of .organization, .folder, .project

  Examples:

  args with NOTIFICATIONCONFIGID="organizations/123/notificationConfigs/config1"
  returns the NOTIFICATIONCONFIGID

  args with NOTIFICATIONCONFIGID="config1" and projects="projects/123" returns
  projects/123/notificationConfigs/config1
  M(organizations|projects|folders)/.+/notificationConfigs/[a-zA-Z0-9-_]{1,128}$[a-zA-Z0-9-_]{1,128}$zNotificationConfig must match either (organizations|projects|folders)/.+/notificationConfigs/[a-zA-Z0-9-_]{1,128})$ or [a-zA-Z0-9-_]{1,128}$.r   )r   r   NOTIFICATIONCONFIGIDr	   r   InvalidNotificationConfigErrorr   GetParentFromNamedArguments)argsr   
id_patternnotification_config_ids       r   &ValidateAndGetNotificationConfigV1Namer    %   s    & ZZU zz12*44			
34

/
/	!  23!! &&t, r   c                     | j                  d      r}d| j                  v rct        j                  d      }|j	                  | j                        r| j                  j                  d      d   S t        j                  d      | j                  S y)z?Returns the location from the location arg, or throws an error.locationr   z^locations/.*$zRWhen providing a full resource path, it must include the pattern '^locations/.*$'.global)IsKnownAndSpecifiedr"   r   r   r	   r   r   r
   )r   long_patterns     r   ValidateAndGetLocationFromV2Argr'   Q   s~    	j)
dmmZZ 01l			DMM	*}}""3'++)) 
 	
 ]]r   c                    t        j                  d      }t        j                  d      }t        j                  d      }| j                  }t        j                  | d      }|j                  |      rt        j                  |        d| d| S |j                  |      r|S |j                  |      r|j                  d      \  }}| d| d| S t        j                  d      )a4  Returns relative resource name for a v2 notification config.

  Validates on regexes for args containing full names with locations or short
  names with resources.

  Args:
    args: an argparse object that should contain .NOTIFICATIONCONFIGID,
      optionally 1 of .organization, .folder, .project; and optionally .location

  Examples:

  args with NOTIFICATIONCONFIGID="organizations/123/notificationConfigs/config1"
  and location="locations/us" returns
  organizations/123/locations/us/notificationConfigs/config1

  args with
  NOTIFICATIONCONFIGID="folders/123/locations/us/notificationConfigs/config1"
  and returns folders/123/locations/us/notificationConfigs/config1

  args with NOTIFICATIONCONFIGID="config1", projects="projects/123", and
  locations="us" returns projects/123/notificationConfigs/config1
  r   r   zZ(organizations|projects|folders)/.+/locations/.+/notificationConfigs/[a-zA-Z0-9-_]{1,128}$v2z/locations/r   zNotificationConfig must match (organizations|projects|folders)/.+/notificationConfigs/[a-zA-Z0-9-_]{1,128})$, (organizations|projects|folders)/.+/locations/.+/notificationConfigs/[a-zA-Z0-9-_]{1,128})$, or [a-zA-Z0-9-_]{1,128}$.)
r   r   r   r   ValidateAndGetLocationr	   r   r   r   r   )r   r    nonregionalized_resource_patternregionalized_resource_patternr   r"   parent_segment
id_segments           r   &ValidateAndGetNotificationConfigV2Namer/   e   s   . zz12*%'ZZU&" #%**b#  44((t4( ,-..t45[
J_`v_wxx #(()?@!! &++,BC#9#?#?$ ^Z [
2G
|TT--#	 r   c                     | j                   }d|v r|t        j                  d      y|t        j                  d      y)zAValidates that only a full resource or resouce args are provided.r   NzOnly provide a full resource name (organizations/123/notificationConfigs/test-config) or an --(organization|folder|project) flag, not both.z{A corresponding parent by a --(organization|folder|project) flag must be provided if it is not included in notification ID.)r   r   r   )r   parentr   s      r    ValidateMutexOnConfigIdAndParentr2      s`    44""11B   ~

/
/	@  r   )__doc__r   googlecloudsdk.command_lib.sccr   r   r   r   r    r'   r/   r2    r   r   <module>r6      s5     ; 	 1 /DQ
)X(6rr   