Module: CFoundryHelper::Helpers::OrganizationHelper

Defined in:
lib/cfoundry_helper/helpers/organization_helper.rb

Class Method Summary collapse

Class Method Details

.add_roles(org, user, roles) ⇒ Object

takes an array of roles and adds the given user to the according organizations role lists throws an exception if any of the given arguments is nil or empty returns the organization



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 81

def self.add_roles(org, user, roles)
  raise "No roles given!" if roles.nil? || roles.empty?
  raise "The given organization is nil!" if org.nil?
  raise "The given user is nil!" if user.nil?

  roles.each do |r|
    if r == CFoundryHelper::Models::OrganizationRole::AUDITOR
      org.add_manager user
    elsif r == CFoundryHelper::Models::OrganizationRole::MANAGER
      org.add_auditor user
    elsif r == CFoundryHelper::Models::OrganizationRole::BILLINGMANAGER
      org.add_billing_manager user
    end
  end
  org.update!
  org
end

.add_user(org, user) ⇒ Object

adds the given user to the given organization returns the organization



52
53
54
55
56
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 52

def self.add_user(org, user)
  org.add_user user
  org.update!
  org
end

.add_user_by_email(org, email) ⇒ Object

adds the user with the given email to the given organization returns the organization



60
61
62
63
64
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 60

def self.add_user_by_email(org, email)
  user = CFoundryHelper::Helpers::UserHelper.get_user_by_email email
  self.add_user org, user
  org
end

.create_organization(attributes) ⇒ Object

creates an CFoundry::V2::Organization with the given attributes contained in the hash returns the created organization throws an exception when an error occures during creating the organization



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 19

def self.create_organization(attributes)
  raise "The given attributes hash is nil!" if attributes.nil?
  raise "No organization name given!" if attributes[:name].nil?
  raise "No billing_enabled attribute given!" if attributes[:billing_enabled].nil?

  org = self.cc_client.organization
  org.name = attributes[:name]
  org.billing_enabled = attributes[:billing_enabled]
  org.create!
  org
end

.delete_organization_recursive(org) ⇒ Object

deletes an organization recursively (all spaces and their contents) returns true if the organization was deleted



33
34
35
36
37
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 33

def self.delete_organization_recursive(org)
  raise "The given organization is nil!" if org.nil?

  org.delete!(recursive: true)
end

.exists?(org_name) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 9

def self.exists?(org_name)
  self.cc_client.organizations.each do |o|
    return true if o.name.eql? org_name
  end
  return false
end

.get_all_org_namesObject

returns an array of all organization names present in the system use with caution (large dataset)



132
133
134
135
136
137
138
139
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 132

def self.get_all_org_names
  arr = []
  orgs = cc_client.organizations
  orgs.each do |o|
    arr << o.name
  end
  arr.sort!
end

.get_organization_by_name(name) ⇒ Object

returns the organization with the given name if it exists throws an exception when the given organization doesn’t exist



41
42
43
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 41

def self.get_organization_by_name(name)
  cc_client.organization_by_name name
end

.get_organizationsObject

returns an array of all CFoundry::V2::Organizations



5
6
7
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 5

def self.get_organizations
  self.cc_client.organizations
end

.get_space_names(org) ⇒ Object

returns an array of space names for the given organization



121
122
123
124
125
126
127
128
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 121

def self.get_space_names(org)
  return [] if org.nil?
  names = Array.new
  org.spaces.each do |s|
    names << s.name
  end
  names
end

.get_users(org) ⇒ Object

returns an array of Cfoundry::V2::Users registered within the given organization



46
47
48
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 46

def self.get_users(org)
  org.users
end

.remove_roles(org, user, roles) ⇒ Object

takes an array of roles and removes the given user from the according organizations role lists throws an exception if any of the given arguments is nil or empty returns the organization



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 102

def self.remove_roles(org, user, roles)
  raise "No roles given!" if roles.nil? || roles.empty?
  raise "The given organization is nil!" if org.nil?
  raise "The given user is nil!" if user.nil?

  roles.each do |r|
    if r == CFoundryHelper::Models::OrganizationRole::AUDITOR
      org.remove_manager user
    elsif r == CFoundryHelper::Models::OrganizationRole::MANAGER
      org.remove_auditor user
    elsif r == CFoundryHelper::Models::OrganizationRole::BILLINGMANAGER
      org.remove_billing_manager user
    end
  end
  org.update!
  org
end

.remove_user(org, user) ⇒ Object

removes the given user from the given organization returns the user



68
69
70
71
72
73
74
75
76
# File 'lib/cfoundry_helper/helpers/organization_helper.rb', line 68

def self.remove_user(org, user)
  # remove user from all sub-arrays
  org.remove_auditor user
  org.remove_manager user
  org.remove_billing_manager user
  org.remove_user user
  org.update!
  user
end