|
| 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 | +} |
0 commit comments