File: create_stack.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 (99 lines) | stat: -rw-r--r-- 4,170 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module Fog
  module OpenStack
    class Orchestration
      class Real
        # Create a stack.
        #
        #
        # * options [Hash]:
        #   * :stack_name [String] Name of the stack to create.
        #   * :template [String] Structure containing the template body.
        #   or (one of the two Template parameters is required)
        #   * :template_url [String] URL of file containing the template body.
        #   * :files [Hash] Hash with files resources.
        #   * :disable_rollback [Boolean] Controls rollback on stack creation failure, defaults to false.
        #   * :parameters [Hash] Hash of providers to supply to template
        #   * :timeout_mins [Integer] Minutes to wait before status is set to CREATE_FAILED
        #
        # @see http://developer.openstack.org/api-ref-orchestration-v1.html

        def create_stack(arg1, arg2 = nil)
          if arg1.kind_of?(Hash)
            # Normal use: create_stack(options)
            options = arg1
          else
            # Deprecated: create_stack(stack_name, options = {})
            Fog::Logger.deprecation("#create_stack(stack_name, options) is deprecated, use #create_stack(options) instead [light_black](#{caller.first})[/]")
            options = {
              :stack_name => arg1
            }.merge(arg2.nil? ? {} : arg2)
          end

          # Templates should always:
          #  - be strings
          #  - contain URI references instead of relative paths.
          # Passing :template_url may not work well with `get_file` and remote `type`:
          #  the python client implementation in shade retrieves from :template_uri
          #  and replaces it with :template.
          #  see https://github.com/openstack-infra/shade/blob/master/shade/openstackcloud.py#L1201
          #  see https://developer.openstack.org/api-ref/orchestration/v1/index.html#create-stack
          file_resolver = OrchestrationUtil::RecursiveHotFileLoader.new(options[:template] || options[:template_url], options[:files])
          options[:template] = file_resolver.template
          options[:files] = file_resolver.files unless file_resolver.files.empty?

          request(
            :expects => 201,
            :path    => 'stacks',
            :method  => 'POST',
            :body    => Fog::JSON.encode(options)
          )
        end
      end

      class Mock
        def create_stack(arg1, arg2 = nil)
          if arg1.kind_of?(Hash)
            # Normal use: create_stack(options)
            options = arg1
          else
            # Deprecated: create_stack(stack_name, options = {})
            Fog::Logger.deprecation("#create_stack(stack_name, options) is deprecated, use #create_stack(options) instead [light_black](#{caller.first})[/]")
            options = {
              :stack_name => arg1
            }.merge(arg2.nil? ? {} : arg2)
          end

          stack_id = Fog::Mock.random_hex(32)
          stack = data[:stacks][stack_id] = {
            'id'                  => stack_id,
            'stack_name'          => options[:stack_name],
            'links'               => [],
            'description'         => options[:description],
            'stack_status'        => 'CREATE_COMPLETE',
            'stack_status_reason' => 'Stack successfully created',
            'creation_time'       => Time.now,
            'updated_time'        => Time.now
          }

          response = Excon::Response.new
          response.status = 201
          response.body = {
            'id'    => stack_id,
            'links' => [{"href" => "http://localhost:8004/v1/fake_tenant_id/stacks/#{options[:stack_name]}/#{stack_id}", "rel" => "self"}]
          }

          if options.key?(:files)
            response.body['files'] = {'foo.sh' => 'hello'}
          end

          if options.key?(:template) || options.key?(:template_url)
            file_resolver = OrchestrationUtil::RecursiveHotFileLoader.new(options[:template] || options[:template_url], options[:files])
            response.body['files'] = file_resolver.files unless file_resolver.files.empty?
          end

          response
        end
      end
    end
  end
end