Class: EksCli::Spotinst::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/eks_cli/spotinst/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(account_id: nil, api_token: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/eks_cli/spotinst/client.rb', line 10

def initialize(account_id: nil, api_token: nil)
  @account_id =  || ENV['SPOTINST_ACCOUNT_ID']
  @api_token = api_token || ENV['SPOTINST_API_TOKEN']
  if @account_id == nil
    raise "please set SPOTINST_ACCOUNT_ID environment variable"
  end

  if @api_token == nil
    raise "please set SPOTINST_API_TOKEN environment variable"
  end
  self.class.headers({"Authorization" => "Bearer #{@api_token}",
                      "Content-Type" => "application/json"})
end

Instance Method Details

#delete_elastigroup(group_id) ⇒ Object



39
40
41
42
# File 'lib/eks_cli/spotinst/client.rb', line 39

def delete_elastigroup(group_id)
  Log.info "deleting elastigroup #{group_id}"
  Log.info self.class.delete("/aws/ec2/group/#{group_id}?accountId=#{@account_id}")
end

#import_asg(region, asg_name, instance_types) ⇒ Object



24
25
26
27
28
# File 'lib/eks_cli/spotinst/client.rb', line 24

def import_asg(region, asg_name, instance_types)
  body = instance_types ? {group: {spotInstanceTypes: instance_types}} : {}
  self.class.post("/aws/ec2/group/autoScalingGroup/import?region=#{region}&accountId=#{@account_id}&autoScalingGroupName=#{asg_name}",
                  body: body.to_json)
end

#list_groupsObject



30
31
32
# File 'lib/eks_cli/spotinst/client.rb', line 30

def list_groups
  self.class.get("/aws/ec2/group?accountId=#{@account_id}")
end

#scale(group_id, min, max) ⇒ Object



34
35
36
37
# File 'lib/eks_cli/spotinst/client.rb', line 34

def scale(group_id, min, max)
  Log.info "scaling elastigroup #{group_id} {#{min}, #{max}}"
  Log.info self.class.put("/aws/ec2/group/#{group_id}/capacity?accountId=#{@account_id}", body: {capacity: {minimum: min, maximum: max, target: max}}.to_json)
end