Skip to content

Commit a183e32

Browse files
committed
feat(entries): added UpdateEntry
1 parent 14ef8ee commit a183e32

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed

authentication.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Client struct {
1313
client *http.Client
1414
baseUri string
1515
credential credentials
16+
17+
ClientUser DvlsUser
1618
}
1719

1820
type credentials struct {
@@ -80,7 +82,7 @@ const (
8082
isLoggedEndpoint string = "/api/is-logged"
8183
)
8284

83-
func NewClient(username string, password string, baseUri string) (Client, DvlsUser, error) {
85+
func NewClient(username string, password string, baseUri string) (Client, error) {
8486
credential := credentials{username: username, password: password}
8587
client := Client{
8688
client: &http.Client{},
@@ -90,10 +92,12 @@ func NewClient(username string, password string, baseUri string) (Client, DvlsUs
9092

9193
user, err := client.login()
9294
if err != nil {
93-
return Client{}, DvlsUser{}, fmt.Errorf("login failed \"%w\"", err)
95+
return Client{}, fmt.Errorf("login failed \"%w\"", err)
9496
}
9597

96-
return client, user, nil
98+
client.ClientUser = user
99+
100+
return client, nil
97101
}
98102

99103
func (c *Client) login() (DvlsUser, error) {

dvls_test.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
const testEntryId string = "76a4fcf6-fec1-4297-bc1e-a327841055ad"
1010
const testVaultId string = "e0f4f35d-8cb5-40d9-8b2b-35c96ea1c9b5"
1111

12-
var testPassword string = "TestK8sPassword"
1312
var testNewEntry DvlsEntry
1413
var testEntry DvlsEntry = DvlsEntry{
1514
ID: testEntryId,
@@ -19,21 +18,18 @@ var testEntry DvlsEntry = DvlsEntry{
1918
ConnectionType: ServerConnectionCredential,
2019
ConnectionSubType: ServerConnectionSubTypeDefault,
2120
Tags: []string{"Test tag 1", "Test tag 2", "testtag"},
22-
Credentials: DvlsEntryCredentials{
23-
Username: "TestK8s",
24-
Password: &testPassword,
25-
},
21+
Credentials: NewEntryCredentials("TestK8s", "TestK8sPassword"),
2622
}
2723

2824
var testClient Client
2925

3026
func Test_NewClient(t *testing.T) {
31-
c, user, err := NewClient(os.Getenv("TEST_USER"), os.Getenv("TEST_PASSWORD"), os.Getenv("TEST_INSTANCE"))
27+
c, err := NewClient(os.Getenv("TEST_USER"), os.Getenv("TEST_PASSWORD"), os.Getenv("TEST_INSTANCE"))
3228
if err != nil {
3329
t.Fatal(err)
3430
}
35-
if user.UserType != UserAuthenticationApplication {
36-
t.Fatalf("user credentials is not an Application. User type %s", user.UserType)
31+
if c.ClientUser.UserType != UserAuthenticationApplication {
32+
t.Fatalf("user credentials is not an Application. User type %s", c.ClientUser.UserType)
3733
}
3834

3935
testClient = c
@@ -42,15 +38,13 @@ func Test_NewClient(t *testing.T) {
4238
t.Run("GetEntryCredentialsPassword", test_GetEntryCredentialsPassword)
4339
t.Run("GetEntry", test_GetEntry)
4440
t.Run("NewEntry", test_NewEntry)
41+
t.Run("UpdateEntry", test_UpdateEntry)
4542
t.Run("DeleteEntry", test_DeleteEntry)
4643
t.Run("GetServerInfo", test_GetServerInfo)
4744
}
4845

4946
func test_GetEntryCredentialsPassword(t *testing.T) {
50-
testSecret := DvlsEntryCredentials{
51-
Username: "TestK8s",
52-
Password: &testPassword,
53-
}
47+
testSecret := testEntry.Credentials
5448
secret, err := testClient.GetEntryCredentialsPassword(testEntry)
5549
if err != nil {
5650
t.Fatal(err)
@@ -96,6 +90,28 @@ func test_NewEntry(t *testing.T) {
9690
if !reflect.DeepEqual(entry, testNewEntry) {
9791
t.Fatalf("fetched entry did not match test entry. Expected %#v, got %#v", testNewEntry, entry)
9892
}
93+
94+
testNewEntry = entry
95+
}
96+
97+
func test_UpdateEntry(t *testing.T) {
98+
testUpdatedEntry := testNewEntry
99+
testUpdatedEntry.EntryName = "TestK8sUpdatedEntry"
100+
testUpdatedEntry.Credentials = NewEntryCredentials("TestK8sUpdatedUser", "TestK8sUpdatedPassword")
101+
102+
entry, err := testClient.UpdateEntry(testUpdatedEntry)
103+
if err != nil {
104+
t.Fatal(err)
105+
}
106+
107+
testUpdatedEntry.ModifiedDate = entry.ModifiedDate
108+
testUpdatedEntry.Tags = entry.Tags
109+
110+
if !reflect.DeepEqual(entry, testUpdatedEntry) {
111+
t.Fatalf("fetched entry did not match test entry. Expected %#v, got %#v", testUpdatedEntry, entry)
112+
}
113+
114+
testNewEntry = entry
99115
}
100116

101117
func test_DeleteEntry(t *testing.T) {

entries.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type DvlsEntry struct {
2727

2828
func (e DvlsEntry) MarshalJSON() ([]byte, error) {
2929
raw := struct {
30+
Id string `json:"id,omitempty"`
3031
RepositoryId string `json:"repositoryId"`
3132
Name string `json:"name"`
3233
Description string `json:"description"`
@@ -58,6 +59,7 @@ func (e DvlsEntry) MarshalJSON() ([]byte, error) {
5859
}
5960
}
6061

62+
raw.Id = e.ID
6163
raw.Keywords = strings.Join(e.Tags, " ")
6264
raw.Description = e.Description
6365
raw.RepositoryId = e.VaultId
@@ -290,6 +292,38 @@ func (c *Client) NewEntry(entry DvlsEntry) (DvlsEntry, error) {
290292
return entry, nil
291293
}
292294

295+
func (c *Client) UpdateEntry(entry DvlsEntry) (DvlsEntry, error) {
296+
if entry.ConnectionType != ServerConnectionCredential || entry.ConnectionSubType != ServerConnectionSubTypeDefault {
297+
return DvlsEntry{}, fmt.Errorf("unsupported entry type (%s %s). Only %s %s is supported", entry.ConnectionType, entry.ConnectionSubType, ServerConnectionCredential, ServerConnectionSubTypeDefault)
298+
}
299+
300+
entry.ModifiedDate = nil
301+
302+
reqUrl, err := url.JoinPath(c.baseUri, entryEndpoint, "save")
303+
if err != nil {
304+
return DvlsEntry{}, fmt.Errorf("failed to build entry url. error: %w", err)
305+
}
306+
307+
entryJson, err := json.Marshal(entry)
308+
if err != nil {
309+
return DvlsEntry{}, fmt.Errorf("failed to marshall body. error: %w", err)
310+
}
311+
312+
resp, err := c.Request(reqUrl, http.MethodPut, bytes.NewBuffer(entryJson))
313+
if err != nil {
314+
return DvlsEntry{}, fmt.Errorf("error while creating entry. error: %w", err)
315+
} else if resp.Result != 1 {
316+
return DvlsEntry{}, fmt.Errorf("unexpected result code %d %s", resp.Result, resp.Message)
317+
}
318+
319+
err = json.Unmarshal(resp.Response, &entry)
320+
if err != nil {
321+
return DvlsEntry{}, fmt.Errorf("failed to unmarshall response body. error: %w", err)
322+
}
323+
324+
return entry, nil
325+
}
326+
293327
func (c *Client) DeleteEntry(entryId string) error {
294328
reqUrl, err := url.JoinPath(c.baseUri, entryEndpoint, entryId)
295329
if err != nil {
@@ -305,3 +339,11 @@ func (c *Client) DeleteEntry(entryId string) error {
305339

306340
return nil
307341
}
342+
343+
func NewEntryCredentials(username string, password string) DvlsEntryCredentials {
344+
creds := DvlsEntryCredentials{
345+
Username: username,
346+
Password: &password,
347+
}
348+
return creds
349+
}

0 commit comments

Comments
 (0)