Module: Occi::Cli::Helpers::LinkHelper

Defined in:
lib/occi/cli/helpers/link_helper.rb

Instance Method Summary collapse

Instance Method Details


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/occi/cli/helpers/link_helper.rb', line 3

def helper_link(options, output = nil)
  location = nil

  unless options.resource.start_with?(options.endpoint) || options.resource.start_with?('/')
    raise "Given resource is not a valid instance URL! #{options.resource.inspect}"
  end

  unless options.links.length == 1
    raise "You can assign only one link at a time!"
  end

  link = sanitize_instance_link(options.links.first)
  unless link.start_with?('/')
    raise "Given link is not a valid instance URL! #{link.inspect}"
  end

  link_kind = helper_link_kind(options, link)
  location = helper_link_create_link(options, link_kind, link)

  return location if output.nil?

  puts location
end

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/occi/cli/helpers/link_helper.rb', line 60

def helper_link_attach_mixins(mixins, link)
  return if mixins.blank?

  Occi::Cli::Log.debug "with mixins: #{mixins.inspect}"

  mixins.to_a.each do |mxn|
    Occi::Cli::Log.debug "Adding mixin #{mxn.inspect} to #{link.inspect}"

    orig_mxn = model.get_by_id(mxn.type_identifier)
    if orig_mxn.blank?
      orig_mxn = mixin(mxn.term, mxn.scheme.chomp('#'), true)
      raise Occi::Cli::Errors::MixinLookupError,
          "The specified mixin is not declared in the model! #{mxn.type_identifier.inspect}" if orig_mxn.blank?
    end

    link.mixins << orig_mxn
  end
end

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/occi/cli/helpers/link_helper.rb', line 44

def helper_link_create_link(options, link_kind, link)
  Occi::Cli::Log.debug "Linking #{link.inspect} to #{options.resource.inspect}"

  link_instance = Occi::Core::Link.new(link_kind)
  link_instance.source = sanitize_instance_link(options.resource)
  link_instance.target = link

  helper_link_attach_mixins(options.mixins, link_instance)

  options.attributes.names.each_pair do |attribute, value|
    link_instance.attributes[attribute.to_s] = value
  end

  create link_instance
end

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/occi/cli/helpers/link_helper.rb', line 27

def helper_link_kind(options, link)
  raise "No valid links given!" if link.blank?

  case link
  when /\/network\//
    link_kind = model.get_by_id("http://schemas.ogf.org/occi/infrastructure#networkinterface")
    raise "#{options.endpoint.inspect} does not support networkinterface links!" unless link_kind
  when /\/storage\//
    link_kind = model.get_by_id("http://schemas.ogf.org/occi/infrastructure#storagelink")
    raise "#{options.endpoint.inspect} does not support storagelink links!" unless link_kind
  else
    raise "Unknown link target #{link.inspect}! Only network and storage targets are supported!"
  end

  link_kind
end