Class: MrMurano::ProductResources

Inherits:
ProductBase show all
Includes:
ProductOnePlatformRpcShim, SyncUpDown
Defined in:
lib/MrMurano/Product-Resources.rb

Overview

Manage the resources on a Product

There isn’t an okami-shim for most of this, it maps right over to 1P-RPC. Or better stated, that’s all okami-shim is.

Instance Method Summary collapse

Methods included from ProductOnePlatformRpcShim

#model_rid

Methods included from SyncUpDown

#dodiff, #ignoring, #locallist, #searchFor, #status, #syncdown, #syncup, #toRemoteItem, #tolocalname

Methods inherited from ProductBase

#endPoint

Methods included from Verbose

#debug, #error, #outf, #tabularize, #verbose, #warning

Methods included from Http

#curldebug, #delete, #get, #http, #http_reset, #isJSON, #json_opts, #post, #postf, #put, #set_def_headers, #showHttpError, #token, #workit

Constructor Details

#initializeProductResources

Returns a new instance of ProductResources.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/MrMurano/Product-Resources.rb', line 14

def initialize
  super
  @uriparts << :proxy
  @uriparts << 'onep:v1'
  @uriparts << :rpc
  @uriparts << :process
  @model_rid = nil

  @itemkey = :rid # this is the key that is the identifier used by murano
  @location = location
end

Instance Method Details

#create(alias_id, format = :string) ⇒ Object

Create a new resource in the prodcut



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/MrMurano/Product-Resources.rb', line 87

def create(alias_id, format=:string)
  raise "Alias cannot be nil" if alias_id.nil?
  # create then map.
  rid = do_rpc({:id=>1,
              :procedure=>:create,
              :arguments=>[:dataport,
                           {:format=>format,
                            :name=>alias_id,
                            :retention=>{:count=>1,:duration=>:infinity}
                           }
                          ]
  })
  return rid unless not rid.kind_of?(String) or rid.match(/\p{XDigit}{40}/)

  do_rpc({:id=>1,
          :procedure=>:map,
          :arguments=>[:alias, rid, alias_id]
  })
end

#docmp(itemA, itemB) ⇒ Object

True if itemA and itemB are different



208
209
210
# File 'lib/MrMurano/Product-Resources.rb', line 208

def docmp(itemA, itemB)
  itemA[:alias] != itemB[:alias] or itemA[:format] != itemB[:format]
end

#download(local, item) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/MrMurano/Product-Resources.rb', line 157

def download(local, item)
  # needs to append/merge with file
  # for now, we'll read, modify, write
  data = fetch(item[:rid])
  item[:format] = data[:description][:format]

  here = []
  if local.exist? then
    local.open('rb') {|io| here = YAML.load(io)}
    here = [] if here == false
    if here.kind_of?(Hash) and here.has_key?('resources') then
      here = here['resources'].map{|i| Hash.transform_keys_to_symbols(i)}
    else
      here = []
    end
  end
  here.delete_if do |i|
    i[:alias] == item[:alias]
  end
  here << item.reject{|k,v| k==:synckey or k==:rid}
  here.map!{|i| Hash.transform_keys_to_strings(i)}
  local.dirname.mkpath
  local.open('wb') do |io|
    io << {'resources'=>here}.to_yaml
  end
end

#fetch(rid) ⇒ Object

Fetch data from one resource



71
72
73
74
75
76
# File 'lib/MrMurano/Product-Resources.rb', line 71

def fetch(rid)
  do_rpc({:id=>1,
          :procedure=>:info,
          :arguments=>[rid, {}],
  })
end

#infoObject

Get 1P info about the prodcut



47
48
49
50
51
52
# File 'lib/MrMurano/Product-Resources.rb', line 47

def info
  do_rpc({:id=>1,
          :procedure=>:info,
          :arguments=>[model_rid, {}]
  })
end

#listObject

Return a list of the product resources as items



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/MrMurano/Product-Resources.rb', line 55

def list()
  data = info()
  ret = []
  data[:aliases].each do |rid, aliases|
    aliases.each do |al|
      ret << {
        :alias => al,
        :rid => rid
      }
    end
  end

  ret
end

#localitems(from) ⇒ Object

Get a local list of items from the single file



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/MrMurano/Product-Resources.rb', line 133

def localitems(from)
  from = Pathname.new(from) unless from.kind_of? Pathname
  debug "#{self.class.to_s}: Getting local items from: #{from}"
  if not from.exist? then
    warning "Skipping missing #{from.to_s}"
    return []
  end
  unless from.file? then
    warning "Cannot read from #{from.to_s}"
    return []
  end

  here = []
  from.open {|io| here = YAML.load(io) }
  return [] if here == false

  if here.kind_of?(Hash) and here.has_key?('resources') then
    here['resources'].map{|i| Hash.transform_keys_to_symbols(i)}
  else
    warning "Unexpected data in #{from.to_s}"
    []
  end
end

#locationObject

Get the location of the product spec file



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/MrMurano/Product-Resources.rb', line 27

def location
  # If location.specs is defined, then all spec files are assume to be relative
  # to that and location.base, otherwise they're relative to only location.base
  #
  # If there is a p-<product.id>.spec key, then that is the file name.
  # Otherwise use product.spec

  name = $cfg['product.spec']
  prid = $cfg['product.id']
  name = $cfg["p-#{prid}.spec"] unless prid.nil? or $cfg["p-#{prid}.spec"].nil?
  raise "No spec file named; run `mr config product.spec <specfile>`" if name.nil?

  unless $cfg['location.specs'].nil? then
    name = ::File.join($cfg['location.specs'], name)
  end
  debug " spec file name => #{name}"
  name
end

#remove(rid) ⇒ Object

Remove a resource by RID



79
80
81
82
83
84
# File 'lib/MrMurano/Product-Resources.rb', line 79

def remove(rid)
  do_rpc({:id=>1,
          :procedure=>:drop,
          :arguments=>[rid]
  })
end

#removelocal(dest, item) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/MrMurano/Product-Resources.rb', line 184

def removelocal(dest, item)
  # needs to append/merge with file
  # for now, we'll read, modify, write
  here = []
  if dest.exist? then
    dest.open('rb') {|io| here = YAML.load(io)}
    here = [] if here == false
    if here.kind_of?(Hash) and here.has_key?('resources') then
      here = here['resources'].map{|i| Hash.transform_keys_to_symbols(i)}
    else
      here = []
    end
  end
  here.delete_if do |it|
    it[:alias] == item[:alias]
  end
  here.map!{|i| Hash.transform_keys_to_strings(i)}
  dest.open('wb') do|io|
    io << {'resources'=>here}.to_yaml
  end
end

#synckey(item) ⇒ Object

Use alias for doing sync compares (The RID will change if destroyed and recreated.)



122
123
124
# File 'lib/MrMurano/Product-Resources.rb', line 122

def synckey(item)
  item[:alias]
end

#tolocalpath(into, item) ⇒ Object



128
129
130
# File 'lib/MrMurano/Product-Resources.rb', line 128

def tolocalpath(into, item)
  into
end

#upload(src, item, modify) ⇒ Object

Upload a resource. this is for SyncUpDown

Parameters:

  • modify

    Bool: True if item exists already and this is changing it



110
111
112
113
114
115
116
117
118
# File 'lib/MrMurano/Product-Resources.rb', line 110

def upload(src, item, modify)
  if modify then
    # this is usually a format change, which can only be set on create.
    # So delete then create.
    remove(item[:rid])
  end
  r = create(item[:alias], item[:format])
  raise "Create Failed: #{r}" unless r.nil?
end