Class: TrainPlugins::AliCloud::Connection

Inherits:
Train::Plugins::Transport::BaseConnection
  • Object
show all
Includes:
Platform
Defined in:
lib/train-alicloud/connection.rb

Overview

You must inherit from BaseConnection.

Instance Method Summary collapse

Methods included from Platform

#platform

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/train-alicloud/connection.rb', line 30

def initialize(options)
  # 'options' here is a hash, Symbol-keyed,
  # of what Train.target_config decided to do with the URI that it was
  # passed by `inspec -t` (or however the application gathered target information)
  # Some plugins might use this moment to capture credentials from the URI,
  # and the configure an underlying SDK accordingly.
  # You might also take a moment to manipulate the options.
  # Have a look at the Local, SSH, and AWS transports for ideas about what
  # you can do with the options.

  # Override for any cli options
  # alicloud://region
  options[:region] = options[:host] || options[:region]

  # Now let the BaseConnection have a chance to configure itself.
  super(options)

  # Force enable caching.
  enable_cache :api_call

  # Why are we doing this?
  ENV["ALICLOUD_REGION"] = @options[:region] if @options[:region]
end

Instance Method Details

#alicloud_client(api:, api_version:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/train-alicloud/connection.rb', line 54

def alicloud_client(api:, api_version:)
  region = @options[:region]

  endpoint ||= if api == "sts"
                 "https://#{api}.aliyuncs.com"
               else
                 "https://#{api}.#{region}.aliyuncs.com"
               end

  RPCClient.new(
    access_key_id:     @options[:access_key_id],
    access_key_secret: @options[:secret_access_key],
    endpoint:          endpoint,
    api_version:       api_version
  )
end

#alicloud_resource(klass, args) ⇒ Object



71
72
73
# File 'lib/train-alicloud/connection.rb', line 71

def alicloud_resource(klass, args)
  klass.new(args)
end

#unique_identifierObject



80
81
82
83
84
# File 'lib/train-alicloud/connection.rb', line 80

def unique_identifier
  # use alicloud account id
  caller_identity = alicloud_client(api: "sts", api_version: "2015-04-01").request(action: "GetCallerIdentity")
  caller_identity["AccountId"]
end

#uriObject

TODO: determine exactly what this is used for



76
77
78
# File 'lib/train-alicloud/connection.rb', line 76

def uri
  "alicloud://#{@options[:region]}"
end