@@ -4,8 +4,11 @@ import (
4
4
"context"
5
5
"fmt"
6
6
7
+ "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
7
8
"github.com/hashicorp/terraform-plugin-framework/datasource"
8
9
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10
+ "github.com/hashicorp/terraform-plugin-framework/schema/validator"
11
+ "github.com/hashicorp/terraform-plugin-framework/types"
9
12
10
13
client "github.com/pomerium/enterprise-client-go"
11
14
"github.com/pomerium/enterprise-client-go/pb"
@@ -22,7 +25,13 @@ type PoliciesDataSource struct {
22
25
}
23
26
24
27
type PoliciesDataSourceModel struct {
25
- Policies []PolicyModel `tfsdk:"policies"`
28
+ Namespace types.String `tfsdk:"namespace"`
29
+ Query types.String `tfsdk:"query"`
30
+ Offset types.Int64 `tfsdk:"offset"`
31
+ Limit types.Int64 `tfsdk:"limit"`
32
+ OrderBy types.String `tfsdk:"order_by"`
33
+ Policies []PolicyModel `tfsdk:"policies"`
34
+ TotalCount types.Int64 `tfsdk:"total_count"`
26
35
}
27
36
28
37
func (d * PoliciesDataSource ) Metadata (_ context.Context , req datasource.MetadataRequest , resp * datasource.MetadataResponse ) {
@@ -34,6 +43,29 @@ func (d *PoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaReques
34
43
MarkdownDescription : "List all policies" ,
35
44
36
45
Attributes : map [string ]schema.Attribute {
46
+ "namespace" : schema.StringAttribute {
47
+ Optional : true ,
48
+ Description : "Namespace to list policies in." ,
49
+ },
50
+ "query" : schema.StringAttribute {
51
+ Optional : true ,
52
+ Description : "Query for policies." ,
53
+ },
54
+ "offset" : schema.Int64Attribute {
55
+ Optional : true ,
56
+ Description : "List offset." ,
57
+ },
58
+ "limit" : schema.Int64Attribute {
59
+ Optional : true ,
60
+ Description : "List limit." ,
61
+ },
62
+ "order_by" : schema.StringAttribute {
63
+ Optional : true ,
64
+ Description : "List order by." ,
65
+ Validators : []validator.String {
66
+ stringvalidator .OneOf ("newest" , "oldest" , "name" ),
67
+ },
68
+ },
37
69
"policies" : schema.ListNestedAttribute {
38
70
Computed : true ,
39
71
NestedObject : schema.NestedAttributeObject {
@@ -42,6 +74,10 @@ func (d *PoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaReques
42
74
Computed : true ,
43
75
Description : "Unique identifier for the policy." ,
44
76
},
77
+ "description" : schema.StringAttribute {
78
+ Computed : true ,
79
+ Description : "Description of the policy." ,
80
+ },
45
81
"name" : schema.StringAttribute {
46
82
Computed : true ,
47
83
Description : "Name of the policy." ,
@@ -53,10 +89,32 @@ func (d *PoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaReques
53
89
"ppl" : schema.StringAttribute {
54
90
Computed : true ,
55
91
Description : "Policy Policy Language (PPL) string." ,
92
+ CustomType : PolicyLanguageType {},
93
+ },
94
+ "rego" : schema.ListAttribute {
95
+ Computed : true ,
96
+ Description : "Rego policies." ,
97
+ ElementType : types .StringType ,
98
+ },
99
+ "enforced" : schema.BoolAttribute {
100
+ Computed : true ,
101
+ Description : "Whether the policy is enforced within the namespace hierarchy." ,
102
+ },
103
+ "explanation" : schema.StringAttribute {
104
+ Computed : true ,
105
+ Description : "Explanation of the policy." ,
106
+ },
107
+ "remediation" : schema.StringAttribute {
108
+ Computed : true ,
109
+ Description : "Remediation of the policy." ,
56
110
},
57
111
},
58
112
},
59
113
},
114
+ "total_count" : schema.Int64Attribute {
115
+ Optional : true ,
116
+ Description : "Total number of policies." ,
117
+ },
60
118
},
61
119
}
62
120
}
@@ -78,10 +136,23 @@ func (d *PoliciesDataSource) Configure(_ context.Context, req datasource.Configu
78
136
d .client = client
79
137
}
80
138
81
- func (d * PoliciesDataSource ) Read (ctx context.Context , _ datasource.ReadRequest , resp * datasource.ReadResponse ) {
139
+ func (d * PoliciesDataSource ) Read (ctx context.Context , req datasource.ReadRequest , resp * datasource.ReadResponse ) {
82
140
var data PoliciesDataSourceModel
83
141
84
- policiesResp , err := d .client .PolicyService .ListPolicies (ctx , & pb.ListPoliciesRequest {})
142
+ resp .Diagnostics .Append (req .Config .Get (ctx , & data )... )
143
+ if resp .Diagnostics .HasError () {
144
+ return
145
+ }
146
+
147
+ listReq := & pb.ListPoliciesRequest {
148
+ Namespace : data .Namespace .ValueString (),
149
+ Query : data .Query .ValueStringPointer (),
150
+ Offset : data .Offset .ValueInt64Pointer (),
151
+ Limit : data .Limit .ValueInt64Pointer (),
152
+ OrderBy : data .OrderBy .ValueStringPointer (),
153
+ }
154
+
155
+ policiesResp , err := d .client .PolicyService .ListPolicies (ctx , listReq )
85
156
if err != nil {
86
157
resp .Diagnostics .AddError ("Error reading policies" , err .Error ())
87
158
return
@@ -99,5 +170,6 @@ func (d *PoliciesDataSource) Read(ctx context.Context, _ datasource.ReadRequest,
99
170
}
100
171
101
172
data .Policies = policies
173
+ data .TotalCount = types .Int64Value (policiesResp .GetTotalCount ())
102
174
resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
103
175
}
0 commit comments