terraform-enterprise-client 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +11 -53
- data/lib/terraform-enterprise-client.rb +1 -39
- data/lib/terraform-enterprise.rb +1 -0
- data/lib/terraform_enterprise.rb +2 -0
- data/lib/terraform_enterprise/api/client.rb +62 -0
- data/lib/terraform_enterprise/api/request.rb +97 -0
- data/lib/terraform_enterprise/api/resource.rb +48 -0
- data/lib/terraform_enterprise/api/resource_request.rb +12 -0
- data/lib/terraform_enterprise/api/resource_requests/configuration_versions.rb +28 -0
- data/lib/{terraform-enterprise/resource_requests/oauth-tokens.rb → terraform_enterprise/api/resource_requests/oauth_tokens.rb} +5 -4
- data/lib/{terraform-enterprise → terraform_enterprise/api}/resource_requests/organizations.rb +8 -7
- data/lib/terraform_enterprise/api/resource_requests/policies.rb +46 -0
- data/lib/terraform_enterprise/api/resource_requests/policy_checks.rb +18 -0
- data/lib/terraform_enterprise/api/resource_requests/runs.rb +53 -0
- data/lib/{terraform-enterprise → terraform_enterprise/api}/resource_requests/teams.rb +8 -8
- data/lib/terraform_enterprise/api/resource_requests/variables.rb +48 -0
- data/lib/terraform_enterprise/api/resource_requests/workspaces.rb +52 -0
- data/lib/terraform_enterprise/api/response.rb +39 -0
- data/lib/terraform_enterprise/api/version.rb +5 -0
- data/lib/terraform_enterprise_client.rb +1 -0
- metadata +24 -88
- data/bin/tfe +0 -4
- data/lib/terraform-enterprise-command-line.rb +0 -29
- data/lib/terraform-enterprise/client/request.rb +0 -82
- data/lib/terraform-enterprise/client/resource.rb +0 -19
- data/lib/terraform-enterprise/client/resource_request.rb +0 -10
- data/lib/terraform-enterprise/client/response.rb +0 -37
- data/lib/terraform-enterprise/commands/command.rb +0 -46
- data/lib/terraform-enterprise/commands/formatter.rb +0 -93
- data/lib/terraform-enterprise/commands/oauth_tokens_command.rb +0 -16
- data/lib/terraform-enterprise/commands/organizations_command.rb +0 -30
- data/lib/terraform-enterprise/commands/strings.rb +0 -79
- data/lib/terraform-enterprise/commands/teams_command.rb +0 -33
- data/lib/terraform-enterprise/commands/variables_command.rb +0 -61
- data/lib/terraform-enterprise/commands/workspaces_command.rb +0 -87
- data/lib/terraform-enterprise/resource_requests/variables.rb +0 -48
- data/lib/terraform-enterprise/resource_requests/workspaces.rb +0 -45
- data/lib/terraform-enterprise/version.rb +0 -3
data/bin/tfe
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'thor'
|
2
|
-
|
3
|
-
require 'terraform-enterprise/version'
|
4
|
-
require 'terraform-enterprise/commands/organizations_command'
|
5
|
-
require 'terraform-enterprise/commands/workspaces_command'
|
6
|
-
require 'terraform-enterprise/commands/oauth_tokens_command'
|
7
|
-
require 'terraform-enterprise/commands/teams_command'
|
8
|
-
require 'terraform-enterprise/commands/variables_command'
|
9
|
-
|
10
|
-
module TerraformEnterprise
|
11
|
-
class CommandLine < Thor
|
12
|
-
desc 'organizations <subcommand>', 'Manage organizations'
|
13
|
-
subcommand 'organizations', TerraformEnterprise::Commands::OrganizationsCommand
|
14
|
-
|
15
|
-
desc 'workspaces <subcommand>', 'Manage workspaces'
|
16
|
-
subcommand 'workspaces', TerraformEnterprise::Commands::WorkspacesCommand
|
17
|
-
|
18
|
-
desc 'oauth_tokens <subcommand>', 'Manage OAuth tokens'
|
19
|
-
subcommand 'oauth_tokens', TerraformEnterprise::Commands::OAuthTokensCommand
|
20
|
-
|
21
|
-
desc 'teams <subcommand>', 'Manage teams'
|
22
|
-
subcommand 'teams', TerraformEnterprise::Commands::TeamsCommand
|
23
|
-
|
24
|
-
desc 'variables <subcommand>', 'Manage variables'
|
25
|
-
subcommand 'variables', TerraformEnterprise::Commands::VariablesCommand
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
TerraformEnterprise::CommandLine.start(ARGV)
|
@@ -1,82 +0,0 @@
|
|
1
|
-
require 'terraform-enterprise/client/response'
|
2
|
-
|
3
|
-
module TerraformEnterprise
|
4
|
-
module API
|
5
|
-
class Request
|
6
|
-
attr_accessor :base
|
7
|
-
|
8
|
-
def initialize(api_key:, host: 'https://app.terraform.io/api/v2', debug: false)
|
9
|
-
@base = host
|
10
|
-
@api_key = api_key
|
11
|
-
@debug = debug
|
12
|
-
@headers = {
|
13
|
-
'Authorization' => "Bearer #{@api_key}",
|
14
|
-
'Content-Type' => 'application/vnd.api+json'
|
15
|
-
}
|
16
|
-
end
|
17
|
-
|
18
|
-
def get(*path)
|
19
|
-
data = path.pop if path.last.is_a?(Hash)
|
20
|
-
request(:get, path, data)
|
21
|
-
end
|
22
|
-
|
23
|
-
def delete(*path)
|
24
|
-
data = path.pop if path.last.is_a?(Hash)
|
25
|
-
request(:delete, path,data)
|
26
|
-
end
|
27
|
-
|
28
|
-
def post(*path)
|
29
|
-
data = path.pop if path.last.is_a?(Hash)
|
30
|
-
request(:post, path, data)
|
31
|
-
end
|
32
|
-
|
33
|
-
def put(*path)
|
34
|
-
data = path.pop if path.last.is_a?(Hash)
|
35
|
-
request(:put,data,data)
|
36
|
-
end
|
37
|
-
|
38
|
-
def patch(*path)
|
39
|
-
data = path.pop if path.last.is_a?(Hash)
|
40
|
-
request(:patch,path,data)
|
41
|
-
end
|
42
|
-
|
43
|
-
def request(method, path, data={}, headers={})
|
44
|
-
request = {
|
45
|
-
method: method,
|
46
|
-
url: uri(path),
|
47
|
-
headers: @headers.merge(headers || {})
|
48
|
-
}
|
49
|
-
if data
|
50
|
-
if method==:get || method==:delete
|
51
|
-
request[:headers][:params] = data
|
52
|
-
else
|
53
|
-
request[:payload] = data.is_a?(String) ? data : data.to_json
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
response = begin
|
58
|
-
RestClient::Request.execute(request)
|
59
|
-
rescue RestClient::ExceptionWithResponse => ex
|
60
|
-
ex.response
|
61
|
-
end
|
62
|
-
|
63
|
-
if @debug
|
64
|
-
puts "[DEBUG] [REQUEST:METHOD] #{request[:method].to_s.upcase} #{request[:url]}"
|
65
|
-
puts "[DEBUG] [REQUEST:HEADERS] #{request[:headers]}"
|
66
|
-
puts "[DEBUG] [REQUEST:PAYLOAD] #{data}"
|
67
|
-
puts "[DEBUG] [RESPONSE:CODE] #{response.code}"
|
68
|
-
puts "[DEBUG] [RESPONSE:BODY] #{response.body}"
|
69
|
-
end
|
70
|
-
|
71
|
-
TerraformEnterprise::API::Response.new(response)
|
72
|
-
end
|
73
|
-
|
74
|
-
private
|
75
|
-
|
76
|
-
def uri(path=[])
|
77
|
-
return path if path.is_a?(String) && path.start_with?("http")
|
78
|
-
"#{@base}/#{path.map{|p| p.to_s}.join('/')}"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module TerraformEnterprise
|
2
|
-
module API
|
3
|
-
class Resource
|
4
|
-
attr_accessor :type, :attributes, :id, :relationships, :links, :errors, :success
|
5
|
-
def initialize(data)
|
6
|
-
@id = data['id']
|
7
|
-
@type = data['type']
|
8
|
-
@attributes = data['attributes'] || {}
|
9
|
-
@relationships = data['relationships'] || {}
|
10
|
-
@links = data['links'] || []
|
11
|
-
@errors = data['errors'] || []
|
12
|
-
end
|
13
|
-
|
14
|
-
def has_errors?
|
15
|
-
!@errors.empty?
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'terraform-enterprise/client/resource'
|
2
|
-
|
3
|
-
module TerraformEnterprise
|
4
|
-
module API
|
5
|
-
class Response
|
6
|
-
attr_reader :code, :body, :data, :resource, :resources, :errors
|
7
|
-
def initialize(rest_client_response)
|
8
|
-
@code = rest_client_response.code
|
9
|
-
@body = parse(rest_client_response.body)
|
10
|
-
@data = (@body.is_a?(Hash) && @body['data']) ? @body['data'] : @body
|
11
|
-
if @data.is_a?(Hash)
|
12
|
-
@resource = TerraformEnterprise::API::Resource.new(@data)
|
13
|
-
end
|
14
|
-
if @data.is_a?(Array) && @data.all?{|a| a.is_a?(Hash)}
|
15
|
-
@resources = @data.map do |item|
|
16
|
-
TerraformEnterprise::API::Resource.new(item)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
if has_errors?
|
20
|
-
@errors = @body['errors']
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def has_errors?
|
25
|
-
@body.is_a?(Hash) && @body['errors'] && @body['errors'].is_a?(Array)
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def parse(str)
|
31
|
-
JSON.parse(str)
|
32
|
-
rescue JSON::ParserError
|
33
|
-
str
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
|
3
|
-
require 'terraform-enterprise-client'
|
4
|
-
require 'terraform-enterprise/commands/formatter'
|
5
|
-
require 'terraform-enterprise/commands/strings'
|
6
|
-
|
7
|
-
module TerraformEnterprise
|
8
|
-
module Commands
|
9
|
-
class Command < Thor
|
10
|
-
class_option :host, type: :string, desc: 'Set host address for private Terraform Enterprise'
|
11
|
-
class_option :token, type: :string, desc: 'Set the auth token, defaults to TERRAFORM_ENTERPRISE_TOKEN environment variable'
|
12
|
-
class_option :color, type: :boolean, default: true, desc: 'If disabled the ANSI color codes will not be used'
|
13
|
-
class_option :except, type: :array, desc: 'List of fields that should not be displayed'
|
14
|
-
class_option :only, type: :array, desc: 'List of fields that should be displayed'
|
15
|
-
class_option :all, type: :boolean, default: false, desc: "Return all fields, not just summary"
|
16
|
-
class_option :value, type: :boolean, default: false, desc: 'Only return the value; i.e. do not show keys'
|
17
|
-
class_option :debug, type: :boolean, default: false, desc: 'Show debug logs'
|
18
|
-
|
19
|
-
no_commands do
|
20
|
-
def render(obj, default_options={})
|
21
|
-
calculated_options = if(options[:all])
|
22
|
-
symbolize_keys(options.to_h)
|
23
|
-
else
|
24
|
-
symbolize_keys(default_options).merge(symbolize_keys(options.to_h))
|
25
|
-
end
|
26
|
-
TerraformEnterprise::Commands::Formatter.render obj, calculated_options
|
27
|
-
end
|
28
|
-
|
29
|
-
def client
|
30
|
-
settings = { }
|
31
|
-
settings[:api_key] = options[:token] || ENV['TERRAFORM_ENTERPRISE_TOKEN']
|
32
|
-
settings[:host] = options[:host] if options[:host]
|
33
|
-
settings[:debug] = options[:debug] if options[:debug]
|
34
|
-
TerraformEnterprise::Client.new(settings)
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def symbolize_keys(hash)
|
40
|
-
JSON.parse(JSON[hash], symbolize_names: true)
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,93 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
require 'colorize'
|
3
|
-
require 'terminal-table'
|
4
|
-
|
5
|
-
require 'terraform-enterprise-client'
|
6
|
-
|
7
|
-
module TerraformEnterprise
|
8
|
-
module Commands
|
9
|
-
module Formatter
|
10
|
-
def self.render(obj, options={})
|
11
|
-
String.disable_colorization = !options[:color]
|
12
|
-
if obj.is_a?(TerraformEnterprise::API::Response)
|
13
|
-
if obj.code >= 200 && obj.code < 300
|
14
|
-
if obj.resources
|
15
|
-
puts render_resource_table(obj.resources, options)
|
16
|
-
elsif obj.resource
|
17
|
-
puts render_resource(obj.resource, options)
|
18
|
-
else
|
19
|
-
puts "Success (#{obj.code})".green
|
20
|
-
end
|
21
|
-
elsif obj.has_errors?
|
22
|
-
obj.errors.each do |error|
|
23
|
-
if error['status'] && error['title']
|
24
|
-
puts "Error (#{error['status']}): #{error['title']}".red
|
25
|
-
else
|
26
|
-
puts "Error (#{obj.code}): #{error}".red
|
27
|
-
end
|
28
|
-
exit(false)
|
29
|
-
end
|
30
|
-
else
|
31
|
-
puts "Unknown server response (#{obj.code})".yellow
|
32
|
-
puts obj.body
|
33
|
-
exit(false)
|
34
|
-
end
|
35
|
-
else
|
36
|
-
puts "Unknown content".yellow
|
37
|
-
puts obj
|
38
|
-
exit(false)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def self.parse_resource(resource, options)
|
45
|
-
parsed_resource = flatten_dotted_hash(resource.attributes)
|
46
|
-
parsed_resource = {'id' => resource.id}.merge(parsed_resource) if resource.id
|
47
|
-
(options[:except] || []).each do |excluded|
|
48
|
-
parsed_resource.delete_if {|key,value| key.to_s.start_with?(excluded.to_s) }
|
49
|
-
end
|
50
|
-
if options[:only] && options[:only].length > 0
|
51
|
-
parsed_resource.select! do |key, value|
|
52
|
-
options[:only].any?{ |included| key.to_s.start_with?(included.to_s) }
|
53
|
-
end
|
54
|
-
end
|
55
|
-
parsed_resource
|
56
|
-
end
|
57
|
-
|
58
|
-
def self.render_resource(resource, options)
|
59
|
-
parsed_resource = parse_resource(resource, options)
|
60
|
-
parsed_resource.keys.map do |key|
|
61
|
-
value = parsed_resource[key]
|
62
|
-
options[:value] ? value : "#{key.bold}: #{value}"
|
63
|
-
end.join("\n")
|
64
|
-
end
|
65
|
-
|
66
|
-
def self.render_resource_table(resources, options)
|
67
|
-
if options[:table] && !options[:value]
|
68
|
-
parsed_resources = resources.map{|resource| parse_resource(resource,options)}
|
69
|
-
keys = parsed_resources.map{|resource| resource.keys }.flatten.uniq
|
70
|
-
rows = parsed_resources.map do |resource|
|
71
|
-
keys.map{|key| resource[key]}
|
72
|
-
end
|
73
|
-
table = Terminal::Table.new headings: keys, rows: rows
|
74
|
-
table
|
75
|
-
else
|
76
|
-
out = resources.map{|resource| render_resource(resource, options)}.join("\n#{'-' * 10}\n")
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.flatten_hash(h,f=[],g={})
|
81
|
-
return g.update({ f=>h }) unless h.is_a?(Hash) || h.is_a?(Array)
|
82
|
-
h.each { |k,r| flatten_hash(r,f+[k],g) } if h.is_a?(Hash)
|
83
|
-
h.each_with_index { |r, k| flatten_hash(r,f+[k],g) } if h.is_a?(Array)
|
84
|
-
g
|
85
|
-
end
|
86
|
-
|
87
|
-
def self.flatten_dotted_hash(source)
|
88
|
-
flat = flatten_hash(source)
|
89
|
-
flat.keys.each_with_object({}) { |key, h| h[key.join('.')] = flat[key] }
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'terraform-enterprise/commands/command'
|
2
|
-
require 'terraform-enterprise-client'
|
3
|
-
|
4
|
-
module TerraformEnterprise
|
5
|
-
module Commands
|
6
|
-
class OAuthTokensCommand < TerraformEnterprise::Commands::Command
|
7
|
-
class_option :organization, required: true, type: :string, desc: STRINGS[:oauth_tokens][:attributes][:organization]
|
8
|
-
|
9
|
-
desc 'list', STRINGS[:oauth_tokens][:commands][:list]
|
10
|
-
option :table, type: :boolean, default: true, desc: STRINGS[:options][:table]
|
11
|
-
def list
|
12
|
-
render client.oauth_tokens.list(options)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'terraform-enterprise/commands/command'
|
2
|
-
|
3
|
-
module TerraformEnterprise
|
4
|
-
module Commands
|
5
|
-
class OrganizationsCommand < TerraformEnterprise::Commands::Command
|
6
|
-
CMD_STR = STRINGS[:organizations][:commands]
|
7
|
-
|
8
|
-
desc 'list', CMD_STR[:list]
|
9
|
-
option :table, type: :boolean, default: true, desc: STRINGS[:options][:table]
|
10
|
-
def list
|
11
|
-
render client.organizations.list, only: [:id, :name, 'created-at', :email]
|
12
|
-
end
|
13
|
-
|
14
|
-
desc 'create <name> <email>', CMD_STR[:create]
|
15
|
-
def create(name, email)
|
16
|
-
render client.organizations.create(name: name, email: email)
|
17
|
-
end
|
18
|
-
|
19
|
-
desc 'get <name>', CMD_STR[:get]
|
20
|
-
def get(name)
|
21
|
-
render client.organizations.get(name:name)
|
22
|
-
end
|
23
|
-
|
24
|
-
desc 'delete <name>', CMD_STR[:delete]
|
25
|
-
def delete(name)
|
26
|
-
render client.organizations.delete(name:name)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,79 +0,0 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module TerraformEnterprise
|
4
|
-
module Commands
|
5
|
-
STRINGS = {
|
6
|
-
options: {
|
7
|
-
table: 'Format list output as a table'
|
8
|
-
},
|
9
|
-
workspaces: {
|
10
|
-
attributes: {
|
11
|
-
terraform_version: 'Version of Terraform to use for this workspace.',
|
12
|
-
working_directory: 'Relative path that Terraform will execute within.',
|
13
|
-
oauth_token: 'VCS Connection (OAuth Conection + Token) to use as identified; obtained from the oauth_tokens subcommand.',
|
14
|
-
branch: 'Repository branch that Terraform will execute from.',
|
15
|
-
ingress_submodules: 'Submodules should be fetched when cloning the VCS repository.',
|
16
|
-
repo: 'Reference to VCS repository in the format :org/:repo.',
|
17
|
-
import_legacy: 'Specifies the legacy Environment to use as the source of the migration/',
|
18
|
-
organization: 'Organization to which this workspaces belongs to.',
|
19
|
-
auto_apply: 'Auto-apply enabled',
|
20
|
-
},
|
21
|
-
commands: {
|
22
|
-
create: 'Create a new workspace',
|
23
|
-
list: 'List worksapces in the organization',
|
24
|
-
get: 'Get workspace details by name',
|
25
|
-
delete: 'Delete the workspace',
|
26
|
-
update: 'Update the workspace',
|
27
|
-
lock: 'Lock the workspace by workspace ID',
|
28
|
-
unlock: 'Unlock the workspace by workspace ID'
|
29
|
-
}
|
30
|
-
},
|
31
|
-
organizations: {
|
32
|
-
commands: {
|
33
|
-
create: 'Create a new organization',
|
34
|
-
list: 'List all the organizations',
|
35
|
-
get: 'Get organization details by name',
|
36
|
-
delete: 'Delete the organization',
|
37
|
-
},
|
38
|
-
attributes: { }
|
39
|
-
},
|
40
|
-
teams: {
|
41
|
-
commands: {
|
42
|
-
create: 'Create a new team',
|
43
|
-
delete: 'Delete the team by ID',
|
44
|
-
list: 'List teams in organization',
|
45
|
-
get: 'Get team details'
|
46
|
-
},
|
47
|
-
attributes: {
|
48
|
-
organization: 'Organization to which this Team belongs to.'
|
49
|
-
}
|
50
|
-
},
|
51
|
-
oauth_tokens: {
|
52
|
-
commands: {
|
53
|
-
list: 'List the OAuth tokens in the organization'
|
54
|
-
},
|
55
|
-
attributes: {
|
56
|
-
organization: 'Organization to which this OAuth Token belongs to.'
|
57
|
-
}
|
58
|
-
},
|
59
|
-
variables: {
|
60
|
-
commands: {
|
61
|
-
create: 'Create a new variable',
|
62
|
-
delete: 'Delete the variable by ID',
|
63
|
-
get: 'Get variable details',
|
64
|
-
list: 'List variables in organization',
|
65
|
-
update: 'Update a variable by ID',
|
66
|
-
},
|
67
|
-
attributes: {
|
68
|
-
organization: 'Organization to which this Variable belongs to.',
|
69
|
-
workspace: 'Workspace to which this Variable belongs to.',
|
70
|
-
category: 'The type of cateogry, probably "terrafomr" or "environment"',
|
71
|
-
hcl: 'Variable should be parsed using HCL',
|
72
|
-
sensitive:'Variable should be marked as sensitive',
|
73
|
-
value: 'Variable value',
|
74
|
-
key: 'Variable key',
|
75
|
-
}
|
76
|
-
},
|
77
|
-
}
|
78
|
-
end
|
79
|
-
end
|