
    t                        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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dlmZ ddddZdZd Zd Zd Zd Zd Z G d de
j:                        Zd Zd Z d Z!ddddZ"	 	 d+d Z#efd!Z$d" Z%d# Z&d$ Z'd% Z(d& Z)	 	 d,d'Z* G d( d)ejV                        Z,e,jZ                  fd*Z.y)-zDConvenience functions and classes for dealing with instances groups.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)lister)path_simplifier)utils)log)
properties)rangez4Lists the named ports for an instance group resourcea  
Named ports are key:value pairs metadata representing
the service name and the port that it's running on. Named ports
can be assigned to an instance group, which indicates that the service
is available on all instances in the group. This information is used
by Application Load Balancers and proxy Network Load Balancers.

*{command}* lists the named ports (name and port tuples)
for an instance group.
z
For example, to list named ports for an instance group:

  $ {command} example-instance-group --zone=us-central1-a

The above example lists named ports assigned to an instance
group named 'example-instance-group' in the ``us-central1-a'' zone.
)briefDESCRIPTIONEXAMPLESi  c                 (    | j                         dk(  S )z#Checks if group reference is zonal.compute.instanceGroups)
Collection)	group_refs    ;lib/googlecloudsdk/api_lib/compute/instance_groups_utils.pyIsZonalGroupr   =   s    				#;	;;    c                     | D cg c]!  }|j                   |k7  r|j                         # }}t        |      r(t        j                  dddj                  |      z        yc c}w )aD  Validate if provided list in zone given as parameter.

  Args:
    instances: list of instances resources to be validated
    zone: a zone all instances must be in order to pass validation

  Raises:
    InvalidArgumentException: If any instance is in different zone
                              than given as parameter.
  	instanceszaThe zone of instance must match the instance group zone. Following instances has invalid zone: %sz, N)zoneSelfLinkanycalliope_exceptionsInvalidArgumentExceptionjoin)r   r   instinvalid_instancess       r   ValidateInstanceInZoner"   B   sx     $-C#,4		T0A }}#,  C	

6
6 3
))%
&'( ( Cs   &A!c              #   D   K   | D ]  }t        ||      D ]  }|   yw)zDExtracts items stored in given attribute of instance group response.N)getattr)	responses	attr_nameresponseitems       r   UnwrapResponser)   V   s&     h),j - s    c                     | j                   S )aN  UriFunc for listing instance-group related subresources.

  Function returns field with URI for objects being subresources of
  instance-groups, with instance fields. Works for list-instances and
  instance-configs list commands.

  Args:
    resource: instance-group subresource with instance field

  Returns:
    URI of instance
  )instance)resources    r   $UriFuncForListInstanceRelatedObjectsr-   ]   s     
		r   c                    |j                   }| j                         dk(  rI|j                  } |j                  d      | j	                         | j
                  | j                        }nH|j                  } |j                  d      | j	                         | j                  | j                        }|j                  |d|fg      }t        t        |d            S )z)Gets the request to fetch instance group.r   Get)instanceGroupr   project)r0   regionr1   requests
namedPorts)apitools_clientr   instanceGroupsGetRequestTypeNamer   r1   regionInstanceGroupsr2   MakeRequestslistr)   )r   compute_clientcomputeservicerequestresultss         r   OutputNamedPortsForGrouprB   m   s    **'77$$G+g$$U+nn&^^!!#G
 **G+g$$U+nn&!!#G ''7E72K1L'M'	nWl3	44r   c                       e Zd ZdZy)FingerprintFetchExceptionzBException thrown when there is a problem with getting fingerprint.N)__name__
__module____qualname____doc__ r   r   rD   rD      s    Jr   rD   c                    | j                   }t        |      rI|j                  }|j                  j	                  |j
                  |j                  |j                        }nH|j                  }|j                  j                  |j
                  |j                  |j                        }g }| j                  |d|fg|      }|rt        j                  |t        d       |d   j                  S )z)Gets fingerprint of given instance group.)r1   r   r0   )r1   r2   r0   r/   )r4   errors_to_collectz'Could not set named ports for resource:)error_messager   )r6   r   r7   MESSAGES_MODULEComputeInstanceGroupsGetRequestr1   r   r0   r:   %ComputeRegionInstanceGroupsGetRequestr2   r;   r
   RaiseExceptionrD   fingerprint)r=   r   r>   r?   r@   errors	resourcess          r   _GetGroupFingerprintrT      s    **')$$G%%EE!!^^-- F /G
 **G%%KK!!-- L /G
 &))%)* *  ) 	!?A 
1	!	!!r   c                    | j                   }| j                  }t        | |      }t        |      rV|j	                  ||      }|j                  |j                         ||j                  |j                        |j                  fS |j                  ||      }|j                  |j                         ||j                  |j                        |j                  fS )a  Returns a request to get named ports and service to send request.

  Args:
    compute_client: GCE API client,
    group_ref: reference to instance group (zonal or regional),
    ports: list of named ports to set

  Returns:
    request, message to send in order to set named ports on instance group,
    service, service where request should be sent
      - regionInstanceGroups for regional groups
      - instanceGroups for zonal groups
  )rQ   r5   )r0   "instanceGroupsSetNamedPortsRequestr   r1   )r0   (regionInstanceGroupsSetNamedPortsRequestr2   r1   )r6   messagesrT   r   "InstanceGroupsSetNamedPortsRequest)ComputeInstanceGroupsSetNamedPortsRequestr9   r   r1   r7   (RegionInstanceGroupsSetNamedPortsRequest/ComputeRegionInstanceGroupsSetNamedPortsRequestr2   r:   )r=   r   portsr>   rX   rQ   request_bodys          r   GetSetNamedPortsRequestForGroupr_      s     **'$$(
 %^Y?+)>> ? L ==nn&+7^^!!	 > # %,$:$:	; ; DD E L CCnn&1=!!	 D # %,$@$@	A Ar   c           	      6   g }|D ]  }|j                  d      dk7  rt        j                  |d      |j                  d      \  }}|j	                         st        j                  |d      |j                  | j                  |t        |                    |S )zValidates named ports flags.:   z+Named ports should follow NAME:PORT format.)nameport)countr   r   splitisdigitappend	NamedPortint)rX   named_portsr]   
named_porthostrd   s         r   ValidateAndParseNamedPortsArgsrn      s    
%j!88
CE E!!#&JD$<<>88
CE E	LL##CI#>?   
,r   z2Sets the list of named ports for an instance groupa  
Named ports are key:value pairs metadata representing
the service name and the port that it's running on. Named ports
can be assigned to an instance group, which
indicates that the service is available on all instances in the
group. This information is used by Application Load Balancers
and proxy Network Load Balancers.

*{command}* sets the list of named ports for all instances
in an instance group.

Note: Running this command will clear all existing named ports.
a2  
For example, to apply the named ports to an entire instance group:

  $ {command} example-instance-group --named-ports=example-service:1111 --zone=us-central1-a

The above example will assign a name 'example-service' for port 1111
to the instance group called 'example-instance-group' in the
``us-central1-a'' zone. The command removes any named ports that are
already set for this instance group.

To clear named ports from instance group provide empty named ports
list as parameter:

  $ {command} example-instance-group --named-ports="" --zone=us-central1-a
c                 Z   t        j                  dddg      }g }|D cg c]  }t        j                  |       }	}|j	                         dk(  r[|	D ]T  }
| j                  |
|j                  |j                  dd      }|j                   ||
|j                                      V |S |j	                         d	k(  rA|j                  j                  } |j                  d
      |j                         |j                  |j                        }i }|j                  |d
|fg      D ]  }|j                  sW|s%t!        dj#                  |j$                              |g }|j                  dj#                  |j$                               f|j                  |t        j                  |j                        <    |	D ]<  }
|
|v r|j                   ||
||
                $|j                   ||
d             > |S t!        dj#                  |j	                                     c c}w )a  Creates reference to instances in instance group (zonal or regional).

  Args:
    resources: Resources parser for the client.
    compute_client: Client for the current release track.
    igm_ref: URL to the target IGM.
    instance_names_or_urls: names or full URLs of target instances.
    skip_instances_on_validation_error: If true, skip instances that are not yet
      allocated to any zone. This can happen when the instance is being created
      in a regional IGM by a resize-request and is still in a queue.
    warnings: A list to collect warnings for skipped instances if
      skip_instances_on_validation_error is true.

  Returns:
    A dict where instance names are keys, and corresponding references are
    values. Unresolved names are present in dict with value None.
  InstanceNameWithReferenceinstance_nameinstance_referencecompute.instanceGroupManagers)r1   r   zcompute.instancesparams
collection)rq   rr   #compute.regionInstanceGroupManagersListManagedInstances)instanceGroupManagerr2   r1   r3   zPCannot perform action on instance {name} as it is not yet allocated to any zone.)rc   NzTSkipped performing action on instance {name} as it is not yet allocated to any zone.Unknown reference type {0})collections
namedtupler	   r9   r   Parser1   r   rh   r   r6   regionInstanceGroupManagersr8   r2   r;   r+   
ValueErrorformatrc   )rS   r=   igm_refinstance_names_or_urls"skip_instances_on_validation_errorwarnings_InstanceNameWithReferenceinstance_referencesname_or_urlnames_to_resolverq   instance_refr?   r@   resolved_referencess                  r   CreateInstanceReferencesr      sp   2  +55!O5I#J L 0/+ ;'/   <<)__
 ll ) % *l   
$)!-!6!6!8:; * DD,,HHG<g$$%;<$\\^~~!G &332G<= 4 ?""1,,2F#(( -3 -  H
//,,2F#(( -3 - $0$9$9 /..


! ")?, *	-	-""&+#6}#EG	H
 	""&+F	G * 
1889K9K9MN
OOws   H(c                     g }t        | |      j                  xs g }t        |      }t        d||      D ]A  }t	        j
                  |       }t        ||      }||||z    |_        |j                  |       C |S )a  Split request into parts according to max_length limit.

  Example:
    requests = SplitInstancesInRequest(
          self.messages.
          ComputeInstanceGroupManagersAbandonInstancesRequest(
              instanceGroupManager=igm_ref.Name(),
              instanceGroupManagersAbandonInstancesRequest=(
                  self.messages.InstanceGroupManagersAbandonInstancesRequest(
                      instances=instances,
                  )
              ),
              project=igm_ref.project,
              zone=igm_ref.zone,
          ), 'instanceGroupManagersAbandonInstancesRequest')

  Then:
    return client.MakeRequests(LiftRequestsList(service, method, requests))

  Args:
    request: _messages.Message, request to split
    request_field: str, name of property inside request holding instances field
    max_length: int, max_length of instances property

  Returns:
    List of requests with instances field length limited by max_length.
  r   )r$   r   lenr   r   CopyProtoMessagerh   )	r@   request_field
max_lengthresultall_instancesnirequest_partfields	            r   SplitInstancesInRequestr   U  s~    < &'=1;;Ar-	-!Az"a,,W5LL-0E#Aa
l3EO
MM,	 #
 
-r   c              #   (   K   |D ]	  }| ||f  yw)z(a, b, [c]) -> [(a, b, c)].NrI   )r?   methodr4   r@   s       r   GenerateRequestTuplesr   ~  s     gFG
$$ s   c                 D   g }|D ]B  \  }}}g }| j                  |||fg|       |j                  ||f       |j                  |       D g }	|D ]O  \  }}|rd}
nd}
t        ||      j                  D ]*  }|	j                  |t        j                  |      |
d       , Q |	S )a  Make *-instances requests with feedback per instance.

  Args:
    client: Compute client.
    requests: [(service, method, request)].
    instances_holder_field: name of field inside request holding list of
      instances.
    errors_to_collect: A list for capturing errors. If any response contains an
      error, it is added to this list.

  Returns:
    A list of request statuses per instance. Requests status is a dictionary
    object, see SendInstancesRequestsAndPostProcessOutputs for details.
  FAILSUCCESSselfLinkinstanceNamestatus)r;   rh   extendr$   r   r	   r9   )clientr4   instances_holder_fieldrK   request_resultsr?   r   r@   rR   status_per_instanceinstance_statusr+   s               r   #MakeRequestsAndGetStatusPerInstancer     s    & /"*gvwF
'6734f=GV,-V$	 #+ (ogvo!oG%;<FF  )..x8#" 	 G ) 
r   c                    t               }| j                  xs g D ]  }|j                  |j                  j                  k7  r|j                  |j                         Bd}d}|j                  xs g D ]>  }|j                  dk(  r|j                  }|j                  dk(  s|j                  dk(  s=d}@ |r|r|j                  ||<   |j                  |j                          |S )a-  Extract from operation instances skipped due to graceful validation.

  Args:
    operation: Operation containing warnings.
    warnings_to_collect: A list to collect warnings unrelated to graceful
      validation.

  Returns:
    Dict from resource path of a skipped instance to validation error.
  NFr+   validation_errorvalidation_outcomeT)
dictr   codeCodeValueValuesEnumNOT_CRITICAL_ERRORrh   messagedatakeyvalue)	operationwarnings_to_collectskipped_instanceswarningskipped_instance_pathis_graceful_validation_warningwarning_metadatas          r   .ExtractSkippedInstancesAndCollectOtherWarningsr     s     f##)r)g ||w22EEE  1 %*" $LL.B.				+ 0 6 6#55#77)-& / &*?18-.   1) ** 
r   c           	         g }|D ]P  \  }}}g }	| j                  |||fg|	ddd      }
|
xs dg\  }|j                  |||	f       |j                  |	       R g }|D ]  \  }}}	|	rDt        ||      j                  D ]*  }|j                  |t        j                  |      dd       , N|j                  r8t        j                  j                  dj                  |j                               t        ||      }t        ||      j                  D ]R  }||j                  d      d	z   d }d}||v rd
}||   }nd}|j                  |t        j                  |      ||d       T  |S )a  Make *-instances requests with feedback per instance.

  Specialized version of MakeRequestsAndGetStatusPerInstance. Checks operations
  for warnings presence to evaluate statuses per instance. Gracefully validated
  requests may produce warnings on operations, indicating instances skipped.
  It would be merged with MakeRequestsAndGetStatusPerInstance after we see
  there's no issues with this implementation.

  Args:
    client: Compute client.
    requests: [(service, method, request)].
    instances_holder_field: name of field inside request holding list of
      instances.
    warnings_to_collect: A list for capturing warnings. If any completed
      operation will contain skipped instances, function will append warning
      suggesting how to find additional details on the operation, warnings
      unrelated to graceful validation will be collected as is.
    errors_to_collect: A list for capturing errors. If any response contains an
      error, it is added to this list.

  Returns:
    See MakeRequestsAndGetStatusPerInstance.
  FT)log_warningsno_followupalways_return_operationNr   r   zUpdated [{0}].
z
/projects/rb   SKIPPEDr   )r   r   r   validationError)r;   rh   r   r$   r   r	   r9   
targetLinkr   r   writer   r   find)r   r4   r   r   rK   r   r?   r   r@   rR   
operationsr   r   r+   r   instance_pathr   r   s                     r   0MakeRequestsAndGetStatusPerInstanceFromOperationr     s   : /"*gvwF$$w&@%A%+2715=A	 % CJ &KYGY78V$ #+ $3 gy&g'=>HH("" +00:$
 	 I 
		

+2293G3GHIH
(* g'=>HH( !|!<q!@!AB--%/.}=
%/"" +00:%/	$
 	 I# %4B 
r   c                    | j                   }|j                         dk(  r|j                  j                  }nR|j                         dk(  r|j                  j                  }n(t        dj                  |j                                     dt        ||      _        g }|j                  |||fg|       |rt        j                  |      g S )a  Prepare *-instances request with --all-instances flag and format output.

  Args:
    api_holder: Compute API holder.
    method_name: Name of the (region) instance groups managers service method to
      call.
    request_template: Partially filled *-instances request (no instances).
    all_instances_holder_field: Name of the field inside request holding
      allInstances field.
    igm_ref: URL to the target IGM.

  Returns:
    Empty list (for consistency with a command version using list of instances).
  rs   rw   rz   T)r   r   r6   instanceGroupManagersr~   r   r   r$   allInstancesr;   r
   RaiseToolException)
api_holdermethod_namerequest_templateall_instances_holder_fieldr   r   r?   rR   s           r   SendAllInstancesRequestr   '  s      &<<$$::GDD$$@@G
1889K9K9MN
OOGK'
67D&.>?@&I

"
"6
** 
)r   c           	   #     K   | j                   }|j                         dk(  r|j                  j                  }	nR|j                         dk(  r|j                  j                  }	n(t        dj                  |j                                     g }
g }t        | j                  |||||      }|D cg c]  }|j                  r|j                   }}|t        ||      _        t        ||      }t        |	||      }g }|r|j                  t        |||||
             n|j                  t!        ||||
             |D cg c]  }|j                  s|j"                   }}|j                  |D cg c]  }t%        |d       c}       |D ]  }|  |r)t'        j(                  t+        j,                  d|             |
rt+        j.                  |
      yc c}w c c}w c c}w w)ap  Make *-instances requests and format output.

  Method resolves instance references, splits them to make batch of requests,
  adds to results statuses for unresolved instances, and yields all statuses
  raising errors, if any, in the end.

  Args:
    api_holder: Compute API holder.
    method_name: Name of the (region) instance groups managers service method to
      call.
    request_template: Partially filled *-instances request (no instances).
    instances_holder_field: Name of the field inside request holding instances
      field.
    igm_ref: URL to the target IGM.
    instances: A list of names of the instances to apply method to.
    per_instance_status_enabled: Enable functionality parsing resulting
      operation for graceful validation related warnings to allow per-instance
      status output. The plan is to gradually enable this for all per-instance
      commands in GA (even where graceful validation is not available / not
      used).
    skip_instances_on_validation_error: If true, skip instances that are not yet
      allocated to any zone. This can happen when the instance is being created
      in a regional IGM by a resize-request and is still in a queue.

  Yields:
    A list of request statuses per instance. Requests status is a dictionary
    object with link to an instance keyed with 'selfLink', instance name keyed
    with 'instanceName', and status indicating if operation succeeded for
    instance keyed with 'status'. Status might be 'FAIL', 'SUCCESS', 'SKIPPED'
    in case of graceful validation, or 'MEMBER_NOT_FOUND' (in case of regional
    MIGs, when instance name cannot be resolved).
  rs   rw   rz   MEMBER_NOT_FOUND)r   r   z!Some requests generated warnings:N)r   r   r6   r   r~   r   r   r   rS   rr   r$   r   r   r   r   r   r   rq   r   r   r   r
   ConstructListr   )r   r   r   r   r   r   per_instance_status_enabledr   r   r?   rK   r   instances_with_referencesr+   r   r4   request_tuplesrequest_status_per_instanceunresolved_instance_namesrc   r   s                        r   *SendInstancesRequestsAndPostProcessOutputsr   L  s%    R &<<$$::GDD$$@@G
1889K9K9MN
OO6( 0/(		$	$ !!/   /B 


 ""+$%57MN((+xH. " &&8N$:<O	 
  &&+FN,B,=	?@ 0/((( /  
 $$+&+$ %78+& 
 ,f
L , KK?/	12 

"
"#4
55 O,
&s,   B+G$-GA0G$<GG$,G A$G$c                       e Zd ZdZdZdZdZy)InstanceGroupFilteringModez?Filtering mode for Instance Groups based on dynamic properties.rb         N)rE   rF   rG   rH   
ALL_GROUPSONLY_MANAGED_GROUPSONLY_UNMANAGED_GROUPSrI   r   r   r   r     s    G*r   r   c                    | j                   }| j                  }g }t        |      }t        |D cg c]  }d|v s|d    c}      }i }|D ]  }	|j	                  |	dt
        j                  j                  j                  j                  id      }
|
j                  |vrt               ||
j                  <   ||
j                     j                  |
j                          g }t        j                  |      D ]b  \  }}|j                  t        j                   |j"                  j$                  ||d|j"                  j&                  |j(                  |             d g }t+        |j"                  d      rt        |D cg c]  }d|v s|d    c}      }i }|D ]b  }|j	                  |d	
      }|j                  |vrt               ||j                  <   ||j                     j                  |j,                         d t        j                  |      D ]b  \  }}|j                  t        j.                  |j"                  j0                  ||d|j"                  j&                  |j(                  |             d t        |      t        |      z   }t        |D cg c]!  }t3        j4                  |j6                        # c}      }|rt9        j:                  |       g }|D ]  }|d   }|j=                  dd      }t3        j4                  |      }||v }|r|t>        j@                  k(  rI|s|t>        jB                  k(  r_|rdnd|d<   |r||d<   |jE                  |        |S c c}w c c}w c c}w )a;  Add information if instance group is managed.

  Args:
    compute_holder: ComputeApiHolder, The compute API holder
    items: list of instance group messages,
    filter_mode: InstanceGroupFilteringMode, managed/unmanaged filtering options
  Returns:
    list of instance groups with computed dynamic properties
  r   r1   zcompute.zonesrt   N)r?   r1   requested_zonesfilter_exprhttp	batch_urlrR   r:   r2   zcompute.regions)rv   )r?   r1   requested_regionsr   r   r   rR   r   z/instanceGroups/z/instanceGroupManagers/YesNo	isManagedinstanceGroupManagerUri)#r   rS   r<   setr}   r   VALUEScorer1   	GetOrFailaddr   six	iteritemsr   r   GetZonalResourcesr6   r   r   r   hasattrr2   GetRegionalResourcesr~   r	   ScopedSuffixr   r
   r   replacer   r   r   rh   )compute_holderitemsfilter_moder   rS   rR   ig
zone_linksproject_to_zonesr   zone_refzonal_instance_group_managersr1   zones regional_instance_group_managersregion_linksproject_to_regionsr2   
region_refregionsinstance_group_managersigminstance_group_managers_refsrA   r(   	self_linkigm_self_linkscoped_suffix
is_manageds                                r   %ComputeInstanceGroupManagerMembershipr    sm      &&&)&
u+%?2&B,BvJ?@*dz((--55??
 #  $H //+.5x''(X%%&**8==9  #%&67ngu!(()A)A&&<<##((""*  8 &("V##%;<uGuB8uGHL??66G?Hj			#5	514:--.++,001B1BC	 
  MM*<=&--f.I.I((DD#%%**$$/  > ()-./  "%(&*(# ""3<<0(&* "+ 	V$'dZ I%%57M#00?M">>J1GGG
3GG
G",$D(5d$%NN4# & 
.W @8 H(&*s   	M$M$=	M)M)&M.)FN)FF)/rH   
__future__r   r   r   r{   enumapitools.base.pyr   googlecloudsdk.api_lib.computer   r   r	   r
   googlecloudsdk.callioper   googlecloudsdk.corer   r   r   	six.movesr   +INSTANCE_GROUP_GET_NAMED_PORT_DETAILED_HELPINSTANCES_MAX_LENGTHr   r"   r)   r-   rB   ErrorrD   rT   r_   rn   SET_NAMED_PORTS_HELPr   r   r   r   r   r   r   r   Enumr   r   r  rI   r   r   <module>r     s   K &  '   % 5 1 : 0 E # * 
  D	/ +.  <
(( 5&K
 0 0 K":&AR  B L (-XPz (<&R%)X"JNb"X !&',d6N  (B'L'L[r   