
                         ~   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mZ ddlmZ ddlm	Z	 ddlmZ ddddZd Zd Z ej.                  ej0                  j2                        ej4                   G d dej6                                      Z ej.                  ej0                  j:                        ej4                   G d dej6                                      Z ej.                  ej0                  j>                        ej4                   G d dej6                                      Z y)zCommand to simulate orgpolicy.    )absolute_import)division)unicode_literals)encoding_helper)
list_pager)utils)orgpolicy_simulator)arg_parsers)base)
exceptions)logza      Understand how changes to organization policies could affect your
      resources.
        ad        To simulate changes to custom constraints defined in
      `./custom-constraint.json`, run:

        $ {command}
        --organization=ORGANIZATION_ID
        --custom-constraints=custom-constraint.json

      To simulate changes to organization policies defined in `./policy.json`,
      run:

        $ {command}
        --organization=ORGANIZATION_ID
        --policies=policy.json

      To simulate changes to both custom constraints defined in
      `./custom-constraint.json` and organization policies defined in
      `./policy.json`, run:

        $ {command}
        --organization=ORGANIZATION_ID
        --policies=policy.json
        --custom-constraints=custom-constraint.json

      See https://cloud.google.com/policy-intelligence/docs/test-organization-policies
      for more information about Policy Simulator for Organization Policy.

      )briefDESCRIPTIONEXAMPLESc                    | j                  dddd       | j                  dt        j                         dt        j                  d	       | j                  d
t        j                         dt        j                  d	       y)"Parses arguments for the commands.z--organizationORGANIZATION_IDTzOrganization ID.)metavarrequiredhelpz
--policiesPOLICIESzPath to the JSON or YAML file that contains the organization
      policy to simulate. Multiple policies can be simulated by providing
      multiple, comma-separated paths. For example: `--policies=p1.json,p2.json`
      )typer   actionr   z--custom-constraintsCUSTOM_CONSTRAINTSa	  Path to the JSON or YAML file that contains the custom constraints
      to simulate. Multiple custom constraints can be simulated by providing
      multiple, comma-separated paths. For example:
      `--custom-constraints=constraint1.json,constraint2.json`
      N)add_argumentr
   ArgListUpdateActionparsers    5lib/surface/policy_intelligence/simulate/orgpolicy.py
_ArgsParser!   K   s    	  ! 	 %%
   	 "%%
  	    c                    | j                   s!| j                  st        j                  d      t	        j
                  |      }g }| j                   r| j                   D ]}  }t        j                  ||      }|j                  st        j                  dd      t        j                  |j                        }|j                  ||      }|j                  |        g }| j                  r| j                  D ]}  }	t        j                  |	|      }
|
j                  st        j                  dd      t        j                  |
j                        }|j                  |
|      }|j                  |        |j!                  ||      }d	| j"                  z   }t        j$                  |      }|j'                  |
      }|j)                  ||      }|j*                  j-                  |j*                        }|j/                  |      }|j1                  |dj3                  |j                              }t5        j6                  |j9                         t5        j:                  |            }|j<                  r|j>                  st@        jB                  jE                  d       |jF                  jI                  |j                        }|j*                  jK                  |j*                        }tM        jN                  ||ddd      S )z>Run the workflow for Policy Simulator for Organization Policy.zBMust specify either `--policies`, `--custom-constraints`, or both.zPolicy namez4'name' field not present in the organization policy.)policypolicy_parentzCustom constraint namez2'name' field not present in the custom constraint.)custom_constraintcustom_constraint_parent)policiescustom_constraintszorganizations/)overlay)violations_previewparent)requestz&Waiting for operation [{}] to completez/No violations found in the violations preview.
)r,   i  orgPolicyViolationspageSize)
batch_sizefieldbatch_size_attribute)(r(   r)   r   ConflictingArgumentsExceptionr	   OrgPolicySimulatorApir   GetPolicyMessageFromFilenameInvalidArgumentExceptionorgpolicy_utilsGetResourceFromPolicyNameGetOrgPolicyPolicyOverlayappend"GetCustomConstraintMessageFromFile#GetOrgPolicyCustomConstraintOverlayGetOrgPolicyOverlayorganizationGetParentFromOrganization,GetPolicysimulatorOrgPolicyViolationsPreview'CreateOrgPolicyViolationsPreviewRequestclient8OrganizationsLocationsOrgPolicyViolationsPreviewsServiceCreateWaitForOperationformatr   JsonToMessage$GetOrgPolicyViolationsPreviewMessageMessageToJsonviolationsCountresourceCountsr   errPrintmessages^PolicysimulatorOrganizationsLocationsOrgPolicyViolationsPreviewsOrgPolicyViolationsListRequestKOrganizationsLocationsOrgPolicyViolationsPreviewsOrgPolicyViolationsServicer   YieldFromList)argsversionop_apir(   policy_filer$   r%   policy_overlayr)   custom_constraint_filer&   r'   constraint_overlayr*   organization_resourcer,   
violationsr-   
op_serviceviolations_preview_operationoperation_response_rawpreviewlist_violations_requestpov_services                           r    _Runrb   i   s   	t66

2
2LN N44& (	]]}}--k.57f[[11BD 	D &??
++m77% 8 'n oon% % 	"&"9"9BB
 
 ##11$@B 	B "1!J!J

 
 ""!EE-#; F =  23 #: &&,> ' @' +T->->>**+@A&BB C *::# ; ' }}UUmm*!+!2!2 "3 " "22".55
&
+
+-.
 ))113##$:;=' 
	 	 (>(>GGMMDE #OO  K  K\\ K iimm+ 
	!	!!%
' 'r"   c                   *    e Zd ZdZeZed        Zd Zy)SimulateAlpha*Simulate changes to organization policies.c                     t        |        yr   Nr!   r   s    r    ArgszSimulateAlpha.Args        vr"   c                 6    t        || j                               S Nrb   ReleaseTrackselfrS   s     r    RunzSimulateAlpha.Run       d'')**r"   N	__name__
__module____qualname____doc___DETAILED_HELPdetailed_helpstaticmethodri   rq    r"   r    rd   rd      #     3 - +r"   rd   c                   *    e Zd ZdZeZed        Zd Zy)SimulateBetare   c                     t        |        yrg   rh   r   s    r    ri   zSimulateBeta.Args   rj   r"   c                 6    t        || j                               S rl   rm   ro   s     r    rq   zSimulateBeta.Run   rr   r"   Nrs   r{   r"   r    r~   r~      r|   r"   r~   c                   *    e Zd ZdZeZed        Zd Zy)
SimulateGAre   c                     t        |        yrg   rh   r   s    r    ri   zSimulateGA.Args   rj   r"   c                 6    t        || j                               S rl   rm   ro   s     r    rq   zSimulateGA.Run   rr   r"   Nrs   r{   r"   r    r   r      r|   r"   r   N)!rw   
__future__r   r   r   apitools.base.pyr   r    googlecloudsdk.api_lib.orgpolicyr   r8   *googlecloudsdk.api_lib.policy_intelligencer	   googlecloudsdk.callioper
   r   r   Bgooglecloudsdk.command_lib.policy_intelligence.simulator.orgpolicygooglecloudsdk.corer   rx   r!   rb   ReleaseTracksrn   ALPHADefaultUniverseOnlyCommandrd   BETAr~   GAr   r{   r"   r    <module>r      s#   % &  ' , ' E J / ( . T #
	
	
	
(V<S'l D%%++,+DLL +  -+ D%%**++4<< +  ,+ D%%(()+ +  *+r"   