
    &                     P    d Z ddlmZ ddlmZ d Zd Zd Zd Z	 	 dd	Z	d
 Z
d Zy)z@Code that's shared between multiple backend-buckets subcommands.    )encoding)iam_utilc                 6   d}|j                   rRg }|j                   j                         D ]1  \  }}|j                  | j                  j	                  ||             3 |S |j
                  ,|j
                  j                  |j
                  j                  }|S )au  Returns the negative caching policy.

  Args:
    client: The client used by gcloud.
    args: The arguments passed to the gcloud command.
    backend_bucket: The backend bucket object. If the backend bucket object
      contains a negative caching policy already, it is used as the base to
      apply changes based on args.

  Returns:
    The negative caching policy.
  N)codettl)negative_caching_policyitemsappendmessages+BackendBucketCdnPolicyNegativeCachingPolicy	cdnPolicynegativeCachingPolicy)clientargsbackend_bucketr   r   r   s         Olib/googlecloudsdk/command_lib/compute/backend_buckets/backend_buckets_utils.pyGetNegativeCachingPolicyr      s     !	!! 11779	c$$
//
E
ES F "# : 
! 	 	  ,  66B . 8 8 N N	      c                     d}|j                   r>g }|j                   D ]-  }|j                  | j                  j                  |             / |S )zReturns bypass cache on request headers.

  Args:
    client: The client used by gcloud.
    args: The arguments passed to the gcloud command.

  Returns:
    The bypass cache on request headers.
  N)
headerName)bypass_cache_on_request_headersr
   r   0BackendBucketCdnPolicyBypassCacheOnRequestHeader)r   r   r   headers       r   GetBypassCacheOnRequestHeadersr   2   sZ     %)!	))&(#66%,,
//
J
J K !" 7
 
)(r   c                 J    | j                  d      xs | j                  d      S )a  Returns true if the request requires a CacheKeyPolicy message.

  Args:
    args: The arguments passed to the gcloud command.

  Returns:
    True if there are cache key policy related arguments which require adding
    a CacheKeyPolicy message in the request.
   cache_key_query_string_whitelistcache_key_include_http_header)IsSpecified)r   s    r   HasCacheKeyPolicyArgsr   G   s,     

=
> <


:
;=r   c                    | j                   j                         }|j                  ,|j                  j                  |j                  j                  }|j                  |j                  |_        |j                  |j                  |_        |S )a`  Returns the cache key policy.

  Args:
    client: The client used by gcloud.
    args: The arguments passed to the gcloud command.
    backend_bucket: The backend bucket object. If the backend bucket object
      contains a cache key policy already, it is used as the base to apply
      changes based on args.

  Returns:
    The cache key policy.
  )r   $BackendBucketCdnPolicyCacheKeyPolicyr   cacheKeyPolicyr   includeHttpHeadersr   queryStringWhitelist)r   r   r   cache_key_policys       r   GetCacheKeyPolicyr&   U   s     __IIK*--9%//>>	''3*.*L*L'	**6-- ) 
r   Nc                    |j                    t        j                  |j                         }n| j                  j	                         }|j                  d      r|j                  |_        |j                  |j                  |_	        |j                  r4| j                  j                  j                  |j                        |_        |j                  |j                  |_        |j                  |j                  |_        |j"                  |j"                  |_        |r|j                  dk(  xr |j                  du }|j&                  s|r|j)                  d       d|_        |j                  dk(  xr |j                  du }|j*                  s|r|j)                  d       d|_        |j                  dk(  xs |j                  dk(  xr |j"                  du }|j,                  s|r|j)                  d       d|_        |j.                  |j.                  |_        t3        | ||      }	|	|	|_        |j6                  rd|_        |r<|j8                  s|j.                  $|j.                  s|j)                  d	       g |_        |j:                  |j:                  |_        t?        | |      }
|
|
|_         |rH|jB                  r|j)                  d
       d|_        |jD                  r|j)                  d       g |_         tG        |      rtI        | ||      |_%        || j                  j	                         k7  r||_         yy)a  Applies the CdnPolicy arguments to the specified backend bucket.

  If there are no arguments related to CdnPolicy, the backend bucket remains
  unmodified.

  Args:
    client: The client used by gcloud.
    args: The arguments passed to the gcloud command.
    backend_bucket: The backend bucket object.
    is_update: True if this is called on behalf of an update command instead of
      a create command, False otherwise.
    cleared_fields: Reference to list with fields that should be cleared. Valid
      only for update command.
  Nsigned_url_cache_max_ageUSE_ORIGIN_HEADERSzcdnPolicy.clientTtlzcdnPolicy.defaultTtlFORCE_CACHE_ALLzcdnPolicy.maxTtlTzcdnPolicy.negativeCachingPolicyzcdnPolicy.serveWhileStalez%cdnPolicy.bypassCacheOnRequestHeaders)&r   r   CopyProtoMessager   BackendBucketCdnPolicyr   r(   signedUrlCacheMaxAgeSecrequest_coalescingrequestCoalescing
cache_modeCacheModeValueValuesEnum	cacheMode
client_ttl	clientTtldefault_ttl
defaultTtlmax_ttlmaxTtlno_client_ttlr
   no_default_ttl
no_max_ttlnegative_cachingnegativeCachingr   r   r   no_negative_caching_policiesserve_while_staleserveWhileStaler   bypassCacheOnRequestHeadersno_serve_while_stale"no_bypass_cache_on_request_headersr   r&   r"   )r   r   r   	is_updatecleared_fields
cdn_policyshould_clean_client_ttlshould_clean_default_ttlshould_clean_max_ttlr   r   s              r   ApplyCdnPolicyArgsrJ   p   s    & )**>+C+CDJ779J	01)-)F)FJ&	(#'#:#:J 	__!??AAt/ 	__ ??J	! ,,J	\\J 	//KDOOt4K 412!j 	//LD4D4D4L 623"j OO/CC 1 OO01K6:lld6J  ../j	&!%!6!6J4VT5CE('>J$	!!!%J))				*43H3H=>)+j&	'!%!7!7J$B64$P!$0-LJ*  78#'j ..CD/1j,4  1&$ OJ6??99;;)N <r   c                    | j                         dk(  rn|j                  j                  }|j                  j	                  | j                         | j                  | j                        }|j                  |d|fg      d   S |j                  j                  }|j                  j                  | j                         | j                        }|j                  |d|fg      d   S )zGets the IAM policy for a backend bucket.

  Args:
    backend_bucket_ref: The backend bucket reference.
    client: The client.

  Returns:
    The IAM policy.
  compute.regionBackendBuckets)resourceregionprojectGetIamPolicyr   )rM   rO   )
Collectionapitools_clientregionBackendBucketsr   .ComputeRegionBackendBucketsGetIamPolicyRequestNamerN   rO   MakeRequestsbackendBuckets(ComputeBackendBucketsGetIamPolicyRequest)backend_bucket_refr   servicerequests       r   rP   rP      s     ""$(FF$$99GooLL#((*!(("** M ,G .' BCDQGG""11'OODD!&&(2D2L2L E ' 
		w@A	B1	EEr   c                    d}| j                         dk(  r|j                  j                  }|j                  j	                  | j                         | j                  |j                  j                  |            }|j                  |d|fg      d   }n| j                         dk(  r|j                  j                  }|j                  j                  | j                         | j                  | j                  |j                  j                  |            }|j                  |d|fg      d   }t        j                  | j                         d	       |S )
zSets the IAM policy for a backend bucket.

  Args:
    backend_bucket_ref: The backend bucket reference.
    client: The client.
    policy: The IAM policy.

  Returns:
    The set IAM policy.
  Nzcompute.backendBuckets)policy)rM   rO   globalSetPolicyRequestSetIamPolicyr   rL   )rM   rN   rO   regionSetPolicyRequestzbackend bucket)rQ   rR   rW   r   (ComputeBackendBucketsSetIamPolicyRequestrU   rO   GlobalSetPolicyRequestrV   rS   .ComputeRegionBackendBucketsSetIamPolicyRequestrN   RegionSetPolicyRequestr   LogSetIamPolicy)rY   r   r]   resultrZ   r[   s         r   r_   r_      sN    &""$(@@$$33GooFF#((*"**%EE  F  
 G G   7NG"D!EFqIF$$&*HH$$99GooLL#((*!(("**%EE  F  
	 M G   7NG"D!EFqIF
-2246FG	-r   )FN)__doc__apitools.base.pyr   googlecloudsdk.command_lib.iamr   r   r   r   r&   rJ   rP   r_    r   r   <module>rk      sA    G & 3!8)*=< "'&*	_*DF0"r   