Class: K8y::Client::APIBuilder

Inherits:
Module
  • Object
show all
Defined in:
lib/k8y/client/api_builder.rb

Constant Summary collapse

VERBS_TO_METHODS =
{
  create: :define_create_resource,
  delete: :define_delete_resource,
  get: :define_get_resource,
  list: :define_get_resources,
  patch: :define_patch_and_apply_resource,
  update: :define_update_resource,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api:, config:, context:) ⇒ APIBuilder

Returns a new instance of APIBuilder.



22
23
24
25
26
27
# File 'lib/k8y/client/api_builder.rb', line 22

def initialize(api:, config:, context:)
  super()
  @api = api
  @config = config
  @context = context
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



11
12
13
# File 'lib/k8y/client/api_builder.rb', line 11

def api
  @api
end

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/k8y/client/api_builder.rb', line 11

def config
  @config
end

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/k8y/client/api_builder.rb', line 11

def context
  @context
end

Instance Method Details

#build!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/k8y/client/api_builder.rb', line 29

def build!
  rest_config = REST::Config.from_kubeconfig(config, context: context, path: api.path)
  rest_client = REST::Client.new(connection: REST::Connection.from_config(rest_config))

  response = rest_client.get(as: :raw)
  resource_descriptions = JSON.parse(response.body)["resources"].map do |resource_description|
    ResourceDescription.from_hash(resource_description)
  end
  resource_descriptions.each do |resource_description|
    next if resource_description.subresource?

    VERBS_TO_METHODS
      .filter { |verb, _| resource_description.has_verb?(verb) }
      .each_value { |method| send(method, api, rest_client, resource_description) }
    api.instance_eval { self.discovered = true }
  end
end