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.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +11 -53
  3. data/lib/terraform-enterprise-client.rb +1 -39
  4. data/lib/terraform-enterprise.rb +1 -0
  5. data/lib/terraform_enterprise.rb +2 -0
  6. data/lib/terraform_enterprise/api/client.rb +62 -0
  7. data/lib/terraform_enterprise/api/request.rb +97 -0
  8. data/lib/terraform_enterprise/api/resource.rb +48 -0
  9. data/lib/terraform_enterprise/api/resource_request.rb +12 -0
  10. data/lib/terraform_enterprise/api/resource_requests/configuration_versions.rb +28 -0
  11. data/lib/{terraform-enterprise/resource_requests/oauth-tokens.rb → terraform_enterprise/api/resource_requests/oauth_tokens.rb} +5 -4
  12. data/lib/{terraform-enterprise → terraform_enterprise/api}/resource_requests/organizations.rb +8 -7
  13. data/lib/terraform_enterprise/api/resource_requests/policies.rb +46 -0
  14. data/lib/terraform_enterprise/api/resource_requests/policy_checks.rb +18 -0
  15. data/lib/terraform_enterprise/api/resource_requests/runs.rb +53 -0
  16. data/lib/{terraform-enterprise → terraform_enterprise/api}/resource_requests/teams.rb +8 -8
  17. data/lib/terraform_enterprise/api/resource_requests/variables.rb +48 -0
  18. data/lib/terraform_enterprise/api/resource_requests/workspaces.rb +52 -0
  19. data/lib/terraform_enterprise/api/response.rb +39 -0
  20. data/lib/terraform_enterprise/api/version.rb +5 -0
  21. data/lib/terraform_enterprise_client.rb +1 -0
  22. metadata +24 -88
  23. data/bin/tfe +0 -4
  24. data/lib/terraform-enterprise-command-line.rb +0 -29
  25. data/lib/terraform-enterprise/client/request.rb +0 -82
  26. data/lib/terraform-enterprise/client/resource.rb +0 -19
  27. data/lib/terraform-enterprise/client/resource_request.rb +0 -10
  28. data/lib/terraform-enterprise/client/response.rb +0 -37
  29. data/lib/terraform-enterprise/commands/command.rb +0 -46
  30. data/lib/terraform-enterprise/commands/formatter.rb +0 -93
  31. data/lib/terraform-enterprise/commands/oauth_tokens_command.rb +0 -16
  32. data/lib/terraform-enterprise/commands/organizations_command.rb +0 -30
  33. data/lib/terraform-enterprise/commands/strings.rb +0 -79
  34. data/lib/terraform-enterprise/commands/teams_command.rb +0 -33
  35. data/lib/terraform-enterprise/commands/variables_command.rb +0 -61
  36. data/lib/terraform-enterprise/commands/workspaces_command.rb +0 -87
  37. data/lib/terraform-enterprise/resource_requests/variables.rb +0 -48
  38. data/lib/terraform-enterprise/resource_requests/workspaces.rb +0 -45
  39. data/lib/terraform-enterprise/version.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8caf7297375b0e1c572b31e0bb298da08e51eb73
4
- data.tar.gz: eb3d6a9a8b143f1dc67c4446e2f666e7a29ab17b
2
+ SHA256:
3
+ metadata.gz: 7415a893ed2fafcf00c51edaf233a85de15b5f435121b3ed728d427e1d9c135f
4
+ data.tar.gz: 913f4bed946e14b29d2308ac6bd297da6262c6332214d35b5ee75cccfedd7cc3
5
5
  SHA512:
6
- metadata.gz: 41a04fbd7f45997ad85f8f13bc27d8a6c2b86369df17cd001f763198340353e3ae80441b9e4b796bc8876e1647e05587dffc9aa83639b70a3b72e5761ccc456a
7
- data.tar.gz: 41615f92de594de92c6dd8224e6d82a73eb21f6c2d93cda01b2295eb2fb7c31edabc9d7f88f544b0c3f15e2350b42444081694a391b36acf01068d9e433f4f0a
6
+ metadata.gz: 318ac9fd413f452d4275ffacfcb482880b73554134536ff9009a54a73c19579b7e9f1a40b17f9445cb2f6d604adf6cf30e74e4385b33ec48ae3b351668aca35d
7
+ data.tar.gz: 8b5d7f675667ed6679b97ca11c3663d297a2daeb4f8d91fe50b5685ecd307b18e85d1dd99356610332d97e8a9af804e0bc189c4d05def7e54f2dd3447c63e3fe
data/README.md CHANGED
@@ -1,20 +1,20 @@
1
- # Terraform Enterprise API Client & Command Line Tool
2
- A simple Ruby API client library and command line tool for the [Terraform Enterprise API](https://www.terraform.io/docs/enterprise/api/index.html).
1
+ # Terraform Enterprise API Client
2
+ A simple Ruby API client library for the [Terraform Enterprise API](https://www.terraform.io/docs/enterprise/api/index.html).
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/terraform-enterprise-client.svg)](https://badge.fury.io/rb/terraform-enterprise-client)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/1fd90e8dda31d1d402e8/maintainability)](https://codeclimate.com/github/skierkowski/terraform-enterprise-client/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/1fd90e8dda31d1d402e8/test_coverage)](https://codeclimate.com/github/skierkowski/terraform-enterprise-client/test_coverage)
7
+ [![Dependency Status](https://gemnasium.com/badges/github.com/skierkowski/terraform-enterprise-client.svg)](https://gemnasium.com/github.com/skierkowski/terraform-enterprise-client)
8
+ [![Inline docs](http://inch-ci.org/github/skierkowski/terraform-enterprise-client.svg?branch=master)](http://inch-ci.org/github/skierkowski/terraform-enterprise-client)
4
9
 
5
10
 
6
11
  ## Requirements
7
12
 
8
- MRI Ruby 2.0 and newer are supported. Alternative interpreters compatible with 2.0+ should work as well.
9
-
10
- Earlier Ruby versions such as 1.8.7, 1.9.2, and 1.9.3 may work but are not supported.
13
+ MRI Ruby 2.3 and newer are supported. Alternative interpreters compatible with 2.3+ should work as well.
11
14
 
12
15
  This gem depends on these other gems for usage at runtime:
13
16
 
14
17
  - [rest-client](https://github.com/rest-client/rest-client)
15
- - [colorize](https://github.com/fazibear/colorize)
16
- - [thor](https://github.com/erikhuda/thor)
17
- - [terminal-table](https://github.com/tj/terminal-table)
18
18
 
19
19
  ## API Client
20
20
 
@@ -27,8 +27,8 @@ This gem depends on these other gems for usage at runtime:
27
27
  ```ruby
28
28
  require 'terraform-enterprise-client'
29
29
 
30
- api_key = ENV['TERRAFORM_ENTERPRISE_TOKEN']
31
- client = TerraformEnterprise::Client.new(api_key: api_key)
30
+ token = ENV['TFE_TOKEN']
31
+ client = TerraformEnterprise::API::Client.new(token: token)
32
32
 
33
33
  # Create a new organization
34
34
  org = client.organizations.create(name:'my-org',email:'maciej@skierkowski.com')
@@ -51,47 +51,5 @@ client.organizations.delete(name:'my-org')
51
51
 
52
52
  There are two main settings that can be modified on initialization:
53
53
 
54
- - `:api_key` (required) - which specifies the API key (formerly called ATLAS_TOKEN)
54
+ - `:token` (required) - which specifies the API key (formerly called ATLAS_TOKEN)
55
55
  - `:host` - a hostname other than the default (`app.terraform.io`) for usage with private installs
56
-
57
- ### Resources
58
-
59
- The number of supported resources is a subset of the resources exposed via the Terraform Enterprise API. Additional resources will be added in future releases. Current supported resources include:
60
-
61
- - `Client#workspaces`
62
- - `Client#organizations`
63
- - `Client#oauth_tokens`
64
-
65
- ## Command Line Tool
66
-
67
- Installing the gem installs the `tfe` command line tool. Running `tfe help` provides the help information and list of available subcomands.
68
-
69
- ### Usage
70
-
71
- All of the resources, actions and paraeters are documented in the tool and available through the `help` subcommand.
72
-
73
- #### Basic Usage
74
-
75
- ```shell
76
- ➭ tfe help
77
- Commands:
78
- tfe help [COMMAND] # Describe available commands or one specific command
79
- tfe oauth_tokens <subcommand> # Manage OAuth tokens
80
- tfe organizations <subcommand> # Manage organizations
81
- tfe workspaces <subcommand> # Manage workspaces
82
- ```
83
-
84
- ### Authentication
85
-
86
- There are two methods for authenticating the CLI with the Terraform Enterprise API. The first is to use the `TERRAFORM_ENTERPRISE_TOKEN` environment variable. The second is to pass in the token on the command line using the `--token` option. The command line option takes precendence over the environment variable.
87
-
88
- ### Scripting
89
-
90
- The CLI is designed to be easy to call from other scripts. A few command line options exist to control the output format to minimize the string parsing needed to extract the desired data from the output:
91
-
92
- - `--no-color` (Boolean, default: false): Removes ANSI color codes from output
93
- - `--except` (Array): Exclude particular fields from the output from being returned. This is a space-separated list of fields to be excluded from the output.
94
- - `--only` (Array): Only return the fields selected in this space-separated list of field keys.
95
- - `--all` (Boolean, default: false): By default most commands only return a subset of fields. Many of the APIs return additional attributes which are used by the UI and likely have little value to you. As such, they are excluded by default. This option will return all of the fields, not just the subset.
96
- - `--value` (Boolean, default: false): The output text by default shows the key and values for each field. If this option is enabled only the value of the fields will be returned. This is particularly useful if you would like to obtain the id of a newly created resource (e.g. `tfe workspcaces create new-ws --organization my-organization --only name --value` would return only the name of the created workspace)
97
- - `--no-table` (Boolean, default: false): For `list` subcommands format the output as a list of key/value paris instead of formatting the list in a table.
@@ -1,39 +1 @@
1
- require 'json'
2
- require 'rest-client'
3
-
4
- require 'terraform-enterprise/client/request'
5
- require 'terraform-enterprise/resource_requests/organizations'
6
- require 'terraform-enterprise/resource_requests/oauth-tokens'
7
- require 'terraform-enterprise/resource_requests/workspaces'
8
- require 'terraform-enterprise/resource_requests/teams'
9
- require 'terraform-enterprise/resource_requests/variables'
10
-
11
- module TerraformEnterprise
12
- class Client
13
- attr_accessor :base
14
-
15
- def initialize(options={})
16
- @request = TerraformEnterprise::API::Request.new(options)
17
- end
18
-
19
- def oauth_tokens
20
- TerraformEnterprise::API::OAuthTokens.new(@request)
21
- end
22
-
23
- def organizations
24
- TerraformEnterprise::API::Organizations.new(@request)
25
- end
26
-
27
- def teams
28
- TerraformEnterprise::API::Teams.new(@request)
29
- end
30
-
31
- def variables
32
- TerraformEnterprise::API::Variables.new(@request)
33
- end
34
-
35
- def workspaces
36
- TerraformEnterprise::API::Workspaces.new(@request)
37
- end
38
- end
39
- end
1
+ require 'terraform_enterprise'
@@ -0,0 +1 @@
1
+ require 'terraform_enterprise'
@@ -0,0 +1,2 @@
1
+ require_relative 'terraform_enterprise/api/client'
2
+ require_relative 'terraform_enterprise/api/version'
@@ -0,0 +1,62 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ require_relative 'request'
5
+ require_relative 'resource_requests/configuration_versions'
6
+ require_relative 'resource_requests/oauth_tokens'
7
+ require_relative 'resource_requests/organizations'
8
+ require_relative 'resource_requests/policies'
9
+ require_relative 'resource_requests/policy_checks'
10
+ require_relative 'resource_requests/runs'
11
+ require_relative 'resource_requests/teams'
12
+ require_relative 'resource_requests/variables'
13
+ require_relative 'resource_requests/workspaces'
14
+
15
+ module TerraformEnterprise
16
+ module API
17
+ # The Terraform Enterprise Client class
18
+ class Client
19
+ attr_accessor :base
20
+
21
+ def initialize(options = {})
22
+ @request = TerraformEnterprise::API::Request.new(options)
23
+ end
24
+
25
+ def configuration_versions
26
+ TerraformEnterprise::API::ConfigurationVersions.new(@request)
27
+ end
28
+
29
+ def oauth_tokens
30
+ TerraformEnterprise::API::OAuthTokens.new(@request)
31
+ end
32
+
33
+ def organizations
34
+ TerraformEnterprise::API::Organizations.new(@request)
35
+ end
36
+
37
+ def teams
38
+ TerraformEnterprise::API::Teams.new(@request)
39
+ end
40
+
41
+ def variables
42
+ TerraformEnterprise::API::Variables.new(@request)
43
+ end
44
+
45
+ def workspaces
46
+ TerraformEnterprise::API::Workspaces.new(@request)
47
+ end
48
+
49
+ def policies
50
+ TerraformEnterprise::API::Policies.new(@request)
51
+ end
52
+
53
+ def policy_checks
54
+ TerraformEnterprise::API::PolicyChecks.new(@request)
55
+ end
56
+
57
+ def runs
58
+ TerraformEnterprise::API::Runs.new(@request)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,97 @@
1
+ require_relative 'response'
2
+
3
+ module TerraformEnterprise
4
+ module API
5
+ # A wrapper for HTTP JSON-API requests to provide convenience request
6
+ # method wrappers.
7
+ class Request
8
+ DEFAULT_DEBUG = false
9
+ DEFAULT_HOST = 'https://app.terraform.io'.freeze
10
+
11
+ def initialize(options = {})
12
+ @host = options[:host] || DEFAULT_HOST
13
+ @token = options[:token]
14
+ @debug = options[:debug] || DEFAULT_DEBUG
15
+ end
16
+
17
+ def get(*path)
18
+ data = path.pop if path.last.is_a?(Hash)
19
+ request(:get, path, data)
20
+ end
21
+
22
+ def delete(*path)
23
+ data = path.pop if path.last.is_a?(Hash)
24
+ request(:delete, path, data)
25
+ end
26
+
27
+ def post(*path)
28
+ data = path.pop if path.last.is_a?(Hash)
29
+ request(:post, path, data)
30
+ end
31
+
32
+ def put(*path)
33
+ data = path.pop if path.last.is_a?(Hash)
34
+ request(:put, path, data)
35
+ end
36
+
37
+ def patch(*path)
38
+ data = path.pop if path.last.is_a?(Hash)
39
+ request(:patch, path, data)
40
+ end
41
+
42
+ def request(method, path, data = {}, extra_headers = {})
43
+ request_options = define_request(method, path, data, extra_headers)
44
+ response = execute(request_options)
45
+ debug(request_options, response, data) if @debug
46
+ TerraformEnterprise::API::Response.new(response)
47
+ end
48
+
49
+ private
50
+
51
+ def base
52
+ "#{@host}/api/v2"
53
+ end
54
+
55
+ def headers
56
+ {
57
+ 'Authorization' => "Bearer #{@token}",
58
+ 'Content-Type' => 'application/vnd.api+json'
59
+ }
60
+ end
61
+
62
+ def define_request(method, path, data = {}, extra_headers = {})
63
+ request_options = {
64
+ method: method,
65
+ url: uri(path),
66
+ headers: headers.merge(extra_headers)
67
+ }
68
+ if %i[get delete].include?(method)
69
+ request_options[:headers][:params] = data
70
+ else
71
+ request_options[:payload] = data.is_a?(String) ? data : data.to_json
72
+ end
73
+ request_options
74
+ end
75
+
76
+ def execute(request_params)
77
+ RestClient::Request.execute(request_params)
78
+ rescue RestClient::ExceptionWithResponse => ex
79
+ ex.response
80
+ end
81
+
82
+ def debug(http_request, http_response, data)
83
+ http_method = http_request[:method].to_s.upcase
84
+ puts "[DEBUG] [REQUEST:METHOD] #{http_method} #{http_request[:url]}"
85
+ puts "[DEBUG] [REQUEST:HEADERS] #{http_request[:headers]}"
86
+ puts "[DEBUG] [REQUEST:PAYLOAD] #{data}"
87
+ puts "[DEBUG] [RESPONSE:CODE] #{http_response.code}"
88
+ puts "[DEBUG] [RESPONSE:BODY] #{http_response.body}"
89
+ end
90
+
91
+ def uri(path = [])
92
+ return path if path.is_a?(String) && path.start_with?('http')
93
+ "#{base}/#{path.map(&:to_s).join('/')}"
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,48 @@
1
+ module TerraformEnterprise
2
+ module API
3
+ # A class wrapper for JSON-API resources
4
+ class Resource
5
+ attr_reader :body
6
+
7
+ def initialize(body)
8
+ @body = body
9
+ end
10
+
11
+ def errors?
12
+ !errors.empty?
13
+ end
14
+
15
+ def data
16
+ @body['data']
17
+ end
18
+
19
+ def id
20
+ data['id']
21
+ end
22
+
23
+ def type
24
+ data['type']
25
+ end
26
+
27
+ def attributes
28
+ data['attributes'] || {}
29
+ end
30
+
31
+ def relationships
32
+ data['relationships'] || {}
33
+ end
34
+
35
+ def links
36
+ data['links'] || []
37
+ end
38
+
39
+ def errors
40
+ data['errors'] || []
41
+ end
42
+
43
+ def included
44
+ (body['included'] || []).map{ |resource| Resource.new('data' => resource)}
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,12 @@
1
+ module TerraformEnterprise
2
+ module API
3
+ # Class is used to get access to the request and parameters for each
4
+ # resource request from the client
5
+ class ResourceRequest
6
+ def initialize(request, params = {})
7
+ @request = request
8
+ @params = params
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ require 'terraform_enterprise/api/resource_request'
2
+
3
+ module TerraformEnterprise
4
+ module API
5
+ # Variables resource request
6
+ class ConfigurationVersions < TerraformEnterprise::API::ResourceRequest
7
+ def list(params = {})
8
+ @request.get(:workspaces, params[:workspace], 'configuration-versions')
9
+ end
10
+
11
+ def get(params = {})
12
+ @request.get('configuration-versions', params[:id])
13
+ end
14
+
15
+ def create(params = {})
16
+ data = {
17
+ type: 'configuration-versions'
18
+ }
19
+ @request.post(:workspaces, params[:workspace], 'configuration-versions', data: data)
20
+ end
21
+
22
+ def upload(params = {})
23
+ headers = { 'Content-Type' => 'application/octet-stream' }
24
+ @request.request(:put, params[:url], params[:content], headers)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,11 +1,12 @@
1
- require "terraform-enterprise/client/resource_request"
1
+ require 'terraform_enterprise/api/resource_request'
2
2
 
3
- module TerraformEnterprise
3
+ module TerraformEnterprise
4
4
  module API
5
+ # OAuth Token resource request
5
6
  class OAuthTokens < TerraformEnterprise::API::ResourceRequest
6
- def list(params={})
7
+ def list(params = {})
7
8
  @request.get(:organizations, params[:organization], 'oauth-tokens')
8
9
  end
9
10
  end
10
11
  end
11
- end
12
+ end
@@ -1,17 +1,18 @@
1
- require "terraform-enterprise/client/resource_request"
1
+ require 'terraform_enterprise/api/resource_request'
2
2
 
3
- module TerraformEnterprise
3
+ module TerraformEnterprise
4
4
  module API
5
+ # Organization resource request
5
6
  class Organizations < TerraformEnterprise::API::ResourceRequest
6
- def list(params={})
7
+ def list
7
8
  @request.get(:organizations)
8
9
  end
9
10
 
10
- def get(params={})
11
+ def get(params = {})
11
12
  @request.get(:organizations, params[:name])
12
13
  end
13
14
 
14
- def create(params={})
15
+ def create(params = {})
15
16
  data = {
16
17
  attributes: params,
17
18
  type: 'organizations'
@@ -20,9 +21,9 @@ module TerraformEnterprise
20
21
  @request.post(:organizations, data: data)
21
22
  end
22
23
 
23
- def delete(params={})
24
+ def delete(params = {})
24
25
  @request.delete(:organizations, params[:name])
25
26
  end
26
27
  end
27
28
  end
28
- end
29
+ end