11class Gd2Client
22 include HTTParty
33
4- def self . from_endpoint ( endpoint )
5- new ( gd2_url : endpoint [ 'gd2_url' ] , secret : endpoint [ 'secret' ] )
4+ UNVERSIONED_APIS = %w[ /version /ping /statedump /endpoints ]
5+
6+ def initialize ( endpoint )
7+ endpoint = endpoint . with_indifferent_access
8+ @gd2_url = endpoint [ :gd2_url ]
9+ @user = endpoint [ :user ] . present? ? endpoint [ :user ] : 'glustercli'
10+ @secret = endpoint [ :secret ] || File . read ( File . join ( 'var' , 'lib' , 'glusterd2' , 'auth' ) )
11+ generate_api_methods
612 end
713
8- def initialize ( gd2_url : 'http://localhost:24007' , user : 'glustercli' , secret : nil )
9- @gd2_url = gd2_url
10- @secret = secret || File . read ( File . join ( 'var' , 'lib' , 'glusterd2' , 'auth' ) )
11- @claim = {
12- iss : user ,
14+ def claim ( method , path )
15+ {
16+ iss : @user ,
1317 iat : DateTime . now . utc ,
1418 exp : DateTime . now . utc + 10 . seconds ,
15- qsh : ''
19+ qsh : Digest :: SHA256 . hexdigest ( method . to_s . upcase + '&' + path )
1620 }
1721 end
1822
19- def jwt_token
20- JWT . encode ( @ claim, @secret , 'HS256' )
23+ def jwt_token ( method , path )
24+ JWT . encode ( claim ( method , path ) , @secret , 'HS256' )
2125 end
2226
2327 def respond_to_missing? ( method , include_private = false )
@@ -30,41 +34,39 @@ def method_missing(m, *args, &block)
3034 end
3135
3236 def http_call ( method , path , opts = { } )
33- @claim [ :qsh ] = Digest ::SHA256 . hexdigest ( method . to_s . upcase + '&' + path )
34- response = HTTParty . public_send (
37+ req_data = { headers : { 'Authorization' => 'Bearer ' + jwt_token ( method , path ) } }
38+ req_data [ :body ] = args [ -1 ] . to_h if %w[ put post patch ] . include? ( method ) && args [ -1 ] . respond_to ( :to_h )
39+ HTTParty . public_send (
3540 method ,
3641 @gd2_url + path ,
37- opts . merge ( headers : { 'Authorization' => 'Bearer ' + jwt_token } )
42+ opts . merge ( req_data )
3843 )
39- return JSON . parse ( response . body ) if response . success?
40- raise Tendrl ::HttpResponseErrorHandler . new ( StandardError . new , cause : 'gd2_api_error' , object_id : method . to_s . capitalize + path . to_s )
41- end
42-
43- def peers
44- get ( '/v1/peers' )
45- end
46-
47- def state
48- get ( '/statedump' )
49- end
50-
51- def volumes
52- get ( '/v1/volumes' )
5344 end
5445
55- def volume ( vol_name )
56- get ( " /v1/volumes/ #{ vol_name } " )
46+ def prefixed_path ( path )
47+ UNVERSIONED_APIS . include? ( path ) ? path : ' /v1' + path
5748 end
5849
59- def bricks ( vol_name )
60- get ( "/v1/volumes/#{ vol_name } /bricks" )
50+ def generate_api_methods
51+ apis . each do |api |
52+ method_name = api [ 'name' ] . split . join ( '_' ) . underscore
53+ action = api [ 'methods' ] . downcase
54+ self . class . send ( :define_method , method_name ) do |*args |
55+ path = api [ 'path' ] . gsub ( /{.*?}/ ) . with_index { |_ , i | args [ i ] }
56+ path = prefixed_path ( path )
57+ response = http_call ( action , path , args [ -1 ] . present? && args [ -1 ] . respond_to? ( :to_h ) ? args [ -1 ] : { } )
58+ return response if response . success?
59+ raise Tendrl ::HttpResponseErrorHandler . new ( response . body , cause : 'gd2_api_error' , object_id : api [ 'methods' ] + path . to_s )
60+ end
61+ end
62+ self
6163 end
6264
6365 def ping?
64- HTTParty . get ( @gd2_url + " /ping" ) . success?
66+ HTTParty . get ( @gd2_url + ' /ping' ) . success?
6567 end
6668
67- def endpoints
68- get ( '/endpoints' )
69+ def apis
70+ get ( '/endpoints' ) . to_a
6971 end
7072end
0 commit comments