
    G*                     d   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j.                  j0                  ej.                  j2                         G d dej4                                      Zy)zUpdates a new Backup Plan.    )absolute_import)division)unicode_literalsN)
exceptions)backup_plans)util)base)flags)log)yamlc                       e Zd ZdZddddZedej                  fd       Z G d d	e	j                        Zd
 ZdefdZy)UpdatezUpdate a specific backup plan.zUpdate a specific backup planz          Update a specific backup plan in the project. It can only be updated in regions supported by the Backup and DR Service.
      a          To update 2 backup rules and description of an existing backup plan ``sample-backup-plan''
        in project ``sample-project'',
        at location ``us-central1'':

        run:

          $ {command} sample-backup-plan --project=sample-project --location=us-central1
            --backup-rule <BACKUP-RULE>
            --backup-rule <BACKUP-RULE>
            --description "This is a sample backup plan"

        To add backup rules to an existing backup plan ``sample-backup-plan''
        in project ``sample-project'',
        at location ``us-central1'':

        run:

          $ {command} sample-backup-plan --project=sample-project --location=us-central1
            --add-backup-rule <BACKUP-RULE>
            --add-backup-rule <BACKUP-RULE>

        To remove a backup rule with id ``sample-daily-rule'' from an existing backup plan ``sample-backup-plan''
        in project ``sample-project'',
        at location ``us-central1'':

        run:

          $ {command} sample-backup-plan --project=sample-project --location=us-central1
            --remove-backup-rule sample-daily-rule

        To override backup rules in an existing backup plan ``sample-backup-plan''
        in project ``sample-project'',
        at location ``us-central1'', pass a file path containing backup rules in YAML or JSON format:
        This flag is mutually exclusive with --add-backup-rule, --remove-backup-rule and --backup-rule flags.

        run:
          $ {command} sample-backup-plan --project=sample-project --location=us-central1
            --backup-rules-fom-file <FILE_PATH>

        Backup Rule Examples:

        1. Hourly backup rule with hourly backup frequency of 6 hours and store it for 30 days, and expect the backups to run only between 10:00 to 20:00 UTC

        <BACKUP-RULE>: rule-id=sample-hourly-rule,retention-days=30,recurrence=HOURLY,hourly-frequency=6,time-zone=UTC,backup-window-start=10,backup-window-end=20

        Properties:
          -- rule-id = "sample-hourly-rule"
          -- retention-days = 30
          -- recurrence = HOURLY
          -- hourly-frequency = 6
          -- time-zone = UTC
          -- backup-window-start = 10
          -- backup-window-end = 20

        2. Daily backup rule with daily backup frequency of 6 hours and store it for 7 days

        <BACKUP-RULE>: rule-id=sample-daily-rule,retention-days=7,recurrence=DAILY,backup-window-start=1,backup-window-end=14

        Properties:
          -- rule-id = "sample-daily-rule"
          -- retention-days = 7
          -- recurrence = DAILY
          -- backup-window-start = 1
          -- backup-window-end = 14

        3. Weekly backup rule with weekly backup frequency on every MONDAY & FRIDAY and store it for 21 days

        <BACKUP-RULE>: rule-id=sample-weekly-rule,retention-days=21,recurrence=WEEKLY,days-of-week="MONDAY FRIDAY",backup-window-start=10,backup-window-end=20

        Properties:
          -- rule-id = "sample-weekly-rule"
          -- retention-days: 21
          -- recurrence = WEEKLY
          -- days-of-week = "MONDAY FRIDAY"
          -- backup-window-start = 10
          -- backup-window-end = 20

        YAML and JSON file examples:

        YAML file example:

        ```
        backup-rules:
        - rule-id: weekly-rule
          retention-days: 7
          recurrence: WEEKLY
          backup-window-start: 0
          backup-window-end: 23
          days-of-week: [MONDAY, TUESDAY]
          time-zone: UTC
        - rule-id: daily-rule
          retention-days: 1
          recurrence: DAILY
          backup-window-start: 1
          backup-window-end: 24
          time-zone: UTC
        ```

        JSON file example:
        ```
        {
          "backup-rules": [
            {
              "rule-id": "weekly-rule",
              "retention-days": 7,
              "recurrence": "WEEKLY",
              "backup-window-start": 0,
              "backup-window-end": 23,
              "days-of-week": ["MONDAY", "TUESDAY"],
              "time-zone": "UTC"
            },
            {
              "rule-id": "daily-rule",
              "retention-days": 1,
              "recurrence": "DAILY",
              "backup-window-start": 1,
              "backup-window-end": 24,
              "time-zone": "UTC"
            }
          ]
        }
        ```
        )BRIEFDESCRIPTIONEXAMPLESparserc                    t         j                  j                  |        t         j                  j                  | d       t	        j
                  | d       t	        j                  |        t	        j                  |        t	        j                  |        t	        j                  |        d}t	        j                  | |       t	        j                  |        t	        j                  |        y)zhSpecifies additional command flags.

    Args:
      parser: Parser object for command line inputs.
    TzkName of the backup plan to be updated.
        The name must be unique for a project and location.
        a          Provide a description of the backup plan, such as specific use cases and
        relevant details, in 2048 characters or less.

        E.g., This is a backup plan that performs a daily backup at 6 p.m. and
        retains data for 3 months.
        N)r	   
ASYNC_FLAGAddToParser
SetDefaultr
   AddBackupPlanResourceArgAddUpdateBackupRuleAddAddBackupRuleAddRemoveBackupRuleAddBackupRulesFromFileAddDescriptionAddLogRetentionDays!AddMaxCustomOnDemandRetentionDays)r   description_helps     ,lib/surface/backup_dr/backup_plans/update.pyArgszUpdate.Args   s     	OO'OOvt,	""	 
f%	6"	f%	  ( 
!12	f%	++F3    c                       e Zd ZdZy)Update.YamlOrJsonLoadErrorz(Error parsing YAML or JSON file content.N)__name__
__module____qualname____doc__ r"   r    YamlOrJsonLoadErrorr$      s    2r"   r*   c                     	 t        j                  |      }|j                  d      S # t        $ r&}| j	                  dj                  |            |d}~ww xY w)z.Parses the backup rules from the file content.zbackup-rulesz5Could not parse content in the backup rules file: {0}N)r   loadget	Exceptionr*   format)selfbackup_rules_file_contentbackup_rulesexcs       r    _GetBackupRulesFromFilezUpdate._GetBackupRulesFromFile   s[    YY89ln-- $$
A
H
H
Ms   %( 	A!AAreturnc           
         t        j                         }|j                  j                  j	                         }|j
                  }|j                  }|j                  }|j                  }|r|s|s|rt        j                  d      |j                  }|j                  }	|j                  }
	 |j                  |      }d}|r| j                  |      }|j!                  |	|||||||
      }g }|	 |	|j                  k7  r|j#                  d       | ||j$                  k7  r|j#                  d       |
 |
|j&                  k7  r|j#                  d       t)        ||||g      r|j#                  d       |j+                  ||dj-                  |            }|j:                  rPt=        j>                  |jA                         dd	t6        jB                  jE                  |jF                        
       |S |jI                  |jK                  |      d|jA                          d      }t=        j>                  |jA                         d       |S # t.        j0                  $ r)}t3        j4                  |t6        j8                        d}~ww xY w)zConstructs and sends request.

    Args:
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the .Args() method.

    Returns:
      ProcessHttpResponse of the request made.
    zq--backup-rules-from-file flag cannot be used with --backup-rule, --add-backup-rule or --remove-backup-rule flags.NdescriptionlogRetentionDaysmaxCustomOnDemandRetentionDaysbackupRules,zbackup planT)kindis_asyncdetailszUpdating backup plan [z/]. (This operation could take up to 2 minutes.))operation_refmessage)r<   )&r   BackupPlansClientCONCEPTSbackup_planParsebackup_rules_from_filebackup_ruleadd_backup_ruleremove_backup_rulecore_exceptionsErrorlog_retention_daysr7   #max_custom_on_demand_retention_daysDescriber4   ParseUpdateappendr8   r9   anyr   joinapitools_exceptions	HttpErrorr   HttpExceptionr   HTTP_ERROR_FORMATasync_r   UpdatedResourceRelativeNameASYNC_OPERATION_MESSAGEr/   nameWaitForOperationGetOperationRef)r0   argsclientrC   r1   update_backup_rulesadd_backup_rulesremove_backup_rulesrK   r7   rL   current_backup_plannew_backup_rules_from_fileupdated_backup_planupdate_mask	operationeresources                     r    Runz
Update.Run   s    ++-F--++113K $ ; ;**++11 /3F!!M 
 00""K00 (-@"OOK8#' 	"%)%A%A%&
" #..

$





-	 k

!0<<<=)

( $7$H$HH-.
-
91 ??@ 	;<	



$	 
	 	=)--
*CHH[,Ai {{	

"
"
$..55innE	 &&,,Y7$[%=%=%?$@ A6 6 ' H 002GO+ (( @$$Q(>(>??@s   +C(I
 
J$JJN)r%   r&   r'   r(   detailed_helpstaticmethodargparseArgumentParserr!   rI   rJ   r*   r4   rP   ri   r)   r"   r    r   r   "   sf     ' /
{A-F 48** 4 4<3O11 3a ar"   r   )r(   
__future__r   r   r   rl   apitools.base.pyr   rR   googlecloudsdk.api_lib.backupdrr   r   googlecloudsdk.api_lib.utilgooglecloudsdk.callioper	   #googlecloudsdk.command_lib.backupdrr
   googlecloudsdk.corerI   r   r   UniverseCompatibleReleaseTracksReleaseTrackALPHAGAUpdateCommandr   r)   r"   r    <module>r{      s    ! &  '  > 8 0 2 ( 5 = # $ D%%++T->->-A-ABST S C Sr"   