/
data_source_policy_set.go
173 lines (142 loc) · 4.01 KB
/
data_source_policy_set.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package tfe
import (
"errors"
"fmt"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceTFEPolicySet() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFEPolicySetRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"organization": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"global": {
Type: schema.TypeBool,
Computed: true,
},
"kind": {
Description: "The policy-as-code framework for the policy. Valid values are sentinel and opa",
Type: schema.TypeString,
Optional: true,
},
"overridable": {
Description: "Whether users can override this policy when it fails during a run. Only valid for OPA policies",
Type: schema.TypeBool,
Optional: true,
},
"policies_path": {
Type: schema.TypeString,
Computed: true,
},
"policy_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"vcs_repo": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Computed: true,
},
"branch": {
Type: schema.TypeString,
Computed: true,
},
"ingress_submodules": {
Type: schema.TypeBool,
Computed: true,
},
"oauth_token_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"workspace_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
},
}
}
func dataSourceTFEPolicySetRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
name := d.Get("name").(string)
organization := d.Get("organization").(string)
listOptions := tfe.PolicySetListOptions{}
for {
policySetList, err := tfeClient.PolicySets.List(ctx, organization, &listOptions)
if err != nil {
if errors.Is(err, tfe.ErrResourceNotFound) {
return fmt.Errorf("could not find policy set %s/%s", organization, name)
}
return fmt.Errorf("Error retrieving policy set %s: %w", name, err)
}
for _, policySet := range policySetList.Items {
// nolint: nestif
if policySet.Name == name {
d.Set("name", policySet.Name)
d.Set("description", policySet.Description)
d.Set("global", policySet.Global)
d.Set("policies_path", policySet.PoliciesPath)
if policySet.Organization != nil {
d.Set("organization", policySet.Organization.Name)
}
if policySet.Kind != "" {
d.Set("kind", policySet.Kind)
}
if policySet.Overridable != nil {
d.Set("overridable", policySet.Overridable)
}
var vcsRepo []interface{}
if policySet.VCSRepo != nil {
vcsRepo = append(vcsRepo, map[string]interface{}{
"identifier": policySet.VCSRepo.Identifier,
"branch": policySet.VCSRepo.Branch,
"ingress_submodules": policySet.VCSRepo.IngressSubmodules,
"oauth_token_id": policySet.VCSRepo.OAuthTokenID,
})
}
d.Set("vcs_repo", vcsRepo)
var policyIDs []interface{}
for _, policy := range policySet.Policies {
policyIDs = append(policyIDs, policy.ID)
}
d.Set("policy_ids", policyIDs)
var workspaceIDs []interface{}
if !policySet.Global {
for _, workspace := range policySet.Workspaces {
workspaceIDs = append(workspaceIDs, workspace.ID)
}
}
d.Set("workspace_ids", workspaceIDs)
d.SetId(policySet.ID)
return nil
}
}
// Exit the loop when we've seen all pages.
if policySetList.CurrentPage >= policySetList.TotalPages {
break
}
// Update the page number to get the next page.
listOptions.PageNumber = policySetList.NextPage
}
return fmt.Errorf("Could not find policy set %s/%s", organization, name)
}