Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 61c4f1a

Browse files
committed
[FAB-8716] Simplify SDK options
Change-Id: I1a9006cc559de62ddcd6065787178cbba29564ed Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent 7f5d553 commit 61c4f1a

20 files changed

+70
-180
lines changed

pkg/fabsdk/client.go

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ type ClientContext struct {
2323
provider clientProvider
2424
}
2525

26-
// ContextOption configures the client context created by the SDK.
27-
type ContextOption func(opts *contextOptions) error
28-
2926
type contextOptions struct {
3027
orgID string
3128
config core.Config
@@ -41,7 +38,6 @@ type clientOptions struct {
4138
type clientProvider func() (*clientContext, error)
4239

4340
type clientContext struct {
44-
opts *contextOptions
4541
identity contextApi.Identity
4642
providers providers
4743
}
@@ -50,14 +46,6 @@ type providers interface {
5046
contextApi.Providers
5147
}
5248

53-
// WithOrg uses the configuration and users from the named organization.
54-
func WithOrg(name string) ContextOption {
55-
return func(opts *contextOptions) error {
56-
opts.orgID = name
57-
return nil
58-
}
59-
}
60-
6149
// WithTargetFilter allows for filtering target peers.
6250
func WithTargetFilter(targetFilter fab.TargetFilter) ClientOption {
6351
return func(opts *clientOptions) error {
@@ -66,35 +54,21 @@ func WithTargetFilter(targetFilter fab.TargetFilter) ClientOption {
6654
}
6755
}
6856

69-
// withConfig allows for overriding the configuration of the client.
70-
// TODO: This should be removed once the depreacted functions are removed.
71-
func withConfig(config core.Config) ContextOption {
72-
return func(opts *contextOptions) error {
73-
opts.config = config
74-
return nil
75-
}
76-
}
77-
7857
// NewClient allows creation of transactions using the supplied identity as the credential.
7958
//Deprecated: use sdk.Context() or sdk.ChannelContext() instead
80-
func (sdk *FabricSDK) NewClient(identityOpt IdentityOption, opts ...ContextOption) *ClientContext {
59+
func (sdk *FabricSDK) NewClient(opts ...ContextOption) *ClientContext {
8160
// delay execution of the following logic to avoid error return from this function.
8261
// this is done to allow a cleaner API - i.e., client, err := sdk.NewClient(args).<Desired Interface>(extra args)
8362
provider := func() (*clientContext, error) {
84-
o, err := newContextOptions(sdk.provider.Config(), opts)
85-
if err != nil {
86-
return nil, errors.WithMessage(err, "unable to retrieve configuration from SDK")
87-
}
8863

89-
identity, err := sdk.newIdentity(identityOpt, WithOrgName(o.orgID))
64+
identity, err := sdk.newIdentity(opts...)
9065
if err != nil {
9166
return nil, errors.WithMessage(err, "unable to create client context")
9267
}
9368

9469
cc := clientContext{
95-
opts: o,
9670
identity: identity,
97-
providers: &context.Client{Providers: &sdk.provider, Identity: identity},
71+
providers: &context.Client{Providers: sdk.provider, Identity: identity},
9872
}
9973
return &cc, nil
10074
}
@@ -104,32 +78,6 @@ func (sdk *FabricSDK) NewClient(identityOpt IdentityOption, opts ...ContextOptio
10478
return &client
10579
}
10680

107-
func newContextOptions(config core.Config, options []ContextOption) (*contextOptions, error) {
108-
// Read default org name from configuration
109-
client, err := config.Client()
110-
if err != nil {
111-
return nil, errors.WithMessage(err, "unable to retrieve client from network config")
112-
}
113-
114-
opts := contextOptions{
115-
orgID: client.Organization,
116-
config: config,
117-
}
118-
119-
for _, option := range options {
120-
err := option(&opts)
121-
if err != nil {
122-
return nil, errors.WithMessage(err, "error in option passed to client")
123-
}
124-
}
125-
126-
if opts.orgID == "" {
127-
return nil, errors.New("must provide default organisation name in configuration")
128-
}
129-
130-
return &opts, nil
131-
}
132-
13381
func newClientOptions(options []ClientOption) (*clientOptions, error) {
13482
opts := clientOptions{}
13583

pkg/fabsdk/client_test.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestFromConfigGoodClientOpt(t *testing.T) {
5454
}
5555

5656
func goodClientOpt() ContextOption {
57-
return func(opts *contextOptions) error {
57+
return func(opts *identityOptions) error {
5858
return nil
5959
}
6060
}
@@ -73,7 +73,7 @@ func TestNewBadClientOpt(t *testing.T) {
7373
}
7474

7575
func badClientOpt() ContextOption {
76-
return func(opts *contextOptions) error {
76+
return func(opts *identityOptions) error {
7777
return errors.New("Bad Opt")
7878
}
7979
}
@@ -124,24 +124,6 @@ func TestWithFilter(t *testing.T) {
124124
}
125125
}
126126

127-
func TestWithConfig(t *testing.T) {
128-
c, err := configImpl.FromFile(clientConfigFile)()
129-
if err != nil {
130-
t.Fatalf("Unexpected error from config: %v", err)
131-
}
132-
opt := withConfig(c)
133-
134-
opts := contextOptions{}
135-
err = opt(&opts)
136-
if err != nil {
137-
t.Fatalf("Expected no error from option, but got %v", err)
138-
}
139-
140-
if opts.config != c {
141-
t.Fatalf("Expected config to be set in opts")
142-
}
143-
}
144-
145127
func TestNoIdentity(t *testing.T) {
146128
sdk, err := New(configImpl.FromFile(clientConfigFile))
147129
if err != nil {
@@ -155,7 +137,7 @@ func TestNoIdentity(t *testing.T) {
155137
}
156138
}
157139

158-
func noopIdentityOpt() IdentityOption {
140+
func noopIdentityOpt() ContextOption {
159141
return func(o *identityOptions) error {
160142
return nil
161143
}

pkg/fabsdk/context.go

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,45 @@ import (
1616
type identityOptions struct {
1717
identity contextApi.Identity
1818
orgName string
19-
user string
19+
userName string
2020
}
2121

22-
// IdentityOption provides parameters for creating a session (primarily from a fabric identity/user)
23-
type IdentityOption func(s *identityOptions) error
22+
// ContextOption provides parameters for creating a session (primarily from a fabric identity/user)
23+
type ContextOption func(s *identityOptions) error
2424

2525
// WithUser uses the named user to load the identity
26-
func WithUser(user string) IdentityOption {
26+
func WithUser(userName string) ContextOption {
2727
return func(o *identityOptions) error {
28-
o.user = user
28+
o.userName = userName
2929
return nil
3030
}
3131
}
3232

3333
// WithIdentity uses a pre-constructed identity object as the credential for the session
34-
func WithIdentity(identity contextApi.Identity) IdentityOption {
34+
func WithIdentity(identity contextApi.Identity) ContextOption {
3535
return func(o *identityOptions) error {
3636
o.identity = identity
3737
return nil
3838
}
3939
}
4040

41-
// WithOrgName uses a pre-constructed identity object as the credential for the session
42-
func WithOrgName(org string) IdentityOption {
41+
// WithOrg uses the named organization
42+
func WithOrg(org string) ContextOption {
4343
return func(o *identityOptions) error {
4444
o.orgName = org
4545
return nil
4646
}
4747
}
4848

49-
type channelContextOptions struct {
50-
identity contextApi.Identity
51-
orgName string
52-
user string
53-
}
54-
55-
// ChannelContextOption provides parameters for creating a channel context
56-
type ChannelContextOption func(s *channelContextOptions) error
57-
58-
// WithChannelUser uses the named user to load the identity
59-
func WithChannelUser(user string) ChannelContextOption {
60-
return func(o *channelContextOptions) error {
61-
o.user = user
62-
return nil
63-
}
64-
}
65-
66-
// WithChannelIdentity uses orgName to load identity
67-
func WithChannelIdentity(identity contextApi.Identity) ChannelContextOption {
68-
return func(o *channelContextOptions) error {
69-
o.identity = identity
70-
return nil
49+
func (sdk *FabricSDK) newIdentity(options ...ContextOption) (contextApi.Identity, error) {
50+
clientConfig, err := sdk.Config().Client()
51+
if err != nil {
52+
return nil, errors.WithMessage(err, "retrieving client configuration failed")
7153
}
72-
}
7354

74-
// WithChannelOrgName uses a pre-constructed identity object as the credential for the session
75-
func WithChannelOrgName(org string) ChannelContextOption {
76-
return func(o *channelContextOptions) error {
77-
o.orgName = org
78-
return nil
55+
opts := identityOptions{
56+
orgName: clientConfig.Organization,
7957
}
80-
}
81-
82-
func (sdk *FabricSDK) newIdentity(options ...IdentityOption) (contextApi.Identity, error) {
83-
opts := identityOptions{}
8458

8559
for _, option := range options {
8660
err := option(&opts)
@@ -93,7 +67,7 @@ func (sdk *FabricSDK) newIdentity(options ...IdentityOption) (contextApi.Identit
9367
return opts.identity, nil
9468
}
9569

96-
if opts.user == "" || opts.orgName == "" {
70+
if opts.userName == "" || opts.orgName == "" {
9771
return nil, errors.New("invalid options to create identity")
9872
}
9973

@@ -102,7 +76,7 @@ func (sdk *FabricSDK) newIdentity(options ...IdentityOption) (contextApi.Identit
10276
return nil, errors.New("invalid options to create identity, invalid org name")
10377
}
10478

105-
user, err := mgr.GetUser(opts.user)
79+
user, err := mgr.GetUser(opts.userName)
10680
if err != nil {
10781
return nil, err
10882
}

pkg/fabsdk/context_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestFabricSDKContext(t *testing.T) {
8484
t.Fatal("context client will have providers even if idenity fails")
8585
}
8686

87-
ctxProvider = sdk.Context(WithUser("INVALID_USER"), WithOrgName("INVALID_ORG_NAME"))
87+
ctxProvider = sdk.Context(WithUser("INVALID_USER"), WithOrg("INVALID_ORG_NAME"))
8888

8989
ctx, err = ctxProvider()
9090

@@ -96,7 +96,7 @@ func TestFabricSDKContext(t *testing.T) {
9696
t.Fatal("context client will have providers even if idenity fails")
9797
}
9898

99-
ctxProvider = sdk.Context(WithUser(identityValidOptUser), WithOrgName(identityValidOptOrg))
99+
ctxProvider = sdk.Context(WithUser(identityValidOptUser), WithOrg(identityValidOptOrg))
100100

101101
ctx, err = ctxProvider()
102102

pkg/fabsdk/fabsdk.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// FabricSDK provides access (and context) to clients being managed by the SDK.
2727
type FabricSDK struct {
2828
opts options
29-
provider context.Provider
29+
provider *context.Provider
3030
}
3131

3232
type options struct {
@@ -175,14 +175,14 @@ func initSDK(sdk *FabricSDK, config core.Config, opts []Option) error {
175175
}
176176

177177
//Initialize sdk provider
178-
sdk.provider = *context.NewProvider(context.WithConfig(config),
178+
sdk.provider = context.NewProvider(context.WithConfig(config),
179179
context.WithCryptoSuite(cryptoSuite),
180180
context.WithSigningManager(signingManager),
181181
context.WithStateStore(stateStore),
182182
context.WithIdentityManager(identityManager))
183183

184184
// Initialize Fabric Provider
185-
fabricProvider, err := sdk.opts.Core.CreateFabricProvider(&sdk.provider)
185+
fabricProvider, err := sdk.opts.Core.CreateFabricProvider(sdk.provider)
186186
if err != nil {
187187
return errors.WithMessage(err, "failed to initialize core fabric provider")
188188
}
@@ -211,7 +211,7 @@ func initSDK(sdk *FabricSDK, config core.Config, opts []Option) error {
211211
}
212212

213213
//update sdk providers list since all required providers are initialized
214-
sdk.provider = *context.NewProvider(context.WithConfig(config),
214+
sdk.provider = context.NewProvider(context.WithConfig(config),
215215
context.WithCryptoSuite(cryptoSuite),
216216
context.WithSigningManager(signingManager),
217217
context.WithStateStore(stateStore),
@@ -235,36 +235,22 @@ func (sdk *FabricSDK) Config() core.Config {
235235
}
236236

237237
//Context creates and returns context client which has all the necessary providers
238-
func (sdk *FabricSDK) Context(options ...IdentityOption) contextApi.ClientProvider {
238+
func (sdk *FabricSDK) Context(options ...ContextOption) contextApi.ClientProvider {
239239

240240
clientProvider := func() (contextApi.Client, error) {
241241
identity, err := sdk.newIdentity(options...)
242-
return &context.Client{Providers: &sdk.provider, Identity: identity}, err
242+
return &context.Client{Providers: sdk.provider, Identity: identity}, err
243243
}
244244

245245
return clientProvider
246246
}
247247

248248
//ChannelContext creates and returns channel context
249-
func (sdk *FabricSDK) ChannelContext(channelID string, options ...ChannelContextOption) contextApi.ChannelProvider {
249+
func (sdk *FabricSDK) ChannelContext(channelID string, options ...ContextOption) contextApi.ChannelProvider {
250250

251251
channelProvider := func() (contextApi.Channel, error) {
252252

253-
channelCtxOpts := channelContextOptions{}
254-
for _, param := range options {
255-
param(&channelCtxOpts)
256-
}
257-
258-
var identityOpts []IdentityOption
259-
if channelCtxOpts.identity != nil {
260-
identityOpts = append(identityOpts, WithIdentity(channelCtxOpts.identity))
261-
} else {
262-
identityOpts = append(identityOpts, WithUser(channelCtxOpts.user))
263-
identityOpts = append(identityOpts, WithOrgName(channelCtxOpts.orgName))
264-
}
265-
266-
clientCtxProvider := sdk.Context(identityOpts...)
267-
253+
clientCtxProvider := sdk.Context(options...)
268254
return context.NewChannel(clientCtxProvider, channelID)
269255

270256
}

pkg/fabsdk/fabsdk_chconfig_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func verifySDK(t *testing.T, sdk *FabricSDK) {
3434
sdk.provider.ChannelProvider().(*chpvdr.ChannelProvider).SetChannelConfig(mocks.NewMockChannelCfg("orgchannel"))
3535

3636
// Get a common client context for the following tests
37-
chCtx1 := sdk.ChannelContext("mychannel", WithChannelUser(sdkValidClientUser), WithChannelOrgName(sdkValidClientOrg2))
38-
chCtx2 := sdk.ChannelContext("orgchannel", WithChannelUser(sdkValidClientUser), WithChannelOrgName(sdkValidClientOrg2))
37+
chCtx1 := sdk.ChannelContext("mychannel", WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg2))
38+
chCtx2 := sdk.ChannelContext("orgchannel", WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg2))
3939

4040
// Test configuration failure for channel client (mychannel does't have event source configured for Org2)
4141
_, err := channel.New(chCtx1)
@@ -105,8 +105,8 @@ func TestNewDefaultTwoValidSDK(t *testing.T) {
105105
// Get a common client context for the following tests
106106
//cc1 := sdk1.NewClient(WithUser(sdkValidClientUser))
107107

108-
cc1CtxC1 := sdk1.ChannelContext("mychannel", WithChannelUser(sdkValidClientUser), WithChannelOrgName(sdkValidClientOrg1))
109-
cc1CtxC2 := sdk1.ChannelContext("orgchannel", WithChannelUser(sdkValidClientUser), WithChannelOrgName(sdkValidClientOrg1))
108+
cc1CtxC1 := sdk1.ChannelContext("mychannel", WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg1))
109+
cc1CtxC2 := sdk1.ChannelContext("orgchannel", WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg1))
110110

111111
// Test SDK1 channel clients ('mychannel', 'orgchannel')
112112
_, err = channel.New(cc1CtxC1)
@@ -120,8 +120,8 @@ func TestNewDefaultTwoValidSDK(t *testing.T) {
120120
}
121121

122122
// Get a common client context for the following tests
123-
cc2CtxC1 := sdk1.ChannelContext("mychannel", WithChannelUser(sdkValidClientUser), WithChannelOrgName(sdkValidClientOrg2))
124-
cc2CtxC2 := sdk1.ChannelContext("orgchannel", WithChannelUser(sdkValidClientUser), WithChannelOrgName(sdkValidClientOrg2))
123+
cc2CtxC1 := sdk1.ChannelContext("mychannel", WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg2))
124+
cc2CtxC2 := sdk1.ChannelContext("orgchannel", WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg2))
125125

126126
// SDK 2 doesn't have 'mychannel' configured
127127
_, err = channel.New(cc2CtxC1)

pkg/fabsdk/fabsdk_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestWithSessionPkg(t *testing.T) {
143143
defer sdk.Close()
144144

145145
// Get resource management
146-
ctx := sdk.Context(WithUser(sdkValidClientUser), WithOrgName(sdkValidClientOrg1))
146+
ctx := sdk.Context(WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg1))
147147
_, err = resmgmt.New(ctx)
148148
if err != nil {
149149
t.Fatalf("Unexpected error getting channel management client: %s", err)

0 commit comments

Comments
 (0)