github.com/hashicorp/nomad/api@v0.0.0-20240306165712-3193ac204f65/sentinel.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package api 5 6 import ( 7 "errors" 8 ) 9 10 // SentinelPolicies is used to query the Sentinel Policy endpoints. 11 type SentinelPolicies struct { 12 client *Client 13 } 14 15 // SentinelPolicies returns a new handle on the Sentinel policies. 16 func (c *Client) SentinelPolicies() *SentinelPolicies { 17 return &SentinelPolicies{client: c} 18 } 19 20 // List is used to dump all of the policies. 21 func (a *SentinelPolicies) List(q *QueryOptions) ([]*SentinelPolicyListStub, *QueryMeta, error) { 22 var resp []*SentinelPolicyListStub 23 qm, err := a.client.query("/v1/sentinel/policies", &resp, q) 24 if err != nil { 25 return nil, nil, err 26 } 27 return resp, qm, nil 28 } 29 30 // Upsert is used to create or update a policy 31 func (a *SentinelPolicies) Upsert(policy *SentinelPolicy, q *WriteOptions) (*WriteMeta, error) { 32 if policy == nil || policy.Name == "" { 33 return nil, errors.New("missing policy name") 34 } 35 wm, err := a.client.put("/v1/sentinel/policy/"+policy.Name, policy, nil, q) 36 if err != nil { 37 return nil, err 38 } 39 return wm, nil 40 } 41 42 // Delete is used to delete a policy 43 func (a *SentinelPolicies) Delete(policyName string, q *WriteOptions) (*WriteMeta, error) { 44 if policyName == "" { 45 return nil, errors.New("missing policy name") 46 } 47 wm, err := a.client.delete("/v1/sentinel/policy/"+policyName, nil, nil, q) 48 if err != nil { 49 return nil, err 50 } 51 return wm, nil 52 } 53 54 // Info is used to query a specific policy 55 func (a *SentinelPolicies) Info(policyName string, q *QueryOptions) (*SentinelPolicy, *QueryMeta, error) { 56 if policyName == "" { 57 return nil, nil, errors.New("missing policy name") 58 } 59 var resp SentinelPolicy 60 wm, err := a.client.query("/v1/sentinel/policy/"+policyName, &resp, q) 61 if err != nil { 62 return nil, nil, err 63 } 64 return &resp, wm, nil 65 } 66 67 type SentinelPolicy struct { 68 Name string 69 Description string 70 Scope string 71 EnforcementLevel string 72 Policy string 73 CreateIndex uint64 74 ModifyIndex uint64 75 } 76 77 type SentinelPolicyListStub struct { 78 Name string 79 Description string 80 Scope string 81 EnforcementLevel string 82 CreateIndex uint64 83 ModifyIndex uint64 84 }