hpe3par_sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ # (c) Copyright 2016-2017 Hewlett Packard Enterprise Development LP
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software distributed
8
+ # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ # CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ # specific language governing permissions and limitations under the License.
11
+
12
+ require_relative 'util'
13
+ require_relative 'models'
14
+
15
+ module Hpe3parSdk
16
+ # Adaptive Flash Cache Rest API methods
17
+ class FlashCacheManager
18
+ def initialize(http)
19
+ @http = http
20
+ @flash_cache_uri = '/flashcache'
21
+ end
22
+
23
+ def create_flash_cache(size_in_gib, mode)
24
+ flash_cache = { 'sizeGiB' => size_in_gib }
25
+
26
+ unless mode.nil?
27
+ mode = { 'mode' => mode }
28
+ flash_cache = Util.merge_hash(flash_cache, mode)
29
+ end
30
+
31
+ info = { 'flashCache' => flash_cache }
32
+ _response, body = @http.post('/', body: info)
33
+ body
34
+ end
35
+
36
+ def get_flash_cache
37
+ _response, body = @http.get(@flash_cache_uri)
38
+ FlashCache.new(body)
39
+ end
40
+
41
+ def flash_cache_exists?
42
+ begin
43
+ get_flash_cache
44
+ return true
45
+ rescue Hpe3parSdk::HTTPNotFound => ex
46
+ return false
47
+ end
48
+ end
49
+
50
+ def delete_flash_cache
51
+ _response, _body = @http.delete(@flash_cache_uri)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,152 @@
1
+ # (c) Copyright 2016-2017 Hewlett Packard Enterprise Development LP
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software distributed
8
+ # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ # CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ # specific language governing permissions and limitations under the License.
11
+
12
+ require_relative 'util'
13
+ require_relative 'exceptions'
14
+ require_relative 'models'
15
+
16
+ module Hpe3parSdk
17
+ # Host Manager Rest API methods
18
+ class HostManager
19
+ def initialize(http, vlun_query_supported = false)
20
+ @http = http
21
+ @vlun_query_supported = vlun_query_supported
22
+ @host_uri = '/hosts'
23
+ end
24
+
25
+ def get_hosts
26
+ response = @http.get(@host_uri)
27
+ host_members = []
28
+ response[1]['members'].each do |host_member|
29
+ host_members.push(Host.new(host_member)) if host_member.key?('name')
30
+ end
31
+ host_members
32
+ end
33
+
34
+ def get_host(name)
35
+ if name.nil? || name.strip.empty?
36
+ raise HPE3PARException.new(nil, 'Host name cannot be nil or empty')
37
+ else
38
+ response = @http.get("#{@host_uri}/#{name}")
39
+ Host.new(response[1])
40
+ end
41
+ end
42
+
43
+ def create_host(name, iscsi_names, fcwwns, optional)
44
+ info = { 'name' => name }
45
+
46
+ if !iscsi_names.nil? && !iscsi_names.empty?
47
+ iscsi = { 'iSCSINames' => iscsi_names }
48
+ info = Util.merge_hash(info, iscsi)
49
+ end
50
+
51
+ if !fcwwns.nil? && !fcwwns.empty?
52
+ fc = { 'FCWWNs' => fcwwns }
53
+ info = Util.merge_hash(info, fc)
54
+ end
55
+
56
+ if !optional.nil? && !optional.empty?
57
+ info = Util.merge_hash(info, optional)
58
+ end
59
+
60
+ response = @http.post(@host_uri, body: info)
61
+ response[1]
62
+ end
63
+
64
+ def modify_host(name, mod_request)
65
+ response = @http.put("#{@host_uri}/#{name}", body: mod_request)
66
+ response[1]
67
+ end
68
+
69
+ def delete_host(name)
70
+ response = @http.delete("#{@host_uri}/#{name}")
71
+ response[1]
72
+ end
73
+
74
+ def query_host_by_fc_path(wwn)
75
+ wwn_query = ''
76
+ if wwn
77
+ tmp_query = []
78
+ tmp_query.push("wwn==#{wwn}")
79
+ wwn_query = "FCPaths[#{tmp_query.join(' OR ')}]"
80
+ end
81
+
82
+ query = ''
83
+ query = wwn_query if !wwn_query.nil? && !wwn_query.empty?
84
+
85
+ query = %("#{query}")
86
+
87
+ response = @http.get("#{@host_uri}?query=#{query}")
88
+ if response[1] && response[1].include?('total') && response[1]['total'] > 0
89
+ return Host.new(response[1]['members'][0])
90
+ else
91
+ return nil
92
+ end
93
+ end
94
+
95
+ def query_host_by_iscsi_path(iqn)
96
+ iqn_query = ''
97
+ if iqn
98
+ tmp_query = []
99
+ tmp_query.push("name==#{iqn}")
100
+ iqn_query = "iSCSIPaths[#{tmp_query.join(' OR ')}]"
101
+ end
102
+
103
+ query = ''
104
+ query = iqn_query if !iqn_query.nil? && !iqn_query.empty?
105
+
106
+ query = %("#{query}")
107
+
108
+ response = @http.get("#{@host_uri}?query=#{query}")
109
+ if response[1] && response[1].include?('total') && response[1]['total'] > 0
110
+ return Host.new(response[1]['members'][0])
111
+ else
112
+ return nil
113
+ end
114
+ end
115
+
116
+ def get_host_vluns(host_name)
117
+ # calling getHost to see if the host exists and raise not found
118
+ # exception if it's not found.
119
+ get_host(host_name)
120
+
121
+ vluns = []
122
+ # Check if the WSAPI supports VLUN querying. If it is supported
123
+ # request only the VLUNs that are associated with the host.
124
+ if @vlun_query_supported
125
+ query = %("hostname EQ #{host_name}")
126
+ response = @http.get("/vluns?query=#{query}")
127
+ response[1]['members'].each do |vlun|
128
+ vluns.push(VLUN.new(vlun))
129
+ end
130
+ else
131
+ all_vluns = VlunManager.new(@http).get_vluns
132
+
133
+ if all_vluns
134
+ all_vluns.each do |vlun|
135
+ vluns.push(vlun) if !vlun.hostname.nil? && (vlun.hostname == host_name)
136
+ end
137
+ end
138
+ end
139
+ vluns
140
+ end
141
+
142
+ def host_exists?(host_name)
143
+ begin
144
+ get_host(host_name)
145
+ return true
146
+ rescue Hpe3parSdk::HTTPNotFound => ex
147
+ return false
148
+ end
149
+ end
150
+
151
+ end
152
+ end
@@ -0,0 +1,128 @@
1
+ # (c) Copyright 2016-2017 Hewlett Packard Enterprise Development LP
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software distributed
8
+ # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ # CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ # specific language governing permissions and limitations under the License.
11
+
12
+ require_relative 'util'
13
+ require_relative 'constants'
14
+ require_relative 'exceptions'
15
+ require_relative 'models'
16
+
17
+ module Hpe3parSdk
18
+ # Host Set Manager Rest API methods
19
+ class HostSetManager
20
+ def initialize(http, host_and_vv_set_filter_supported = false)
21
+ @http = http
22
+ @host_set_uri = '/hostsets'
23
+ @host_set_filter_supported = host_and_vv_set_filter_supported
24
+ end
25
+
26
+ def get_host_sets
27
+ response = @http.get(@host_set_uri)
28
+ host_set_members = []
29
+ response[1]['members'].each do |host_set_member|
30
+ host_set_members.push(HostSet.new(host_set_member))
31
+ end
32
+ host_set_members
33
+ end
34
+
35
+ def get_host_set(name)
36
+ if name.nil? || name.strip.empty?
37
+ raise HPE3PARException.new(nil, 'HostSet name cannot be nil or empty')
38
+ else
39
+ response = @http.get("#{@host_set_uri}/#{name}")
40
+ HostSet.new(response[1])
41
+ end
42
+ end
43
+
44
+ def create_host_set(name, domain, comment, setmembers)
45
+ info = { 'name' => name }
46
+
47
+ info['domain'] = domain if domain
48
+
49
+ info['comment'] = comment if comment
50
+
51
+ if setmembers
52
+ members = { 'setmembers' => setmembers }
53
+ info = Util.merge_hash(info, members)
54
+ end
55
+
56
+ response = @http.post(@host_set_uri, body: info)
57
+ if response[0] && response[0].include?('location')
58
+ host_set_id = response[0]['location'].rpartition('/api/v1/hostsets/')[-1]
59
+ return host_set_id
60
+ else
61
+ return nil
62
+ end
63
+ end
64
+
65
+ def delete_host_set(name)
66
+ @http.delete("#{@host_set_uri}/#{name}")
67
+ end
68
+
69
+ def modify_host_set(name, action, setmembers, newName = nil, comment = nil)
70
+ info = {}
71
+
72
+ info['action'] = action if action
73
+
74
+ info['newName'] = newName if newName
75
+
76
+ info['comment'] = comment if comment
77
+
78
+ if setmembers
79
+ members = { 'setmembers' => setmembers }
80
+ info = Util.merge_hash(info, members)
81
+ end
82
+
83
+ response = @http.put("#{@host_set_uri}/#{name}", body: info)
84
+ response[1]
85
+ end
86
+
87
+ def add_hosts_to_host_set(set_name, setmembers)
88
+ modify_host_set(set_name, SetCustomAction::MEM_ADD, setmembers)
89
+ end
90
+
91
+ def remove_hosts_from_host_set(set_name, setmembers)
92
+ modify_host_set(set_name, SetCustomAction::MEM_REMOVE, setmembers)
93
+ end
94
+
95
+ def find_host_sets(host_name)
96
+ host_sets = []
97
+ if @host_set_filter_supported
98
+ query = %("setmembers EQ #{host_name}")
99
+ response = @http.get("#{@host_set_uri}?query=#{query}")
100
+ host_sets_list = response[1]['members']
101
+ host_sets_list.each do |host_set|
102
+ host_sets.push(HostSet.new(host_set))
103
+ end
104
+ else
105
+ host_sets_list = get_host_sets
106
+ host_sets_list.each do |host_set|
107
+ if !host_set.setmembers.nil? && !host_set.setmembers.empty? && host_set.setmembers.include?(host_name)
108
+ host_sets.push(host_set)
109
+ end
110
+ end
111
+ end
112
+ host_sets
113
+ end
114
+
115
+ def host_set_exists?(host_name)
116
+ begin
117
+ get_host_set(host_name)
118
+ return true
119
+ rescue Hpe3parSdk::HTTPNotFound => ex
120
+ return false
121
+ end
122
+ end
123
+
124
+ def host_in_host_set_exists?(set_name, host_name)
125
+ end
126
+
127
+ end
128
+ end
@@ -0,0 +1,164 @@
1
+ # (c) Copyright 2016-2017 Hewlett Packard Enterprise Development LP
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software distributed
8
+ # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ # CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ # specific language governing permissions and limitations under the License.
11
+
12
+ require 'httparty'
13
+ require 'json'
14
+ require 'logger'
15
+ require_relative 'exceptions'
16
+ require_relative 'multi_log'
17
+ require_relative 'util'
18
+
19
+ module Hpe3parSdk
20
+ class HTTPJSONRestClient
21
+ USER_AGENT = 'ruby-3parclient'.freeze
22
+ SESSION_COOKIE_NAME = 'X-Hp3Par-Wsapi-Sessionkey'.freeze
23
+ CONTENT_TYPE = 'application/json'.freeze
24
+
25
+ attr_accessor :http_log_debug, :api_url, :session_key, :suppress_ssl_warnings, :timeout, :secure,
26
+ :logger, :log_level
27
+
28
+ def initialize(api_url, secure = false, http_log_debug = false,
29
+ suppress_ssl_warnings = false, timeout = nil)
30
+ @api_url = api_url
31
+ @secure = secure
32
+ @http_log_debug = http_log_debug
33
+ @suppress_ssl_warnings = suppress_ssl_warnings
34
+ @timeout = timeout
35
+ @session_key = nil
36
+ HTTParty::Logger.add_formatter('custom', CustomHTTPFormatter)
37
+ @httparty_log_level = :info
38
+ @httparty_log_format = :custom
39
+ set_debug_flag
40
+ end
41
+
42
+ # This turns on/off http request/response debugging output to console
43
+ def set_debug_flag
44
+ if @http_log_debug
45
+ @httparty_log_level = :debug
46
+ @httparty_log_format = :curl
47
+ end
48
+ end
49
+
50
+ def authenticate(user, password, _optional = nil)
51
+ begin
52
+ @session_key = nil
53
+
54
+ info = {:user => user, :password => password}
55
+
56
+ auth_url = '/credentials'
57
+ headers, body = post(auth_url, body: info)
58
+ @session_key = body['key']
59
+ rescue => ex
60
+ Util.log_exception(ex, caller_locations(1, 1)[0].label)
61
+ end
62
+ end
63
+
64
+ def set_url(api_url)
65
+ # should be http://<Server:Port>/api/v1
66
+ @api_url = api_url.chomp('/')
67
+ end
68
+
69
+ def get(url, **kwargs)
70
+ headers, payload = get_headers_and_payload(kwargs)
71
+ response = HTTParty.get(api_url + url,
72
+ headers: headers,
73
+ verify: secure, logger: Hpe3parSdk.logger,
74
+ log_level: @httparty_log_level,
75
+ log_format: @httparty_log_format)
76
+ process_response(response)
77
+ end
78
+
79
+ def post(url, **kwargs)
80
+ headers, payload = get_headers_and_payload(kwargs)
81
+ response = HTTParty.post(api_url + url,
82
+ headers: headers,
83
+ body: payload,
84
+ verify: secure, logger: Hpe3parSdk.logger,
85
+ log_level: @httparty_log_level,
86
+ log_format: @httparty_log_format)
87
+ process_response(response)
88
+ end
89
+
90
+ def put(url, **kwargs)
91
+ headers, payload = get_headers_and_payload(kwargs)
92
+ response = HTTParty.put(api_url + url,
93
+ headers: headers,
94
+ body: payload,
95
+ verify: secure, logger: Hpe3parSdk.logger,
96
+ log_level: @httparty_log_level,
97
+ log_format: @httparty_log_format)
98
+ process_response(response)
99
+ end
100
+
101
+ def delete(url, **kwargs)
102
+ headers, payload = get_headers_and_payload(kwargs)
103
+ response = HTTParty.delete(api_url + url,
104
+ headers: headers,
105
+ verify: secure, logger: Hpe3parSdk.logger,
106
+ log_level: @httparty_log_level,
107
+ log_format: @httparty_log_format)
108
+ process_response(response)
109
+ end
110
+
111
+ def process_response(response)
112
+ headers = response.headers
113
+ body = response.parsed_response
114
+
115
+ if response.code != 200
116
+ if !body.nil? && body.key?('code') && body.key?('desc')
117
+ exception = Hpe3parSdk.exception_from_response(response, body)
118
+ raise exception
119
+ end
120
+ end
121
+ [headers, body]
122
+ end
123
+
124
+ def log_exception(exception, caller_location)
125
+ formatted_stack_trace = exception.backtrace
126
+ .map { |line| "\t\tfrom #{line}" }
127
+ .join($/)
128
+ err_msg = "(#{caller_location}) #{exception}#{$/} #{formatted_stack_trace}"
129
+ Hpe3parSdk.logger.error(err_msg)
130
+ end
131
+
132
+ def unauthenticate
133
+ # delete the session on the 3Par
134
+ unless @session_key.nil?
135
+ begin
136
+ delete('/credentials/%s' % session_key)
137
+ @session_key = nil
138
+ rescue => ex
139
+ Util.log_exception(ex, caller_locations(1, 1)[0].label)
140
+ end
141
+ end
142
+ end
143
+
144
+ def get_headers_and_payload(**kwargs)
145
+ if session_key
146
+ kwargs['headers'] = kwargs.fetch('headers', {})
147
+ kwargs['headers'][SESSION_COOKIE_NAME] = session_key
148
+ end
149
+
150
+ kwargs['headers'] = kwargs.fetch('headers', {})
151
+ kwargs['headers']['User-Agent'] = USER_AGENT
152
+ kwargs['headers']['Accept'] = CONTENT_TYPE
153
+ if kwargs.key?(:body)
154
+ kwargs['headers']['Content-Type'] = CONTENT_TYPE
155
+ kwargs[:body] = kwargs[:body].to_json
156
+ payload = kwargs[:body]
157
+ else
158
+ payload = nil
159
+ end
160
+ [kwargs['headers'], payload]
161
+ end
162
+ end
163
+ end
164
+