summaryrefslogtreecommitdiff
path: root/app/controllers/admin/serverless/domains_controller.rb
blob: 1d4f10e033f6d56491d58c610435c9813c28829b (plain)
1
2
3
4
5
6
7
8
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
# frozen_string_literal: true

class Admin::Serverless::DomainsController < Admin::ApplicationController
  before_action :check_feature_flag
  before_action :domain, only: [:update, :verify, :destroy]

  def index
    @domain = PagesDomain.instance_serverless.first_or_initialize
  end

  def create
    if PagesDomain.instance_serverless.exists?
      return redirect_to admin_serverless_domains_path, notice: _('An instance-level serverless domain already exists.')
    end

    @domain = PagesDomain.instance_serverless.create(create_params)

    if @domain.persisted?
      redirect_to admin_serverless_domains_path, notice: _('Domain was successfully created.')
    else
      render 'index'
    end
  end

  def update
    if domain.update(update_params)
      redirect_to admin_serverless_domains_path, notice: _('Domain was successfully updated.')
    else
      render 'index'
    end
  end

  def destroy
    if domain.serverless_domain_clusters.exists?
      return redirect_to admin_serverless_domains_path,
                         status: :conflict,
                         notice: _('Domain cannot be deleted while associated to one or more clusters.')
    end

    domain.destroy!

    redirect_to admin_serverless_domains_path,
                status: :found,
                notice: _('Domain was successfully deleted.')
  end

  def verify
    result = VerifyPagesDomainService.new(domain).execute

    if result[:status] == :success
      flash[:notice] = _('Successfully verified domain ownership')
    else
      flash[:alert] = _('Failed to verify domain ownership')
    end

    redirect_to admin_serverless_domains_path
  end

  private

  def domain
    @domain = PagesDomain.instance_serverless.find(params[:id])
  end

  def check_feature_flag
    render_404 unless Feature.enabled?(:serverless_domain)
  end

  def update_params
    params.require(:pages_domain).permit(:user_provided_certificate, :user_provided_key)
  end

  def create_params
    params.require(:pages_domain).permit(:domain, :user_provided_certificate, :user_provided_key)
  end
end