Class: OneviewSDK::ImageStreamer::API300::GoldenImage

Inherits:
Resource show all
Defined in:
lib/oneview-sdk/image-streamer/resource/api300/golden_image.rb

Overview

Golden Image resource implementation for Image Streamer

Constant Summary collapse

BASE_URI =
'/rest/golden-images'.freeze
READ_TIMEOUT =

in seconds (5 minutes)

300
ACCEPTED_FORMATS =

Supported upload extensions

%w(.zip .ZIP).freeze

Constants inherited from Resource

Resource::UNIQUE_IDENTIFIERS

Instance Attribute Summary

Attributes inherited from Resource

#api_version, #client, #data, #logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, #[], #[]=, build_query, #create, #create!, #delete, #each, #eql?, #exists?, find_by, from_file, get_all, #like?, #refresh, #retrieve!, #schema, schema, #set, #set_all, #to_file, #update

Constructor Details

#initialize(client, params = {}, api_ver = nil) ⇒ GoldenImage

Create a resource object, associate it with a client, and set its properties.

Parameters:

  • client (OneviewSDK::ImageStreamer::Client)

    The client object for the Image Streamer appliance

  • params (Hash) (defaults to: {})

    The options for this resource (key-value pairs)

  • api_ver (Integer) (defaults to: nil)

    The api version to use when interracting with this resource.


28
29
30
31
32
# File 'lib/oneview-sdk/image-streamer/resource/api300/golden_image.rb', line 28

def initialize(client, params = {}, api_ver = nil)
  super
  # Default values:
  @data['type'] ||= 'GoldenImage'
end

Class Method Details

.add(client, file_path, data_options, timeout = READ_TIMEOUT) ⇒ OneviewSDK::ImageStreamer::API300::GoldenImage

Upload a golden image from the specified local file path. Only the .zip format file can be used for upload.

Parameters:

  • client (OneviewSDK::ImageStreamer::Client)

    The client object for the Image Streamer appliance

  • file_path (String)
  • data_options (Hash)

    Attributes of the golden image, passed in the request

  • timeout (Integer) (defaults to: READ_TIMEOUT)

    The number of seconds to wait for the request to complete

Options Hash (data_options):

  • :name (String)

    The name of the Golden Image (required)

  • :description (String)

    The description of the Golden Image (required)

Returns:

Raises:


88
89
90
91
92
93
94
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
# File 'lib/oneview-sdk/image-streamer/resource/api300/golden_image.rb', line 88

def self.add(client, file_path, data_options, timeout = READ_TIMEOUT)
  data_options = Hash[data_options.map { |k, v| [k.to_s, v] }] # Convert symbols hash keys to string
  raise NotFound, "ERROR: File '#{file_path}' not found!" unless File.file?(file_path)
  raise InvalidFormat, 'ERROR: File with extension not supported!' unless ACCEPTED_FORMATS.include? File.extname(file_path)
  raise IncompleteResource, 'Please set the name of the golden image!' unless data_options['name']
  raise IncompleteResource, 'Please set the description of the golden image!' unless data_options['description']
  options = {}
  options['Content-Type'] = 'multipart/form-data'
  options['X-Api-Version'] = client.api_version.to_s
  options['auth'] = client.token
  options['file'] = File.basename(file_path)
  url = URI.parse(URI.escape("#{client.url}#{BASE_URI}"))

  File.open(file_path) do |file|
    req = Net::HTTP::Post::Multipart.new(
      url.path,
      { 'file' => UploadIO.new(file, 'application/octet-stream', File.basename(file_path)) }.merge(data_options),
      options
    )

    http_request = Net::HTTP.new(url.host, url.port)
    http_request.use_ssl = true if url.scheme == 'https'
    if client.ssl_enabled
      http_request.cert_store = client.cert_store if client.cert_store
    else http_request.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    http_request.read_timeout = timeout

    http_request.start do |http|
      response = http.request(req)
      data = client.response_handler(response)
      return OneviewSDK::ImageStreamer::API300::GoldenImage.new(client, data)
    end
  end
end

Instance Method Details

#download(file_path, timeout = READ_TIMEOUT) ⇒ True

Downloads the content of the selected golden image to the specified file path.

Parameters:

  • file_path (String)
  • timeout (Integer) (defaults to: READ_TIMEOUT)

    The number of seconds to wait for the request to complete

Returns:

  • (True)

    When was saved successfully


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
# File 'lib/oneview-sdk/image-streamer/resource/api300/golden_image.rb', line 48

def download(file_path, timeout = READ_TIMEOUT)
  ensure_client && ensure_uri
  uri = URI.parse(URI.escape("#{client.url}#{BASE_URI}/download/#{@data['uri'].split('/').last}"))
  req = Net::HTTP::Get.new(uri.request_uri)

  options = {}
  options['Content-Type'] = 'application/json'
  options['X-Api-Version'] = @client.api_version.to_s
  options['auth'] = @client.token
  options.each do |key, val|
    req[key] = val
  end

  http_request = Net::HTTP.new(uri.host, uri.port)
  http_request.use_ssl = true
  http_request.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http_request.read_timeout = timeout

  http_request.start do |http|
    http.request(req) do |res|
      client.response_handler(res) unless res.code.to_i.between?(200, 204)
      File.open(file_path, 'wb') do |file|
        res.read_body do |segment|
          file.write(segment)
        end
      end
    end
  end
  true
end

#download_details_archive(file_path) ⇒ True

Download the details of the golden image capture logs which has been archived based on the specific attribute ID.

Parameters:

  • file_path (String)

Returns:

  • (True)

    When was saved successfully


37
38
39
40
41
42
# File 'lib/oneview-sdk/image-streamer/resource/api300/golden_image.rb', line 37

def download_details_archive(file_path)
  ensure_client && ensure_uri
  resp = @client.rest_api(:get, "#{BASE_URI}/archive/#{@data['uri'].split('/').last}")
  File.open(file_path, 'wb') { |file| file.write(resp.body) }
  true
end

#set_build_plan(build_plan) ⇒ Object

Sets the build plan

Parameters:

Raises:


136
137
138
139
140
# File 'lib/oneview-sdk/image-streamer/resource/api300/golden_image.rb', line 136

def set_build_plan(build_plan)
  build_plan.retrieve! unless build_plan['uri']
  raise NotFound, 'The build plan was not found!' unless build_plan['uri']
  set('buildPlanUri', build_plan['uri'])
end

#set_os_volume(os_volume) ⇒ Object

Sets the OS volume

Parameters:

Raises:


127
128
129
130
131
# File 'lib/oneview-sdk/image-streamer/resource/api300/golden_image.rb', line 127

def set_os_volume(os_volume)
  os_volume.retrieve! unless os_volume['uri']
  raise NotFound, 'The os volume was not found!' unless os_volume['uri']
  set('osVolumeURI', os_volume['uri'])
end