Skip to content

Commit d20f937

Browse files
Merge pull request #45 from datadrivers/feature/routing-rule-support
Add support for routing rules
2 parents c06d6b7 + 205bb3f commit d20f937

File tree

4 files changed

+188
-1
lines changed

4 files changed

+188
-1
lines changed

client.go

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ type Client interface {
5555
RoleDelete(string) error
5656
RoleRead(string) (*Role, error)
5757
RoleUpdate(string, Role) error
58+
RoutingRuleCreate(*RoutingRule) error
59+
RoutingRuleDelete(string) error
60+
RoutingRuleRead(string) (*RoutingRule, error)
61+
RoutingRuleUpdate(*RoutingRule) error
62+
RoutingRulesLists() ([]RoutingRule, error)
5863
ScriptCreate(*Script) error
5964
ScriptDelete(string) error
6065
ScriptLists() ([]Script, error)

routing_rule.go

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const (
10+
routingRulesAPIEndpoint = "service/rest/v1/routing-rules"
11+
12+
RoutingRuleModeAllow RoutingRuleMode = "ALLOW"
13+
RoutingRuleModeBlock RoutingRuleMode = "BLOCK"
14+
)
15+
16+
type RoutingRuleMode string
17+
18+
// RoutingRule is like a filter you can apply to groups in terms of security access and general component retrieval, and can reduce the number of repositories within a group accessed in order to retrieve an component.
19+
type RoutingRule struct {
20+
// Name of the routing rule
21+
Name string `json:"name"`
22+
23+
//Description of the routing rule
24+
Description string `json:"description,omitempty"`
25+
26+
// The mode describe how to hande with mathing requests
27+
// Possible values: "BLOCK" or "ALLOW"
28+
Mode RoutingRuleMode `json:"mode"`
29+
30+
// Regular expressions used to identify request paths that are allowed or blocked (depending on above mode)
31+
Matchers []string `json:"matchers"`
32+
}
33+
34+
// IsValid checks the values of the enum RoutingRuleMode
35+
func (m RoutingRuleMode) IsValid() error {
36+
switch m {
37+
case RoutingRuleModeAllow, RoutingRuleModeBlock:
38+
return nil
39+
}
40+
return fmt.Errorf("Invalid routing rule mode. Possible values are ALLOW or BLOCK")
41+
}
42+
43+
func (c *client) RoutingRulesLists() ([]RoutingRule, error) {
44+
body, resp, err := c.Get(routingRulesAPIEndpoint, nil)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
if resp.StatusCode != http.StatusOK {
50+
return nil, fmt.Errorf("%s", string(body))
51+
}
52+
53+
var rules []RoutingRule
54+
if err := json.Unmarshal(body, &rules); err != nil {
55+
return nil, fmt.Errorf("could not unmarschal RoutingRules: %v", err)
56+
}
57+
return rules, nil
58+
}
59+
60+
func (c *client) RoutingRuleRead(name string) (*RoutingRule, error) {
61+
body, resp, err := c.Get(fmt.Sprintf("%s/%s", routingRulesAPIEndpoint, name), nil)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
if resp.StatusCode != http.StatusOK {
67+
return nil, fmt.Errorf("%s", string(body))
68+
}
69+
var rule RoutingRule
70+
if err := json.Unmarshal(body, &rule); err != nil {
71+
return nil, fmt.Errorf("could not unmarschal RoutingRules: %v", err)
72+
}
73+
return &rule, nil
74+
}
75+
76+
func (c *client) RoutingRuleCreate(rule *RoutingRule) error {
77+
if err := rule.Mode.IsValid(); err != nil {
78+
return err
79+
}
80+
ioReader, err := jsonMarshalInterfaceToIOReader(rule)
81+
if err != nil {
82+
return err
83+
}
84+
body, resp, err := c.Post(routingRulesAPIEndpoint, ioReader)
85+
if err != nil {
86+
return err
87+
}
88+
89+
if resp.StatusCode != http.StatusNoContent {
90+
return fmt.Errorf("%s", string(body))
91+
}
92+
93+
return nil
94+
}
95+
96+
func (c *client) RoutingRuleUpdate(rule *RoutingRule) error {
97+
ioReader, err := jsonMarshalInterfaceToIOReader(rule)
98+
if err != nil {
99+
return err
100+
}
101+
102+
body, resp, err := c.Put(fmt.Sprintf("%s/%s", routingRulesAPIEndpoint, rule.Name), ioReader)
103+
if err != nil {
104+
return err
105+
}
106+
107+
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
108+
return fmt.Errorf("%s", string(body))
109+
}
110+
111+
return nil
112+
}
113+
114+
func (c *client) RoutingRuleDelete(name string) error {
115+
body, resp, err := c.Delete(fmt.Sprintf("%s/%s", routingRulesAPIEndpoint, name))
116+
if err != nil {
117+
return err
118+
}
119+
120+
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
121+
return fmt.Errorf("%s", string(body))
122+
}
123+
return err
124+
}

routing_rule_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func testRoutingRule(name string, mode RoutingRuleMode) *RoutingRule {
10+
return &RoutingRule{
11+
Name: name,
12+
Mode: mode,
13+
Description: fmt.Sprintf("Go client routing roule %s", name),
14+
Matchers: []string{
15+
"match1",
16+
},
17+
}
18+
}
19+
20+
func TestRoutingRuleModeIsValid(t *testing.T) {
21+
var mode RoutingRuleMode
22+
23+
mode = "allow"
24+
assert.Error(t, mode.IsValid(), "Invalid routing rule mode. Possible values are ALLOW or BLOCK")
25+
mode = "ALLOW"
26+
assert.Nil(t, mode.IsValid())
27+
mode = "BLOCK"
28+
assert.Nil(t, mode.IsValid())
29+
}
30+
31+
func TestRoutingRuleCreateReadUpdateDelete(t *testing.T) {
32+
client := getTestClient()
33+
name := "test-routing-rule"
34+
createdRoutingRule := testRoutingRule(name, RoutingRuleModeAllow)
35+
36+
err := client.RoutingRuleCreate(createdRoutingRule)
37+
assert.Nil(t, err)
38+
39+
readRoutingRule, err := client.RoutingRuleRead(name)
40+
assert.Equal(t, createdRoutingRule, readRoutingRule)
41+
assert.Nil(t, err)
42+
43+
updatedRoutingRule := testRoutingRule(name, RoutingRuleModeBlock)
44+
45+
err = client.RoutingRuleUpdate(updatedRoutingRule)
46+
assert.Nil(t, err)
47+
48+
readRoutingRule, err = client.RoutingRuleRead(name)
49+
assert.Equal(t, updatedRoutingRule, readRoutingRule)
50+
assert.Nil(t, err)
51+
52+
err = client.RoutingRuleDelete(name)
53+
assert.Nil(t, err)
54+
55+
readRoutingRule, err = client.RoutingRuleRead(name)
56+
assert.Nil(t, readRoutingRule)
57+
assert.Error(t, err, "Did not find a routing rule with the name 'test-routing-rule'")
58+
}

scripts/start-nexus.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash -e
22

33
LOCAL_PORT=8081
4-
NEXUS_VERSION=3.25.1
4+
NEXUS_VERSION=3.29.0
55

66
echo "Starting Nexus container..."
77
docker run -d --rm -v "${PWD}/nexus.properties":/nexus-data/etc/nexus.properties \

0 commit comments

Comments
 (0)