-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
215 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"cSpell.words": [ | ||
"creds" | ||
"creds", | ||
"etag" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# frozen_string_literal: true | ||
|
||
require "aserto/directory" | ||
require_relative "../interceptors/headers" | ||
require_relative "config" | ||
|
||
module Aserto | ||
module Directory | ||
module V3 | ||
class Client | ||
attr_reader :reader, :writer, :importer, :exporter, :model | ||
|
||
# Creates a new Directory V3 Client | ||
# | ||
# @param config [Aserto::Directory::V3::Config] the service configuration | ||
# Base configuration | ||
# If non-nil, this configuration is used for any client that doesn't have its own configuration. | ||
# If nil, only clients that have their own configuration will be created. | ||
# { | ||
# url: "base_url", | ||
# tenant_id: "base_tenant_id", | ||
# api_key: "base_api_key" | ||
# | ||
# Reader Configuration | ||
# reader: { | ||
# url: "reader_url", | ||
# tenant_id: "reader_tenant_id", | ||
# api_key: "reader_api_key" | ||
# }, | ||
# | ||
# Writer Configuration | ||
# writer: { | ||
# url: "writer_url", | ||
# tenant_id: "writer_tenant_id", | ||
# api_key: "writer_api_key" | ||
# }, | ||
# | ||
# Importer Configuration | ||
# importer: { | ||
# url: "importer_url", | ||
# tenant_id: "importer_tenant_id", | ||
# api_key: "importer_api_key" | ||
# }, | ||
# | ||
# Exporter Configuration | ||
# exporter: { | ||
# url: "exporter_url", | ||
# tenant_id: "exporter_tenant_id", | ||
# api_key: "exporter_api_key" | ||
# }, | ||
# | ||
# Model Configuration | ||
# model: { | ||
# url: "model_url", | ||
# tenant_id: "model_tenant_id", | ||
# api_key: "model_api_key" | ||
# } | ||
# } | ||
# | ||
# @return [Aserto::Directory::V3::Client] the new Directory V3 Client | ||
def initialize(config) | ||
base_config = ::Aserto::Directory::V3::Config.new(config) | ||
|
||
@reader = create_client(:reader, base_config.reader) | ||
@writer = create_client(:writer, base_config.writer) | ||
@importer = create_client(:importer, base_config.importer) | ||
@exporter = create_client(:exporter, base_config.exporter) | ||
@model = create_client(:model, base_config.model) | ||
end | ||
|
||
private | ||
|
||
class NullClient | ||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def method_missing(method, *_args) | ||
puts "Cannot call '#{method}': '#{@name.to_s.capitalize}' client is not initialized." | ||
end | ||
|
||
def respond_to_missing?(_name, _include_private) | ||
true | ||
end | ||
end | ||
|
||
SERVICE_MAP = { | ||
reader: ::Aserto::Directory::Reader::V3::Reader::Stub, | ||
writer: ::Aserto::Directory::Writer::V3::Writer::Stub, | ||
importer: ::Aserto::Directory::Importer::V3::Importer::Stub, | ||
exporter: ::Aserto::Directory::Exporter::V3::Exporter::Stub, | ||
model: ::Aserto::Directory::Model::V3::Model::Stub | ||
}.freeze | ||
|
||
def create_client(type, config) | ||
return NullClient.new(type) unless config | ||
|
||
SERVICE_MAP[type].new( | ||
config.url, | ||
config.credentials, | ||
interceptors: config.interceptors | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../interceptors/headers" | ||
|
||
module Aserto | ||
module Directory | ||
module V3 | ||
class Config | ||
attr_reader :reader, :writer, :importer, :exporter, :model | ||
|
||
class BaseConfig | ||
attr_reader :url, :credentials, :interceptors | ||
|
||
def initialize(url, credentials, interceptors) | ||
@url = url | ||
@credentials = credentials | ||
@interceptors = interceptors | ||
end | ||
end | ||
|
||
def initialize(config) | ||
@base = { | ||
url: config[:url] || "directory.prod.aserto.com:8443", | ||
api_key: config[:api_key], | ||
tenant_id: config[:tenant_id] | ||
} | ||
|
||
@reader = build(**(config[:reader] || {})) | ||
@writer = build(**(config[:writer] || {})) | ||
@importer = build(**(config[:importer] || {})) | ||
@exporter = build(**(config[:exporter] || {})) | ||
@model = build(**(config[:model] || {})) | ||
end | ||
|
||
private | ||
|
||
def build(url: @base[:url], api_key: @base[:api_key], tenant_id: @base[:tenant_id], cert_path: nil) | ||
return if url.nil? || url.empty? || api_key.nil? || api_key.empty? || tenant_id.nil? || tenant_id.empty? | ||
|
||
BaseConfig.new(url, load_creds(cert_path), [Interceptors::Headers.new(api_key, tenant_id)]) | ||
end | ||
|
||
def load_creds(cert_path) | ||
if cert_path && File.file?(cert_path) | ||
GRPC::Core::ChannelCredentials.new(File.read(cert_path)) | ||
else | ||
GRPC::Core::ChannelCredentials.new | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Aserto::Directory::V3::Client do | ||
let(:client) { described_class.new(tenant_id: "1234", api_key: "basic test") } | ||
|
||
describe ".reader" do | ||
describe ".get_object" do | ||
before do | ||
GrpcMock.stub_request("/aserto.directory.reader.v3.Reader/GetObject").to_return do | ||
Aserto::Directory::Reader::V3::GetObjectResponse.new( | ||
{ result: { id: "id", type: "type", display_name: "display_name" } } | ||
) | ||
end | ||
end | ||
|
||
it "returns the correct object" do | ||
expect(client.reader.get_object( | ||
Aserto::Directory::Reader::V3::GetObjectRequest.new( | ||
object_id: "id", | ||
object_type: "type" | ||
) | ||
).to_h).to eq( | ||
{ | ||
result: { | ||
created_at: nil, | ||
display_name: "display_name", | ||
etag: "", | ||
id: "id", | ||
properties: nil, | ||
type: "type", | ||
updated_at: nil | ||
}, | ||
relations: [], | ||
page: nil | ||
} | ||
) | ||
end | ||
end | ||
end | ||
end |