
                             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Z ej&                  ej(                  j*                         G d dej,                               Zy)zJImplements command to look up all effective guest policies of an instance.    )absolute_import)division)unicode_literals)base_classes)utils)base)flags)log)resource_projectorNc                   Z    e Zd ZdZdZdZdZeeefZd Zd Z	d Z
d Zd	 Zed
        Zd Zy)LookupzDisplay the guest policies that are applied to an instance.

  ## EXAMPLES

    To view all guest policies that are applied to an instance named
    `my-instance`, run:

          $ {command} my-instance

  Architecture	ShortNameVersionc                     t         j                  j                  ||j                  t        j                  |j
                              S )N)scope_lister)r	   INSTANCE_ARGResolveAsResource	resourcesGetInstanceZoneScopeListerclient)selfholderargss      6lib/surface/compute/os_config/guest_policies/lookup.py_GetInstanceRefzLookup._GetInstanceRef1   s>    //55fmmD 0      c                    	 t        j                  t        j                  j                        }|j
                  }|j                  }|j                  |j                         |j                  d|j                        }|j                  j                  j                  |      }|j                  j                  S # t         $ r%}dt#        j$                  |      v rg cY d }~S |d }~ww xY w)NzguestInventory/)instanceproject	queryPathzonezGThe resource 'guestInventory/' of type 'Guest Attribute' was not found.)r   ComputeApiHolderr   ReleaseTrackGAr   messages)ComputeInstancesGetGuestAttributesRequestNamer    r"   apitools_client	instancesGetGuestAttributes
queryValueitems	Exceptionsix	text_type)r   instance_refr   r   r&   requestresponsees           r   !_GetGuestInventoryGuestAttributesz(Lookup._GetGuestInventoryGuestAttributes8   s    ,,T->->-A-ABf}}fhBB$$&&&%  	 C "g
 ''11DDWMh  &&& a(
)	g	s$   B6B9 9	C'C"C' C""C'c                 z    t        j                  |      }i }|D ]  }|d   }|| j                  v s|d   ||<     |S )Nkeyvalue)r   MakeSerializable_OS_INFO_FIELD_KEYS)r   guest_attributesguest_attributes_jsonos_infoguest_attributeguest_attribute_keys         r   
_GetOsInfozLookup._GetOsInfoJ   sW    .?? G0+E2	 8 8	8'6w'?#$ 1
 Nr   c                 L    |j                  ||j                  |||            S )N)osArchitectureosShortName	osVersion)r   !lookupEffectiveGuestPolicyRequest)?OsconfigProjectsZonesInstancesLookupEffectiveGuestPolicyRequest!LookupEffectiveGuestPolicyRequest)r   r&   instance_nameos_architectureos_shortname
os_versions         r   _CreateRequestzLookup._CreateRequestV   s:    SS*2	*	**$  
+ 

 T  r   c                 $    |j                  |      S )N)LookupEffectiveGuestPolicy)r   servicer2   s      r   _GetResponsezLookup._GetResponseb   s    --g66r   c                 |    t         j                  j                  | d       | j                  j	                  d       y)See base class.zlook up guest policies for)operation_typea  
      table(
        packages:format="table[box,title="PACKAGES"](
          source,
          package.name,
          package.desiredState,
          package.manager,
          package.version)",
        packageRepositories:format="table[box,title='PACKAGE REPOSITORIES'](
          source,
          packageRepository.apt:format='table[box,title="APT"](
            uri,
            distribution,
            components.list())',
          packageRepository.goo:format='table[box,title="GOO"](
            name,
            url)',
          packageRepository.yum:format='table[box,title="YUM"](
            id,
            baseUrl)',
          packageRepository.zypper:format='table[box,title="ZYPPER"](
            id,
            baseUrl)')",
        softwareRecipes:format="table[box,title='SOFTWARE RECIPES'](
          source,
          softwareRecipe.name,
          softwareRecipe.version,
          softwareRecipe.desiredState
        )"
      )
    N)r	   r   AddArgumentdisplay_info	AddFormat)parsers    r   ArgszLookup.Argse   s<     
""; # =
!! # 	r   c                    | j                         }t        j                  |      }| j                  ||      }| j	                  |      }| j                  |      }|j                  | j                        }|j                  | j                        }|j                  | j                        }	t        j                  |      }
t        j                  |      }| j                  ||j                         |||	      }| j                  |
j                   |      }t#        |j$                  |j&                  |j(                  g      s<t*        j,                  j/                  dj1                  |j                                      |S )rR   z)No effective guest policy found for [{}].)r$   r   r#   r   r5   r@   get_OS_ARCHITECTURE_KEY_OS_SHORTNAME_KEY_OS_VERSION_KEYosconfig_api_utilsGetClientInstanceGetClientMessagesrL   RelativeNamerP   projects_zones_instancesanypackagespackageRepositoriessoftwareRecipesr
   statusPrintformat)r   r   release_trackr   r1   r;   r=   rI   rJ   rK   r   r&   r2   r3   s                 r   Runz
Lookup.Run   sG   %%'M**=9F''5L==lKoo./Gkk$";";<O;;t556LT112J11-@F!33MBH!!(L,E,E,G"1<MG  !@!@'JH877    
jjBII

#
#
%' ( Or   N)__name__
__module____qualname____doc__r[   r\   r]   r:   r   r5   r@   rL   rP   staticmethodrX   rk    r   r   r   r      s^    	 (!/-/@(*$

7 "	 "	Hr   r   )ro   
__future__r   r   r   googlecloudsdk.api_lib.computer   (googlecloudsdk.api_lib.compute.os_configr   r^   googlecloudsdk.callioper   ,googlecloudsdk.command_lib.compute.instancesr	   googlecloudsdk.corer
   googlecloudsdk.core.resourcer   r/   ReleaseTracksr$   BETACommandr   rq   r   r   <module>r|      sd    Q &  ' 7 P ( > # ; 
 D%%**+FT\\ F ,Fr   