Skip to content

Commit

Permalink
Implement internal Juju secrets add, update, and remove functionality
Browse files Browse the repository at this point in the history
This commit introduces several changes to the Juju client in the `internal/juju/client.go` file.
It includes the implementation of methods for adding, updating, and removing secrets. Additionally,

Furthermore, the commit includes changes to the `secret.go` file, introducing new types for managinng secrets.
It also includes changes to the `interfaces.go` file, defining new interfaces for the Juju client API.

Add secretURI to UpdateSecret

Add secretURI to DeleteSecret

Add AutoPrunt to UpdateSecret schema

Add SecretId to ReadSecret func instead of name.

Add lost Asserts.

Add secretNotFoundError

Extract mocks creation into separate suite.

Introduce typedError(err) usage in ClientAPI funcs.

Add renaming to UpdateSecret

Use struct raather than pointer for Output structures.

Introcue NewName in Update input struct.

Use pointers in all places in structs where the parameter is not
neccessary.
  • Loading branch information
anvial committed Apr 3, 2024
1 parent 1e971f1 commit 78f7a60
Show file tree
Hide file tree
Showing 8 changed files with 589 additions and 81 deletions.
2 changes: 2 additions & 0 deletions internal/juju/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Client struct {
Offers offersClient
SSHKeys sshKeysClient
Users usersClient
Secrets secretsClient
}

type jujuModel struct {
Expand Down Expand Up @@ -87,6 +88,7 @@ func NewClient(ctx context.Context, config ControllerConfiguration) (*Client, er
Offers: *newOffersClient(sc),
SSHKeys: *newSSHKeysClient(sc),
Users: *newUsersClient(sc),
Secrets: *newSecretsClient(sc),
}, nil
}

Expand Down
41 changes: 41 additions & 0 deletions internal/juju/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the Apache License, Version 2.0, see LICENCE file for details.

package juju

import (
"testing"

"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
)

type JujuSuite struct {
suite.Suite

testModelName string

mockConnection *MockConnection
mockSharedClient *MockSharedClient
}

func (s *JujuSuite) setupMocks(t *testing.T) *gomock.Controller {
s.testModelName = "test-secret-model"

ctlr := gomock.NewController(t)

s.mockConnection = NewMockConnection(ctlr)
s.mockConnection.EXPECT().Close().Return(nil).AnyTimes()

log := func(msg string, additionalFields ...map[string]interface{}) {
s.T().Logf("logging from shared client %q, %+v", msg, additionalFields)
}
s.mockSharedClient = NewMockSharedClient(ctlr)
s.mockSharedClient.EXPECT().Debugf(gomock.Any(), gomock.Any()).Do(log).AnyTimes()
s.mockSharedClient.EXPECT().Errorf(gomock.Any(), gomock.Any()).Do(log).AnyTimes()
s.mockSharedClient.EXPECT().Tracef(gomock.Any(), gomock.Any()).Do(log).AnyTimes()
s.mockSharedClient.EXPECT().JujuLogger().Return(&jujuLoggerShim{}).AnyTimes()
s.mockSharedClient.EXPECT().GetConnection(&s.testModelName).Return(s.mockConnection, nil).AnyTimes()

return ctlr
}
12 changes: 12 additions & 0 deletions internal/juju/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
apiapplication "github.com/juju/juju/api/client/application"
apiclient "github.com/juju/juju/api/client/client"
apiresources "github.com/juju/juju/api/client/resources"
apisecrets "github.com/juju/juju/api/client/secrets"
apicommoncharm "github.com/juju/juju/api/common/charm"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/resources"
"github.com/juju/juju/core/secrets"
"github.com/juju/juju/rpc/params"
"github.com/juju/names/v4"
)
Expand Down Expand Up @@ -62,3 +64,13 @@ type ResourceAPIClient interface {
AddPendingResources(args apiresources.AddPendingResourcesArgs) ([]string, error)
ListResources(applications []string) ([]resources.ApplicationResources, error)
}

type SecretAPIClient interface {
CreateSecret(name, description string, data map[string]string) (string, error)
ListSecrets(reveal bool, filter secrets.Filter) ([]apisecrets.SecretDetails, error)
UpdateSecret(
uri *secrets.URI, name string, autoPrune *bool,
newName string, description string, data map[string]string,
) error
RemoveSecret(uri *secrets.URI, name string, revision *int) error
}
87 changes: 85 additions & 2 deletions internal/juju/mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/juju/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

package juju_test

//go:generate go run go.uber.org/mock/mockgen -package juju -destination mock_test.go github.com/juju/terraform-provider-juju/internal/juju SharedClient,ClientAPIClient,ApplicationAPIClient,ModelConfigAPIClient,ResourceAPIClient
//go:generate go run go.uber.org/mock/mockgen -package juju -destination mock_test.go github.com/juju/terraform-provider-juju/internal/juju SharedClient,ClientAPIClient,ApplicationAPIClient,ModelConfigAPIClient,ResourceAPIClient,SecretAPIClient
//go:generate go run go.uber.org/mock/mockgen -package juju -destination jujuapi_mock_test.go github.com/juju/juju/api Connection
Loading

0 comments on commit 78f7a60

Please sign in to comment.