
    A                         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	Z G d
 de	j                        Z G d de      Zd Zy)zBUtilities for the API to configure cross-project networking (XPN).    )absolute_import)division)unicode_literals)
list_pager)base_classes)
exceptions)utilsv1c                       e Zd Zy)XpnApiErrorN)__name__
__module____qualname__     -lib/googlecloudsdk/api_lib/compute/xpn_api.pyr   r      s    r   r   c                   Z    e Zd ZdZd Zd ZddZd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zy)	XpnClientzA client for interacting with the cross-project networking (XPN) API.

  The XPN API is a subset of the Google Compute Engine API.
  c                 V    || _         |j                  | _        |j                  | _        y N)compute_clientapitools_clientclientmessages)selfr   s     r   __init__zXpnClient.__init__(   s$    (D 00DK"++DMr   c                 >    | j                   j                  |g|      S )N)requestserrors_to_collect)r   MakeRequests)r   requesterrorss      r   _MakeRequestzXpnClient._MakeRequest-   s(    ++  , " "r   Nc                     g }t        | j                  ||            }|r2|xs d}dj                  |      }t        j                  |t
        |       |d   S )Nzcomplete all requestszCould not {0}:r   )listr#   formatr	   RaiseExceptionr   )r   request_tupleoperation_msgr"   resultsmsgs         r   _MakeRequestSynczXpnClient._MakeRequestSync2   sX    F4$$]F;<G#>'>m##M2c6;41:r   c                     | j                   j                  d| j                  j                  |      f}dj	                  |      }| j                  ||       y)z4Enable the project with the given ID as an XPN host.EnableXpnHostprojectzenable [{project}] as XPN hostN)r   projectsr   #ComputeProjectsEnableXpnHostRequestr&   r,   r   r0   r(   r+   s       r   
EnableHostzXpnClient.EnableHost=   sV     	99'9JLM +
1
1'
1
BC--r   c                     | j                   j                  d| j                  j                  |      f}dj	                  |      }| j                  ||       y)z5Disable the project with the given ID as an XPN host.DisableXpnHostr/   zdisable [{project}] as XPN hostN)r   r1   r   $ComputeProjectsDisableXpnHostRequestr&   r,   r3   s       r   DisableHostzXpnClient.DisableHostF   sV     	::7:KMM ,
2
27
2
CC--r   c                     | j                   j                  d| j                  j                  |      f}dj	                  |      }| j                  ||      S )z'Get the XPN host for the given project.
GetXpnHostr/   z$get XPN host for project [{project}])r   r1   r    ComputeProjectsGetXpnHostRequestr&   r,   r3   s       r   GetHostProjectzXpnClient.GetHostProjectO   sY     	66w6GIM 1
7
7
7
HC  44r   c                     | j                   j                  |      }t        j                  | j                  j
                  |dddd      S )Nr/   GetXpnResources
maxResults  	resourcesmethodbatch_size_attribute
batch_sizefield)r   %ComputeProjectsGetXpnResourcesRequestr   YieldFromListr   r1   )r   r0   r!   s      r   ListEnabledResourceszXpnClient.ListEnabledResourcesX   sN    mmAA B G## ) r   c                     | j                   j                  || j                   j                  |            }t        j                  | j
                  j                  |dddd      S )ay  List the projects in an organization that are enabled as XPN hosts.

    Args:
      project: str, project ID to make the request with.
      organization_id: str, the ID of the organization to list XPN hosts
          for. If None, the organization is inferred from the project.

    Returns:
      Generator for `Project`s corresponding to XPN hosts in the organization.
    )organization)r0   projectsListXpnHostsRequestListXpnHostsr?   r@   itemsrB   )r   "ComputeProjectsListXpnHostsRequestProjectsListXpnHostsRequestr   rH   r   r1   )r   r0   organization_idr!   s       r   ListOrganizationHostProjectsz&XpnClient.ListOrganizationHostProjectsc   sl     mm>>$(MM$M$M( %N %* ? +G ##) r   c                 &   | j                   j                  | j                   j                  ||            }| j                   j                  ||      }| j                  j
                  d|f}dj                  ||      }| j                  ||       y)a,  Associate the given resource with the given XPN host project.

    Args:
      host_project: str, ID of the XPN host project
      associated_resource: ID of the resource to associate with host_project
      xpn_resource_type: XpnResourceId.TypeValueValuesEnum, the type of the
         resource
    idtypexpnResource)r0    projectsEnableXpnResourceRequestEnableXpnResourcezAenable resource [{0}] as an associated resource for project [{1}]N)r    ProjectsEnableXpnResourceRequestXpnResourceId'ComputeProjectsEnableXpnResourceRequestr   r1   r&   r,   )r   host_projectassociated_resourcexpn_resource_typeprojects_enable_requestr!   r(   r+   s           r   _EnableXpnAssociatedResourcez&XpnClient._EnableXpnAssociatedResourcez   s     #mmLLMM//"" 0 $ M 
 mmCC)@ D BG [[))+>HM &':L I --r   c                     | j                   j                  j                  }| j                  |||j                         y)zAssociate the given project with the given XPN host project.

    Args:
      host_project: str, ID of the XPN host project
      associated_project: ID of the project to associate
    r`   N)r   r\   TypeValueValuesEnumrb   PROJECTr   r^   associated_project	xpn_typess       r   EnableXpnAssociatedProjectz$XpnClient.EnableXpnAssociatedProject   s;     ++??I%%(I<M<M & Or   c                 &   | j                   j                  | j                   j                  ||            }| j                   j                  ||      }| j                  j
                  d|f}dj                  ||      }| j                  ||       y)a2  Disassociate the given resource from the given XPN host project.

    Args:
      host_project: str, ID of the XPN host project
      associated_resource: ID of the resource to disassociate from host_project
      xpn_resource_type: XpnResourceId.TypeValueValuesEnum, the type of the
         resource
    rT   rW   )r0   !projectsDisableXpnResourceRequestDisableXpnResourcezBdisable resource [{0}] as an associated resource for project [{1}]N)r   !ProjectsDisableXpnResourceRequestr\   (ComputeProjectsDisableXpnResourceRequestr   r1   r&   r,   )r   r^   r_   r`   projects_disable_requestr!   r(   r+   s           r   _DisableXpnAssociatedResourcez'XpnClient._DisableXpnAssociatedResource   s      $}}NNMM//"" 0 $  O  
 mmDD*B E DG [[))+?IM &':L I --r   c                     | j                   j                  j                  }| j                  |||j                         y)zDisassociate the given project from the given XPN host project.

    Args:
      host_project: str, ID of the XPN host project
      associated_project: ID of the project to disassociate from host_project
    rd   N)r   r\   re   rq   rf   rg   s       r   DisableXpnAssociatedProjectz%XpnClient.DisableXpnAssociatedProject   s;     ++??I&&(I<M<M ' Or   r   )r   r   r   __doc__r   r#   r,   r4   r8   r<   rI   rR   rb   rj   rq   rs   r   r   r   r   r   "   sE    
,
"
	..5	...	O..	Or   r   c                 V    t        j                  |       }t        |j                        S r   )r   ComputeApiHolderr   r   )release_trackholders     r   GetXpnClientry      s!    ((7&	6==	!!r   N)rt   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.computer   r   r	   _DEFAULT_API_VERSIONErrorr   objectr   ry   r   r   r   <module>r      sP    I &  ' ' 7 5 0  *"" ZO ZOz"r   