
    ,                         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 ej                  ee      Z G d dej                        Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Zy)z+General utilties for Cloud Source commands.    )absolute_import)division)unicode_literals)apis)
exceptions)
properties)	resources
sourcerepov1c                       e Zd ZdZy)InvalidTopicErrorz4Raised when a topic cannot be found within the repo.N)__name__
__module____qualname____doc__     -lib/googlecloudsdk/command_lib/source/util.pyr   r       s    <r   r   c                     t         j                  j                  d| j                  xs2 t        j
                  j                  j                  j                               S )Nzsourcerepo.projects)
projectsId)r	   REGISTRYCreateprojectr   VALUEScore	GetOrFail)argss    r   CreateProjectResourcer   $   sL    				"	"K!2!2!7!7!?!?!I!I!K 
# 
M Mr   c                 F    t         j                  j                  d||       S )Nzpubsub.projects.topics)r   topicsId)r	   r   r   )
topic_nametopic_projects     r   CreateTopicResourcer#   *   s)    				"	" 
# 
 r   c                     t        |       }|j                         }| j                  }t        j	                  ||d       S )N)enablePrivateKeyChecknamepubsubConfigs)r   RelativeNameenable_pushblock	_MESSAGESProjectConfig)r   project_refproject_namer)   s       r   ParseProjectConfigWithPushblockr.   2   sG    %d++))+,**		 	 , 
! 
 r   c                     t        |       }| j                  r0t        || j                  | j                        }t        |||d      S | j                  rt        ||d      S | j                  rt        || |d      S |S )z8Parse and create a new Repo message with modified topic.reporesource_name
GetTopicName	add_topic_ParsePubsubConfigmessage_formatservice_account_AddTopicToResourceremove_topic_RemoveTopicFromResourceupdate_topic_UpdateTopicInResource)r   r0   r!   
new_configs       r    ParseSourceRepoWithModifiedTopicr?   >   s}    D!*	^^#J0C0C$($8$8:JJF< <#JFKK!*dDOO	+r   c                     t        |       }| j                  r0t        || j                  | j                        }t        |||d      S | j                  rt        ||d      S | j                  rt        || |d      S |S )zAParse and create a new ProjectConfig message with modified topic.r   r1   r3   )r   project_configr!   r>   s       r   #ParseProjectConfigWithModifiedTopicrB   N   s    D!*	^^#J0C0C$($8$8:JJiI I#N)= =!D.	C C 
r   c                 j   | j                   r4| j                  j                   j                         }|j                         S | j                  r4| j                  j                  j                         }|j                         S | j                  j                  j                         }|j                         S )z<Get the topic name based on project and topic_project flags.)r5   CONCEPTSParser:   r<   r(   )r   	topic_refs     r   r4   r4   `   s    	^^''--/I 
			!! **002I 
			!! **002I				!!r   c                 6   |j                   g }n|j                   j                  }t        j                  }|dk(  rt        j                  }|j                  |j                  j                  | |              ||j                  |j                  |            S )z;Add the PubsubConfig message to Repo/ProjectConfig message.r0   )keyvalueadditionalPropertiesr&   r'   )	r'   rK   r*   r+   RepoappendPubsubConfigsValueAdditionalPropertyr&   )r!   r>   resourcer2   config_additional_propertiesresource_msg_modules         r   r9   r9   l   s    ##% #+#9#9#N#N !//f#..%%,,??
 @ ,- 
=='::; ; =
> >r   c                    |j                   t        dj                  | |            |j                   j                  }t	        |      D ]  \  }}|j
                  | k(  s||=  n t        dj                  | |            t        j                  }|dk(  rt        j                  } ||j                  |j                  |            S )z5Remove the topic from the Repo/ProjectConfig message.9Invalid topic [{0}]: No topics are configured in the {1}.TInvalid topic [{0}]: You must specify a topic that is already configured in the {1}.r0   rJ   rL   )r'   r   formatrK   	enumeraterH   r*   r+   rM   r&   rO   )r!   rQ   r2   rR   iconfigrS   s          r   r;   r;      s    #
 **0&]*KM M "*!7!7!L!L9:iazzZ
&q
) ;
  K#VJ>@ @ "//f#..	=='::; ; =
> >r   c                    |j                   t        dj                  | |            |j                   j                  }t	        |      D ]4  \  }}|j
                  | k(  st        |j                  |      ||   _         n t        dj                  | |            t        j                  }|dk(  rt        j                  } ||j                  |j                  |            S )zDUpdate the topic in the configuration and return a new repo message.rU   rV   r0   rJ   rL   )r'   r   rW   rK   rX   rH   _UpdateConfigWithArgsrI   r*   r+   rM   r&   rO   )r!   r   rQ   r2   rR   rY   rZ   rS   s           r   r=   r=      s     #
 **0&]*KM M "*!7!7!L!L9:iazzZ.C
,,/"1%+	 ;  K#VJ>@ @ "//f#..	=='::; ; =
> >r   Nc                     t         j                  j                  }|dk(  r|j                  }n|j                  }t         j                  |||       S )z&Parse and create PubsubConfig message.protobuf)messageFormatserviceAccountEmailtopic)r*   PubsubConfigMessageFormatValueValuesEnumPROTOBUFJSON)r!   r7   r8   message_format_enumsparsed_message_formats        r   r6   r6      sW    "//LLz!099 155			)) 
  
 r   c                 p    t        | d      }t        j                  j                  }||j                  k(  ryy)Nr_   r^   json)getattrr*   rb   rc   rd   )pubsub_configmessage_format_typerf   s      r   _GetMessageFormatStringrm      s6    ?"//LL0999	r   c                     |j                   xs t        |       }|j                  xs t        | d      }| j                  }t        |||      S )Nr`   )r7   rm   r8   rj   ra   r6   )rk   r   r7   r8   r!   s        r   r\   r\      sP    &&P*A-*P.(( KGM4I-K/""*	J	HHr   )NN)r   
__future__r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r   r	   	_API_NAME_API_VERSIONGetMessagesModuler*   Errorr   r   r#   r.   r?   rB   r4   r9   r;   r=   r6   rm   r\   r   r   r   <module>rv      s    2 &  ' , * * )	"D""9l;	=
(( =M	 $	">*>4>8Ir   