
    G                        d Z ddlmZ ddlmZ ddlmZ ddlZddlZddl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Z ej.                  d      ZdZdZdZegZd Zd Zd Z G d de       Z!y)#Cloud Marketplace Solutions client.    )absolute_import)division)unicode_literalsN)
exceptions)
list_pager)apis)DetailedArgumentError)log)	resources)resource_printerz?PERMISSION_DENIED: Permission (.+) denied on 'projects/(.+?)/.*v1alpha1globalpforgc                 ^    | t         v ryt        dt        j                  t               z        )zDValidates product property. Returns custom error message if invalid.zAllowed products are %sN)_ALLOWED_PRODUCTSr
   jsondumps)products    ,lib/googlecloudsdk/api_lib/mps/mps_client.py_ValidateProductr   .   s0    !!
 9 $

+< =!> ? ?    c                     t        | t        j                        r%t        j                  | d      }|j
                  }|S t        j                  |       }|S )zEReturns a best-effort error message created from an API client error.z	{message})error_format)
isinstanceapitools_exceptions	HttpErrorapilib_exceptionsHttpExceptionmessagesix	text_type)errorparsed_errorerror_messages      r   _ParseErrorr&   7   sR    *445$225@KML ((M 
 MM%(M	r   c                    | r| D cg c]  }t         j                  |       }}t        d |D              rKt        t	        d |D                    dk(  r-d|d   j                  d      d|d   j                  d      g} | S c c}w )zHIf all errors are PERMISSION_DENIEDs, use a single global error instead.c              3   $   K   | ]  }|d u 
 y wN .0matchs     r   	<genexpr>z-_CollapseRegionalIAMErrors.<locals>.<genexpr>F   s     37%E7s   c              3   >   K   | ]  }|j                  d         yw)   N)groupr+   s     r   r.   z-_CollapseRegionalIAMErrors.<locals>.<genexpr>G   s     8uEKKNs   r0   zPERMISSION_DENIED: Permission r   z denied on projects/   )_REGIONAL_IAM_REGEXr-   alllensetr1   )errorsematchess      r   _CollapseRegionalIAMErrorsr:   B   s    5;<V"((+VG<37338889Q>!!!$gaj&6&6q&9; <f	- =s   Bc                       e Zd ZdZefdZed        Zed        Z	 	 	 	 	 	 ddZ		 	 ddZ
ddZd	 Zd
 Zd Zd Zd Zd ZddZd Zd ZddZd Zd ZddZd Zd ZddZd Zd Zy)	MpsClientr   c                 B   t        j                  d|      | _        t        j                  d|      | _        | j                  j
                  | _        | j                  j                  | _        | j                  j                  | _
        | j                  j                  | _        | j                  j                  | _        | j                  j                  | _        | j                  j"                  | _        | j&                  j(                  j*                  j,                  | j&                  j(                  j*                  j.                  | j&                  j(                  j*                  j0                  | j&                  j(                  j*                  j2                  d| _        y )Nmarketplacesolutions)UNSPECIFIED	DEDICATEDUNCAPPED_SHAREDCAPPED_SHARED)r	   GetClientInstance_clientGetMessagesModule	_messages!projects_locations_powerInstancespower_instances_serviceprojects_locations_powerVolumespower_volumes_serviceprojects_locations_powerImagespower_images_service projects_locations_powerNetworkspower_networks_serviceprojects_locations_powerSshKeyspower_sshkeys_serviceprojects_locations_operationsoperation_serviceprojects_locationslocations_servicemessagesPowerInstanceVirtualCpuTypeValueValuesEnumVIRTUAL_CPU_TYPE_UNSPECIFIEDr@   rA   rB   )power_instance_vitual_cpu_type_to_message)selfapi_versions     r   __init__zMpsClient.__init__Q   s   ))*@+NDL++,BKPDN $(<<#Q#QD !%!M!MD $ K KD"&,,"O"OD!%!M!MD!\\GGD!\\<<D }}22445Q5Q]]002299==66884466}}	6D2r   c                     | j                   S r)   )rD   rZ   s    r   clientzMpsClient.clienti   s    <<r   c                     | j                   S r)   )rF   r^   s    r   rU   zMpsClient.messagesm   s    >>r   Nc              #   N  K   d}g }| j                  |      D ]  }|j                  j                  d      d   }|	r
|t        k(  r- ||j                        }	  t	        ||      ||      }|dz  }t	        ||      }|rt        t        ||            }|D ]  }| |
|dz  }|r   |rt        j                         }d}|
rS|dkD  rNt        j                  t        t        |            ||	       t!        j"                  |j%                                yt'        |      }t        j                  t        t        |            ||	       t)        j*                  |j%                               y# t
        $ r%}|j                  t        |             Y d}~d}~ww xY ww)
a  Make a series of List requests, across locations in a project.

    Args:
      service: apitools_base.BaseApiService, A service with a .List() method.
      project_resource: str, The resource name of the project.
      request_class: class, The class type of the List RPC request.
      resource: string, The name (in plural) of the resource type.
      global_params: protorpc.messages.Message, The global query parameters to
        provide when calling the given method.
      limit: int, The maximum number of records to yield. None if all available
        records should be yielded.
      method: str, The name of the method used to fetch resources.
      predicate: lambda, A function that returns true for items to be yielded.
      skip_global_region: bool, True if global region must be filtered out while
      iterating over regions
      allow_partial_server_failure: bool, if True don't fail and only print a
        warning if some requests fail as long as at least one succeeds. If
        False, fail the complete command if at least one request fails.

    Yields:
      protorpc.message.Message, The resources listed by the service.

    r   /parent)global_paramsr0   NzAlist[title="Some requests did not succeed.",always-display-title])out)ListLocationsnamesplit_GLOBAL_REGIONgetattr	Exceptionappendr&   listfilterioStringIOr   Printsortedr6   r   warninggetvaluer:   r   Error)rZ   serviceproject_resourcerequest_classresourcerf   limitmethod	predicateskip_global_regionallow_partial_server_failureresponse_countr7   locationlocation_namerequestresponser8   itemsitembuffmtcollapsed_errorss                          r   AggregateYieldFromListz MpsClient.AggregateYieldFromListq   s    D NF&&'78mm))#.r2m	 ?X]]3g+77F+=2! h)e	VIu-.$
=


 ! 90  KKMc%c	%.1*<vc&k2CSACLLN# 6f=vc*:&;<csKs||~..   k!n%s7   AF%E426F%)CF%4	F"=FF%F""F%c                     | j                   j                  d|z         }t        j                  | j                  ||d|d      S )zMake a List Locations request.z	projects/ri   pageSize	locations)r|   batch_size_attribute
batch_sizefield)rU   0MarketplacesolutionsProjectsLocationsListRequestr   YieldFromListrT   )rZ   ry   r|   	page_sizer   s        r   rh   zMpsClient.ListLocations   sR    
 mmLL++ M -G##' r   c                     t        |       |t        k(  r6d}| j                  | j                  || j                  j
                  ||      S y)z(Make a series of List Instance requests.powerInstancesr|   N)r   _PFORGr   rH   rU   >MarketplacesolutionsProjectsLocationsPowerInstancesListRequestrZ   ry   r   r|   power_resources        r   AggregateListInstancesz MpsClient.AggregateListInstances   sS    W&'n((

&
&

--
V
V
 )   r   c                     t        |       |j                         }|t        k(  r7| j                  j	                  |      }| j
                  j                  |      S y)zBMake a Get Instance request. Return details of specified instance.r   N)r   RelativeNamer   rU   =MarketplacesolutionsProjectsLocationsPowerInstancesGetRequestrH   GetrZ   r   r{   power_requests       r   GetInstancezMpsClient.GetInstance   s[    W$$&H&mmaa b m ))--m<<	 r   c                     t        |       |j                         }|t        k(  rA| j                  j	                  |      }| j
                  j                  |      j                  S y)z8Make a List Instances request. Return list of instances.rd   N)r   r   r   rU   r   rH   Listr   rZ   r   location_resourcer   r   s        r   ListInstanceszMpsClient.ListInstances   sa    W --/H&mmbb c m ))..}=LLL	 r   c                    g }|D ]|  }t         j                  j                  ||j                         |j                         dd      j	                         }|j                  | j                  j                  |             ~ |S )z9Parse network attachments in flag to create network list.
projectsIdlocationsIdz5marketplacesolutions.projects.locations.powerNetworksparams
collection)powerNetwork)r   REGISTRYParseNamer   rn   rU   NetworkAttachment)rZ   r   projectnetwork_attachmentnetworksnetpower_networks          r   ParseNetworkAttachmentsz!MpsClient.ParseNetworkAttachments   s     H!((..
#LLN%]]_ M / N O[ln  oodmm55=5QR " Or   c	           
         t        |       |t        k(  r4|j                         }	|	j                         }
t        j                  j                  ||
j                         |	j                         dd      j                         }| j                  j                  |j                         ||| j                  |	|
|      ||| j                  |         }|j                         j                  d      d   }| j                  j                  |||j                         j                               }| j                  j                  |      S y)	zCreate an Instance resource.r   z3marketplacesolutions.projects.locations.powerImagesr   )ri   	bootImage	memoryGibnetworkAttachments
systemTypevirtualCpuCoresvirtualCpuTyperb   rc   )powerInstancepowerInstanceIdre   N)r   r   Parentr   r   r   r   r   rU   rV   r   rY   rj   @MarketplacesolutionsProjectsLocationsPowerInstancesCreateRequestrH   Create)rZ   r   instance_resourceboot_image_namesystem_type
memory_gibnetwork_attachment_namesvirtual_cpu_coresvirtual_cpu_typer   r   
boot_imageinstance_msginstance_idr   s                  r   CreateInstancezMpsClient.CreateInstance   sB    W&"))+h!g%%++
#LLN%]]_ K , L MYLN  ]]00 --/!99(GMef +GG  1 !l &224::3?Ckmmdd$%"))+88: e <m ))00??3 r   c                     |t         k(  rE| j                  j                  |j                               }| j                  j                  |      S y)z+Delete an existing instance share resource.r   N)r   rU   @MarketplacesolutionsProjectsLocationsPowerInstancesDeleteRequestr   rH   Delete)rZ   r   r   r   s       r   DeleteInstancezMpsClient.DeleteInstance'  sN     &^^ --/ _ 1g))0099 r   c                 j   g }||j                  d       ||j                  d       |t        k(  r| j                  j                  |j	                         ||      }| j                  j                  |j	                         |dj                  |            }| j                  j                  |      S y)z+Update an existing instance share resource.Nr   r   )ri   r   r   ,)ri   r   
updateMask)	rn   r   rU   rV   r   ?MarketplacesolutionsProjectsLocationsPowerInstancesPatchRequestjoinrH   Patch)rZ   r   r   r   r   updated_fieldsr   r   s           r   UpdateInstancezMpsClient.UpdateInstance0  s     NL)$/0&]]00 --/+ 1 -l
 mmcc --/|X[X`X`aoXp d rm))//>> r   c                     t        |       |t        k(  r6d}| j                  | j                  || j                  j
                  ||      S y)z&Make a series of List Volume requests.powerVolumesr   N)r   r   r   rJ   rU   <MarketplacesolutionsProjectsLocationsPowerVolumesListRequestr   s        r   AggregateListVolumeszMpsClient.AggregateListVolumesC  S    W&%n((

$
$

--
T
T
 )   r   c                     t        |       |j                         }|t        k(  r7| j                  j	                  |      }| j
                  j                  |      S y)z>Make a Get Volume request. Return details of specified volume.r   N)r   r   r   rU   ;MarketplacesolutionsProjectsLocationsPowerVolumesGetRequestrJ   r   r   s       r   	GetVolumezMpsClient.GetVolumeP  [    W$$&H&mm__ ` m ''++M::	 r   c                     t        |       |j                         }|t        k(  rA| j                  j	                  |      }| j
                  j                  |      j                  S y)z4Make a List Volumes request. Return list of volumes.rd   N)r   r   r   rU   r   rJ   r   r   r   s        r   ListVolumeszMpsClient.ListVolumesZ  a    W --/H&mm`` a m '',,];HHH	 r   c                     t        |       |t        k(  r6d}| j                  | j                  || j                  j
                  ||      S y)z%Make a series of List Image requests.powerImagesr   N)r   r   r   rL   rU   ;MarketplacesolutionsProjectsLocationsPowerImagesListRequestr   s        r   AggregateListImageszMpsClient.AggregateListImagesd  sS    W&$n((

#
#

--
S
S
 )   r   c                     t        |       |j                         }|t        k(  r7| j                  j	                  |      }| j
                  j                  |      S y)z<Make a Get Image request. Return details of specified image.r   N)r   r   r   rU   :MarketplacesolutionsProjectsLocationsPowerImagesGetRequestrL   r   r   s       r   GetImagezMpsClient.GetImageq  s[    W$$&H&mm^^ _ m &&**=99	 r   c                     t        |       |j                         }|t        k(  rA| j                  j	                  |      }| j
                  j                  |      j                  S y)z2Make a List Images request. Return list of images.rd   N)r   r   r   rU   r   rL   r   r   r   s        r   
ListImageszMpsClient.ListImages{  sa    W --/H&mm__ ` m &&++M:FFF	 r   c                     t        |       |t        k(  r6d}| j                  | j                  || j                  j
                  ||      S y)z(Make a series of List Networks requests.powerNetworksr   N)r   r   r   rN   rU   =MarketplacesolutionsProjectsLocationsPowerNetworksListRequestr   s        r   AggregateListNetworkszMpsClient.AggregateListNetworks  sS    W&&n((

%
%

--
U
U
 )   r   c                     t        |       |j                         }|t        k(  r7| j                  j	                  |      }| j
                  j                  |      S y)z@Make a Get Network request. Return details of specified network.r   N)r   r   r   rU   <MarketplacesolutionsProjectsLocationsPowerNetworksGetRequestrN   r   r   s       r   
GetNetworkzMpsClient.GetNetwork  s[    W$$&H&mm`` a m ((,,];;	 r   c                    t        |       |j                         }	 |t        k(  rA| j                  j	                  |      }| j
                  j                  |      j                  S y# t        j                  $ r}|cY d}~S d}~ww xY w)z6Make a List Networks request. Return list of networks.rd   N)
r   r   r   rU   r   rN   r   r   r   rw   )rZ   r   r   r   r   r8   s         r   ListNetworkszMpsClient.ListNetworks  s    W --/H	F	cc d 
 **//>LLL	 

  hs   A	A( (B;B<BBc                     t        |       |t        k(  r6d}| j                  | j                  || j                  j
                  ||      S y)z(Make a series of List SSH keys requests.powerSshKeysr   N)r   r   r   rP   rU   <MarketplacesolutionsProjectsLocationsPowerSshKeysListRequestr   s        r   AggregateListSSHKeyszMpsClient.AggregateListSSHKeys  r   r   c                     t        |       |j                         }|t        k(  r7| j                  j	                  |      }| j
                  j                  |      S y)z@Make a Get SSH Key request. Return details of specified SSH key.r   N)r   r   r   rU   ;MarketplacesolutionsProjectsLocationsPowerSshKeysGetRequestrP   r   r   s       r   	GetSSHKeyzMpsClient.GetSSHKey  r   r   c                     t        |       |j                         }|t        k(  rA| j                  j	                  |      }| j
                  j                  |      j                  S y)z6Make a List SSH Keys request. Return list of SSH keys.rd   N)r   r   r   rU   r   rP   r   r   r   s        r   ListSSHKeyszMpsClient.ListSSHKeys  r   r   )NNr   NTT)NNr)   )__name__
__module____qualname____doc___DEFAULT_API_VERSIONr\   propertyr_   rU   r   rh   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r*   r   r   r<   r<   N   s    +!5 0     ,0#'$*'+04:>J/\ "=	M %@R:?&;I:G<;Ir   r<   )"r	  
__future__r   r   r   rq   r   reapitools.base.pyr   r   r   googlecloudsdk.api_lib.utilr	   r   %googlecloudsdk.calliope.parser_errorsr
   googlecloudsdk.corer   r   googlecloudsdk.core.resourcer   r!   compiler3   r
  rk   r   r   r   r&   r:   objectr<   r*   r   r   <module>r     s    * &  ' 
  	 ? ' , G G * # ) 9 
 bjjEG ! 	H ?zI zIr   