Class: SFRest::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrest/update.rb

Overview

Drive updates on the Site Factory

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Update

Returns a new instance of Update.

Parameters:



7
8
9
# File 'lib/sfrest/update.rb', line 7

def initialize(conn)
  @conn = conn
end

Instance Method Details

#change_updater_start_time(updater_id, new_update_time) ⇒ Object

Changes the updater start time.



110
111
112
113
114
115
116
117
# File 'lib/sfrest/update.rb', line 110

def change_updater_start_time(updater_id, new_update_time)
  current_path = '/api/v1/update/change_time'
  payload = {
    'updater_id' => updater_id,
    'new_update_time' => new_update_time
  }.to_json
  @conn.post(current_path, payload)
end

#list_vcs_refs(type = 'sites', stack_id = 1) ⇒ Object

Lists vcs refs.



28
29
30
31
# File 'lib/sfrest/update.rb', line 28

def list_vcs_refs(type = 'sites', stack_id = 1)
  current_path = "/api/v1/vcs?type=#{type}&stack_id=#{stack_id}"
  @conn.get(current_path)
end

#modify_status(site_creation, site_duplication, domain_management, bulk_operations) ⇒ Object

Modifies the status information.



18
19
20
21
22
23
24
25
# File 'lib/sfrest/update.rb', line 18

def modify_status(site_creation, site_duplication, domain_management, bulk_operations)
  current_path = '/api/v1/status'
  payload = { 'site_creation' => site_creation,
              'site_duplication' => site_duplication,
              'domain_management' => domain_management,
              'bulk_operations' => bulk_operations }.to_json
  @conn.put(current_path, payload)
end

#multi_stack?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/sfrest/update.rb', line 99

def multi_stack?
  @multi_stack ||= @conn.codebase.list['stacks'].size > 1
end

#pause_updateObject

Pauses current update.



79
80
81
82
83
# File 'lib/sfrest/update.rb', line 79

def pause_update
  current_path = '/api/v1/update/pause'
  payload = { 'pause' => true }.to_json
  @conn.post(current_path, payload)
end

#resume_updateObject

Resumes current update.



86
87
88
89
90
# File 'lib/sfrest/update.rb', line 86

def resume_update
  current_path = '/api/v1/update/pause'
  payload = { 'pause' => false }.to_json
  @conn.post(current_path, payload)
end

#start_update(ref) ⇒ Object

Starts an update.



34
35
36
37
38
39
40
41
# File 'lib/sfrest/update.rb', line 34

def start_update(ref)
  if update_version == 'v2'
    raise InvalidApiVersion, 'There is more than one codebase use sfrest.update.update directly.'
  end

  update_data = { scope: 'sites', sites_type: 'code, db', sites_ref: ref }
  update(update_data)
end

#status_infoObject

Gets the status information.



12
13
14
15
# File 'lib/sfrest/update.rb', line 12

def status_info
  current_path = '/api/v1/status'
  @conn.get(current_path)
end

#terminate_updater(update_id) ⇒ Object

Terminates an updater.



104
105
106
107
# File 'lib/sfrest/update.rb', line 104

def terminate_updater(update_id)
  current_path = "/api/v1/update/#{update_id}"
  @conn.delete(current_path)
end

#update(datum) ⇒ Object

Starts an update. The rest api supports the following scope: sites|factory|both (defaults to ‘sites’) start_time: sites_type: code|code, db| code, db, registry (defaults to ‘code, db’) factory_type: code|code, db (defaults to ‘code, db’) sites_ref: factory_ref: This method does not filter or validate so that it can be used for negative cases. (missing data)



52
53
54
55
56
57
# File 'lib/sfrest/update.rb', line 52

def update(datum)
  validate_request datum
  current_path = "/api/#{update_version}/update"
  payload = datum.to_json
  @conn.post(current_path, payload)
end

#update_listObject

Gets the list of updates.



67
68
69
70
# File 'lib/sfrest/update.rb', line 67

def update_list
  current_path = '/api/v1/update'
  @conn.get(current_path)
end

#update_progress(update_id) ⇒ Object

Gets the progress of an update by id.



73
74
75
76
# File 'lib/sfrest/update.rb', line 73

def update_progress(update_id)
  current_path = "/api/v1/update/#{update_id}/status"
  @conn.get(current_path)
end

#update_versionObject

Determines the api version to use for updates. it is possible for there to be two codebases and not have a v2 endpoint.



95
96
97
# File 'lib/sfrest/update.rb', line 95

def update_version
  multi_stack? && v2_endpoint? ? 'v2' : 'v1'
end

#v2_endpoint?Boolean

Determines if the v2 endpoint exists. A factory with the endpoint will raise an SFRest::BadRequestError A factory without the endpoint will raise SFRest::InvalidResponse

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sfrest/update.rb', line 122

def v2_endpoint?
  return @has_v2_endpoint unless @has_v2_endpoint.nil?

  begin
    @conn.post '/api/v2/update', '{}'
  rescue SFRest::BadRequestError
    @has_v2_endpoint = true
  rescue SFRest::InvalidResponse => e
    return @has_v2_endpoint = false if e.message =~ /Invalid data, status 404/

    raise e
  end
end

#validate_request(datum) ⇒ Object

Raises:



59
60
61
62
63
64
# File 'lib/sfrest/update.rb', line 59

def validate_request(datum)
  v1_keys = %i[scope sites_ref factory_ref sites_type factory_type db_update_arguments]
  v2_keys = %i[sites factory]
  key_overlap = binding.local_variable_get("#{update_version}_keys") & datum.keys
  raise InvalidDataError, "An invalid stucture was passed to the #{update_version} endpoint" if key_overlap.empty?
end