class Jets::Resource::ApiGateway::DomainName

def cert_arn

def cert_arn
  Jets.config.domain.cert_arn
end

def definition

def definition
  properties = {
    domain_name: domain_name,
    endpoint_configuration: {
      types: endpoint_types
    }
  }
  # Can really only be REGIONAL or EDGE
  if endpoint_types.include?("REGIONAL")
    properties[:regional_certificate_arn] = cert_arn
  end
  if endpoint_types.include?("EDGE")
    properties[:certificate_arn] = cert_arn
  end
  {
    domain_name: {
      type: "AWS::ApiGateway::DomainName",
      properties: properties
    }
  }
end

def domain_name

def domain_name
  subdomain = Jets.project_namespace
  managed_domain_name = "#{subdomain}.#{Jets.config.domain.hosted_zone_name}"
  name = Jets.config.domain.name || managed_domain_name
  # Strip trailing period if there is one set accidentally or else get this error
  #   Trailing period should be omitted from domain name (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException
  name.sub(/\.$/,'')
end

def endpoint_types

def endpoint_types
  [Jets.config.domain.endpoint_type].flatten
end

def outputs

def outputs
  {
    "DomainName" => "!Ref DomainName",
  }
end