Class: ItamaeMitsurin::Resource::AwsEbsVolume

Inherits:
Base
  • Object
show all
Defined in:
lib/itamae-mitsurin/resource/aws_ebs_volume.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes, #current_attributes, #notifications, #recipe, #resource_name, #subscriptions, #updated

Instance Method Summary collapse

Methods inherited from Base

#action_nothing, define_attribute, inherited, #initialize, #resource_type, #run

Constructor Details

This class inherits a constructor from ItamaeMitsurin::Resource::Base

Instance Method Details

#action_attach(options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/itamae-mitsurin/resource/aws_ebs_volume.rb', line 55

def action_attach(options)
  ec2 = ::Aws::EC2::Client.new(region: attributes.region)
  volumes = ec2.describe_volumes({
    filters: [
      {
        name: 'tag:Name',
        values: [ attributes.name ],
      },
    ],
  }).volumes

  unless volumes.empty?
    if volumes[0][:attachments].empty?
      @volume = ec2.attach_volume({
        volume_id: @volume.volume_id,
        instance_id: attributes.instance_id,
        device: attributes.device
      })
      ec2.wait_until(:volume_in_use, volume_ids: [ @volume.volume_id ])

      ItamaeMitsurin.logger.color(:green) do
        ItamaeMitsurin.logger.info "attach volume compleated!"
      end
      updated!
    end
  else
    @volume = volumes[0]
  end
end

#action_create(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/itamae-mitsurin/resource/aws_ebs_volume.rb', line 17

def action_create(options)
  ec2 = ::Aws::EC2::Client.new(region: attributes.region)
  volumes = ec2.describe_volumes({
    filters: [
      {
        name: 'tag:Name',
        values: [ attributes.name ],
      },
    ],
  }).volumes

  if volumes.empty?
    @volume = ec2.create_volume(
      size: attributes[:size], # attributes.size returns the size of attributes hash
      availability_zone: attributes.availability_zone,
      volume_type: attributes.volume_type,
    )
    ec2.wait_until(:volume_available, volume_ids: [ @volume.volume_id ])

    ec2.create_tags({
      resources: [ @volume.volume_id ],
      tags: [
        {
          key: 'Name',
          value: attributes.name,
        },
      ],
    })

    ItamaeMitsurin.logger.color(:green) do
      ItamaeMitsurin.logger.info "create volume compleated!"
    end
    updated!
  else
    @volume = volumes[0]
  end
end