<commit_before>require 'delegate'

class Snapshot < SimpleDelegator
  class VolumeNotFoundError < StandardError; end
  class VolumeRetiredError < StandardError; end

  class << self
    def create(infra, volume_id, physical_id)
      ec2 = infra.ec2

      begin
        resp = ec2.create_snapshot(volume_id: volume_id)
        ec2.create_tags({resources: [resp.snapshot_id], tags: [{key: 'instance-id', value: physical_id}]})
      rescue Aws::EC2::Errors::IncorrectState => e
        raise VolumeRetiredError if e.message =~ /retired/
      rescue Aws::EC2::Errors::InvalidVolumeNotFound => e
        raise VolumeNotFoundError
      end

      new(infra, resp.snapshot_id)
    end
  end

  # ==== Args
  # [infra] {Infrastructure}
  # [snapshot_id] {String}
  # ==== return
  # {Snapshot}
  def initialize(infra, snapshot_id)
    snapshot = Aws::EC2::Snapshot.new(snapshot_id, client: infra.ec2)
    __setobj__(snapshot)
  end

  def create_volume
    # orig_volume = client.describe_volumes(volume_ids: [volume_id]).volumes.first
    client.create_volume(availability_zone: az, snapshot_id: snapshot_id)
  end

  def latest_status
    # owner_ids: ['self'] を指定しないと最新の state が降ってこないような気がする
    # パラメータによってレスポンスの state が異なることがある
    resp = client.describe_snapshots(
      owner_ids: ['self'],
      snapshot_ids: [snapshot_id],
      filters: [
        { name: 'volume-id', values: [volume_id] },
        { name: 'snapshot-id', values: [snapshot_id]}
      ]
    )

    resp.snapshots.first.state
  end

end
<commit_msg>raise with original message<commit_after>require 'delegate'

class Snapshot < SimpleDelegator
  class VolumeNotFoundError < StandardError; end
  class VolumeRetiredError < StandardError; end

  class << self
    def create(infra, volume_id, physical_id)
      ec2 = infra.ec2

      begin
        resp = ec2.create_snapshot(volume_id: volume_id)
        ec2.create_tags({resources: [resp.snapshot_id], tags: [{key: 'instance-id', value: physical_id}]})
      rescue Aws::EC2::Errors::IncorrectState => e
        raise VolumeRetiredError, e.message if e.message =~ /retired/
      rescue Aws::EC2::Errors::InvalidVolumeNotFound => e
        raise VolumeNotFoundError, e.message
      end

      new(infra, resp.snapshot_id)
    end
  end

  # ==== Args
  # [infra] {Infrastructure}
  # [snapshot_id] {String}
  # ==== return
  # {Snapshot}
  def initialize(infra, snapshot_id)
    snapshot = Aws::EC2::Snapshot.new(snapshot_id, client: infra.ec2)
    __setobj__(snapshot)
  end

  def create_volume
    # orig_volume = client.describe_volumes(volume_ids: [volume_id]).volumes.first
    client.create_volume(availability_zone: az, snapshot_id: snapshot_id)
  end

  def latest_status
    # owner_ids: ['self'] を指定しないと最新の state が降ってこないような気がする
    # パラメータによってレスポンスの state が異なることがある
    resp = client.describe_snapshots(
      owner_ids: ['self'],
      snapshot_ids: [snapshot_id],
      filters: [
        { name: 'volume-id', values: [volume_id] },
        { name: 'snapshot-id', values: [snapshot_id]}
      ]
    )

    resp.snapshots.first.state
  end

end

{'fraction_non_alphanumeric': 0.11298701298701298, 'fraction_numerical': 0.005194805194805195, 'mean_word_length': 3.4267241379310347, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4854748', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1061, 'n_words': 213}