Class: Rancher::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/rancher/type.rb

Overview

An Object type inside of Rancher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Type

Returns a new instance of Type.



8
9
10
# File 'lib/rancher/type.rb', line 8

def initialize(schema)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



6
7
8
# File 'lib/rancher/type.rb', line 6

def schema
  @schema
end

Class Method Details

.list_href(url, opts) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rancher/type.rb', line 61

def self.list_href(url, opts)
  qs = []
  opts ||= {}

  uri = Addressable::URI.parse(url) unless url.empty? || url.nil?
  uri.query_values.each do |key, value|
    qs.push("#{key}=#{value}")
  end if uri.query_values

  if opts[:filters]
    opts[:filters].each do |field, value|
      qs.push("#{field}=#{value}") unless value.is_a?(Array)

      next unless value.is_a?(Array)

      qs.concat value.map do |val|
        if val.is_a?(Hash) && (val.key?(:modifier) || val.key?(:value))
          name = "#{field}"
          name += "_#{val[:modifier]}" if val.key?(:modifier) && val[:modifier] != '' && val[:modifier] != 'eq'

          str = "#{name}="

          str += "#{val[:value]}" if val.key?(:value)

          str
        else
          "#{field}=#{val}"
        end
      end
    end
  end

  if opts[:sort] && opts[:sort].key?(:name)
    qs.push("sort=#{opts[:sort][:name]}")
    qs.push('order=desc') if opts[:sort].key?(:order) && opts[:sort][:order] == 'desc'
  end

  if opts[:pagination] && opts[:pagination].key?(:limit)
    qs.push("limit=#{opts[:pagination][:limit]}")
    qs.push("marker=#{opts[:pagination][:marker]}") if opts[:pagination].key?(:marker)
  end

  qs.concat opts[:include].map do |inc|
    "include=#{inc}"
  end if opts[:include] && opts[:include].is_a?(Array)

  uri.query = qs.join('&') if qs.size > 0

  uri.to_s
end

Instance Method Details

#by_id(id) ⇒ Object



12
13
14
# File 'lib/rancher/type.rb', line 12

def by_id(id)
  Rancher.get url(id)
end

#collection_field(name) ⇒ Object



51
52
53
54
# File 'lib/rancher/type.rb', line 51

def collection_field(name)
  fields = @schema.get_collectionFields
  fields[name.to_sym] if fields[name.to_sym]
end

#create(attrs) ⇒ Object



29
30
31
32
33
# File 'lib/rancher/type.rb', line 29

def create(attrs)
  attrs = attrs.meta if attrs.is_a?(Rancher::Resource)

  Rancher.post(url, attrs)
end

#query(filters = {}, sort = {}, pagination = {}, include = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rancher/type.rb', line 16

def query(filters = {}, sort = {}, pagination = {}, include = {})
  opts = {
    filters: filters,
    sort: sort,
    pagination: pagination,
    include: include
  }

  link = self.class.list_href(url, opts)

  Rancher.get link
end

#remove!(id_or_obj) ⇒ Object



35
36
37
38
39
# File 'lib/rancher/type.rb', line 35

def remove!(id_or_obj)
  id = id_or_obj.get_id if id_or_obj.is_a?(Rancher::Resource)

  Rancher.delete url(id)
end

#resource_field(name) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/rancher/type.rb', line 41

def resource_field(name)
  if schema.in_meta?('resourceFields')
    fields = schema.get_resourceFields
  else
    fields = schema.get_fields
  end

  fields[name.to_sym] if fields[name.to_sym]
end

#url(id = nil) ⇒ Object



56
57
58
59
# File 'lib/rancher/type.rb', line 56

def url(id = nil)
  id = "/#{id}" unless id.nil?
  "#{@schema.get_link('collection')}#{id}"
end