github.com/imran-kn/cilium-fork@v1.6.9/pkg/client/policy.go (about) 1 // Copyright 2016-2017 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package client 16 17 import ( 18 "github.com/cilium/cilium/api/v1/client/policy" 19 "github.com/cilium/cilium/api/v1/models" 20 "github.com/cilium/cilium/pkg/api" 21 ) 22 23 // PolicyPut inserts the `policyJSON` 24 func (c *Client) PolicyPut(policyJSON string) (*models.Policy, error) { 25 params := policy.NewPutPolicyParams().WithPolicy(policyJSON).WithTimeout(api.ClientTimeout) 26 resp, err := c.Policy.PutPolicy(params) 27 if err != nil { 28 return nil, Hint(err) 29 } 30 return resp.Payload, nil 31 } 32 33 // PolicyGet returns policy rules 34 func (c *Client) PolicyGet(labels []string) (*models.Policy, error) { 35 params := policy.NewGetPolicyParams().WithLabels(labels).WithTimeout(api.ClientTimeout) 36 resp, err := c.Policy.GetPolicy(params) 37 if err != nil { 38 return nil, Hint(err) 39 } 40 return resp.Payload, nil 41 } 42 43 // PolicyCacheGet returns the contents of a SelectorCache. 44 func (c *Client) PolicyCacheGet() (models.SelectorCache, error) { 45 params := policy.NewGetPolicySelectorsParams().WithTimeout(api.ClientTimeout) 46 resp, err := c.Policy.GetPolicySelectors(params) 47 if err != nil { 48 return nil, Hint(err) 49 } 50 return resp.Payload, nil 51 } 52 53 // PolicyDelete deletes policy rules 54 func (c *Client) PolicyDelete(labels []string) (*models.Policy, error) { 55 params := policy.NewDeletePolicyParams().WithLabels(labels).WithTimeout(api.ClientTimeout) 56 resp, err := c.Policy.DeletePolicy(params) 57 if err != nil { 58 return nil, Hint(err) 59 } 60 return resp.Payload, Hint(err) 61 } 62 63 // PolicyResolveGet resolves policy for a Trace Selector with source and destination identity. 64 func (c *Client) PolicyResolveGet(traceSelector *models.TraceSelector) (*models.PolicyTraceResult, error) { 65 params := policy.NewGetPolicyResolveParams().WithTraceSelector(traceSelector).WithTimeout(api.ClientTimeout) 66 resp, err := c.Policy.GetPolicyResolve(params) 67 if err != nil { 68 return nil, Hint(err) 69 } 70 return resp.Payload, nil 71 }