opsource_client 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/README.md +43 -0
- data/Rakefile +1 -0
- data/doc/Cloud-REST-API-v09-032113.pdf +0 -0
- data/lib/opsource_client/client.rb +105 -0
- data/lib/opsource_client/exceptions.rb +14 -0
- data/lib/opsource_client/network/natrule.rb +28 -0
- data/lib/opsource_client/version.rb +22 -0
- data/lib/opsource_client.rb +4 -0
- data/opsource_client.gemspec +25 -0
- data/spec/lib/opsource_client/client_spec.rb +16 -0
- data/spec/spec_helper.rb +15 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d712d0ada0a834e9dd720bd956e83f7982ce19d6
|
4
|
+
data.tar.gz: e82a84a1716dd37e43c8b689bd6f02a521a71314
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a9f9bb74a08c8e27e0d2093b50e2c4e02cee5f9051d62c263723f35b764e7ca5d1a4da9698b4b6bfea871c46a7b6c5f51dbf273f10fcfe35ff99eda7638c18d1
|
7
|
+
data.tar.gz: f15b452f1b761fefc44fa28c8ed06f0b0519ca9babe284bebb85b4758b2e505a4fcf2cfba5692beec43ea3d15fa39ae2cf421b41cc4944ef5763ac576be8d0ae
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
## ruby-opsource
|
2
|
+
=============
|
3
|
+
|
4
|
+
(WORK IN PROGRESS ...)
|
5
|
+
|
6
|
+
Ruby client for Opsource cloud's REST API. Opsource cloud is formally Dimension Data now.
|
7
|
+
|
8
|
+
0. API spec - https://github.com/udayakiran/opsource_client/raw/master/doc/Cloud-REST-API-v09-032113.pdf
|
9
|
+
|
10
|
+
0. API details - http://cloud.dimensiondata.com/saas-solutions/services/public-cloud/api
|
11
|
+
|
12
|
+
To use it as a gem -
|
13
|
+
|
14
|
+
gem 'opsource_client', git: 'https://github.com/udayakiran/opsource_client.git'
|
15
|
+
|
16
|
+
To use it as a rails plugin -
|
17
|
+
|
18
|
+
git clone https://github.com/udayakiran/opsource_client.git
|
19
|
+
|
20
|
+
copy opsource_client to vendors/plugins
|
21
|
+
|
22
|
+
## Configuration -
|
23
|
+
|
24
|
+
This is a one time configuration where you specify your opsource account's organization id,
|
25
|
+
username and password of the admin account using which API calls need to be made.
|
26
|
+
|
27
|
+
client = OpsourceClient::Client.new
|
28
|
+
client.organization_id = organization_id
|
29
|
+
client.admin_username = admin_username
|
30
|
+
client.admin_password = admin_password
|
31
|
+
|
32
|
+
|
33
|
+
## Sample API call -
|
34
|
+
|
35
|
+
To create a NAT rule
|
36
|
+
|
37
|
+
opc = OpsourceClient::Client.new
|
38
|
+
|
39
|
+
# ...
|
40
|
+
|
41
|
+
opc.create_natrule({:net_id => network_id, :sourceIp => private_ip, :name => private_ip})
|
42
|
+
|
43
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
Binary file
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'uri'
|
4
|
+
require 'xmlsimple'
|
5
|
+
require 'cgi'
|
6
|
+
require 'singleton'
|
7
|
+
require 'active_support'
|
8
|
+
|
9
|
+
module OpsourceClient
|
10
|
+
class Client
|
11
|
+
|
12
|
+
API_ENDPOINT = "https://api.opsourcecloud.net/oec/0.9/"
|
13
|
+
|
14
|
+
attr_accessor :api_endpoint, :organization_id, :admin_username, :admin_password
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
self.api_endpoint = API_ENDPOINT
|
18
|
+
self.organization_id = '123456'
|
19
|
+
self.admin_username = 'USER'
|
20
|
+
self.admin_password = 'PWD'
|
21
|
+
end
|
22
|
+
|
23
|
+
def api_base_url
|
24
|
+
"#{api_endpoint}/#{@org_id}"
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def request(type, endpoint, body = nil)
|
30
|
+
begin
|
31
|
+
uri = URI.parse(api_base_url + endpoint)
|
32
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
33
|
+
http.use_ssl = true
|
34
|
+
|
35
|
+
# construct the call data and access token
|
36
|
+
req = Net::HTTP.const_get(type.to_s.camelize).new(uri.request_uri, initheader = {'Content-Type' =>'application/json', 'User-Agent' => 'WePay Ruby SDK'})
|
37
|
+
req.content_type = "text/xml" if body
|
38
|
+
req.body = body if body
|
39
|
+
req.basic_auth @username, @password
|
40
|
+
response = http.request(req)
|
41
|
+
|
42
|
+
rescue TimeoutError => error
|
43
|
+
raise OpsourceClient::Exceptions::ConnectionError.new("Timeout error. Invalid Api URL or Opsource server is probably down: \"#{@url}\"")
|
44
|
+
rescue SocketError, Errno::ECONNREFUSED => se
|
45
|
+
raise OpsourceClient::Exceptions::ConnectionError.new("Socket error. Could not connect to Opsource server.")
|
46
|
+
end
|
47
|
+
|
48
|
+
response
|
49
|
+
end
|
50
|
+
|
51
|
+
def post(endpoint, request_xml = nil)
|
52
|
+
res = request(:post, endpoint, request_xml)
|
53
|
+
handle_response res
|
54
|
+
end
|
55
|
+
|
56
|
+
def get(endpoint, request_xml = nil)
|
57
|
+
res = request(:get, endpoint, request_xml)
|
58
|
+
handle_response res
|
59
|
+
end
|
60
|
+
|
61
|
+
def handle_response(response)
|
62
|
+
begin
|
63
|
+
h = symbolize_response(response.body)
|
64
|
+
rescue OpsourceClient::Exceptions::RequestError => e
|
65
|
+
raise OpsourceClient::Exceptions::ApiError.new("Received an Invalid Response. This might mean you sent an invalid request or Opsource is having issues.")
|
66
|
+
rescue => e
|
67
|
+
raise OpsourceClient::Exceptions::ApiError.new("There was an error while trying to connect to Opsource - #{e.inspect}")
|
68
|
+
end
|
69
|
+
h
|
70
|
+
end
|
71
|
+
|
72
|
+
def symbolize_response(response)
|
73
|
+
begin
|
74
|
+
# we'll not use 'KeyToSymbol' because it doesn't symbolize the keys for node attributes
|
75
|
+
opts = { 'ForceArray' => false, 'ForceContent' => false } #
|
76
|
+
hash = XmlSimple.xml_in(response, opts)
|
77
|
+
hash.delete_if {|k, v| k =~ /(xmlns|xmlns:ns)/ } #removing the namespaces from response
|
78
|
+
return symbolize_keys(hash)
|
79
|
+
rescue Exception => e
|
80
|
+
raise OpsourceClient::Exceptions::RequestError.new("Impossible to convert XML to hash. Error: #{e.message}")
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def symbolize_keys(arg)
|
85
|
+
case arg
|
86
|
+
when Array
|
87
|
+
arg.map { |elem| symbolize_keys elem }
|
88
|
+
when Hash
|
89
|
+
Hash[
|
90
|
+
arg.map { |key, value|
|
91
|
+
k = key.is_a?(String) ? key.to_sym : key
|
92
|
+
v = symbolize_keys value
|
93
|
+
[k,v]
|
94
|
+
}]
|
95
|
+
else
|
96
|
+
arg
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def xml_header
|
101
|
+
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module OpsourceClient
|
2
|
+
module Exceptions
|
3
|
+
CODES = {
|
4
|
+
"REASON_10" => "Failure at network",
|
5
|
+
"REASON_20" => "Unrecoverable failure cause by a timeout.",
|
6
|
+
"REASON_250" => "Network does not exist for this organization.",
|
7
|
+
"REASON_320" => "Invalid Input Data - SourceIP must be a valid IPv4 address."
|
8
|
+
}
|
9
|
+
|
10
|
+
class ApiError < StandardError; end
|
11
|
+
class ConnectionError < StandardError; end
|
12
|
+
class RequestError < StandardError; end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module OpsourceClient
|
2
|
+
class Client
|
3
|
+
|
4
|
+
def all_natrules(params)
|
5
|
+
req_params = {:net_id => ''}.merge(params)
|
6
|
+
self.get("/network/#{req_params[:net_id]}/natrule", nil)
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_natrule(params)
|
10
|
+
req_params = {:net_id => '', :sourceIp => '', :name => ''}.merge(params)
|
11
|
+
self.post("/network/#{req_params[:net_id]}/natrule", create_natrule_request_xml(req_params))
|
12
|
+
end
|
13
|
+
|
14
|
+
def delete(params)
|
15
|
+
req_params = {:net_id => '', :natrule_id => ''}.merge(params)
|
16
|
+
self.get("/network/#{req_params[:net_id]}/natrule/#{req_params[:natrule_id]}?delete", nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_natrule_request_xml(params)
|
20
|
+
xml = xml_header
|
21
|
+
xml += '<NatRule xmlns="http://oec.api.opsource.net/schemas/network">'
|
22
|
+
xml += "<name>#{params[:name]}</name> <sourceIp>#{params[:sourceIp]}</sourceIp>"
|
23
|
+
xml += "</NatRule>"
|
24
|
+
xml
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module OpsourceClient
|
2
|
+
VERSION = "0.0.1"
|
3
|
+
|
4
|
+
{"xmlns:ns3"=>"http://oec.api.opsource.net/schemas/organization",
|
5
|
+
"xmlns:ns4"=>"http://oec.api.opsource.net/schemas/network",
|
6
|
+
"xmlns:ns2"=>"http://oec.api.opsource.net/schemas/directory",
|
7
|
+
"xmlns:ns8"=>"http://oec.api.opsource.net/schemas/general",
|
8
|
+
"xmlns:ns7"=>"http://oec.api.opsource.net/schemas/datacenter",
|
9
|
+
"xmlns:ns6"=>"http://oec.api.opsource.net/schemas/whitelabel",
|
10
|
+
"xmlns:ns5"=>"http://oec.api.opsource.net/schemas/vip",
|
11
|
+
"xmlns:ns9"=>"http://oec.api.opsource.net/schemas/admin",
|
12
|
+
"natIp"=>["207.20.54.2"],
|
13
|
+
"xmlns"=>"http://oec.api.opsource.net/schemas/server",
|
14
|
+
"sourceIp"=>["10.166.152.201"],
|
15
|
+
"id"=>["ac371318-f755-4dc1-bbc3-51a1522ca2fe"],
|
16
|
+
"name"=>["10.166.152.201"],
|
17
|
+
"xmlns:ns11"=>"http://oec.api.opsource.net/schemas/reset",
|
18
|
+
"xmlns:ns10"=>"http://oec.api.opsource.net/schemas/multigeo",
|
19
|
+
"xmlns:ns13"=>"http://oec.api.opsource.net/schemas/storage",
|
20
|
+
"xmlns:ns12"=>"http://oec.api.opsource.net/schemas/support",
|
21
|
+
"xmlns:ns14"=>"http://oec.api.opsource.net/schemas/manualimport"}
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "opsource_client/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "opsource_client"
|
7
|
+
s.version = OpsourceClient::VERSION
|
8
|
+
s.authors = ["Udaya Kiran"]
|
9
|
+
s.email = ["udaykiran.vit@gmail.com"]
|
10
|
+
s.licenses = ['MIT']
|
11
|
+
s.summary = %q{Opsource REST API client gem}
|
12
|
+
s.description = %q{Simple wrapper to access opsource cloud's api'}
|
13
|
+
|
14
|
+
s.rubyforge_project = "opsource_client"
|
15
|
+
s.homepage = "https://github.com/udayakiran/opsource_client"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency 'xml-simple', '~> 1'
|
23
|
+
s.add_development_dependency 'rspec', '~> 0'
|
24
|
+
s.add_dependency "activesupport", ">= 2"
|
25
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OpsourceClient::Client do
|
4
|
+
|
5
|
+
it "should be configurable" do
|
6
|
+
client = OpsourceClient::Client.new
|
7
|
+
client.api_endpoint.should == "https://api.opsourcecloud.net/oec/0.9/"
|
8
|
+
|
9
|
+
client.api_endpoint = "http://google.com/"
|
10
|
+
client.api_endpoint.should == "http://google.com/"
|
11
|
+
|
12
|
+
client.admin_username = "hi"
|
13
|
+
client.admin_username.should == "hi"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
|
3
|
+
Bundler.require :default, :test
|
4
|
+
spec_root = File.expand_path(File.dirname(__FILE__))
|
5
|
+
|
6
|
+
|
7
|
+
$: << spec_root
|
8
|
+
|
9
|
+
require File.join(File.dirname(__FILE__), '..', 'lib','opsource_client')
|
10
|
+
|
11
|
+
Dir[File.join(spec_root, "support/*.rb")].each { |f| require f }
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.expect_with :rspec, :stdlib
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: opsource_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Udaya Kiran
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: xml-simple
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2'
|
55
|
+
description: Simple wrapper to access opsource cloud's api'
|
56
|
+
email:
|
57
|
+
- udaykiran.vit@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- Gemfile
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- doc/Cloud-REST-API-v09-032113.pdf
|
66
|
+
- lib/opsource_client.rb
|
67
|
+
- lib/opsource_client/client.rb
|
68
|
+
- lib/opsource_client/exceptions.rb
|
69
|
+
- lib/opsource_client/network/natrule.rb
|
70
|
+
- lib/opsource_client/version.rb
|
71
|
+
- opsource_client.gemspec
|
72
|
+
- spec/lib/opsource_client/client_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: https://github.com/udayakiran/opsource_client
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project: opsource_client
|
94
|
+
rubygems_version: 2.4.5
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Opsource REST API client gem
|
98
|
+
test_files: []
|