
    4)                         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  G d d	ej                        Zy)
Cloud Backup Plans client.    )absolute_import)division)unicode_literalsN)util)
exceptionsc                   F     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
 xZS )	BackupPlansClientr   c                 `    t         t        |           | j                  j                  | _        y )N)superr
   __init__clientprojects_locations_backupPlansservice)self	__class__s    3lib/googlecloudsdk/api_lib/backupdr/backup_plans.pyr   zBackupPlansClient.__init__!   s"    	
T+-;;==DL    c                    g }|D ]  }| j                   j                         }d|vrdn|d   |_        | j                   j                  |d   |d         |_        | j                   j                  j                  |d         |_        d|v r
|d   |_        d|v r;|d   D cg c]'  }| j                   j                  j                  |      ) c}|_	        d	|v r|d	   j                  d
      }| j                   j                  | j                   j                  j                  |d         | j                   j                  j                  |d               |_        d|v r
|d   |_        d|v r;|d   D cg c]'  }| j                   j                  j!                  |      ) c}|_        | j                   j%                  |d   |d   |      }|j'                  |        |S c c}w c c}w )Nz	time-zoneUTCzbackup-window-startzbackup-window-end)startHourOfDayendHourOfDay
recurrencezhourly-frequencyzdays-of-weekzweek-day-of-month-r      )weekOfMonth	dayOfWeekzdays-of-monthmonthsrule-idzretention-days)ruleIdbackupRetentionDaysstandardSchedule)messagesStandardScheduletimeZoneBackupWindowbackupWindowRecurrenceTypeValueValuesEnumrecurrenceTypehourlyFrequency"DaysOfWeekValueListEntryValuesEnum
daysOfWeeksplitWeekDayOfMonthWeekOfMonthValueValuesEnumDayOfWeekValueValuesEnumweekDayOfMonthdaysOfMonthMonthsValueListEntryValuesEnumr   
BackupRuleappend)	r   backup_rulesbackup_rules_messagebackup_rulestandard_scheduledayweek_day_of_monthmonthbackup_rule_messages	            r   _ParseBackupRulesz#BackupPlansClient._ParseBackupRules%   s2   #--88:k1%{;7O   (,}}'A'A$%:;"#67 (B ($
 --
(
(
F
F,' &
 
{	*,78J,K)	;	&
 #>2	(
 3 MM**MM 3	(
$ 
	+'(;<BB3G+/==+G+G44OO!!$ mm22KK!!$	 ,H ,
( 
K	'(3O(D%	[	  %X.$
. MM**II%P.$
  !MM44Y')*:;, 5 
 !!"56[ $\  ;(
&$
s   !,G"4,G'c	           
         |j                         j                         }	|j                         }
| j                  j	                  ||      }|||_        |{| j                  j                  j                  |j                         D cg c]6  \  }}| j                  j                  j                  j                  ||      8 c}}      |_	        | j                  |      |_        |||_        |t        |      |_        | j                  j                  |	||
      }| j                   j#                  |      S c c}}w )a:  Creates a Backup Plan.

    Args:
      resource: The Backup Plan resource.
      backup_vault: The Backup Vault resource.
      resource_type: The resource type of the Backup Plan.
      backup_rules: The backup rules of the Backup Plan.
      log_retention_days: The log retention days of the Backup Plan.
      description: The description of the Backup Plan.
      labels: The labels of the Backup Plan.
      max_custom_on_demand_retention_days: The custom on demand retention days
        limit of the Backup Plan.

    Returns:
      The created Backup Plan.
    )resourceTypebackupVault)keyvalue)additionalProperties)parent
backupPlanbackupPlanId)ParentRelativeNameNamer#   
BackupPlandescriptionLabelsValueitemsAdditionalPropertylabelsr>   backupRuleslogRetentionDaysintmaxCustomOnDemandRetentionDays1BackupdrProjectsLocationsBackupPlansCreateRequestr   Create)r   resourcebackup_vaultresource_typer6   log_retention_daysrL   rP   #max_custom_on_demand_retention_daysrE   backup_plan_idbackup_planrB   rC   requests                  r   rV   zBackupPlansClient.CreateW   sH   6 __++-F]]_N--**"  + K  +k==33??
 !'	  !/*#u mm&&22EE F  !/	  @ k #44\BK%%7k"*6
1
2 0 mmMM# N G
 <<w''' s   	;D<
c                     | j                   j                  |j                               }| j                  j	                  |      S )zDescribes a Backup Plan.

    Args:
      resource: The Backup Plan resource.

    Returns:
      The described Backup Plan.
    name)r#   .BackupdrProjectsLocationsBackupPlansGetRequestrI   r   Getr   rW   r^   s      r   DescribezBackupPlansClient.Describe   sA     mmJJ""$ K G <<G$$r   c	           	         |t        d      | j                  j                  |j                        }	|||	_        |||	_        |t        |      |	_        |j                  D 
ch c]  }
|
j                   }}
|| j                  |      |	_        |	S |t        j                  |D 
cg c]  }
|
d   	 c}
      }|j                         D cg c]  \  }}|dkD  s| }}}|rt        j                  dd| d      t!        t#        |D 
cg c]  }
|
d   |vr|
d    c}
            }|rt        j                  dd| d      |D 
cg c]  }
|
d   	 }}
|j                  D 
cg c]  }
|
j                  |vr|
 c}
|	_        |	j                  j%                  | j                  |             n|j                  |	_        |*|	j                  j%                  | j                  |             |ot!        t#        |D cg c]  }||vr|
 c}            }|rt        j                  dd| d      |	j                  D 
cg c]  }
|
j                  |vr|
 c}
|	_        |	S c c}
w c c}
w c c}}w c c}
w c c}
w c c}
w c c}w c c}
w )	aH  Parses the update request for a Backup Plan.

    Args:
      description: The description of the Backup Plan.
      new_backup_rules_from_file: The backup rules to update from file in the
        Backup Plan.
      update_backup_rules: The backup rules to update in the Backup Plan.
      add_backup_rules: The backup rules to add to the Backup Plan.
      remove_backup_rules: The backup rules to remove from the Backup Plan.
      current_backup_plan: The current Backup Plan.
      log_retention_days: The log retention days of the Backup Plan.
      max_custom_on_demand_retention_days: The custom on demand retention days
        limit of the Backup Plan.

    Returns:
      The updated Backup Plan.

    Raises:
      InvalidArgumentException: If the backup rules are invalid.
      ValueError: If the backup plan is not found.
    zCould not find the backup plan.)r@   r   r   zRules z+ found in more than one --backup-rule flag.z` not found in the backup plan. The --backup-rule flag can only be used to modify existing rules.z not found in the backup plan.)
ValueErrorr#   rK   r@   rL   rR   rS   rT   rQ   r    r>   collectionsCounterrN   r   InvalidArgumentExceptionlistsetextend)r   rL   new_backup_rules_from_fileupdate_backup_rulesadd_backup_rulesremove_backup_rulescurrent_backup_planrZ   r[   updated_backup_planrulecurrent_rule_idsrule_idsrule_idcountduplicate_rule_idsnot_found_rule_idsupdate_rule_idss                     r   ParseUpdatezBackupPlansClient.ParseUpdate   s6   @ "899--22(55 3  (3%%-?**6
1
2 8 1D0O0OP0O0OP!-(,(>(>
$)% ! &$$':
;':t4	?':
;h )1(8(8nguEAI'(8   
11'( )# #
 	

  )%)d)_$44 y/)% !	 

 
11'( ) 
 	
 6II5HTi5HoI *55)5d[[/ 5)%
 %%,,

 
 !4
5 )<(G(G%#%%,,

 
 !1
2 &,%,g,, ,% !	 

 
11'((FG
 	

 /::):4[[ 33 :)% { Q <% J)%)s6   &I$+I)I.I.I4I9*I>JJc                     t        j                         }| j                  j                  ||j	                         ||      }| j
                  j                  |      S )zUpdates a Backup Plan.

    Args:
      resource: The Backup Plan resource.
      backup_plan: The updated Backup Plan.
      update_mask: The update mask to edit the Backup Plan.

    Returns:
      The updated Backup Plan.
    )rF   ra   	requestId
updateMask)command_utilGenerateRequestIdr#   0BackupdrProjectsLocationsBackupPlansPatchRequestrI   r   Patch)r   rW   r]   update_mask
request_idr^   s         r   UpdatezBackupPlansClient.Update  sX     //1JmmLL""$	 M G <<g&&r   c                     | j                   j                  |j                               }| j                  j	                  |      S )z}Deletes a Backup Plan.

    Args:
      resource: The Backup Plan resource.

    Returns:
      The deleted Backup Plan.
    r`   )r#   1BackupdrProjectsLocationsBackupPlansDeleteRequestrI   r   Deleterd   s      r   r   zBackupPlansClient.Delete  sA     mmMM""$ N G <<w''r   )__name__
__module____qualname____doc__r   r>   rV   re   r|   r   r   __classcell__)r   s   @r   r
   r
      s-    ">0 d8(t%jX'((r   r
   )r   
__future__r   r   r   rh   googlecloudsdk.api_lib.backupdrr   googlecloudsdk.callioper   #googlecloudsdk.command_lib.backupdrr   BackupDrClientBaser
    r   r   <module>r      s3    ! &  '  0 . D
M(// M(r   