File: projects.rb

package info (click to toggle)
ruby-fog-openstack 1.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,784 kB
  • sloc: ruby: 47,937; makefile: 5; sh: 4
file content (75 lines) | stat: -rw-r--r-- 3,258 bytes parent folder | download | duplicates (3)
1
2
3
4
5
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'fog/openstack/models/collection'
require 'fog/openstack/identity/v3/models/project'

module Fog
  module OpenStack
    class Identity
      class V3
        class Projects < Fog::OpenStack::Collection
          model Fog::OpenStack::Identity::V3::Project

          def all(options = {})
            if service.openstack_cache_ttl > 0
              cached_project, expires = Fog::OpenStack::Identity::V3::Project.cache[{:token   => service.auth_token,
                                                                                     :options => options}]
              return cached_project if cached_project && expires > Time.now
            end

            project_to_cache = load_response(service.list_projects(options), 'projects')
            if service.openstack_cache_ttl > 0
              cache = Fog::OpenStack::Identity::V3::Project.cache
              cache[{:token => service.auth_token, :options => options}] = [project_to_cache,
                                                                            Time.now + service.openstack_cache_ttl]
              Fog::OpenStack::Identity::V3::Project.cache = cache
            end
            project_to_cache
          end

          def create(attributes)
            super(attributes)
          end

          def auth_projects(options = {})
            load(service.auth_projects(options).body['projects'])
          end

          def find_by_id(id, options = {}) # options can include :subtree_as_ids, :subtree_as_list, :parents_as_ids, :parents_as_list
            if options.kind_of? Symbol # Deal with a single option being passed on its own
              options = {options => nil}
            end

            if service.openstack_cache_ttl > 0
              cached_project, expires = Fog::OpenStack::Identity::V3::Project.cache[{:token   => service.auth_token,
                                                                                     :id      => id,
                                                                                     :options => options}]
              return cached_project if cached_project && expires > Time.now
            end
            project_hash = service.get_project(id, options).body['project']
            top_project = project_from_hash(project_hash, service)
            if options.include? :subtree_as_list
              top_project.subtree.map! { |proj_hash| project_from_hash(proj_hash['project'], service) }
            end
            if options.include? :parents_as_list
              top_project.parents.map! { |proj_hash| project_from_hash(proj_hash['project'], service) }
            end

            if service.openstack_cache_ttl > 0
              cache = Fog::OpenStack::Identity::V3::Project.cache
              cache[{:token => service.auth_token, :id => id, :options => options}] = [
                top_project, Time.now + service.openstack_cache_ttl
              ]
              Fog::OpenStack::Identity::V3::Project.cache = cache
            end
            top_project
          end

          private

          def project_from_hash(project_hash, service)
            Fog::OpenStack::Identity::V3::Project.new(project_hash.merge(:service => service))
          end
        end
      end
    end
  end
end