jfoundry 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/LICENSE +746 -0
  2. data/Rakefile +10 -0
  3. data/lib/cc_api_stub/applications.rb +53 -0
  4. data/lib/cc_api_stub/domains.rb +32 -0
  5. data/lib/cc_api_stub/frameworks.rb +22 -0
  6. data/lib/cc_api_stub/helper.rb +139 -0
  7. data/lib/cc_api_stub/login.rb +21 -0
  8. data/lib/cc_api_stub/organization_users.rb +21 -0
  9. data/lib/cc_api_stub/organizations.rb +70 -0
  10. data/lib/cc_api_stub/routes.rb +26 -0
  11. data/lib/cc_api_stub/runtimes.rb +22 -0
  12. data/lib/cc_api_stub/service_bindings.rb +22 -0
  13. data/lib/cc_api_stub/service_instances.rb +22 -0
  14. data/lib/cc_api_stub/services.rb +21 -0
  15. data/lib/cc_api_stub/spaces.rb +49 -0
  16. data/lib/cc_api_stub/users.rb +85 -0
  17. data/lib/cc_api_stub.rb +16 -0
  18. data/lib/jfoundry/auth_token.rb +63 -0
  19. data/lib/jfoundry/baseclient.rb +177 -0
  20. data/lib/jfoundry/chatty_hash.rb +46 -0
  21. data/lib/jfoundry/client.rb +39 -0
  22. data/lib/jfoundry/concerns/proxy_options.rb +17 -0
  23. data/lib/jfoundry/errors.rb +163 -0
  24. data/lib/jfoundry/rest_client.rb +331 -0
  25. data/lib/jfoundry/signature/version.rb +27 -0
  26. data/lib/jfoundry/signer.rb +13 -0
  27. data/lib/jfoundry/test_support.rb +3 -0
  28. data/lib/jfoundry/timer.rb +13 -0
  29. data/lib/jfoundry/trace_helpers.rb +64 -0
  30. data/lib/jfoundry/upload_helpers.rb +222 -0
  31. data/lib/jfoundry/v2/app.rb +357 -0
  32. data/lib/jfoundry/v2/app_event.rb +13 -0
  33. data/lib/jfoundry/v2/base.rb +92 -0
  34. data/lib/jfoundry/v2/client.rb +78 -0
  35. data/lib/jfoundry/v2/domain.rb +20 -0
  36. data/lib/jfoundry/v2/managed_service_instance.rb +13 -0
  37. data/lib/jfoundry/v2/model.rb +209 -0
  38. data/lib/jfoundry/v2/model_magic/attribute.rb +49 -0
  39. data/lib/jfoundry/v2/model_magic/client_extensions.rb +170 -0
  40. data/lib/jfoundry/v2/model_magic/has_summary.rb +49 -0
  41. data/lib/jfoundry/v2/model_magic/queryable_by.rb +39 -0
  42. data/lib/jfoundry/v2/model_magic/to_many.rb +138 -0
  43. data/lib/jfoundry/v2/model_magic/to_one.rb +81 -0
  44. data/lib/jfoundry/v2/model_magic.rb +93 -0
  45. data/lib/jfoundry/v2/organization.rb +22 -0
  46. data/lib/jfoundry/v2/quota_definition.rb +12 -0
  47. data/lib/jfoundry/v2/route.rb +25 -0
  48. data/lib/jfoundry/v2/service.rb +20 -0
  49. data/lib/jfoundry/v2/service_auth_token.rb +10 -0
  50. data/lib/jfoundry/v2/service_binding.rb +10 -0
  51. data/lib/jfoundry/v2/service_broker.rb +11 -0
  52. data/lib/jfoundry/v2/service_instance.rb +13 -0
  53. data/lib/jfoundry/v2/service_plan.rb +13 -0
  54. data/lib/jfoundry/v2/space.rb +18 -0
  55. data/lib/jfoundry/v2/stack.rb +10 -0
  56. data/lib/jfoundry/v2/user.rb +104 -0
  57. data/lib/jfoundry/v2/user_provided_service_instance.rb +7 -0
  58. data/lib/jfoundry/validator.rb +41 -0
  59. data/lib/jfoundry/version.rb +4 -0
  60. data/lib/jfoundry/zip.rb +56 -0
  61. data/lib/jfoundry.rb +5 -0
  62. data/lib/tasks/gem_release.rake +42 -0
  63. data/vendor/errors/README.md +3 -0
  64. data/vendor/errors/v1.yml +189 -0
  65. data/vendor/errors/v2.yml +470 -0
  66. metadata +269 -0
@@ -0,0 +1,81 @@
1
+ module JFoundry::V2::ModelMagic
2
+ module ToOne
3
+ def to_one(name, opts = {})
4
+ to_one_relations[name] = opts
5
+
6
+ association_name = opts[:as] || name
7
+ default = opts[:default]
8
+
9
+ if has_default = opts.key?(:default)
10
+ defaults[:"#{name}_guid"] = default
11
+ end
12
+
13
+ #
14
+ # def MODEL
15
+ #
16
+ define_method(name) do
17
+ return @cache[name] if @cache.key?(name)
18
+ return @client.send(association_name) unless persisted?
19
+
20
+ @cache[name] =
21
+ if @manifest && @manifest[:entity].key?(name)
22
+ @client.send(:"make_#{association_name}", @manifest[:entity][name])
23
+ elsif url = send("#{name}_url")
24
+ @client.send(:"#{association_name}_from", url)
25
+ else
26
+ default
27
+ end
28
+ end
29
+
30
+ #
31
+ # def create_MODEL
32
+ #
33
+ define_method("create_#{name}") do |*args|
34
+ associated_instance = @client.send(:"#{association_name}")
35
+ args.first.each do |name, value|
36
+ associated_instance.send("#{name}=", value)
37
+ end if args.first.is_a? Hash
38
+
39
+ associated_instance.create!
40
+ self.send("#{name}=", associated_instance)
41
+ end
42
+
43
+ #
44
+ # def MODEL_url
45
+ #
46
+ define_method(:"#{name}_url") do
47
+ manifest[:entity][:"#{name}_url"]
48
+ end
49
+
50
+ #
51
+ # def MODEL=
52
+ #
53
+ define_method(:"#{name}=") do |assigned_value|
54
+ klass = self.class.objects[association_name]
55
+
56
+ unless has_default && assigned_value == default
57
+ JFoundry::Validator.validate_type(assigned_value, klass)
58
+ end
59
+
60
+ @manifest ||= {}
61
+ @manifest[:entity] ||= {}
62
+
63
+ old_guid = @manifest[:entity][:"#{name}_guid"]
64
+ association_guid = assigned_value ? assigned_value.guid : nil
65
+
66
+ if old_guid != (association_guid)
67
+ old_obj =
68
+ @cache[name] || klass.new(@client, old_guid, @manifest[:entity][name])
69
+
70
+ @changes[name] = [old_obj, assigned_value]
71
+ end
72
+
73
+ @cache[name] = assigned_value
74
+
75
+ @manifest[:entity][:"#{name}_guid"] = association_guid
76
+ @diff[:"#{name}_guid"] = association_guid
77
+ assigned_value
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,93 @@
1
+ require "jfoundry/validator"
2
+ require "jfoundry/v2/model_magic/client_extensions"
3
+ require "jfoundry/v2/model_magic/has_summary"
4
+ require "jfoundry/v2/model_magic/attribute"
5
+ require "jfoundry/v2/model_magic/to_one"
6
+ require "jfoundry/v2/model_magic/to_many"
7
+ require "jfoundry/v2/model_magic/queryable_by"
8
+
9
+ module JFoundry::V2
10
+ # object name -> module containing query methods
11
+ #
12
+ # e.g. app -> { app_by_name, app_by_space_guid, ... }
13
+ QUERIES = Hash.new do |h, k|
14
+ h[k] = Module.new
15
+ end
16
+
17
+ module BaseClientMethods
18
+ end
19
+
20
+ module ClientMethods
21
+ end
22
+
23
+ module ModelMagic
24
+ include ModelMagic::ClientExtensions
25
+ include ModelMagic::HasSummary
26
+ include ModelMagic::Attribute
27
+ include ModelMagic::ToOne
28
+ include ModelMagic::ToMany
29
+ include ModelMagic::QueryableBy
30
+
31
+ attr_reader :scoped_organization, :scoped_space
32
+
33
+ def object_name
34
+ @object_name ||=
35
+ name.split("::").last.gsub(
36
+ /([a-z])([A-Z])/,
37
+ '\1_\2').downcase.to_sym
38
+ end
39
+
40
+ def plural_object_name
41
+ :"#{object_name}s"
42
+ end
43
+
44
+ def defaults
45
+ @defaults ||= {}
46
+ end
47
+
48
+ def attributes
49
+ @attributes ||= {}
50
+ end
51
+
52
+ def to_one_relations
53
+ @to_one_relations ||= {}
54
+ end
55
+
56
+ def to_many_relations
57
+ @to_many_relations ||= {}
58
+ end
59
+
60
+ def inherited(klass)
61
+ add_client_methods(klass)
62
+ has_summary
63
+ end
64
+
65
+ def scoped_to_organization(relation = :organization)
66
+ @scoped_organization = relation
67
+ end
68
+
69
+ def scoped_to_space(relation = :space)
70
+ @scoped_space = relation
71
+ end
72
+
73
+ def self.params_from(args)
74
+ options, _ = args
75
+ options ||= {}
76
+ options[:depth] ||= 1
77
+
78
+ params = {}
79
+ options.each do |k, v|
80
+ case k
81
+ when :depth
82
+ params[:"inline-relations-depth"] = v
83
+ when :query
84
+ params[:q] = v.join(":")
85
+ when :user_provided
86
+ params[:"return_user_provided_service_instances"] = v
87
+ end
88
+ end
89
+
90
+ params
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,22 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class Organization < Model
5
+ attribute :name, :string
6
+ attribute :billing_enabled, :boolean
7
+ attribute :status, :string
8
+ #attribute :users, :string
9
+
10
+ to_many :spaces
11
+ to_many :domains
12
+ to_many :users
13
+ to_many :managers, :as => :user
14
+ to_many :billing_managers, :as => :user
15
+ to_many :auditors, :as => :user
16
+
17
+ to_one :quota_definition
18
+
19
+ queryable_by :name, :space_guid, :user_guid, :manager_guid,
20
+ :billing_manager_guid, :auditor_guid
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class QuotaDefinition < Model
5
+ attribute :name, :string
6
+ attribute :non_basic_services_allowed, :boolean
7
+ attribute :total_services, :integer
8
+ attribute :memory_limit, :integer
9
+
10
+ queryable_by :name
11
+ end
12
+ end
@@ -0,0 +1,25 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class Route < Model
5
+ attribute :host, :string
6
+ validates_format_of :host, :with => /\A[a-z]+([a-z0-9\-]*[a-z0-9]+)?\Z/i
7
+ validates_length_of :host, :maximum => 63
8
+ validates_presence_of :domain
9
+ validates_presence_of :space
10
+ to_one :domain
11
+ to_one :space
12
+
13
+ queryable_by :host, :domain_guid
14
+
15
+ def name
16
+ "#{host}.#{domain.name}"
17
+ end
18
+
19
+ private
20
+
21
+ def attribute_for_error(error)
22
+ error.is_a?(JFoundry::RouteHostTaken) ? :host : :base
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class Service < Model
5
+ attribute :label, String
6
+ attribute :provider, String
7
+ attribute :url, :url
8
+ attribute :description, String
9
+ attribute :version, String
10
+ attribute :info_url, :url
11
+ attribute :acls, { "users" => [String], "wildcards" => [String] },
12
+ :default => nil
13
+ attribute :timeout, Integer, :default => nil
14
+ attribute :active, :boolean, :default => false
15
+ attribute :extra, String
16
+ to_many :service_plans
17
+
18
+ queryable_by :service_plan_guid
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class ServiceAuthToken < Model
5
+ attribute :label, :string
6
+ attribute :provider, :string
7
+ #attribute :token, :string
8
+ attribute :access_key, :string
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class ServiceBinding < Model
5
+ to_one :app
6
+ to_one :service_instance
7
+
8
+ queryable_by :app_guid, :service_instance_guid
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class ServiceBroker < Model
5
+ attribute :name, :string
6
+ attribute :broker_url, :string
7
+ attribute :token, :string
8
+
9
+ queryable_by :name
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class ServiceInstance < Model
5
+ attribute :name, :string
6
+ to_one :space
7
+ to_many :service_bindings
8
+
9
+ scoped_to_space
10
+
11
+ queryable_by :name, :space_guid, :service_plan_guid, :service_binding_guid, :gateway_name
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class ServicePlan < Model
5
+ attribute :name, :string
6
+ attribute :description, :string
7
+ attribute :extra, :string
8
+ to_one :service
9
+ to_many :service_instances
10
+
11
+ queryable_by :service_guid, :service_instance_guid
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry::V2
4
+ class Space < Model
5
+ attribute :name, :string
6
+ to_one :organization
7
+ to_many :developers, :as => :user
8
+ to_many :managers, :as => :user
9
+ to_many :auditors, :as => :user
10
+ to_many :apps
11
+ to_many :domains
12
+ to_many :service_instances
13
+
14
+ scoped_to_organization
15
+
16
+ queryable_by :name, :organization_guid, :developer_guid, :app_guid
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+
2
+ require "jfoundry/v2/model"
3
+
4
+ module JFoundry::V2
5
+ class Stack < Model
6
+ attribute :name, :string
7
+
8
+ queryable_by :name
9
+ end
10
+ end
@@ -0,0 +1,104 @@
1
+ require "jfoundry/v2/model"
2
+
3
+ module JFoundry
4
+ module V2
5
+ class User < Model
6
+ to_many :spaces
7
+ to_many :organizations
8
+ to_many :managed_organizations, :as => :organization
9
+ to_many :billing_managed_organizations, :as => :organization
10
+ to_many :audited_organizations, :as => :organization
11
+ to_many :managed_spaces, :as => :space
12
+ to_many :audited_spaces, :as => :space
13
+ attribute :admin, :boolean
14
+ to_one :default_space, :as => :space
15
+
16
+ attribute :guid, :string # guid is explicitly set for users
17
+
18
+ queryable_by :space_guid, :organization_guid, :managed_organization_guid,
19
+ :billing_managed_organization_guid, :audited_organization_guid,
20
+ :managed_space_guid, :audited_space_guid
21
+
22
+ def guid
23
+ @guid
24
+ end
25
+
26
+ alias set_guid_attribute guid=
27
+
28
+ def guid=(x)
29
+ @guid = x
30
+ set_guid_attribute(x)
31
+ end
32
+
33
+ alias :admin? :admin
34
+
35
+ # optional metadata from UAA
36
+ attr_accessor :emails, :name
37
+
38
+ def email
39
+ # if the email collection is nil or empty? collect from UAA
40
+ #p "email................."
41
+ return unless @emails && @emails.first
42
+ @emails.first[:value]
43
+ =begin
44
+ get_meta_from_uaa if @emails.nil?
45
+
46
+ return unless @emails && @emails.first
47
+ @emails.first[:value]
48
+ =end
49
+ end
50
+
51
+ def given_name
52
+ p "given_name............."
53
+ =begin
54
+ get_meta_from_uaa if @name.nil?
55
+
56
+ return unless @name && @name[:givenName] != email
57
+ @name[:givenName]
58
+ =end
59
+ end
60
+
61
+ def family_name
62
+ p "family_name............."
63
+ =begin
64
+ get_meta_from_uaa if @name.nil?
65
+
66
+ return unless @name && @name[:familyName] != email
67
+ @name[:familyName]
68
+ =end
69
+ end
70
+
71
+ def full_name
72
+ if @name && @name[:fullName]
73
+ @name[:fullName]
74
+ elsif given_name && family_name
75
+ "#{given_name} #{family_name}"
76
+ end
77
+ end
78
+
79
+ def delete! (options = {})
80
+ super(options)
81
+ #@client.base.uaa.delete_user(guid)
82
+ p "delete!............"
83
+ true
84
+ end
85
+
86
+ private
87
+
88
+ def get_meta_from_uaa
89
+ p "get_meta_from_uaa......."
90
+ =begin
91
+ user = @client.base.uaa.user(guid)
92
+ return if user.nil?
93
+ return if not user[:error].nil?
94
+
95
+ @emails = user[:emails]
96
+ @name ||= {}
97
+ @name[:familyName] = user[:name][:familyname]
98
+ @name[:givenName] = user[:name][:givenname]
99
+ =end
100
+ end
101
+
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,7 @@
1
+ require "jfoundry/v2/service_instance"
2
+
3
+ module JFoundry::V2
4
+ class UserProvidedServiceInstance < ServiceInstance
5
+ attribute :credentials, :hash
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ module JFoundry
2
+ module Validator
3
+ class << self
4
+ def value_matches?(val, type)
5
+ return true if val.nil?
6
+
7
+ case type
8
+ when Class
9
+ val.is_a?(type)
10
+ when Regexp
11
+ val.is_a?(String) && !!(val =~ type)
12
+ when :url
13
+ value_matches?(val, URI::regexp(%w(http https)))
14
+ when :https_url
15
+ value_matches?(val, URI::regexp("https"))
16
+ when :boolean
17
+ val.is_a?(TrueClass) || val.is_a?(FalseClass)
18
+ when Array
19
+ val.all? do |x|
20
+ value_matches?(x, type.first)
21
+ end
22
+ when Hash
23
+ val.is_a?(Hash) &&
24
+ type.all? { |name, subtype|
25
+ val.key?(name) && value_matches?(val[name], subtype)
26
+ }
27
+ when nil
28
+ true
29
+ else
30
+ val.is_a?(Object.const_get(type.to_s.capitalize))
31
+ end
32
+ end
33
+
34
+ def validate_type(val, type)
35
+ unless value_matches?(val, type)
36
+ raise JFoundry::Mismatch.new(type, val)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ module JFoundry # :nodoc:
2
+ # JFoundry library version number.
3
+ VERSION = "0.1.0".freeze
4
+ end
@@ -0,0 +1,56 @@
1
+ require "zip/zipfilesystem"
2
+
3
+ module JFoundry
4
+ # Generic Zpi API. Uses rubyzip underneath, but may be changed in the future
5
+ # to use system zip command if necessary.
6
+ module Zip
7
+ # Directory entries to exclude from packing.
8
+ PACK_EXCLUSION_GLOBS = %w(.. . *~ #*# *.log)
9
+
10
+ module_function
11
+
12
+ # Get the entries in the zip file. Returns an array of the entire
13
+ # contents, recursively (not just top-level).
14
+ def entry_lines(file)
15
+ entries = []
16
+ ::Zip::ZipFile.foreach(file) do |zentry|
17
+ entries << zentry
18
+ end
19
+ entries
20
+ end
21
+
22
+ # Unpack a zip +file+ to directory +dest+.
23
+ def unpack(file, dest)
24
+ ::Zip::ZipFile.foreach(file) do |zentry|
25
+ epath = "#{dest}/#{zentry}"
26
+ dirname = File.dirname(epath)
27
+ FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
28
+ zentry.extract(epath) unless File.exists?(epath)
29
+ end
30
+ end
31
+
32
+ # Determine what files in +dir+ to pack.
33
+ def files_to_pack(dir)
34
+ Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).select do |f|
35
+ File.exists?(f) &&
36
+ PACK_EXCLUSION_GLOBS.none? do |e|
37
+ File.fnmatch(e, File.basename(f))
38
+ end
39
+ end
40
+ end
41
+
42
+ # Package directory +dir+ as file +zipfile+.
43
+ def pack(dir, zipfile)
44
+ files = files_to_pack(dir)
45
+ return false if files.empty?
46
+
47
+ ::Zip::ZipFile.open(zipfile, true) do |zf|
48
+ files.each do |f|
49
+ zf.add(f.sub("#{dir}/",''), f)
50
+ end
51
+ end
52
+
53
+ true
54
+ end
55
+ end
56
+ end
data/lib/jfoundry.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "jfoundry/version"
2
+ require "active_model"
3
+ require "jfoundry/client"
4
+
5
+ I18n.load_path += Dir["config/locales/*.yml"]
@@ -0,0 +1,42 @@
1
+ require 'active_support/core_ext'
2
+
3
+ namespace :gem do
4
+ desc "Bump gem version, push to RubyGems, push to Github, add release notes"
5
+ task :release, [:version] do |_, args|
6
+ version = args[:version] || 'rc'
7
+ old_version = gem_version
8
+
9
+ sh! "gem bump --version #{version} --no-commit"
10
+ sh! "git add lib/jfoundry/version.rb"
11
+
12
+ print_with_purpose "Bumping to version #{gem_version}"
13
+ generate_release_notes(old_version)
14
+ sh!("git commit -m 'Bumping to version #{gem_version}.'")
15
+ sh!("git push")
16
+ sh!("gem release --tag")
17
+ end
18
+
19
+ private
20
+ def generate_release_notes(old_version)
21
+ print_with_purpose "Generating release notes..."
22
+ file_name = "release_#{gem_version}"
23
+ sh!("anchorman notes --name=#{file_name} --from=v#{old_version}")
24
+ sh!("git add release_notes")
25
+ end
26
+
27
+ def sh!(cmd)
28
+ `#{cmd}`
29
+ raise "borked with #{$?}" unless $?.success?
30
+ end
31
+
32
+ def print_with_purpose(text)
33
+ puts "\033[34m#{text}\033[0m"
34
+ end
35
+
36
+ def gem_version
37
+ silence_warnings do
38
+ load "lib/jfoundry/version.rb"
39
+ end
40
+ Gem::Specification.load("jfoundry.gemspec").version.to_s
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ # errors
2
+
3
+ Shared error codes and messages for Cloud Foundry