Class: Fog::Openstack::Planning::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/planning.rb,
lib/fog/openstack/requests/planning/get_plan.rb,
lib/fog/openstack/requests/planning/list_plans.rb,
lib/fog/openstack/requests/planning/list_roles.rb,
lib/fog/openstack/requests/planning/patch_plan.rb,
lib/fog/openstack/requests/planning/create_plan.rb,
lib/fog/openstack/requests/planning/delete_plan.rb,
lib/fog/openstack/requests/planning/add_role_to_plan.rb,
lib/fog/openstack/requests/planning/get_plan_templates.rb,
lib/fog/openstack/requests/planning/remove_role_from_plan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fog/openstack/planning.rb', line 95

def initialize(options={})
  @openstack_auth_token = options[:openstack_auth_token]

  unless @openstack_auth_token
    missing_credentials = Array.new
    @openstack_api_key  = options[:openstack_api_key]
    @openstack_username = options[:openstack_username]

    missing_credentials << :openstack_api_key  unless @openstack_api_key
    missing_credentials << :openstack_username unless @openstack_username
    raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
  end

  @openstack_tenant               = options[:openstack_tenant]
  @openstack_auth_uri             = URI.parse(options[:openstack_auth_url])
  @openstack_management_url       = options[:openstack_management_url]
  @openstack_must_reauthenticate  = false
  @openstack_service_type         = options[:openstack_service_type] || ['management'] # currently Tuskar is configured as 'management' service in Keystone
  @openstack_service_name         = options[:openstack_service_name]
  @openstack_endpoint_type        = options[:openstack_endpoint_type] || 'adminURL'
  @openstack_region               = options[:openstack_region]

  @connection_options = options[:connection_options] || {}

  @current_user = options[:current_user]
  @current_tenant = options[:current_tenant]

  authenticate

  @persistent = options[:persistent] || false
  @connection = Fog::Core::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Instance Attribute Details

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



93
94
95
# File 'lib/fog/openstack/planning.rb', line 93

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



92
93
94
# File 'lib/fog/openstack/planning.rb', line 92

def current_user
  @current_user
end

Instance Method Details

#add_role_to_plan(plan_uuid, role_uuid) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/planning/add_role_to_plan.rb', line 5

def add_role_to_plan(plan_uuid, role_uuid)
  request(
    :expects => [201],
    :method  => 'POST',
    :path    => "plans/#{plan_uuid}/roles",
    :body => Fog::JSON.encode({'uuid' => role_uuid})
  )
end

#create_plan(parameters) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/planning/create_plan.rb', line 5

def create_plan(parameters)
  request(
    :expects => [201],
    :method  => 'POST',
    :path    => "plans",
    :body => Fog::JSON.encode(parameters)
  )
end

#credentialsObject



128
129
130
131
132
133
134
135
# File 'lib/fog/openstack/planning.rb', line 128

def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url,
    :current_user             => @current_user,
    :current_tenant           => @current_tenant }
end

#delete_plan(plan_uuid) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/planning/delete_plan.rb', line 5

def delete_plan(plan_uuid)
  request(
    :expects => [204],
    :method  => 'DELETE',
    :path    => "plans/#{plan_uuid}"
  )
end

#get_plan(plan_uuid) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/planning/get_plan.rb', line 5

def get_plan(plan_uuid)
  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => "plans/#{plan_uuid}"
  )
end

#get_plan_templates(plan_uuid) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/planning/get_plan_templates.rb', line 5

def get_plan_templates(plan_uuid)
  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => "plans/#{plan_uuid}/templates"
  )
end

#list_plansObject



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/planning/list_plans.rb', line 5

def list_plans
  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'plans'
  )
end

#list_roles(parameters = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/openstack/requests/planning/list_roles.rb', line 5

def list_roles(parameters=nil)
  if parameters
    query = parameters.each { |k, v| parameters[k] = URI::encode(v) }
  else
    query = {}
  end

  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'roles',
    :query   => query
  )
end

#patch_plan(plan_uuid, parameters) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/planning/patch_plan.rb', line 5

def patch_plan(plan_uuid, parameters)
  request(
    :expects => [201],
    :method  => 'PATCH',
    :path    => "plans/#{plan_uuid}",
    :body => Fog::JSON.encode(parameters)
  )
end

#reloadObject



137
138
139
# File 'lib/fog/openstack/planning.rb', line 137

def reload
  @connection.reset
end

#remove_role_from_plan(plan_uuid, role_uuid) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/openstack/requests/planning/remove_role_from_plan.rb', line 5

def remove_role_from_plan(plan_uuid, role_uuid)
  request(
    :expects => [200],
    :method  => 'DELETE',
    :path    => "plans/#{plan_uuid}/roles/#{role_uuid}"
  )
end

#request(params) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/fog/openstack/planning.rb', line 141

def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :path     => "#{@path}/#{params[:path]}"#,
    }))
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Compute::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end