Skip to content

Commit

Permalink
Merge pull request blackducksoftware#19 from rrati/policy-rules-client
Browse files Browse the repository at this point in the history
Added policy rules api/client
  • Loading branch information
robpacheco authored Oct 18, 2018
2 parents 10d1621 + e338e7c commit c221869
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
61 changes: 61 additions & 0 deletions hubapi/policyrules-api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2018 Synopsys, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hubapi

type PolicyRuleList struct {
TotalCount uint32 `json:"totalCount"`
Items []PolicyRule `json:"items"`
Meta Meta `json:"_meta"`
}

type PolicyRule struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
Overridable bool `json:"overridable"`
Severity string `json:"severity"`
Expression PolicyExpression `json:"expression"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
CreatedByUser string `json:"createdByUser"`
UpdatedAt string `json:"updatedAt"`
UpdatedBy string `json:"updatedBy"`
UpdatedByUser string `json:"updatedByUser"`
Meta Meta `json:"_meta"`
}

type PolicyExpression struct {
Operator string `json:"operator"`
Expressions []Expression `json:"expressions"`
}

type Expression struct {
Name string `json:"name"`
Operation string `json:"operation"`
Parameters ExpressionParameter `json:"parameters"`
}

type ExpressionParameter struct {
Values []string `json:"values"`
Data []map[string]string `json:"data"`
}

type PolicyRuleRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
Overridable bool `json:"overridable"`
Expression PolicyExpression `json:"expression"`
Severity string `json:"severity"`
}
73 changes: 73 additions & 0 deletions hubclient/policyrules-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2018 Synopsys, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hubclient

import (
"fmt"

"github.com/blackducksoftware/hub-client-go/hubapi"

log "github.com/sirupsen/logrus"
)

func (c *Client) ListPolicyRules(options *hubapi.GetListOptions) (*hubapi.PolicyRuleList, error) {
params := ""
if options != nil {
params = fmt.Sprintf("?%s", hubapi.ParameterString(options))
}

policyRuleURL := fmt.Sprintf("%s/api/policy-rules%s", c.baseURL, params)

var policyRuleList hubapi.PolicyRuleList
err := c.HttpGetJSON(policyRuleURL, &policyRuleList, 200)

if err != nil {
log.Errorf("Error trying to retrieve policy rule list: %+v.", err)
return nil, err
}

return &policyRuleList, nil
}

func (c *Client) GetPolicyRule(link hubapi.ResourceLink) (*hubapi.PolicyRule, error) {
var policyRule hubapi.PolicyRule
err := c.HttpGetJSON(link.Href, &policyRule, 200)

if err != nil {
log.Errorf("Error trying to retrieve a policy rule: %+v.", err)
return nil, err
}

return &policyRule, nil
}

func (c *Client) CreatePolicyRule(policyRuleRequest *hubapi.PolicyRuleRequest) (string, error) {
policyRuleURL := fmt.Sprintf("%s/api/policy-rules", c.baseURL)
location, err := c.HttpPostJSON(policyRuleURL, policyRuleRequest, "application/json", 201)

if err != nil {
return location, err
}

if location == "" {
log.Warnf("Did not get a location header back for policy rule creation")
}

return location, err
}

func (c *Client) DeletePolicyRule(policyRuleURL string) error {
return c.HttpDelete(policyRuleURL, "application/json", 204)
}

0 comments on commit c221869

Please sign in to comment.