Skip to content

Commit 1b752a2

Browse files
feat: Add ConfigService to be able to manage ConfigProperty values. (#36)
1 parent fd99d18 commit 1b752a2

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Client struct {
3939
Analysis AnalysisService
4040
BOM BOMService
4141
Component ComponentService
42+
Config ConfigService
4243
Finding FindingService
4344
Event EventService
4445
License LicenseService
@@ -87,6 +88,7 @@ func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
8788
client.Analysis = AnalysisService{client: &client}
8889
client.BOM = BOMService{client: &client}
8990
client.Component = ComponentService{client: &client}
91+
client.Config = ConfigService{client: &client}
9092
client.Finding = FindingService{client: &client}
9193
client.Event = EventService{client: &client}
9294
client.License = LicenseService{client: &client}

config.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dtrack
2+
3+
import (
4+
"context"
5+
"net/http"
6+
)
7+
8+
type ConfigPropertyType string
9+
10+
type ConfigProperty struct {
11+
GroupName string `json:"groupName"`
12+
Name string `json:"propertyName"`
13+
Value string `json:"propertyValue,omitempty"`
14+
Type string `json:"propertyType"`
15+
Description string `json:"description,omitempty"`
16+
}
17+
18+
type ConfigService struct {
19+
client *Client
20+
}
21+
22+
func (cs ConfigService) GetAll(ctx context.Context) (cps []ConfigProperty, err error) {
23+
req, err := cs.client.newRequest(ctx, http.MethodGet, "/api/v1/configProperty")
24+
if err != nil {
25+
return
26+
}
27+
_, err = cs.client.doRequest(req, &cps)
28+
return
29+
}
30+
31+
func (cs ConfigService) Get(ctx context.Context, groupName, propertyName string) (cp ConfigProperty, err error) {
32+
cps, err := cs.GetAll(ctx)
33+
if err != nil {
34+
return
35+
}
36+
for _, cp := range cps {
37+
if cp.GroupName != groupName {
38+
continue
39+
}
40+
if cp.Name != propertyName {
41+
continue
42+
}
43+
return cp, nil
44+
}
45+
return
46+
}
47+
48+
func (cs ConfigService) Update(ctx context.Context, config ConfigProperty) (cp ConfigProperty, err error) {
49+
req, err := cs.client.newRequest(ctx, http.MethodPost, "/api/v1/configProperty", withBody(config))
50+
if err != nil {
51+
return
52+
}
53+
_, err = cs.client.doRequest(req, &cp)
54+
return
55+
}
56+
57+
func (cs ConfigService) UpdateAll(ctx context.Context, configs []ConfigProperty) (cps []ConfigProperty, err error) {
58+
req, err := cs.client.newRequest(ctx, http.MethodPost, "/api/v1/configProperty/aggregate", withBody(configs))
59+
if err != nil {
60+
return
61+
}
62+
_, err = cs.client.doRequest(req, &cps)
63+
return
64+
}

config_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package dtrack
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/require"
6+
"testing"
7+
)
8+
9+
const TEST_URL = "https://localhost"
10+
11+
func TestGetConfigProperty(t *testing.T) {
12+
client := setUpContainer(t, testContainerOptions{
13+
APIPermissions: []string{
14+
PermissionSystemConfiguration,
15+
},
16+
})
17+
18+
property, err := client.Config.Get(context.Background(), "general", "base.url")
19+
require.NoError(t, err)
20+
require.Equal(t, property.GroupName, "general")
21+
require.Equal(t, property.Name, "base.url")
22+
require.Equal(t, property.Type, "URL")
23+
require.Equal(t, property.Description, "URL used to construct links back to Dependency-Track from external systems")
24+
}
25+
26+
func TestUpdateConfigProperty(t *testing.T) {
27+
client := setUpContainer(t, testContainerOptions{
28+
APIPermissions: []string{
29+
PermissionSystemConfiguration,
30+
},
31+
})
32+
33+
property, err := client.Config.Get(context.Background(), "general", "base.url")
34+
require.NoError(t, err)
35+
require.Empty(t, property.Value)
36+
37+
property.Value = TEST_URL
38+
39+
property, err = client.Config.Update(context.Background(), property)
40+
require.NoError(t, err)
41+
require.Equal(t, property.GroupName, "general")
42+
require.Equal(t, property.Name, "base.url")
43+
require.Equal(t, property.Type, "URL")
44+
require.Equal(t, property.Description, "URL used to construct links back to Dependency-Track from external systems")
45+
46+
require.Equal(t, property.Value, TEST_URL)
47+
}
48+
49+
func TestUpdateAllConfigProperty(t *testing.T) {
50+
client := setUpContainer(t, testContainerOptions{
51+
APIPermissions: []string{
52+
PermissionSystemConfiguration,
53+
},
54+
})
55+
56+
baseUrl, err := client.Config.Get(context.Background(), "general", "base.url")
57+
require.NoError(t, err)
58+
badgeEnabled, err := client.Config.Get(context.Background(), "general", "badge.enabled")
59+
require.NoError(t, err)
60+
defaultLocale, err := client.Config.Get(context.Background(), "general", "default.locale")
61+
require.NoError(t, err)
62+
63+
require.Empty(t, baseUrl.Value)
64+
require.Equal(t, badgeEnabled.Value, "false")
65+
require.Empty(t, defaultLocale.Value)
66+
67+
baseUrl.Value = TEST_URL
68+
badgeEnabled.Value = "true"
69+
defaultLocale.Value = "de"
70+
71+
cps, err := client.Config.UpdateAll(context.Background(), []ConfigProperty{baseUrl, badgeEnabled, defaultLocale})
72+
require.NoError(t, err)
73+
require.Equal(t, len(cps), 3)
74+
require.Equal(t, cps[0], baseUrl)
75+
require.Equal(t, cps[1], badgeEnabled)
76+
require.Equal(t, cps[2], defaultLocale)
77+
}
78+
79+
func TestUnsetConfigProperty(t *testing.T) {
80+
client := setUpContainer(t, testContainerOptions{
81+
APIPermissions: []string{
82+
PermissionSystemConfiguration,
83+
},
84+
})
85+
86+
baseUrl, err := client.Config.Get(context.Background(), "general", "base.url")
87+
require.NoError(t, err)
88+
baseUrl.Value = TEST_URL
89+
baseUrl, err = client.Config.Update(context.Background(), baseUrl)
90+
require.NoError(t, err)
91+
require.Equal(t, baseUrl.Value, TEST_URL)
92+
93+
baseUrl.Value = ""
94+
baseUrl, err = client.Config.Update(context.Background(), baseUrl)
95+
require.NoError(t, err)
96+
require.Empty(t, baseUrl.Value)
97+
}

0 commit comments

Comments
 (0)