Skip to content

Commit fbec789

Browse files
(fix): Handle none existing config in case of creation
For Bundle- and Bundlesubinterfaces creation fails, as the gnmi Update/Patch call, checks whether the object exists or not. In our case object, we try to create, does not exists in the conf DB, so the call will fail.
1 parent 662088c commit fbec789

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

internal/provider/cisco/gnmiext/v2/client.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Client interface {
6161
Patch(ctx context.Context, conf ...Configurable) error
6262
Update(ctx context.Context, conf ...Configurable) error
6363
Delete(ctx context.Context, conf ...Configurable) error
64+
Create(ctx context.Context, conf ...Configurable) error
6465
}
6566

6667
// Client is a gNMI client offering convenience methods for device configuration
@@ -143,14 +144,18 @@ func (c *client) GetState(ctx context.Context, conf ...Configurable) error {
143144
// If the current configuration equals the desired configuration, the operation is skipped.
144145
// For partial updates that merge changes, use [Client.Patch] instead.
145146
func (c *client) Update(ctx context.Context, conf ...Configurable) error {
146-
return c.set(ctx, false, conf...)
147+
return c.set(ctx, false, true, conf...)
147148
}
148149

149150
// Patch merges the configuration for the given set of items.
150151
// If the current configuration equals the desired configuration, the operation is skipped.
151152
// For full replacement of configuration, use [Client.Update] instead.
152153
func (c *client) Patch(ctx context.Context, conf ...Configurable) error {
153-
return c.set(ctx, true, conf...)
154+
return c.set(ctx, true, true, conf...)
155+
}
156+
157+
func (c *client) Create(ctx context.Context, conf ...Configurable) error {
158+
return c.set(ctx, false, false, conf...)
154159
}
155160

156161
// Delete resets the configuration for the given set of items.
@@ -256,7 +261,7 @@ func (c *client) get(ctx context.Context, dt gpb.GetRequest_DataType, conf ...Co
256261
// configuration. Otherwise, a full replacement is done.
257262
// If the current configuration equals the desired configuration, the operation
258263
// is skipped.
259-
func (c *client) set(ctx context.Context, patch bool, conf ...Configurable) error {
264+
func (c *client) set(ctx context.Context, patch bool, retrieve bool, conf ...Configurable) error {
260265
if len(conf) == 0 {
261266
return nil
262267
}
@@ -267,16 +272,20 @@ func (c *client) set(ctx context.Context, patch bool, conf ...Configurable) erro
267272
return err
268273
}
269274
got := cp.Deep(cf)
270-
err = c.GetConfig(ctx, got)
271-
if err != nil && !errors.Is(err, ErrNil) {
272-
return fmt.Errorf("gnmiext: failed to retrieve current config for %s: %w", cf.XPath(), err)
273-
}
274-
// If the current configuration is equal to the desired configuration, skip the update.
275-
// This avoids unnecessary updates and potential disruptions.
276-
if err == nil && reflect.DeepEqual(cf, got) {
277-
c.logger.V(1).Info("Configuration is already up-to-date", "path", cf.XPath())
278-
continue
275+
276+
if retrieve {
277+
err = c.GetConfig(ctx, got)
278+
if err != nil && !errors.Is(err, ErrNil) {
279+
return fmt.Errorf("gnmiext: failed to retrieve current config for %s: %w", cf.XPath(), err)
280+
}
281+
// If the current configuration is equal to the desired configuration, skip the update.
282+
// This avoids unnecessary updates and potential disruptions.
283+
if err == nil && reflect.DeepEqual(cf, got) {
284+
c.logger.V(1).Info("Configuration is already up-to-date", "path", cf.XPath())
285+
continue
286+
}
279287
}
288+
280289
b, err := c.Marshal(cf)
281290
if err != nil {
282291
return err

internal/provider/cisco/iosxr/provider_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ func (m *MockClient) Delete(ctx context.Context, conf ...gnmiext.Configurable) e
118118
return nil
119119
}
120120

121+
func (m *MockClient) Create(ctx context.Context, conf ...gnmiext.Configurable) error {
122+
return nil
123+
}
124+
121125
func (m *MockClient) GetState(ctx context.Context, conf ...gnmiext.Configurable) error {
122126
if m.GetStateFunc != nil {
123127
return m.GetStateFunc(ctx, conf...)

0 commit comments

Comments
 (0)