Class: Brightbox::Image

Inherits:
Api
  • Object
show all
Defined in:
lib/brightbox-cli/images.rb

Instance Attribute Summary

Attributes inherited from Api

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Api

#attributes, cache_all!, cached_get, conn, #created_on, #exists?, find, find_all_or_warn, find_by_handle, find_or_call, #fog_attributes, #fog_model, #initialize, klass_name, #method_missing, #respond_to_missing?, #to_s

Constructor Details

This class inherits a constructor from Brightbox::Api

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Brightbox::Api

Class Method Details

.allObject



5
6
7
# File 'lib/brightbox-cli/images.rb', line 5

def self.all
  conn.images
end

.default_field_orderObject



18
19
20
# File 'lib/brightbox-cli/images.rb', line 18

def self.default_field_order
  %i[id owner type created_on status size arch name]
end

.filter_images(images, options = {}) ⇒ Object

Filter out images that are not of the right type, account or status if the option is passed



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
# File 'lib/brightbox-cli/images.rb', line 23

def self.filter_images(images, options = {})
  # Remove images that don't match the given type
  if options[:t]
    images.select! { |i| i.type == options[:t] }
  end

  # Remove statuses that don't match the argument
  if options[:s]
    images.select! { |i| i.status == options[:s] }
  end

  # Remove images that don't belong to the specified owner id
  if options[:l]
    if options[:l] == "brightbox"
      images.select!(&:official)
    else
      images.select! { |i| i.owner_id == options[:l] }
    end
  end

  unless options[:a]
     = Account.conn.(:nested => false)
    images.reject! { |i| !i.official && i.owner_id != ["id"] }
  end

  snapshots = images.select { |i| i.source_type == "snapshot" }
  images -= snapshots

  images.sort! { |a, b| a.default_sort_fields <=> b.default_sort_fields }
  snapshots.sort! { |a, b| a.created_at <=> b.created_at }
  images + snapshots
end

.get(id) ⇒ Object



9
10
11
# File 'lib/brightbox-cli/images.rb', line 9

def self.get(id)
  conn.images.get(id)
end

.register(options = {}) ⇒ Object



13
14
15
16
# File 'lib/brightbox-cli/images.rb', line 13

def self.register(options = {})
  image = conn.create_image(options)
  find image["id"]
end

.require_account?Boolean

Returns:

  • (Boolean)


3
# File 'lib/brightbox-cli/images.rb', line 3

def self.require_account?; true; end

Instance Method Details

#default_sort_fieldsObject



114
115
116
117
118
119
120
121
122
# File 'lib/brightbox-cli/images.rb', line 114

def default_sort_fields
  [
    official ? 0 : 1,
    name,
    arch,
    status_sort_code,
    - created_at.to_i
  ]
end

#public?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/brightbox-cli/images.rb', line 99

def public?
  public
end

#statusObject



70
71
72
73
74
75
76
77
78
# File 'lib/brightbox-cli/images.rb', line 70

def status
  if fog_attributes[:status] == "available"
    public? ? "public" : "private"
  elsif fog_attributes[:status] == "deprecated"
    public? ? "deprecated" : "private"
  else
    fog_attributes[:status]
  end
end

#status_sort_codeObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/brightbox-cli/images.rb', line 103

def status_sort_code
  case fog_attributes[:status]
  when "available"
    (public? ? 1 : 2)
  when "deprecated"
    3
  else
    4
  end
end

#to_rowObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/brightbox-cli/images.rb', line 80

def to_row
  fog_attributes.merge(
    id: fog_model.id,
    status: status,
    locked: locked?,
    username: username,
    arch: arch,
    name: name.to_s,
    owner: official ? "brightbox" : owner_id,
    type: type,
    created_at: created_at,
    created_on: created_on,
    description: description,
    licence_name: licence_name,
    size: virtual_size,
    min_ram: min_ram
  )
end

#typeObject



62
63
64
65
66
67
68
# File 'lib/brightbox-cli/images.rb', line 62

def type
  if official
    "official"
  else
    source_type
  end
end

#update(options) ⇒ Object



56
57
58
59
60
# File 'lib/brightbox-cli/images.rb', line 56

def update(options)
  self.class.conn.update_image(id, options)
  reload
  self
end