Class: Ec2::Profile

Inherits:
Object
  • Object
show all
Includes:
Helper, Logger
Defined in:
lib/ec2/profile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#error

Methods included from Logger

#debug?, #logger, logger, #stderr, stderr

Constructor Details

#initialize(api: nil, data: nil) ⇒ Profile



14
15
16
17
18
# File 'lib/ec2/profile.rb', line 14

def initialize(api: nil, data: nil)
  @data = deep_copy(data) || {}
  @api = api
  init_network
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



12
13
14
# File 'lib/ec2/profile.rb', line 12

def api
  @api
end

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/ec2/profile.rb', line 12

def data
  @data
end

Instance Method Details

#create(transform: true, &block) ⇒ Object



20
21
22
23
# File 'lib/ec2/profile.rb', line 20

def create(transform: true, &block)
  instance_eval &block
  transform_name_to_id if transform
end

#extends(value) ⇒ Object



36
37
38
# File 'lib/ec2/profile.rb', line 36

def extends(value)
  @data.store "extends", value
end

#init_networkObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/ec2/profile.rb', line 25

def init_network
  return if @data['network_interfaces'].is_a? Array
  @data['network_interfaces'] = [
    {
      "DeviceIndex" => 0,
      "AssociatePublicIpAddress" => true,
      "SecurityGroupId" => []
     }
  ]
end

#security_group(name, interface: 0) ⇒ Object



44
45
46
# File 'lib/ec2/profile.rb', line 44

def security_group(name, interface: 0)
  @data["network_interfaces"][interface]["SecurityGroupId"] << name
end

#security_groups(*names, interface: 0) ⇒ Object



52
53
54
# File 'lib/ec2/profile.rb', line 52

def security_groups(*names, interface: 0)
  names.each { |name| security_group name, interface: interface }
end

#size(value) ⇒ Object



40
41
42
# File 'lib/ec2/profile.rb', line 40

def size(value)
  @data.store "size", value
end

#subnet(name, interface: 0) ⇒ Object



48
49
50
# File 'lib/ec2/profile.rb', line 48

def subnet(name, interface: 0)
  @data["network_interfaces"][interface]["SubnetId"] = name
end

#volume(device:, type: "gp2", size:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ec2/profile.rb', line 56

def volume(device:, type: "gp2", size:)
  @data["volumes"] ||= []
  volumes = @data["volumes"]
  volume = {
    "device" => device,
    "type" => type,
    "size" => size
  }
  existing_volume = volumes.find { |v| v["device"] == device }
  if existing_volume
    existing_volume.merge! volume
  else
    volumes << volume
  end
end