Sha256: 9d016cff735f182ad34d3d0f6ec4d5f52ab14588767bfe8e9c2870a959928ff0

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'json'
require 'rest-client'

module StackAgent
  class Instance
    attr_accessor :instance_token

    def initialize(instance_token = nil)
      self.instance_token = instance_token
    end

    def register
      config = StackAgent.configuration

      return false unless config.app_token && config.name && config.uri

      url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks"

      data = {
        group: config.group,
        name: config.name,
        uri: config.uri
      }

      begin
        response = RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).post(data)
        self.instance_token = JSON.parse(response.body)['id']
      rescue RestClient::BadRequest => ex
        false
      end
    end

    def unregister
      raise 'Not registered' unless registered?

      config = StackAgent.configuration
      url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks/#{instance_token}"

      begin
        RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).delete
        true
      rescue RestClient::BadRequest => ex
        false
      end
    end

    def registered?
      instance_token != nil
    end

    def self.stacks
      config = StackAgent.configuration
      url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks"
      response = RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).get
      JSON.parse(response.body)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stack-agent-0.1.0 lib/stack-agent/instance.rb