-
Notifications
You must be signed in to change notification settings - Fork 65
/
policy.go
112 lines (99 loc) · 4.54 KB
/
policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package chef
import (
"fmt"
)
// PolicyService is the service for interacting with chef server policies endpoint
type PolicyService struct {
client *Client
}
// PolicyGetResponse is returned from the chef-server for Get Requests to /policies
type PoliciesGetResponse map[string]Policy
type Policy struct {
Uri string `json:"uri,omitempty"`
Revisions map[string]interface{} `json:"revisions,omitempty"`
}
// PolicyGetResponse is returned from chef-server for Get Requests to /policies/<policy-name>
type PolicyGetResponse map[string]PolicyRevision
type PolicyRevision map[string]PolicyRevisionDetail
type PolicyRevisionDetail map[string]interface{}
// RevisionDetailsResponse is returned from the chef-server for Get Requests to /policies/<policy-name>/revisions/<rev-id>
type RevisionDetailsResponse struct {
RevisionID string `json:"revision_id,omitempty"`
Name string `json:"name,omitempty"`
RunList []string `json:"run_list,omitempty"`
NamedRunList map[string][]string `json:"named_run_lists,omitempty"`
IncludedPolicyLocks []IncludedPolicyLocks `json:"included_policy_locks,omitempty"`
CookbookLocks map[string]CookbookLock `json:"cookbook_locks,omitempty"`
DefaultAttributes map[string]interface{} `json:"default_attributes,omitempty"`
OverrideAttributes map[string]interface{} `json:"override_attributes,omitempty"`
SolutionDependencies SolutionDep `json:"solution_dependencies,omitempty"`
}
// IncludedPolicyLocks are the included policies locks
type IncludedPolicyLocks struct {
Name string `json:"name,omitempty"`
RevisionID string `json:"revision_id,omitempty"`
SourceOptions map[string]string `json:"source_options,omitempty"`
}
type CookbookLock struct {
Version string `json:"version,omitempty"`
Identifier string `json:"identifier,omitempty"`
DottedIdentifier string `json:"dotted_decimal_identifier,omitempty"`
Origin string `json:"origin,omitempty"`
Source string `json:"source,omitempty"`
CacheKey string `json:"cache_key,omitempty"`
SCM SCMDetail `json:"scm_info,omitempty"`
SourceOptions map[string]string `json:"source_options,omitempty"`
}
type SCMDetail struct {
Name string `json:"scm,omitempty"`
Remote string `json:"remote,omitempty"`
Revision string `json:"revision,omitempty"`
WorkingTreeClean bool `json:"working_tree_clean,omitempty"`
Published bool `json:"published,omitempty"`
SynchronizedRemoteBranches []string `json:"synchronized_remote_branches,omitempty"`
}
type SolutionDep struct {
PolicyFile [][]string `json:"Policyfile,omitempty"`
Dependencies interface{} `json:"dependencies,omitempty"`
}
// List lists the policies in the Chef server.
// Chef API docs: https://docs.chef.io/api_chef_server/#policies
// GET /policies
func (c *PolicyService) List() (data PoliciesGetResponse, err error) {
err = c.client.magicRequestDecoder("GET", "policies", nil, &data)
return
}
// Get returns details for a specific policy
//
// GET /policies/name
func (c *PolicyService) Get(name string) (data PolicyGetResponse, err error) {
path := fmt.Sprintf("policies/%s", name)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// Delete deletes a policy
//
// DELETE /policies/name
func (c *PolicyService) Delete(policyName string) (data PolicyGetResponse, err error) {
path := fmt.Sprintf("policies/%s", policyName)
err = c.client.magicRequestDecoder("DELETE", path, nil, &data)
return
}
// GetRevisionDetails returns details of a specific revision from Chef Server
//
// GET /policies/<policy-name>/revisions/<revision-id>
func (c *PolicyService) GetRevisionDetails(policyName string, revisionID string) (data RevisionDetailsResponse, err error) {
path := fmt.Sprintf("policies/%s/revisions/%s", policyName, revisionID)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// DeleteRevision deletes a revisiom from a policy
//
// GET /policies/<policy-name>/revisions/<revision-id>
func (c *PolicyService) DeleteRevision(policyName string, revisionID string) (data RevisionDetailsResponse, err error) {
path := fmt.Sprintf("policies/%s/revisions/%s", policyName, revisionID)
err = c.client.magicRequestDecoder("DELETE", path, nil, &data)
return
}
// TODO: Add these methods
// POST policies/policy-name/revisions