Module: Delphix

Includes:
Utils
Defined in:
lib/delphix.rb,
lib/delphix/utils.rb,
lib/delphix/version.rb,
lib/delphix/web_client.rb,
lib/delphix/web_request.rb,
lib/delphix/web_response.rb

Overview

Author: Stefano Harding <[email protected]> License: Apache License, Version 2.0 Copyright: © 2014-2015 Stefano Harding

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, Utils, Version Classes: WebClient, WebRequest, WebResponse

Constant Summary collapse

API_ENDPOINT =
'/resources/json/delphix'
HTTP_HEADERS =
{
  'Accept'       =>  'application/json; charset=UTF-8',
  'Content-Type' =>  'application/json; charset=UTF-8',
  'User-Agent'   =>  'Delphix-Ruby-Client/1.0.0'
}
VERSION =
Delphix::Version.string
@@timeout =

Default timeout value in seconds.

10
@@default_headers =

Default headers

{}

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#request_id, #utc_httpdate

Class Attribute Details

.api_passwdString

Returns password for authentication.

Returns:

  • (String)

    password for authentication



91
92
93
# File 'lib/delphix.rb', line 91

def api_passwd
  @api_passwd
end

.api_userString

Returns username to authenticate with.

Returns:

  • (String)

    username to authenticate with



88
89
90
# File 'lib/delphix.rb', line 88

def api_user
  @api_user
end

.api_versionHash

Returns containing the major, minor and micro version numbers.

Returns:

  • (Hash)

    containing the major, minor and micro version numbers.



82
83
84
# File 'lib/delphix.rb', line 82

def api_version
  @api_version
end

.last_requestHash

Returns retruns the last request.

Returns:

  • (Hash)

    retruns the last request



69
70
71
# File 'lib/delphix.rb', line 69

def last_request
  @last_request
end

.last_responseHash

Returns retruns the last response.

Returns:

  • (Hash)

    retruns the last response



72
73
74
# File 'lib/delphix.rb', line 72

def last_response
  @last_response
end

.serverString

Returns Delphix server address.

Returns:

  • (String)

    Delphix server address



85
86
87
# File 'lib/delphix.rb', line 85

def server
  @server
end

.sessionHash, ...

Returns:

  • (Hash)

    retruns current session state

  • (#code)

    the response code from Delphix engine

  • (#headers)

    beautified with symbols and underscores

  • (#body)

    parsed response body

  • (#raw_body)

    un-parsed response body



79
80
81
# File 'lib/delphix.rb', line 79

def session
  @session
end

.verboseNothing

Returns enables verbosity.

Returns:

  • (Nothing)

    enables verbosity



94
95
96
# File 'lib/delphix.rb', line 94

def verbose
  @verbose
end

Class Method Details

.api_url(resource = nil) ⇒ URL

Returns the API endpoint for a given resource namespace by combining the server address with the appropriate HTTP headers.

Parameters:

  • resource (Resource) (defaults to: nil)

    namespace

Returns:

  • (URL)

    return the URL for the API endpoint



105
106
107
# File 'lib/delphix.rb', line 105

def self.api_url(resource = nil)
  'http://' + @server + resource
end

.clear_default_headersHash

Returns The HTTP headers sent with a POST/GET/DELETE.

Returns:

  • (Hash)

    The HTTP headers sent with a POST/GET/DELETE.



89
90
91
# File 'lib/delphix/web_client.rb', line 89

def self.clear_default_headers
  @@default_headers = {}
end

.cookiesHash

Establish a session with the Delphix engine and return an identifier through browser cookies. This session will be reused in subsequent calls, the same session credentials and state are preserved without requiring a re-authentication call. Sessions do not persisit between incovations.

Returns:

  • (Hash)

    cookies containing the new session cookies



123
124
125
126
127
# File 'lib/delphix.rb', line 123

def self.cookies
  @resp ||= Delphix.post session_url,
    type: 'APISession', version: @api_version
  @resp.cookies
end

.default_header(name, value) ⇒ Hash

Returns The result of the key/value pair.

Parameters:

  • name (String, Symbol)

    A string or symbol used to identify the key portion of the HTTP headers.

  • value (String, Symbol, Array)

    A string, symbol or array containing the values of the HTTP header.

Returns:

  • (Hash)

    The result of the key/value pair.



82
83
84
# File 'lib/delphix/web_client.rb', line 82

def self.default_header(name, value)
  @@default_headers[name] = value
end

.login(user = @api_user, passwd = @api_passwd) ⇒ Fixnum, ...

Authenticates the session so that API calls can be made. Only supports basic password authentication.

Parameters:

  • user (String) (defaults to: @api_user)

    user name to authenticate with

  • passwd (String) (defaults to: @api_passwd)

    password to authenticate with

Returns:

  • (Fixnum, #code)

    the response code from Delphix engine

  • (Hash, #headers)

    headers, beautified with symbols and underscores

  • (Hash, #body)

    body parsed response body where applicable (JSON responses are parsed to Objects/Associative Arrays)

  • (Hash, #raw_body)

    raw_body un-parsed response body



148
149
150
151
# File 'lib/delphix.rb', line 148

def self.(user = @api_user, passwd = @api_passwd)
  Delphix.post ,
    type: 'LoginRequest', username: user, password: passwd
end

.timeout(seconds) ⇒ undefined

Parameters:

  • seconds (Integer)

    Set the number of seconds to wait for an HTTP timeout.

Returns:

  • (undefined)


98
99
100
# File 'lib/delphix/web_client.rb', line 98

def self.timeout(seconds)
  @@timeout = seconds
end

Instance Method Details

#alertString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to alert.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#containerString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to container.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#databaseString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to database.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#environmentString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to environment.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#groupString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to group.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#hostString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to host.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#jobString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to job.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#loginString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to login.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#methodFixnum, ...

Define the #get, #post, and #delete helper methods for sending HTTP requests to the Delphix engine. You shouldn’t need to use these methods directly, but they can be useful for debugging.

The following HTTP methods are supported by the Delphix Appliance:

 GET - Retrieve data from the server where complex input is not needed.
       All GET requests are guaranteed to be read-only, but not all
       read-only requests are required to use GET. Simple input
       (strings, number, boolean values) can be passed as query
       parameters.
POST - Issue a read/write operation, or make a read-only call that
       requires complex input. The optional body of the call is
       expressed as JSON.
DELETE - Delete an object on the system. For languages that don't provide
       a native wrapper for DELETE, or for delete operations with
       optional input, all delete operations can also be invoked as POST
       to the same URL with /delete appended to it.

Each method returns a hash that responds to #code, #headers, #body and #raw_body obtained from parsing the JSON object in the response body.

API request responses are parsed to Objects/Associative Arrays)

Parameters:

  • url (String<URL>)

    url the url of where to send the request

  • parameters (Hash{Symbol => String})

    key-value data of the HTTP

  • block (Block)

    block to execute when the request returns

Returns:

  • (Fixnum, #code)

    the response code from Delphix engine

  • (Hash, #headers)

    headers, beautified with symbols and underscores

  • (Hash, #body)

    body parsed response body where applicable (JSON

  • (Hash, #raw_body)

    raw_body un-parsed response body



135
136
137
138
139
140
# File 'lib/delphix/web_client.rb', line 135

[:get, :post, :delete].each do |method|
  define_singleton_method(method) do |url, parameters = {}, &callback|
    WebClient.request(method.to_sym, url, @@default_headers,
      parameters.to_json, @@timeout, &callback)
  end
end

#policyString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to policy.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#repositoryString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to repository.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#sessionString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to session.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#snapshotString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to snapshot.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#sourceString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to source.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#sourceconfigString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to sourceconfig.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#templateString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to template.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#timeflowString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to timeflow.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end

#userString

A helper method to return the URL for the resource by using the resource_url shorthand.

Returns:

  • (String)

    The API path to user.



238
239
240
241
242
243
244
245
# File 'lib/delphix.rb', line 238

[ :alert, :container, :database, :environment, :group, :host, :job,
  :login, :policy, :repository, :session, :snapshot, :source,
  :sourceconfig, :template, :timeflow, :user
].each do |name|
  define_singleton_method(name.to_s + '_url') do
    api_url('/resources/json/delphix/' + name.to_s)
  end
end