Class: ZuoraConnect::StaticController

Inherits:
ApplicationController show all
Defined in:
app/controllers/zuora_connect/static_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#ldap_login

Instance Method Details

#healthObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/zuora_connect/static_controller.rb', line 10

def health
  if params[:error].present?
    begin
      raise ZuoraConnect::Exceptions::Error.new('This is an error')
    rescue => ex
      case params[:error]
      when 'Log'
        Rails.logger.error("Error in Health", ex)
      when 'Exception'
        raise
      end
    end
  end

  render json: {
    message: "Alive",
    status: 200
  }, status: 200
end

#initialize_appObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/zuora_connect/static_controller.rb', line 30

def initialize_app
  begin
    authenticate_connect_app_request
    unless performed?
      @appinstance.new_session(:session => @appinstance.data_lookup(:session => session))
      render json: {
        message: 'Success',
        status: 200
      }, status: 200
    end
  rescue => ex
    Rails.logger.error("Failed to Initialize application", ex)
    if performed?
      Rails.logger.error("Failed to Initialize application #{performed?}", ex)
    else
      render json: {
        message: "Failure initializing app instance",
        status: 500
      }, status: 500
    end
  end
end

#instance_dropObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/controllers/zuora_connect/static_controller.rb', line 121

def instance_drop
  host = request.headers.fetch("HOST", nil)
  if host.present? && ZuoraConnect::AppInstance::INTERNAL_HOSTS.include?(host)
    ZuoraConnect::AppInstance.read_master_db do
      instance_id = params[:id]
      @appinstance = ZuoraConnect::AppInstance.find(instance_id)
      if @appinstance.drop_instance
        ZuoraConnect::AppInstance.destroy(instance_id)
        msg = Apartment::Tenant.drop(instance_id)

        if msg.error_message.present?
          render json: { "message" => msg.error_message }, status: :bad_request
        else
          render json: {
            status: 200,
            message: 'Success',
            app_instance_id: instance_id
          }, status: 200
        end
      else
        render json: { "message" => @appinstance.drop_message }, status: :bad_request
      end
    end
  else
    render json: { "message" => "Host #{host} is not internal" }, status: :bad_request
  end
rescue StandardError => e
  message = 'Failed to drop instance'
  if performed?
    Rails.logger.error("#{message}: #{performed?}", e)
  else
    Rails.logger.error(message, e)
    render json: {
      status: 500,
      message: message
    }, status: 500
  end
end

#instance_userObject



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
# File 'app/controllers/zuora_connect/static_controller.rb', line 75

def instance_user
  ZuoraConnect::AppInstance.read_master_db do
    ZuoraConnect.logger.with_fields = {} if ZuoraConnect.logger.is_a?(Ougai::Logger)
    Rails.logger.with_fields = {} if Rails.logger.is_a?(Ougai::Logger)

    if defined?(ElasticAPM) && ElasticAPM.running? && ElasticAPM.respond_to?(:set_label)
      ElasticAPM.set_label(:trace_id, request.uuid)
    end

    unless params[:id].present?
      render json: {
        status: 400,
        message: 'No app instance id provided'
      }, status: :bad_request
      return
    end

    @appinstance = ZuoraConnect::AppInstance.find(params[:id]).new_session
  end

  zuora_client = @appinstance.send(ZuoraConnect::AppInstance::LOGIN_TENANT_DESTINATION).client
  client_describe, = zuora_client.rest_call(
    url: zuora_client.rest_endpoint('genesis/user/info').gsub('v1/', ''),
    session_type: zuora_client.class == ZuoraAPI::Oauth ? :bearer : :basic
  )

  render json: {
    status: 200,
    message: 'Success',
    user_id: client_describe['coreUserId'],
    username: client_describe['username'],
    email: client_describe['workEmail']
  }, status: 200
rescue ActiveRecord::RecordNotFound
  render json: {
    status: 400,
    message: 'No app instance found'
  }, status: :bad_request
rescue StandardError => e
  Rails.logger.error('Error occurred getting user details', e)
  render json: {
    status: 500,
    message: 'Failed to get user details'
  }, status: 500
end

#provisionObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/zuora_connect/static_controller.rb', line 53

def provision
  create_new_instance
  unless performed?
    render json: {
      status: 200,
      message: 'Success',
      app_instance_id: @appinstance.id
    }, status: 200
  end
rescue StandardError => e
  message = 'Failed to provision new instance'
  if performed?
    Rails.logger.error("#{message}: #{performed?}", e)
  else
    Rails.logger.error(message, e)
    render json: {
      status: 500,
      message: message
    }, status: 500
  end
end