
    }                        d 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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 ej,                   ej.                  ej0                  j2                  ej0                  j4                  ej0                  j6                         G d dej8                                      Zy)z&gcloud dns record-sets import command.    )absolute_import)division)unicode_literalsN)
exceptions)
list_pager)import_util)util)base)flags)log)filesc                   6    e Zd ZdZed        Zed        Zd Zy)Importa  Import record-sets into your managed-zone.

  This command imports record-sets contained within the specified record-sets
  file into your managed-zone. Note that NS records for the origin of the zone
  and the SOA NS field are not imported since name-servers are managed by
  Cloud DNS. By default, record-sets cannot be imported if there are any
  conflicts. A conflict exists if an existing record-set has the same name and
  type as a record-set that is being imported. In contrast, if the
  --delete-all-existing flag is used, the imported record-sets will replace all
  the records-sets currently in the managed-zone.

  ## EXAMPLES

  To import record-sets from a yaml record-sets file, run:

    $ {command} YAML_RECORDS_FILE --zone=MANAGED_ZONE

  To import record-sets from a zone file, run:

    $ {command} ZONE_FILE --zone-file-format --zone=MANAGED_ZONE

  To replace all the record-sets in your zone with records from a yaml
  file, run:

    $ {command} YAML_RECORDS_FILE --delete-all-existing --zone=MANAGED_ZONE
  c                     | j                         t        j                   j                  t        j                   j                  fv S )N)ReleaseTrackr
   BETAALPHA)clss    %lib/surface/dns/record_sets/import.py_IsBetaOrAlphazImport._IsBetaOrAlphaB   s:    $"3"3"8"8"&"3"3"9"9"; ; ;    c                    t        j                         j                  |       t        j                         j                  |       |j	                  dd       |j	                  dddd       |j	                  d	ddd
       |j	                  dddd       |j
                  j                  t         j                         y )Nrecords_filezFile from which record-sets should be imported. For examples of YAML-formatted and BIND zone-formatted records files, refer to https://cloud.google.com/dns/records#importing_and_exporting_record_sets)helpz--zone-file-formatF
store_truez|Indicates that the input records-file is in BIND zone format. If omitted, indicates that the records-file is in YAML format.)requiredactionr   z--delete-all-existingzoIndicates that all existing record-sets should be deleted before importing the record-sets in the records-file.z--replace-origin-nszPIndicates that NS records for the origin of a zone should be imported if defined)r   
GetZoneArgAddToParserGetLocationArgadd_argumentdisplay_info	AddFormatCHANGES_FORMAT)r   parsers     r   ArgszImport.ArgsG   s    	""6*	&&v.
D  E O	  Q :	  ; 	    !!%"6"67r   c                 ^	   t        j                  | j                         |      }t        j                  j                  |j                        s.t        j                  dj                  |j                              t        j                  j                  |j                        r.t        j                  dj                  |j                              t        j                  |      }t        j                  |      j                  |j                  t        j                   ||      d      }	 |j"                  j%                  |j&                  |j(                        }|dk(  r|j*                  |_        |j,                  j/                  |      }i }|j"                  j9                  |j&                  |j;                               }	|dk(  r|j*                  |	_        t=        j>                  |j@                  |	d      D ]  }
|
||
jB                  |
jD                  f<    	 tG        jH                  |j                        5 }|jJ                  r#t        jL                  ||jN                  |	      }n&t        jP                  || jS                         |
      }d d d        t        jZ                  ||j\                  |jN                  |j^                  |	      }|s;dj                  |j                        }t`        jb                  je                  |       y |j"                  jg                  ||jB                  |j&                        }|dk(  r|j*                  |_        |jh                  jk                  |      }t        j                   ||d      }|jl                  |d<   t        j                  |      j                  d d|      }dj                  |j                  |j;                               }t`        jb                  je                  |       ta        jn                  |       |S # t0        j2                  $ r}t5        j6                  |      d }~ww xY w# 1 sw Y   xY w# tT        $ rB}d}|j                  |j                  |jV                        }t        jX                  |      d }~ww xY w)Nz&Specified record file [{0}] not found.z*Specified record file [{0}] is a directoryzdns.managedZones)params
collection)projectmanagedZonev2rrsets)field)api_version)include_extended_recordsr/   zJUnable to read record-sets from specified records-file [{0}] because [{1}]z6Nothing to do, all the records in [{0}] already exist.)changer+   r*   managedZones)parentchangeIdzdns.changes)liner)   r(   z8Imported record-sets from [{0}] into managed-zone [{1}].)8r	   GetApiFromTrackAndArgsr   ospathexistsr   r   RecordsFileNotFoundformatisdirRecordsFileIsADirectoryGetApiClientGetRegistryParsezoneGetParamsForRegistryMESSAGES_MODULEDnsManagedZonesGetRequestr*   r+   locationr2   Getapitools_exceptions	HttpErrorcalliope_exceptionsHttpException DnsResourceRecordSetsListRequestNamer   YieldFromListresourceRecordSetsnametyper   
FileReaderzone_file_formatRecordSetsFromZoneFilednsNameRecordSetsFromYamlFiler   	ExceptionmessageUnableToReadRecordsFileComputeChangedelete_all_existingreplace_origin_nsr   statusPrintDnsChangesCreateRequestchangesCreateidCreatedResource)selfargsr/   dnszone_refget_requestrA   errorcurrentlist_requestrecordimport_fileimportedexpmsgr1   create_requestresultparam
change_refs                       r   Runz
Import.Rune   s    --d.?.?.A4HK77>>$++,++
2
9
9$:K:K
LN N	ww}}T&&'//
6
=
=!" " 

K
(C ,22		((d;% 3 'H
	5''AA""0D0D B Fk 
	#}}!!+.d
 G&&GG  hmmo H ?L d"mml**H>,2gv{{FKK()>
5D--.+   774<<[B( !77'+':':'<%'( /  &&  !F DKK


c	jjs ((@@499h6F6F A HN d $n[[/F%%k4OE		E*!!+.44  5 J E
K
K8==?,CJJS
#MA (( 5--e445" /.  5cJJt((#++6c//44	5sJ   .A"P$  Q! ?AQQ! $Q7QQQQ! !	R,*=R''R,N)__name__
__module____qualname____doc__classmethodr   r&   rt    r   r   r   r   #   s5    6 ; ; 8 8:[r   r   )rx   
__future__r   r   r   r7   apitools.base.pyr   rG   r   googlecloudsdk.api_lib.dnsr   r	   googlecloudsdk.callioper
   rI   googlecloudsdk.command_lib.dnsr   googlecloudsdk.corer   googlecloudsdk.core.utilr   UnicodeIsSupportedReleaseTracksr   GAr   r   Commandr   rz   r   r   <module>r      s     - &  ' 	 > ' 2 + ( E 0 # * D%%(($*;*;*@*@%%++-ZT\\ Z- Zr   