Class: Jets::Router::State

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices
Defined in:
lib/jets/router/state.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #aws_lambda, #aws_options, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

included

Class Method Details

.save_apigw_stateObject



6
7
8
9
10
11
12
# File 'lib/jets/router/state.rb', line 6

def self.save_apigw_state
  state = Jets::Router::State.new
  state.save("methods", Jets::Cfn::Builder::Api::Pages::Methods.pages)
  state.save("resources", Jets::Cfn::Builder::Api::Pages::Resources.pages)
  # To avoid API limits for calculating changed routes in next deploy
  state.save("routes", Jets::Router.routes)
end

Instance Method Details

#load(filename) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jets/router/state.rb', line 14

def load(filename)
  resp = nil
  begin
    resp = s3.get_object(
      bucket: Jets.aws.s3_bucket,
      key: s3_storage_path(filename),
    )
  rescue Aws::S3::Errors::NoSuchBucket
    # Allow JETS_TEMPLATES=1 jets build to work with no-bucket-yet
    return nil
  end
  text = resp.body.read
  JSON.load(text)
rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::PermanentRedirect
end

#s3_storage_path(filename) ⇒ Object

Examples:

jets/state/apigw/resources.json
jets/state/apigw/methods.json
jets/state/apigw/routes.json

Fetch or loaded in:

resources.json: Jets::Cfn::Builder::Api::Pages::Resources#old_pages
methods.json:   Jets::Cfn::Builder::Api::Pages::Methods#old_pages
routes.json:    Jets::Cfn::Resource::ApiGateway::RestApi::Routes::Change::Base#deployed_routes

Saved in:

Jets::Cfn::Ship#save_apigw_state


63
64
65
# File 'lib/jets/router/state.rb', line 63

def s3_storage_path(filename)
  "jets/state/apigw/#{filename}.json"
end

#save(filename, data) ⇒ Object

Save previously deployed APIGW routes state



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jets/router/state.rb', line 32

def save(filename, data)
  body = data.respond_to?(:to_json) ? data.to_json : JSON.generate(data)
  body = JSON.pretty_generate(JSON.parse(body)) # pretty generate
  if ENV['JETS_API_STATE_DEBUG'] # useful with jets build --templates
    puts "WARN: JETS_API_STATE_DEBUG=1 detected. Saving to tmp/#{filename}.json instead of S3.".color(:yellow)
    IO.write("tmp/#{filename}.json", body)
  else
    s3.put_object(
      body: body,
      bucket: Jets.aws.s3_bucket,
      key: s3_storage_path(filename),
    )
  end
end