
                             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d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZy)z;Declarative Response Hooks for Cloud SCC's Asset responses.    )absolute_import)division)print_function)unicode_literals)_JsonValueToPythonValue)securitycenter_client)InvalidSCCInputErrorc                     ~t        |       }t        |      dkD  rt        d      |D ]  }|j                  j                  c S  y)z+Returns security marks from asset response.   zPListAssetResponse must only return one asset since it is filtered by Asset Name.N)listlenr	   assetsecurityMarks)responseargslist_asset_responseasset_results       ;lib/googlecloudsdk/command_lib/scc/assets/response_hooks.py ExtractSecurityMarksFromResponser      sM    
X	!
	  *l+++ *    c                     ~t        |       }|st        d      t        |      dkD  rt        d      |D ]W  }t        |j                  j
                  j                  d      d         |j                  j                  j                  d}|c S  y)zDReturns asset that matches the user provided asset or resource-name.z!Asset or resource does not exist.r   zaListAssetResponse must only return one asset since it is filtered by Asset Name or Resource Name./   )r   resourceNameN)	r   r	   r   intr   namesplitsecurityCenterPropertiesr   )r   r   r   r   result_dictionarys        r   (ExtractMatchingAssetFromDescribeResponser    '   s    
X	
B
CC	!
	*+ + *l\'',,2237:;$**CCPP  *r   c                     ~t        |       }t        |      }t        |      }t        |      }t	        ||      }t        |      }d|i}|S )z<Returns Parent for the user provided asset or resource-name.parent)_ValidateAndGetAssetResult_GetAssetResourceParent_ExtractOrganization_FilterOnResourceName	_GetAsset
_GetParent)	r   r   r   asset_parentorganizationresource_name_filterr   r"   r   s	            r   )ExtractMatchingAssetFromGetParentResponser,   9   sV    
+H5,(6,%l3,.|<
L"6
7%e&(	r   c                     ~t        |       }t        |      }t        |      }t        |      }t	        ||      }t        |      }d|i}|S )z?Returns ProjectId for the user provided asset or resource-name.	projectId)r#   _GetAssetProjectr%   r&   r'   _GetProjectId)	r   r   r   asset_projectr*   r+   r   
project_idr   s	            r   *ExtractMatchingAssetFromGetProjectResponser3   G   sV    
+H5,"<0-%l3,.}=
L"6
7%U#*"J/	r   c                 n    t        |       }|st        d      t        |      dkD  rt        d      |d   S )NzAsset or resource does not exist for the provided Organization. Please verify that both the OrganizationId and AssetId/ResourceName are correct.r   z>An asset can not have multiple projects. Something went wrong.r   )r   r	   r   )r   r   s     r   r#   r#   U   sN    X	
	  		!
HJ J	Q	r   c                 `    | j                   j                  j                  }|t        d      |S )NzAsset does not have a parent.)r   r   resourceParentr	   )r   r)   s     r   r$   r$   b   s1    ##<<KK,
>
??	r   c                 `    | j                   j                  j                  }|t        d      |S )Nz/Organization assets do not belong to a Project.)r   r   resourceProjectr	   )r   r1   s     r   r/   r/   i   s5    $$==MM-
9; ;	r   c                     d| z   dz   S )Nz'securityCenterProperties.resourceName="" )asset_propertys    r   r&   r&   q   s    	3n	Dt	KKr   c                     | j                   j                  D cg c]  }|j                  dk(  r|j                   }}|st	        d      t        |d         S c c}w )Nr.   z#No projectId exists for this asset.r   resourcePropertiesadditionalPropertieskeyvaluer	   r   )r   xr2   s      r   r0   r0   u   se     ''<<<!	
+	 gg<  
 

D
EE	 A	//   "Ac                     | j                   j                  D cg c]  }|j                  dk(  r|j                   }}|st	        d      t        |d         S c c}w )Nr   z No parent exists for this asset.r   r>   )r   rC   r"   s      r   r(   r(      sd     ''<<<!	
& gg< 	 
 

A
BB	 	++rD   c                     | j                   j                  j                  d      d   dz   | j                   j                  j                  d      d   z   S )Nr   r   r   )r   r   r   )r   s    r   r%   r%      sO    



!
!
'
'
,Q
/#
5



!
!
'
'
,Q
/0 1r   Nc                     t        j                         }|j                  | |      }|j                  }t	        |      dk7  rt        d      |d   j                  S )N)r"   request_filterr   zCSomething went wrong while retrieving the ProjectId for this Asset.r   )	sc_clientAssetsClientListlistAssetsResultsr   r	   r   )r"   rH   asset_service_clientlist_asset_response_for_projectlist_asset_resultss        r   r'   r'      si    "//1$8$=$=N %> %4!6HH	!
MO O	A		$	$$r   )N)__doc__
__future__r   r   r   r   apitools.base.py.extra_typesr   googlecloudsdk.api_lib.sccr   rI   %googlecloudsdk.command_lib.scc.errorsr	   r   r    r,   r3   r#   r$   r/   r&   r0   r(   r%   r'   r;   r   r   <module>rU      s]    B &  % ' @ I F	,$
 L0,1
%r   