Class: Pvectl::Config::Models::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/config/models/context.rb

Overview

Represents a context linking a cluster to a user.

Context is an immutable value object that binds together a cluster and user configuration, similar to kubectl contexts. It optionally includes a default node for operations.

Examples:

Creating a context

context = Context.new(
  name: "production",
  cluster_ref: "pve-prod",
  user_ref: "admin",
  default_node: "pve1"
)

Creating from YAML config hash

hash = {
  "name" => "prod",
  "context" => {
    "cluster" => "production",
    "user" => "admin-prod",
    "default-node" => "pve1"
  }
}
context = Context.from_hash(hash)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, cluster_ref:, user_ref:, default_node: nil) ⇒ Context

Creates a new Context instance.

Parameters:

  • name (String)

    unique name for this context

  • cluster_ref (String)

    name of the cluster to use

  • user_ref (String)

    name of the user to use

  • default_node (String, nil) (defaults to: nil)

    optional default node



50
51
52
53
54
55
# File 'lib/pvectl/config/models/context.rb', line 50

def initialize(name:, cluster_ref:, user_ref:, default_node: nil)
  @name = name
  @cluster_ref = cluster_ref
  @user_ref = user_ref
  @default_node = default_node
end

Instance Attribute Details

#cluster_refString (readonly)

Returns reference to cluster name.

Returns:

  • (String)

    reference to cluster name



36
37
38
# File 'lib/pvectl/config/models/context.rb', line 36

def cluster_ref
  @cluster_ref
end

#default_nodeString? (readonly)

Returns default node for operations.

Returns:

  • (String, nil)

    default node for operations



42
43
44
# File 'lib/pvectl/config/models/context.rb', line 42

def default_node
  @default_node
end

#nameString (readonly)

Returns unique name identifying this context.

Returns:

  • (String)

    unique name identifying this context



33
34
35
# File 'lib/pvectl/config/models/context.rb', line 33

def name
  @name
end

#user_refString (readonly)

Returns reference to user name.

Returns:

  • (String)

    reference to user name



39
40
41
# File 'lib/pvectl/config/models/context.rb', line 39

def user_ref
  @user_ref
end

Class Method Details

.from_hash(hash) ⇒ Context

Creates a Context from a kubeconfig-style hash structure.

Examples:

Hash structure

{
  "name" => "prod",
  "context" => {
    "cluster" => "production",
    "user" => "admin-prod",
    "default-node" => "pve1"
  }
}

Parameters:

  • hash (Hash)

    hash with “name” and “context” keys

Returns:

  • (Context)

    new context instance



71
72
73
74
75
76
77
78
79
80
# File 'lib/pvectl/config/models/context.rb', line 71

def self.from_hash(hash)
  context_data = hash["context"] || {}

  new(
    name: hash["name"],
    cluster_ref: context_data["cluster"],
    user_ref: context_data["user"],
    default_node: context_data["default-node"]
  )
end

Instance Method Details

#to_hashHash

Converts the context to a kubeconfig-style hash structure.

Returns:

  • (Hash)

    hash representation suitable for YAML serialization



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pvectl/config/models/context.rb', line 85

def to_hash
  context_data = {
    "cluster" => cluster_ref,
    "user" => user_ref
  }
  context_data["default-node"] = default_node if default_node

  {
    "name" => name,
    "context" => context_data
  }
end