Class: Brightbox::CollaboratingAccount

Inherits:
Account
  • Object
show all
Defined in:
lib/brightbox-cli/collaborating_account.rb

Overview

A Collaborating account combines all of a users own accounts and those that they have access to via an open collaboration.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Account

#cloud_ip_limit, get, #to_s

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, klass_name, #method_missing, require_account?, #respond_to_missing?, #to_s

Constructor Details

#initialize(fog_model, collaboration = nil) ⇒ CollaboratingAccount

Simpler initialiser than the superclass.



43
44
45
46
47
48
49
50
51
52
# File 'lib/brightbox-cli/collaborating_account.rb', line 43

def initialize(fog_model, collaboration = nil)
  @fog_model = fog_model
  @id = fog_model.id
  # Rather than merging, we have store the collaboration as a secondary item
  @collaboration = collaboration

  return unless @collaboration.nil? && @fog_model.attributes["resource_type"] == "collaboration"

  @collaboration = @fog_model
end

Dynamic Method Handling

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

Class Method Details

.allObject



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

def self.all
  accounts = conn.accounts.all
  collaborations = conn.user_collaborations.all

  accepted_collaborations = collaborations.select do |col|
    col.status == "accepted"
  end

  pending_collaborations = collaborations.select do |col|
    col.status == "pending"
  end

  col_accounts = []

  accepted_collaboration_ids = accepted_collaborations.map(&:account_id)
  accounts.each do |acc|
    if accepted_collaboration_ids.include?(acc.id)
      collab = accepted_collaborations.find { |col| col. == acc.id }
      col_accounts << new(acc, collab)
    else
      col_accounts << new(acc)
    end
  end

  pending_collaborations.each do |col|
    col_accounts << new(col)
  end

  col_accounts
end

.default_field_orderObject



135
136
137
# File 'lib/brightbox-cli/collaborating_account.rb', line 135

def self.default_field_order
  %i[id cloud_ips_limit lb_limit ram_limit ram_used ram_free role name]
end

Instance Method Details

#account?Boolean

Is this record based on an account?



55
56
57
# File 'lib/brightbox-cli/collaborating_account.rb', line 55

def account?
  resource_type == "account"
end

#cloud_ips_limitObject



80
81
82
# File 'lib/brightbox-cli/collaborating_account.rb', line 80

def cloud_ips_limit
  account? ? attributes[:cloud_ips_limit] : ""
end

#collaboration?Boolean

Is this record based on a collaboration?



60
61
62
# File 'lib/brightbox-cli/collaborating_account.rb', line 60

def collaboration?
  resource_type == "collaboration"
end

#idObject



68
69
70
71
72
73
74
# File 'lib/brightbox-cli/collaborating_account.rb', line 68

def id
  if collaboration?
    
  else
    @id
  end
end

#lb_limitObject



84
85
86
# File 'lib/brightbox-cli/collaborating_account.rb', line 84

def lb_limit
  account? ? attributes[:load_balancers_limit] : ""
end

#nameObject



76
77
78
# File 'lib/brightbox-cli/collaborating_account.rb', line 76

def name
  account? ? fog_model.name : acc_details(:name)
end

#ram_freeObject



96
97
98
99
100
101
102
# File 'lib/brightbox-cli/collaborating_account.rb', line 96

def ram_free
  if account?
    [ram_limit.to_i - ram_used.to_i, 0].max
  else
    ""
  end
end

#ram_limitObject



88
89
90
# File 'lib/brightbox-cli/collaborating_account.rb', line 88

def ram_limit
  account? ? attributes[:ram_limit] : ""
end

#ram_usedObject



92
93
94
# File 'lib/brightbox-cli/collaborating_account.rb', line 92

def ram_used
  account? ? attributes[:ram_used] : ""
end

#resource_typeObject



64
65
66
# File 'lib/brightbox-cli/collaborating_account.rb', line 64

def resource_type
  attributes[:resource_type] || attributes["resource_type"]
end

#roleObject

The “role” between the requesting user and the account

It is either “owner”, “collaborator” or “(invited)”

You’d think it would be somehow related to the role but no.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/brightbox-cli/collaborating_account.rb', line 110

def role
  if @collaboration
    if @collaboration.attributes[:status] == "accepted"
      "collaborator"
    else
      "(invited)"
    end
  else
    "owner"
  end
end

#to_rowObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/brightbox-cli/collaborating_account.rb', line 122

def to_row
  {
    :id => id,
    :cloud_ips_limit => cloud_ips_limit,
    :lb_limit => lb_limit,
    :ram_limit => ram_limit,
    :ram_used => ram_used,
    :ram_free => ram_free,
    :role => role,
    :name => name
  }
end