File: volume.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (142 lines) | stat: -rw-r--r-- 3,992 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
54
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module Fog
  module AWS
    class Compute
      class Volume < Fog::Model
        identity  :id,                    :aliases => 'volumeId'

        attribute :attached_at,           :aliases => 'attachTime'
        attribute :availability_zone,     :aliases => 'availabilityZone'
        attribute :created_at,            :aliases => 'createTime'
        attribute :delete_on_termination, :aliases => 'deleteOnTermination'
        attribute :device
        attribute :encrypted
        attribute :key_id,                :aliases => ['KmsKeyId', 'kmsKeyId']
        attribute :iops
        attribute :server_id,             :aliases => 'instanceId'
        attribute :size
        attribute :snapshot_id,           :aliases => 'snapshotId'
        attribute :state,                 :aliases => 'status'
        attribute :tags,                  :aliases => 'tagSet'
        attribute :type,                  :aliases => 'volumeType'

        def initialize(attributes = {})
          # assign server first to prevent race condition with persisted?
          @server = attributes.delete(:server)
          super
        end

        def destroy
          requires :id

          service.delete_volume(id)
          true
        end

        def ready?
          state == 'available'
        end

        def modification_in_progress?
          modifications.any? { |m| m['modificationState'] != 'completed' }
        end

        def modifications
          requires :identity
          service.describe_volumes_modifications('volume-id' => self.identity).body['volumeModificationSet']
        end

        def save
          if identity
            update_params = {
              'Size'       => self.size,
              'Iops'       => self.iops,
              'VolumeType' => self.type
            }

            service.modify_volume(self.identity, update_params)
            true
          else
            requires :availability_zone
            requires_one :size, :snapshot_id

            requires :iops if type == 'io1'

            data = service.create_volume(availability_zone, size, create_params).body
            merge_attributes(data)

            if tags = self.tags
              # expect eventual consistency
              Fog.wait_for { service.volumes.get(identity) }
              service.create_tags(identity, tags)
            end

            attach(@server, device) if @server && device
          end

          true
        end

        def server
          requires :server_id
          service.servers.get(server_id)
        end

        def snapshots
          requires :id
          service.snapshots(:volume => self)
        end

        def snapshot(description)
          requires :id
          service.create_snapshot(id, description)
        end

        def force_detach
          detach(true)
        end

        def attach(new_server, new_device)
          if !persisted?
            @server = new_server
            self.availability_zone = new_server.availability_zone
          elsif new_server
            wait_for { ready? }
            @server = nil
            self.server_id = new_server.id
            service.attach_volume(server_id, id, new_device)
            reload
          end
        end

        def detach(force = false)
          @server = nil
          self.server_id = nil
          if persisted?
            service.detach_volume(id, 'Force' => force)
            reload
          end
        end

        def server=(_)
          raise NoMethodError, 'use Fog::AWS::Compute::Volume#attach(server, device)'
        end

        private

        def attachmentSet=(new_attachment_set)
          merge_attributes(new_attachment_set.first || {})
        end

        def create_params
          {
            'Encrypted'  => encrypted,
            'KmsKeyId'   => key_id,
            'Iops'       => iops,
            'SnapshotId' => snapshot_id,
            'VolumeType' => type
          }
        end
      end
    end
  end
end