Class: OracleCloud::Assets

Inherits:
Object
  • Object
show all
Defined in:
lib/oraclecloud/assets.rb

Direct Known Subclasses

IPAssociations, Instances, Orchestrations, SSHKeys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Assets

Returns a new instance of Assets.



23
24
25
26
27
28
29
# File 'lib/oraclecloud/assets.rb', line 23

def initialize(client)
  @client = client
  @create_opts = {}

  local_init
  validate!
end

Instance Attribute Details

#asset_klassObject (readonly)

Returns the value of attribute asset_klass.



20
21
22
# File 'lib/oraclecloud/assets.rb', line 20

def asset_klass
  @asset_klass
end

#asset_typeObject (readonly)

Returns the value of attribute asset_type.



20
21
22
# File 'lib/oraclecloud/assets.rb', line 20

def asset_type
  @asset_type
end

#clientObject (readonly)

Returns the value of attribute client.



20
21
22
# File 'lib/oraclecloud/assets.rb', line 20

def client
  @client
end

#create_optsObject

Returns the value of attribute create_opts.



21
22
23
# File 'lib/oraclecloud/assets.rb', line 21

def create_opts
  @create_opts
end

Instance Method Details

#allObject



41
42
43
44
45
46
47
# File 'lib/oraclecloud/assets.rb', line 41

def all
  all_asset_ids_by_container.each_with_object([]) do |(container, asset_names), memo|
    asset_names.each do |asset_name|
      memo << @asset_klass.new(client, "#{container}/#{asset_name}")
    end
  end
end

#all_asset_ids_by_containerObject



57
58
59
60
61
# File 'lib/oraclecloud/assets.rb', line 57

def all_asset_ids_by_container
  containers.each_with_object({}) do |container, memo|
    memo[container] = asset_ids_for_container(container)
  end
end

#asset_ids_for_container(container) ⇒ Object



63
64
65
# File 'lib/oraclecloud/assets.rb', line 63

def asset_ids_for_container(container)
  directory(container)
end

#by_name(name) ⇒ Object



67
68
69
# File 'lib/oraclecloud/assets.rb', line 67

def by_name(name)
  @asset_klass.new(client, strip_identity_domain(name))
end

#containersObject



49
50
51
# File 'lib/oraclecloud/assets.rb', line 49

def containers
  directory('')
end

#create(opts) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/oraclecloud/assets.rb', line 75

def create(opts)
  @create_opts = opts

  validate_create_options!
  response = client.http_post("/#{asset_type}/", create_request_payload.to_json)
  name     = strip_identity_domain(response['name'])
  @asset_klass.new(client, name)
end

#create_request_payloadObject

Raises:

  • (NoMethodError)


84
85
86
87
88
# File 'lib/oraclecloud/assets.rb', line 84

def create_request_payload
  # this should be defined in each Assets subclass with a formatted
  # payload used to create the Asset
  raise NoMethodError, "#{self.class} does not define create_request_payload"
end

#directory(path) ⇒ Object



71
72
73
# File 'lib/oraclecloud/assets.rb', line 71

def directory(path)
  ids_from_results(client.directory(asset_type, path))
end

#ids_from_results(results) ⇒ Object



53
54
55
# File 'lib/oraclecloud/assets.rb', line 53

def ids_from_results(results)
  results['result'].map { |x| x.split('/').last }
end

#local_initObject



31
32
33
34
# File 'lib/oraclecloud/assets.rb', line 31

def local_init
  # this should be redefined in each Assets subclass with things like
  # the @asset_type to use in API calls
end

#strip_identity_domain(name) ⇒ Object



95
96
97
# File 'lib/oraclecloud/assets.rb', line 95

def strip_identity_domain(name)
  name.gsub("/Compute-#{client.identity_domain}/", '')
end

#validate!Object



36
37
38
39
# File 'lib/oraclecloud/assets.rb', line 36

def validate!
  raise "#{self.class} did not define an asset_type variable" if asset_type.nil?
  raise "#{self.class} did not define an asset_klass variable" if asset_klass.nil?
end

#validate_create_options!Object



90
91
92
93
# File 'lib/oraclecloud/assets.rb', line 90

def validate_create_options!
  # this should be redefined in each Assets subclass with any validation
  # of creation options that should be done prior to creation
end