Module: Aptible::CLI::Subcommands::BackupRetentionPolicy

Included in:
Agent
Defined in:
lib/aptible/cli/subcommands/backup_retention_policy.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/aptible/cli/subcommands/backup_retention_policy.rb', line 9

def self.included(thor)
  thor.class_eval do
    include Helpers::Environment
    include Term::ANSIColor

    desc 'backup_retention_policy [ENVIRONMENT_HANDLE]',
         'Show the current backup retention policy for the environment'
    define_method 'backup_retention_policy' do |env|
       = ensure_environment(environment: env)
      policy = .backup_retention_policies.first
      unless policy
        raise Thor::Error, "Environment #{env} does not have a " \
                           'custom backup retention policy'
      end

      Formatter.render(Renderer.current) do |root|
        root.object do |node|
          ResourceFormatter.inject_backup_retention_policy(
            node, policy, 
          )
        end
      end
    end

    desc 'backup_retention_policy:set [ENVIRONMENT_HANDLE] ' \
         '[--daily DAILY_BACKUPS] [--monthly MONTHLY_BACKUPS] ' \
         '[--yearly YEARLY_BACKUPS] [--make-copy|--no-make-copy] ' \
         '[--keep-final|--no-keep-final] [--force]',
         "Change the environment's backup retention policy"
    option :daily, type: :numeric,
                   desc: 'Number of daily backups to retain'
    option :monthly, type: :numeric,
                     desc: 'Number of monthly backups to retain'
    option :yearly, type: :numeric,
                    desc: 'Number of yearly backups to retain'
    option :make_copy, type: :boolean,
                       desc: 'If backup copies should be created'
    option :keep_final,
           type: :boolean,
           desc: 'If final backups should be kept when databases are ' \
                 'deprovisioned'
    option :force,
           type: :boolean,
           desc: 'Do not prompt for confirmation if the new policy ' \
                 'retains fewer backups than the current policy'
    define_method 'backup_retention_policy:set' do |env|
      if options.empty?
        raise Thor::Error,
              'Please specify at least one attribute to change'
      end

       = ensure_environment(environment: env)
      current_policy = .backup_retention_policies.first

      # If an attribute isn't provided, use the value from the current
      # policy
      attrs = {}
      %i(daily monthly yearly make_copy keep_final).each do |a|
        opt = options[a]
        attrs[a] = opt.nil? ? current_policy.try(a) : opt
      end

      # If any of the attribues are missing, raise an error so that
      # we're not relying on the server's defaults
      if attrs.values.any?(&:nil?)
        raise Thor::Error, "Environment #{env} does not have a " \
                           'custom backup retention policy. Please ' \
                           'specify all attributes to create one.'
      end

      # Check if the number of backups over any period have been reduced
      do_confirm = %i(daily monthly yearly).any? do |a|
        cur = current_policy.try(a)
        next if cur.nil?
        cur > attrs[a]
      end

      # Check if any of the boolean fields have been disabled
      do_confirm ||= %i(make_copy keep_final).any? do |a|
        cur = current_policy.try(a)
        next if cur.nil?
        cur && !attrs[a]
      end

      if do_confirm && !options[:force]
        m = 'The specified backup retention policy retains fewer ' \
            "backups than the environment's current policy. This may " \
            'result in the deletion of existing, automated backups ' \
            "if they exceed the new policy's rules and it may " \
            "violate your company's internal compliance controls. " \
            'For more information, see https://www.aptible.com/docs' \
            '/core-concepts/managed-databases/managing-databases' \
            '/database-backups.'

        m = set_color(m, :yellow)
        print_wrapped m
        confirmation = yes?('Do you want to proceed [y/N]:')
        puts ''
        raise Thor::Error, 'Aborting' unless confirmation
      end

      new_policy = .create_backup_retention_policy!(**attrs)

      Formatter.render(Renderer.current) do |root|
        root.object do |node|
          ResourceFormatter.inject_backup_retention_policy(
            node, new_policy.reload, 
          )
        end
      end
    end
  end
end