Skip to content

Commit 3708c30

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 a138c9d commit 3708c30

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Client interface {
6363
Patch(ctx context.Context, conf ...Configurable) error
6464
Update(ctx context.Context, conf ...Configurable) error
6565
Delete(ctx context.Context, conf ...Configurable) error
66+
Create(ctx context.Context, conf ...Configurable) error
6667
}
6768

6869
// Client is a gNMI client offering convenience methods for device configuration
@@ -148,14 +149,18 @@ func (c *client) GetState(ctx context.Context, conf ...Configurable) error {
148149
// If the current configuration equals the desired configuration, the operation is skipped.
149150
// For partial updates that merge changes, use [Client.Patch] instead.
150151
func (c *client) Update(ctx context.Context, conf ...Configurable) error {
151-
return c.set(ctx, false, conf...)
152+
return c.set(ctx, false, true, conf...)
152153
}
153154

154155
// Patch merges the configuration for the given set of items.
155156
// If the current configuration equals the desired configuration, the operation is skipped.
156157
// For full replacement of configuration, use [Client.Update] instead.
157158
func (c *client) Patch(ctx context.Context, conf ...Configurable) error {
158-
return c.set(ctx, true, conf...)
159+
return c.set(ctx, true, true, conf...)
160+
}
161+
162+
func (c *client) Create(ctx context.Context, conf ...Configurable) error {
163+
return c.set(ctx, false, false, conf...)
159164
}
160165

161166
// Delete resets the configuration for the given set of items.
@@ -261,7 +266,7 @@ func (c *client) get(ctx context.Context, dt gpb.GetRequest_DataType, conf ...Co
261266
// configuration. Otherwise, a full replacement is done.
262267
// If the current configuration equals the desired configuration, the operation
263268
// is skipped.
264-
func (c *client) set(ctx context.Context, patch bool, conf ...Configurable) error {
269+
func (c *client) set(ctx context.Context, patch bool, retrieve bool, conf ...Configurable) error {
265270
if len(conf) == 0 {
266271
return nil
267272
}
@@ -272,16 +277,20 @@ func (c *client) set(ctx context.Context, patch bool, conf ...Configurable) erro
272277
return err
273278
}
274279
got := cp.Deep(cf)
275-
err = c.GetConfig(ctx, got)
276-
if err != nil && !errors.Is(err, ErrNil) {
277-
return fmt.Errorf("gnmiext: failed to retrieve current config for %s: %w", cf.XPath(), err)
278-
}
279-
// If the current configuration is equal to the desired configuration, skip the update.
280-
// This avoids unnecessary updates and potential disruptions.
281-
if err == nil && reflect.DeepEqual(cf, got) {
282-
c.logger.V(1).Info("Configuration is already up-to-date", "path", cf.XPath())
283-
continue
280+
281+
if retrieve {
282+
err = c.GetConfig(ctx, got)
283+
if err != nil && !errors.Is(err, ErrNil) {
284+
return fmt.Errorf("gnmiext: failed to retrieve current config for %s: %w", cf.XPath(), err)
285+
}
286+
// If the current configuration is equal to the desired configuration, skip the update.
287+
// This avoids unnecessary updates and potential disruptions.
288+
if err == nil && reflect.DeepEqual(cf, got) {
289+
c.logger.V(1).Info("Configuration is already up-to-date", "path", cf.XPath())
290+
continue
291+
}
284292
}
293+
285294
b, err := c.Marshal(cf)
286295
if err != nil {
287296
return err

internal/provider/cisco/iosxr/provider_test.go

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

119+
func (m *MockClient) Create(ctx context.Context, conf ...gnmiext.Configurable) error {
120+
return nil
121+
}
122+
119123
func (m *MockClient) GetState(ctx context.Context, conf ...gnmiext.Configurable) error {
120124
if m.GetStateFunc != nil {
121125
return m.GetStateFunc(ctx, conf...)

0 commit comments

Comments
 (0)