Class: BasePathMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/internal/app/functions/jets/base_path_mapping.rb

Instance Method Summary collapse

Constructor Details

#initialize(event, stage_name) ⇒ BasePathMapping

Returns a new instance of BasePathMapping.



5
6
7
8
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 5

def initialize(event, stage_name)
  @event, @stage_name = event, stage_name
  aws_config_update!
end

Instance Method Details

#apigatewayObject



93
94
95
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 93

def apigateway
  @apigateway ||= Aws::APIGateway::Client.new(aws_options)
end

#aws_config_update!Object

Override the AWS retry settings. The aws-sdk-core has expondential backup with this formula:

2 ** c.retries * c.config.retry_base_delay

So the max delay will be 2 ** 7 * 0.6 = 76.8s

Useful links:

https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-core/lib/aws-sdk-core/plugins/retry_errors.rb
https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html


21
22
23
24
25
26
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 21

def aws_config_update!
  Aws.config.update(
    retry_limit: 7, # default: 3
    retry_base_delay: 0.6, # default: 0.3
  )
end

#aws_optionsObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 101

def aws_options
  options = {
    retry_limit: 7, # default: 3
    retry_base_delay: 0.6, # default: 0.3
  }
  options.merge!(
    log_level: :debug,
    logger: Logger.new($stdout),
  ) if ENV['JETS_DEBUG_AWS_SDK']
  options
end

#base_pathObject



75
76
77
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 75

def base_path
  @base_path ||= parameter_value('BasePath') || ''
end

#cfnObject



97
98
99
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 97

def cfn
  @cfn ||= Aws::CloudFormation::Client.new(aws_options)
end

#createObject



54
55
56
57
58
59
60
61
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 54

def create
  apigateway.create_base_path_mapping(
    domain_name: domain_name, # required
    base_path: base_path,
    rest_api_id: rest_api_id, # required
    stage: @stage_name,
  )
end

#delete(fail_silently = false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 41

def delete(fail_silently=false)
  apigateway.delete_base_path_mapping(
    domain_name: domain_name, # required
    base_path: base_path.empty? ? '(none)' : base_path,
  )
# https://github.com/tongueroo/jets/issues/255
# Used to return: Aws::APIGateway::Errors::NotFoundException
# Now returns: Aws::APIGateway::Errors::InternalFailure
# So we'll use a more generic error
rescue Aws::APIGateway::Errors::ServiceError => e
  raise(e) unless fail_silently
end

#deleting_parent?Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 84

def deleting_parent?
  stack = cfn.describe_stacks(stack_name: parent_stack_name).stacks.first
  stack.stack_status == 'DELETE_IN_PROGRESS'
end

#deployment_stackObject



63
64
65
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 63

def deployment_stack
  @deployment_stack ||= cfn.describe_stacks(stack_name: @event['StackId']).stacks.first
end

#domain_nameObject



71
72
73
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 71

def domain_name
  @domain_name ||= parameter_value('DomainName')
end

#parameter_value(parameter_key) ⇒ Object



79
80
81
82
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 79

def parameter_value(parameter_key)
  param = deployment_stack[:parameters].find { |p| p.parameter_key == parameter_key }
  param&.parameter_value # possible for this to be nil when removing the config: IE: config.domain.name = nil
end

#parent_stack_nameObject



89
90
91
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 89

def parent_stack_name
  deployment_stack[:root_id]
end

#rest_api_idObject



67
68
69
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 67

def rest_api_id
  @rest_api_id ||= parameter_value('RestApi')
end

#should_delete?Boolean

Dont delete the newly created base path mapping unless this is an operation where we’re fully deleting the stack

Returns:

  • (Boolean)


37
38
39
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 37

def should_delete?
  deleting_parent?
end

#updateObject

Cannot use update_base_path_mapping to update the base_mapping because it doesnt allow us to change the rest_api_id. So we delete and create.



30
31
32
33
# File 'lib/jets/internal/app/functions/jets/base_path_mapping.rb', line 30

def update
  delete(true)
  create
end