Skip to content

Commit d5dad09

Browse files
authored
Merge pull request #116 from datadrivers/feature/add-mail-config-service
feat: add mail config service
2 parents a250867 + b52c35a commit d5dad09

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

nexus3/mail_config.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package nexus3
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/datadrivers/go-nexus-client/nexus3/pkg/client"
9+
"github.com/datadrivers/go-nexus-client/nexus3/pkg/tools"
10+
"github.com/datadrivers/go-nexus-client/nexus3/schema"
11+
)
12+
13+
const (
14+
MailConfigsAPIEndpoint = basePath + "v1/email"
15+
)
16+
17+
type MailConfigService client.Service
18+
19+
func NewMailConfigService(c *client.Client) *MailConfigService {
20+
21+
s := &MailConfigService{
22+
Client: c,
23+
}
24+
return s
25+
}
26+
27+
func (s *MailConfigService) Get() (*schema.MailConfig, error) {
28+
body, resp, err := s.Client.Get(MailConfigsAPIEndpoint, nil)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
if resp.StatusCode != http.StatusOK {
34+
return nil, fmt.Errorf("%s", string(body))
35+
}
36+
var mailconfig schema.MailConfig
37+
if err := json.Unmarshal(body, &mailconfig); err != nil {
38+
return nil, fmt.Errorf("could not unmarschal MailConfig: %v", err)
39+
}
40+
return &mailconfig, nil
41+
}
42+
43+
func (s *MailConfigService) Create(mailconfig *schema.MailConfig) error {
44+
ioReader, err := tools.JsonMarshalInterfaceToIOReader(mailconfig)
45+
if err != nil {
46+
return err
47+
}
48+
body, resp, err := s.Client.Put(MailConfigsAPIEndpoint, ioReader)
49+
if err != nil {
50+
return err
51+
}
52+
53+
if resp.StatusCode != http.StatusNoContent {
54+
return fmt.Errorf("%s", string(body))
55+
}
56+
57+
return nil
58+
}
59+
60+
func (s *MailConfigService) Update(mailconfig *schema.MailConfig) error {
61+
ioReader, err := tools.JsonMarshalInterfaceToIOReader(mailconfig)
62+
if err != nil {
63+
return err
64+
}
65+
66+
body, resp, err := s.Client.Put(MailConfigsAPIEndpoint, ioReader)
67+
if err != nil {
68+
return err
69+
}
70+
71+
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
72+
return fmt.Errorf("%s", string(body))
73+
}
74+
75+
return nil
76+
}
77+
78+
func (s *MailConfigService) Delete() error {
79+
body, resp, err := s.Client.Delete(MailConfigsAPIEndpoint)
80+
if err != nil {
81+
return err
82+
}
83+
84+
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
85+
return fmt.Errorf("%s", string(body))
86+
}
87+
return err
88+
}

nexus3/mail_config_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package nexus3
2+
3+
import (
4+
"testing"
5+
6+
"github.com/datadrivers/go-nexus-client/nexus3/schema"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func testMailConfig(enabled *bool, host string, port int, username *string, password *string, fromAddress string, subjectPrefix *string) *schema.MailConfig {
11+
b := new(bool)
12+
*b = false
13+
14+
return &schema.MailConfig{
15+
Enabled: enabled,
16+
Host: host,
17+
Port: port,
18+
Username: username,
19+
Password: password,
20+
FromAddress: fromAddress,
21+
SubjectPrefix: subjectPrefix,
22+
StartTlsEnabled: b,
23+
StartTlsRequired: b,
24+
SslOnConnectEnabled: b,
25+
SslServerIdentityCheckEnabled: b,
26+
NexusTrustStoreEnabled: b,
27+
}
28+
}
29+
30+
func TestMailConfigCreateReadUpdateDelete(t *testing.T) {
31+
client := getTestClient()
32+
33+
enabled := true
34+
username := "uname"
35+
usernameUpdated := "username"
36+
password := "secret"
37+
subjectPrefix := "prefix"
38+
39+
// Create
40+
err := client.MailConfig.Create(testMailConfig(&enabled, "example.org", 42, &username, &password, "[email protected]", &subjectPrefix))
41+
assert.Nil(t, err)
42+
43+
// Read
44+
readMailConfig, err := client.MailConfig.Get()
45+
assert.Nil(t, err)
46+
assert.Equal(t, readMailConfig, testMailConfig(&enabled, "example.org", 42, &username, nil, "[email protected]", &subjectPrefix))
47+
48+
// Update
49+
err = client.MailConfig.Update(testMailConfig(&enabled, "example.org", 42, &usernameUpdated, nil, "[email protected]", &subjectPrefix))
50+
assert.Nil(t, err)
51+
52+
// Check updated value
53+
readMailConfig, _ = client.MailConfig.Get()
54+
assert.Equal(t, readMailConfig, testMailConfig(&enabled, "example.org", 42, &usernameUpdated, nil, "[email protected]", &subjectPrefix))
55+
56+
// Delete
57+
err = client.MailConfig.Delete()
58+
assert.Nil(t, err)
59+
}

nexus3/nexus.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type NexusClient struct {
2424
RoutingRule *RoutingRuleService
2525
Security *security.SecurityService
2626
Script *ScriptService
27+
MailConfig *MailConfigService
2728
}
2829

2930
// NewClient returns an instance of client that implements the Client interface
@@ -36,5 +37,6 @@ func NewClient(config client.Config) *NexusClient {
3637
RoutingRule: NewRoutingRuleService(client),
3738
Security: security.NewSecurityService(client),
3839
Script: NewScriptService(client),
40+
MailConfig: NewMailConfigService(client),
3941
}
4042
}

nexus3/schema/mail_config.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package schema
2+
3+
type MailConfig struct {
4+
// Toogle if mail config is active or not
5+
Enabled *bool `json:"enabled,omitempty"`
6+
7+
// Host
8+
Host string `json:"host"`
9+
10+
// Port
11+
Port int `json:"port"`
12+
13+
// Username
14+
Username *string `json:"username,omitempty"`
15+
16+
// Password
17+
Password *string `json:"password,omitempty"`
18+
19+
// FromAddress
20+
FromAddress string `json:"fromAddress"`
21+
22+
// Subject Prefix
23+
SubjectPrefix *string `json:"subjectPrefix,omitempty"`
24+
25+
// StartTlsEnabled
26+
StartTlsEnabled *bool `json:"startTlsEnabled,omitempty"`
27+
28+
// StartTlsRequired
29+
StartTlsRequired *bool `json:"startTlsRequired,omitempty"`
30+
31+
// sslOnConectEnabled
32+
SslOnConnectEnabled *bool `json:"sslOnConnectEnabled,omitempty"`
33+
34+
// sslServerIdentityCheckEnabled
35+
SslServerIdentityCheckEnabled *bool `json:"sslServerIdentityCheckEnabled,omitempty"`
36+
37+
// nexusTrustStoreEnabled
38+
NexusTrustStoreEnabled *bool `json:"nexusTrustStoreEnabled,omitempty"`
39+
}

0 commit comments

Comments
 (0)