
    `8                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lZd Z	 	 	 ddZ G d de      Zy
)z8API library for VPC Service Controls Service Perimeters.    )absolute_import)division)unicode_literals)
list_pager)util)waiter)log)	resourcesNc                 F    |t        || |       |j                  |        yy)a  Sets specified field to the provided value and adds it to update mask.

  Args:
    field_name: The name of the field to set the value of.
    field_value: The value to set the field to. If it is None, the field will
      NOT be set.
    obj: The object on which the value is to be set.
    update_mask: The update mask to add this field to.

  Returns:
    True if the field was set and False otherwise.
  TF)setattrappend)
field_namefield_valueobjupdate_masks       8lib/googlecloudsdk/api_lib/accesscontextmanager/zones.py_SetIfNotNoner      s*     C[)z"	    c                    | j                         }g }t        d|||       t        d|||       t        d|	||       t        d|
||       |l|j                  d       g }|D ]T  }t        |t        j
                        r|j                  |       n|j                  |j                                ||_        V |r|j                  d       ||_        n[||W| j                         }g }t        d|||       t        d	|||       ||_        |j                  |D cg c]  }d
|z   	 c}       |sdg fS ||D cg c]  }dj                  ||       c}fS c c}w c c}w )z5Returns a ServicePerimeterConfig and its update mask.r
   restrictedServicesingressPoliciesegressPoliciesNaccessLevelsvpcAccessibleServicesallowedServicesenableRestrictionzvpcAccessibleServices.z{}.{})ServicePerimeterConfigr   r   
isinstancesixstring_typesRelativeNamer   r   VpcAccessibleServicesextendformat)messagesmask_prefixr
   restricted_serviceslevelsvpc_allowed_servicesenable_vpc_accessible_servicesvpc_yaml_flag_usedvpc_accessible_services_configingress_policiesegress_policiesconfigmasklevel_nameslservice_filterservice_filter_maskmitems                      r   _CreateServicePerimeterConfigr7   2   s|    **,&	$Y5$&964H!#3VTB /64@KKK	As''	(1 	1>>+,'f  KK'(#AF $0		)335N#%9>%'%'E "57#1F KK7JK7J!)A-7JKL	8O	E'..d3E	EE L
 Fs   E:Ec                       e Zd ZdZddZd ZddZd Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 	 	 	 	 	 dd	Z
d
 Zd Zy)ClientzBHigh-level API client for VPC Service Controls Service Perimeters.Nc                     |xs t        j                  |      | _        |xs | j                  j                  | _        y )N)version)r   	GetClientclientMESSAGES_MODULEr%   )selfr=   r%   r;   s       r   __init__zClient.__init__k   s-    ;DNN7;DK; ; ;DMr   c                     | j                   j                  j                  | j                  j	                  |j                                     S )N)name)r=    accessPolicies_servicePerimetersGetr%   =AccesscontextmanagerAccessPoliciesServicePerimetersGetRequestr!   )r?   zone_refs     r   rD   z
Client.Geto   sC    ;;77;;	F	F&&( 
G 
*+ +r   c                     | j                   j                  |j                               }t        j                  | j
                  j                  ||dd d      S )N)parentpageSizeservicePerimeters)limitbatch_size_attribute
batch_sizefield)r%   >AccesscontextmanagerAccessPoliciesServicePerimetersListRequestr!   r   YieldFromListr=   rC   )r?   
policy_refrK   reqs       r   ListzClient.Listu   sW    
--
V
V&&( W *C##44'!# #r   c                    | j                   j                  |      }| j                   j                  |j                         |      }| j                  j
                  j                  |      }t        j                  | j                  j                        }t        j                  j                  |j                  d      }t        j                  ||dj                  |j!                                     S )N)etag)rH   commitServicePerimetersRequestaccesscontextmanager.operations
collectionz!Waiting for COMMIT operation [{}])r%   CommitServicePerimetersRequest@AccesscontextmanagerAccessPoliciesServicePerimetersCommitRequestr!   r=   rC   Commitr   CloudOperationPollerNoResources
operationscore_resourcesREGISTRYParserB   WaitForr$   Name)r?   rQ   rU   
commit_reqrR   	operationpolleroperation_refs           r   r\   zClient.Commit   s    ==4=HJ
--
X
X&&('1 Y 3C <<CCCHI33DKK4J4JKF"++11#D 2 FM>>+22=3E3E3GHJ Jr   c                    | j                   }t        |      }|j                  } |||j                         dj	                  |            }| j
                  j                  j                  |      }t        j                  | j
                  j                  | j
                  j                  |      }t        j                  j                  |j                  d      }	t        j                   ||	dj#                  |	j%                                     S )z2Applies a PATCH to the provided Service Perimeter.,)servicePerimeterrB   
updateMaskrW   rX   z Waiting for PATCH operation [{}])r%   sorted?AccesscontextmanagerAccessPoliciesServicePerimetersPatchRequestr!   joinr=   rC   Patchr   OperationPollerr^   r_   r`   ra   rB   r   rb   r$   rc   )
r?   perimeter_ref	perimeterr   r5   request_typerequestre   rf   rg   s
             r   _ApplyPatchzClient._ApplyPatch   s    A%K	II "'')88K(G
 <<BB7KI!!$++"N"N"&++"8"8-IF"++11#D 2 FM>>*11-2D2D2FGI Ir   c                 h   | j                   }|j                         }g }t        d|||       t        d|||       t        d|||       t        d|||       t        |d|||||	|
|||      \  }}||_        |j                  |       |st        j                  d       |S | j                  |||      S )ag  Patch a service perimeter.

    Args:
      perimeter_ref: resources.Resource, reference to the perimeter to patch
      description: str, description of the zone or None if not updating
      title: str, title of the zone or None if not updating
      perimeter_type: PerimeterTypeValueValuesEnum type enum value for the level
        or None if not updating
      resources: list of str, the names of resources (for now, just
        'projects/...') in the zone or None if not updating.
      restricted_services: list of str, the names of services
        ('example.googleapis.com') that *are* restricted by the access zone or
        None if not updating.
      levels: list of Resource, the access levels (in the same policy) that must
        be satisfied for calls into this zone or None if not updating.
      vpc_allowed_services: list of str, the names of services
        ('example.googleapis.com') that *are* allowed to be made within the
        access zone, or None if not updating.
      enable_vpc_accessible_services: bool, whether to restrict the set of APIs
        callable within the access zone, or None if not updating.
      vpc_yaml_flag_used: bool, whether the vpc yaml flag was used.
      vpc_accessible_services_config: VpcAccessibleServices, or None if not
        updating.
      ingress_policies: list of IngressPolicy, or None if not updating.
      egress_policies: list of EgressPolicy, or None if not updating.
      etag: str, the optional etag for the version of the Perimeter that
        this operation is to be performed on.

    Returns:
      ServicePerimeter, the updated Service Perimeter.
    titledescriptionperimeterTyperU   statusr%   r&   r
   r'   r(   r)   r*   r+   r,   r-   r.   zHThe update specified results in an identical resource. Skipping request.)	r%   ServicePerimeterr   r7   rz   r#   r	   warningru   r?   rq   rx   rw   perimeter_typer
   r'   r(   r)   r*   r+   r,   r-   r.   rU   r5   rr   r   r/   config_mask_additionss                       r   ro   zClient.Patch   s    ` 	A""$IK'5)[9-iE/>9kJ&$	;7$A/1'E-'E)'%)!F! I,- 	kk
T M9kBBr   c                    | j                   }|j                         }g }t        d|||      r&|j                         |_        |j                  d       t        d|||       t        d|||       t        d|||       t        |d|||||	|
|||      \  }}||_        |j                  |       d|_	        |j                  d	       | j                  |||      S )
a  Patch the dry-run config (spec) for a Service Perimeter.

    Args:
      perimeter_ref: resources.Resource, reference to the perimeter to patch
      description: str, description of the zone or None if not updating
      title: str, title of the zone or None if not updating
      perimeter_type: PerimeterTypeValueValuesEnum type enum value for the level
        or None if not updating
      resources: list of str, the names of resources (for now, just
        'projects/...') in the zone or None if not updating.
      restricted_services: list of str, the names of services
        ('example.googleapis.com') that *are* restricted by the access zone or
        None if not updating.
      levels: list of Resource, the access levels (in the same policy) that must
        be satisfied for calls into this zone or None if not updating.
      vpc_allowed_services: list of str, the names of services
        ('example.googleapis.com') that *are* allowed to be made within the
        access zone, or None if not updating.
      enable_vpc_accessible_services: bool, whether to restrict the set of APIs
        callable within the access zone, or None if not updating.
      vpc_yaml_flag_used: bool, whether the vpc yaml flag was used.
      vpc_accessible_services_config: VpcAccessibleServices, or None if not
        updating.
      ingress_policies: list of IngressPolicy, or None if not updating.
      egress_policies: list of EgressPolicy, or None if not updating.
      etag: str, the optional etag for the version of the Perimeter that
        this operation is to be performed on.

    Returns:
      ServicePerimeter, the updated Service Perimeter.
    rw   rB   rx   ry   rU   specr{   TuseExplicitDryRunSpec)r%   r|   r   r!   rB   r   r7   r   r#   r   ru   r~   s                       r   PatchDryRunConfigzClient.PatchDryRunConfig   s    ` 	A""$IKWeY<$113in -iE/>9kJ&$	;7$A/1'E-'E)'%)!F! IN,-&*I#./M9kBBr   c                     | j                  |      }| j                  }|j                         }g d}|j                  |_        d|_        d|_        | j                  |||      S )zPromotes a Service Perimeter's dry-run config to enforcement config.

    Args:
      perimeter_ref: resources.Resource, reference to the perimeter to patch

    Returns:
      ServicePerimeter, the updated Service Perimeter.
    )rz   r   r   NF)rD   r%   r|   r   rz   r   ru   )r?   rq   original_perimeterr5   rr   r   s         r   EnforceDryRunConfigzClient.EnforceDryRunConfig@  sa     -0A""$I=K)..IIN&+I#M9kBBr   c                     | j                   j                         }||_        d|_        ddg}| j	                  |||      S )a/  Unsets the spec for a Service Perimeter.

    Args:
      perimeter_ref: resources.Resource, reference to the perimeter to patch.
      use_explicit_dry_run_spec: The value to use for the perimeter field of the
        same name.

    Returns:
      ServicePerimeter, the updated Service Perimeter.
    Nr   r   )r%   r|   r   r   ru   )r?   rq   use_explicit_dry_run_specrr   r   s        r   	UnsetSpeczClient.UnsetSpecR  sE     ..0I&?I#IN23KM9kBBr   )NNv1)N)NNNNNNNNFNNNN)__name__
__module____qualname____doc__r@   rD   rS   r\   ru   ro   r   r   r    r   r   r9   r9   h   s    J<+	#JI. %)%)OCh %)%)LC\C$Cr   r9   )NNN)r   
__future__r   r   r   apitools.base.pyr   +googlecloudsdk.api_lib.accesscontextmanagerr   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer	   r
   r_   r   r   r7   objectr9   r   r   r   <module>r      sP    ? &  ' ( < . # ; 
8 BF37263FlyCV yCr   