module SakurraForm::Helper

Public Instance Methods

build_state_network(col_network) click to toggle source
# File lib/sakurraform/helper.rb, line 49
    def build_state_network(col_network)
      table_data = []
      col_network.resources.each do |resource|
        table_datum = {}
#        pp resource
        table_datum[:name] = resource.name
        table_datum[:mode] = resource.mode
        table_datum[:sakurraform_name] = resource.resource_id ? resource.resource_id : 'not created'
        table_datum[:sakura_id] = resource.remote_state ? resource.remote_state[:id] : 'not created'
        if resource.mode == 'router'
          table_datum[:subnets] = resource.remote_state && resource.remote_state[:subnets] ? resource.remote_state[:subnets].first['NetworkAddress'] + '/' + resource.remote_state[:subnets].first['NetworkMaskLen'].to_s  : 'not created'
          table_datum[:gateway] = resource.remote_state && resource.remote_state[:subnets] ? resource.remote_state[:subnets].first['DefaultRoute'] : 'not created'
        end

        table_data << table_datum
      end
      table_data
    end
build_state_server(col_server) click to toggle source
# File lib/sakurraform/helper.rb, line 68
def build_state_server(col_server)
  table_data = []
  col_server.resources.each do |resource|
    table_datum = {}
    # pp resource
    table_datum[:name] = resource.name
    table_datum[:sakurraform_name] = resource.resource_id ? resource.resource_id : 'not created'
    table_datum[:sakura_id] = resource.remote_state ? resource.remote_state[:id] : 'not created'
    table_datum[:status] = resource.remote_state && resource.remote_state[:instance] ? resource.remote_state[:instance]['Status'] : 'not created'
    table_datum[:last_state_changed] = resource.remote_state && resource.remote_state[:instance] ? resource.remote_state[:instance]['StatusChangedAt'] : 'not created'
    table_datum[:ipaddress] = resource.remote_state && resource.remote_state[:interfaces] ? resource.remote_state[:interfaces].first['UserIPAddress'] : 'not created'

    table_datum[:network] = resource.remote_state && resource.remote_state[:interfaces] ? resource.remote_state[:interfaces].map {|i| i['Switch']['ID'] } : 'not created'

    table_data << table_datum
  end
  table_data
end
fog_client(service) click to toggle source
# File lib/sakurraform/helper.rb, line 36
def fog_client(service)
  # Pending
end
get_offset_address(ip, offset) click to toggle source
# File lib/sakurraform/helper.rb, line 44
def get_offset_address(ip, offset)
  dec_ip = IPAddress(ip).to_i + offset
  IPAddr.new(dec_ip, Socket::AF_INET).to_s
end
init_s3() click to toggle source
# File lib/sakurraform/helper.rb, line 87
def init_s3
  AWS::S3.new(
    :access_key_id => Fog.credentials[:sakura_object_storage_bucket],
    :secret_access_key => Fog.credentials[:sakura_object_storage_token] ,
    :s3_endpoint => 'b.sakurastorage.jp',
    :use_ssl => true
  )
end
name_plus_uuid(name) click to toggle source
# File lib/sakurraform/helper.rb, line 40
def name_plus_uuid(name)
  [name, UUID.generate].join('-')
end
resolve_id_by_name(type, name) click to toggle source
# File lib/sakurraform/helper.rb, line 4
def resolve_id_by_name(type, name)
  files = Dir.glob("state/#{type}/#{name}-????????-????-????-????-????????????.yml")
  return nil if files.empty?
  raise "Abort: Same name detected." if files.size > 1
  File.basename(files.first, '.yml')
end
resolve_sakura_id_by_combined(c_resource) click to toggle source
# File lib/sakurraform/helper.rb, line 24
def resolve_sakura_id_by_combined(c_resource)
  type, name = split_combined_resource(c_resource)
  sakura_id = resolve_sakura_id_by_name(type, name)
  raise "Abort: Depend resource #{c_resource} not created." unless sakura_id
  sakura_id
end
resolve_sakura_id_by_name(type, name) click to toggle source
# File lib/sakurraform/helper.rb, line 18
def resolve_sakura_id_by_name(type, name)
  resouce_id = resolve_id_by_name(type, name)
  return nil unless resouce_id
  resolve_sakura_id_by_resource_id(type, resouce_id)
end
resolve_sakura_id_by_resource_id(type, resouce_id) click to toggle source
# File lib/sakurraform/helper.rb, line 11
def resolve_sakura_id_by_resource_id(type, resouce_id)
  state_file = "state/#{type}/#{resouce_id}.yml"
  return nil unless File.exists?(state_file)
  resource = YAML.load(File.read(state_file))
  resource[:id]
end
split_combined_resource(c_resource) click to toggle source
# File lib/sakurraform/helper.rb, line 31
def split_combined_resource(c_resource)
  type, name = c_resource.split('[').first, c_resource.match(/\[([\w]+)\]/)[1]
  [ type, name ]
end