cfoundry_helper 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cfoundry_helper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cfoundry_helper"
8
+ spec.version = CFoundryHelper::VERSION
9
+ spec.authors = ["Julian Weber"]
10
+ spec.email = ["jweber@anynines.com"]
11
+ spec.description = %q{This gem provides additional helper classes and scripts for the cfoundry gem.}
12
+ spec.summary = %q{This gem provides additional helper classes and scripts for the cfoundry gem.}
13
+ spec.homepage = "http://www.anynines.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+
26
+ # cf integration-test-support dependencies
27
+ spec.add_development_dependency "nats"
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "cf-uaa-lib"
30
+ spec.add_development_dependency "activesupport"
31
+ spec.add_development_dependency "httpclient"
32
+ spec.add_development_dependency "yajl-ruby"
33
+
34
+ spec.add_development_dependency "pry"
35
+ spec.add_development_dependency "pry-debugger"
36
+
37
+ spec.add_dependency "cfoundry"
38
+ spec.add_dependency "activesupport"
39
+ end
@@ -0,0 +1,22 @@
1
+ production: &defaults
2
+ uaa:
3
+ site: "http://uaa.any9app.com"
4
+ client_id: ""
5
+ client_secret: ""
6
+ cloud_controller:
7
+ site: "http://api.any9app.com"
8
+ username: ""
9
+ password: ""
10
+
11
+ development:
12
+ <<: *defaults
13
+
14
+ test:
15
+ uaa:
16
+ site: ""
17
+ client_id: ""
18
+ client_secret: ""
19
+ cloud_controller:
20
+ site: ""
21
+ user: ""
22
+ password: ""
@@ -0,0 +1,18 @@
1
+ require 'bundler'
2
+ require 'cfoundry'
3
+ require 'active_support/core_ext/object/try'
4
+
5
+ Bundler.require
6
+
7
+ module CFoundryHelper
8
+ autoload :Helpers, File.expand_path('../cfoundry_helper/helpers', __FILE__)
9
+ autoload :Models, File.expand_path('../cfoundry_helper/models', __FILE__)
10
+
11
+ def self.env
12
+ unless ENV['RAILS_ENV']
13
+ return :development
14
+ else
15
+ ENV['RAILS_ENV'].to_sym
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module CFoundryHelper::Helpers
2
+ autoload :OrganizationHelper, File.expand_path('../helpers/organization_helper', __FILE__)
3
+ autoload :ClientHelper, File.expand_path('../helpers/client_helper', __FILE__)
4
+ autoload :UserHelper, File.expand_path('../helpers/user_helper', __FILE__)
5
+ autoload :SpaceHelper, File.expand_path('../helpers/space_helper', __FILE__)
6
+ end
@@ -0,0 +1,106 @@
1
+ require 'yaml'
2
+
3
+ module CFoundryHelper::Helpers
4
+ module ClientHelper
5
+ @@config = nil
6
+ @@scim_client = nil
7
+ @@cloud_controller_client = nil
8
+ @@config_file_path = nil
9
+ @@auth_token = nil
10
+
11
+ def self.set_config_file_path(path)
12
+ @@config_file_path = path
13
+ end
14
+
15
+ def self.config_file_path
16
+ return @@config_file_path
17
+ end
18
+
19
+ def self.get_cc_target_url
20
+ return self.cloud_controller_client.target
21
+ end
22
+
23
+ def self.get_auth_token
24
+ self.cloud_controller_client if @@auth_token.nil?
25
+ return @@auth_token
26
+ end
27
+
28
+ ##
29
+ # Use this client to connect to the uaa-service and
30
+ # check wether a user alredy exists.
31
+ #
32
+ # scim is a "System for Cross-domain Identity Management" and
33
+ # it is uesd by the uaa service.
34
+ # see http://www.simplecloud.info/
35
+ def self.scim_client
36
+ # just return the already initialized client if present
37
+ return @@scim_client unless @@scim_client.nil?
38
+
39
+ self.read_config_file
40
+ token_issuer = CF::UAA::TokenIssuer.new(
41
+ @@config['uaa']['site'],
42
+ @@config['uaa']['client_id'],
43
+ @@config['uaa']['client_secret'])
44
+
45
+ token_info = token_issuer.client_credentials_grant
46
+ access_token = token_info.info["access_token"]
47
+ @@scim_client = CF::UAA::Scim.new(@@config['uaa']['site'], "bEareR #{access_token}")
48
+ end
49
+
50
+ ##
51
+ # Use this client to connect to the cloudcontroller
52
+ # and register a new user.
53
+ def self.cloud_controller_client
54
+ # just return the already initialized client if it was explicit setted.
55
+ # Don't cache the client in this method because long running apps will
56
+ # have some trouble with expired auth tokens.
57
+ # TODO: Refresh the token with the uaa refresh method.
58
+ return @@cloud_controller_client if @@cloud_controller_client
59
+
60
+ self.read_config_file
61
+ token_issuer = CF::UAA::TokenIssuer.new(@@config['uaa']['site'], "cf")
62
+ token_info = token_issuer.implicit_grant_with_creds(@@config['cloud_controller'])
63
+ access_token = token_info.info["access_token"]
64
+ token = CFoundry::AuthToken.from_hash({:token => "bearer #{access_token}"})
65
+ @@auth_token = token
66
+ CFoundry::V2::Client.new(@@config['cloud_controller']['site'], token)
67
+ end
68
+
69
+
70
+ def self.set_scim_client(client)
71
+ @@scim_client = client
72
+ end
73
+
74
+ def self.set_cc_client(client)
75
+ @@cloud_controller_client = client
76
+ end
77
+
78
+ protected
79
+
80
+ # reads the uaa and cc configuration from a config file
81
+ def self.read_config_file
82
+ # try to set the config file path from the env if not set already
83
+ self.set_config_file_path_from_env if @@config_file_path.nil?
84
+
85
+ if @@config.nil?
86
+ self.check_config_file_path
87
+ @@config = YAML.load_file(@@config_file_path)[CFoundryHelper.env.to_s]
88
+ end
89
+ @@config
90
+ end
91
+
92
+ def self.check_config_file_path
93
+ raise "No configuration file path has been set! Please call ClientHelper.set_config_file_path first or set a valid CFOUNDRY_HELPER_CONFIG env variable!" if @@config_file_path.nil?
94
+ raise "There's no configuration file on #{@@config_file_path}!" if !File.exists? @@config_file_path
95
+ end
96
+
97
+ # sets the configuration file path from reading the CFOUNDRY_HELPER_CONFIG env variable
98
+ def self.set_config_file_path_from_env
99
+ config_file_path = ENV["CFOUNDRY_HELPER_CONFIG"]
100
+ unless config_file_path.nil?
101
+ self.set_config_file_path config_file_path
102
+ end
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,148 @@
1
+ module CFoundryHelper::Helpers
2
+ module OrganizationHelper
3
+
4
+ # returns an array of all CFoundry::V2::Organizations
5
+ def self.get_organizations
6
+ self.cc_client.organizations
7
+ end
8
+
9
+ def self.exists?(org_name)
10
+ self.cc_client.organizations.each do |o|
11
+ return true if o.name.eql? org_name
12
+ end
13
+ return false
14
+ end
15
+
16
+ # creates an CFoundry::V2::Organization with the given attributes contained in the hash
17
+ # returns the created organization
18
+ # throws an exception when an error occures during creating the organization
19
+ def self.create_organization(attributes)
20
+ raise "The given attributes hash is nil!" if attributes.nil?
21
+ raise "No organization name given!" if attributes[:name].nil?
22
+ raise "No billing_enabled attribute given!" if attributes[:billing_enabled].nil?
23
+
24
+ org = self.cc_client.organization
25
+ org.name = attributes[:name]
26
+ org.billing_enabled = attributes[:billing_enabled]
27
+ org.create!
28
+ org
29
+ end
30
+
31
+ # deletes an organization recursively (all spaces and their contents)
32
+ # returns true if the organization was deleted
33
+ def self.delete_organization_recursive(org)
34
+ raise "The given organization is nil!" if org.nil?
35
+
36
+ org.delete!(recursive: true)
37
+ end
38
+
39
+ # returns the organization with the given name if it exists
40
+ # throws an exception when the given organization doesn't exist
41
+ def self.get_organization_by_name(name)
42
+ cc_client.organization_by_name name
43
+ end
44
+
45
+ # returns an array of Cfoundry::V2::Users registered within the given organization
46
+ def self.get_users(org)
47
+ org.users
48
+ end
49
+
50
+ # adds the given user to the given organization
51
+ # returns the organization
52
+ def self.add_user(org, user)
53
+ org.add_user user
54
+ org.update!
55
+ org
56
+ end
57
+
58
+ # adds the user with the given email to the given organization
59
+ # returns the organization
60
+ def self.add_user_by_email(org, email)
61
+ user = CFoundryHelper::Helpers::UserHelper.get_user_by_email email
62
+ self.add_user org, user
63
+ org
64
+ end
65
+
66
+ # removes the given user from the given organization
67
+ # returns the user
68
+ def self.remove_user(org, user)
69
+ # remove user from all sub-arrays
70
+ org.remove_auditor user
71
+ org.remove_manager user
72
+ org.remove_billing_manager user
73
+ org.remove_user user
74
+ org.update!
75
+ user
76
+ end
77
+
78
+ # takes an array of roles and adds the given user to the according organizations role lists
79
+ # throws an exception if any of the given arguments is nil or empty
80
+ # returns the organization
81
+ def self.add_roles(org, user, roles)
82
+ raise "No roles given!" if roles.nil? || roles.empty?
83
+ raise "The given organization is nil!" if org.nil?
84
+ raise "The given user is nil!" if user.nil?
85
+
86
+ roles.each do |r|
87
+ if r == CFoundryHelper::Models::OrganizationRole::AUDITOR
88
+ org.add_manager user
89
+ elsif r == CFoundryHelper::Models::OrganizationRole::MANAGER
90
+ org.add_auditor user
91
+ elsif r == CFoundryHelper::Models::OrganizationRole::BILLINGMANAGER
92
+ org.add_billing_manager user
93
+ end
94
+ end
95
+ org.update!
96
+ org
97
+ end
98
+
99
+ # takes an array of roles and removes the given user from the according organizations role lists
100
+ # throws an exception if any of the given arguments is nil or empty
101
+ # returns the organization
102
+ def self.remove_roles(org, user, roles)
103
+ raise "No roles given!" if roles.nil? || roles.empty?
104
+ raise "The given organization is nil!" if org.nil?
105
+ raise "The given user is nil!" if user.nil?
106
+
107
+ roles.each do |r|
108
+ if r == CFoundryHelper::Models::OrganizationRole::AUDITOR
109
+ org.remove_manager user
110
+ elsif r == CFoundryHelper::Models::OrganizationRole::MANAGER
111
+ org.remove_auditor user
112
+ elsif r == CFoundryHelper::Models::OrganizationRole::BILLINGMANAGER
113
+ org.remove_billing_manager user
114
+ end
115
+ end
116
+ org.update!
117
+ org
118
+ end
119
+
120
+ # returns an array of space names for the given organization
121
+ def self.get_space_names(org)
122
+ return [] if org.nil?
123
+ names = Array.new
124
+ org.spaces.each do |s|
125
+ names << s.name
126
+ end
127
+ names
128
+ end
129
+
130
+ # returns an array of all organization names present in the system
131
+ # use with caution (large dataset)
132
+ def self.get_all_org_names
133
+ arr = []
134
+ orgs = cc_client.organizations
135
+ orgs.each do |o|
136
+ arr << o.name
137
+ end
138
+ arr.sort!
139
+ end
140
+
141
+ private
142
+
143
+ # gets the current cloud controller client from the ClientHelper module
144
+ def self.cc_client
145
+ CFoundryHelper::Helpers::ClientHelper.cloud_controller_client
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,125 @@
1
+ module CFoundryHelper::Helpers
2
+ module SpaceHelper
3
+
4
+ # creates a space with the given name within the given organization
5
+ # throws an exception if the given spacename is nil or empty
6
+ # throws an exception if the given organization is nil
7
+ # throws an exception if a space with the given name already exists within the given organization
8
+ def self.create_space(org, spacename)
9
+ raise "The given organization is nil!" if org.nil?
10
+ raise "The given spacename is nil!" if spacename.nil?
11
+ raise "The given spacename is empty!" if spacename.eql?('')
12
+ raise "The #{spacename} space already exists within #{org.name}!" if self.exists?(org, spacename)
13
+
14
+ space = self.cc_client.space
15
+ space.name = spacename
16
+ space.organization = org
17
+ space.create!
18
+ space
19
+ end
20
+
21
+ # deletes a space
22
+ # throws an exception if the given space is nil
23
+ # throws an exception if the given space is not empty
24
+ # returns true on deletion
25
+ def self.delete_space(space)
26
+ raise "The given space is nil!" if space.nil?
27
+ unless is_empty? space
28
+ raise "The given space is not empty!"
29
+ end
30
+ space.delete!
31
+ return true
32
+ end
33
+
34
+ # deletes a given space recursively (all apps, services, ....)
35
+ # returns true if the space was deleted
36
+ def self.delete_space_recursive(space)
37
+ raise "The given space is nil!" if space.nil?
38
+ space.delete!(:recursive => true)
39
+ end
40
+
41
+ # returns true if no apps, service_instances, routes,
42
+ def self.is_empty?(space)
43
+ return true if space.nil?
44
+ if space.apps.count > 0 || space.service_instances.count > 0 || space.domains.count > 0
45
+ return false
46
+ else
47
+ return true
48
+ end
49
+ end
50
+
51
+ # returns the given the space with the given name within the given organization
52
+ # returns nil of no space has been found
53
+ def self.get_space(org, spacename)
54
+ org.space_by_name spacename
55
+ end
56
+
57
+ # returns the space with the given name in the organization with the given name
58
+ # returns nil if no space has been found
59
+ def self.get_space_by_name(org_name, space_name)
60
+ org = CFoundryHelper::Helpers::OrganizationHelper.get_organization_by_name org_name
61
+ return nil if org.nil?
62
+ return self.get_space(org, space_name)
63
+ end
64
+
65
+ # returns true if a space with the given name exists within the given organization
66
+ # returns false if no space with the given name exists within the given organization
67
+ def self.exists?(org, spacename)
68
+ return !org.space_by_name(spacename).nil?
69
+ end
70
+
71
+ def self.exists_by_name?(org_name, space_name)
72
+ return !self.get_space_by_name(org_name, space_name).nil?
73
+ end
74
+
75
+ # takes an array of roles and adds the given user to the according space's role lists
76
+ # throws an exception if any of the given arguments is nil or empty
77
+ # throws an exception if the given user is not registered with the space's organization
78
+ # returns the space
79
+ def self.add_roles(space, user, roles)
80
+ raise "No roles given!" if roles.nil? || roles.empty?
81
+ raise "The given space is nil!" if space.nil?
82
+ raise "The given user is nil!" if user.nil?
83
+
84
+ roles.each do |r|
85
+ if r == CFoundryHelper::Models::SpaceRole::MANAGER
86
+ space.add_manager user
87
+ elsif r == CFoundryHelper::Models::SpaceRole::AUDITOR
88
+ space.add_auditor user
89
+ elsif r == CFoundryHelper::Models::SpaceRole::DEVELOPER
90
+ space.add_developer user
91
+ end
92
+ end
93
+ space.update!
94
+ space
95
+ end
96
+
97
+ # takes an array of roles and removes the given user from the according space role lists
98
+ # throws an exception if any of the given arguments is nil or empty
99
+ # returns the space
100
+ def self.remove_roles(space, user, roles)
101
+ raise "No roles given!" if roles.nil? || roles.empty?
102
+ raise "The given space is nil!" if space.nil?
103
+ raise "The given user is nil!" if user.nil?
104
+
105
+ roles.each do |r|
106
+ if r == CFoundryHelper::Models::SpaceRole::MANAGER
107
+ space.remove_manager user
108
+ elsif r == CFoundryHelper::Models::SpaceRole::AUDITOR
109
+ space.remove_auditor user
110
+ elsif r == CFoundryHelper::Models::SpaceRole::DEVELOPER
111
+ space.remove_developer user
112
+ end
113
+ end
114
+ space.update!
115
+ space
116
+ end
117
+
118
+ private
119
+
120
+ # gets the current cloud controller client from the ClientHelper module
121
+ def self.cc_client
122
+ CFoundryHelper::Helpers::ClientHelper.cloud_controller_client
123
+ end
124
+ end
125
+ end