forked from blackducksoftware/hub-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request blackducksoftware#19 from rrati/policy-rules-client
Added policy rules api/client
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |