Skip to content

Commit fdbacb7

Browse files
authored
policies: data fields (#31)
Add additional policy fields for data sources.
1 parent 842803d commit fdbacb7

File tree

3 files changed

+104
-5
lines changed

3 files changed

+104
-5
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ go.work.sum
2525
.env
2626
.DS_Store
2727
bin
28+
29+
.vscode

internal/provider/policies_data_source.go

+75-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
78
"github.com/hashicorp/terraform-plugin-framework/datasource"
89
"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"
912

1013
client "github.com/pomerium/enterprise-client-go"
1114
"github.com/pomerium/enterprise-client-go/pb"
@@ -22,7 +25,13 @@ type PoliciesDataSource struct {
2225
}
2326

2427
type PoliciesDataSourceModel struct {
25-
Policies []PolicyModel `tfsdk:"policies"`
28+
NamespaceID types.String `tfsdk:"namespace_id"`
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"`
2635
}
2736

2837
func (d *PoliciesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
@@ -34,6 +43,29 @@ func (d *PoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaReques
3443
MarkdownDescription: "List all policies",
3544

3645
Attributes: map[string]schema.Attribute{
46+
"namespace_id": 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+
},
3769
"policies": schema.ListNestedAttribute{
3870
Computed: true,
3971
NestedObject: schema.NestedAttributeObject{
@@ -42,6 +74,10 @@ func (d *PoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaReques
4274
Computed: true,
4375
Description: "Unique identifier for the policy.",
4476
},
77+
"description": schema.StringAttribute{
78+
Computed: true,
79+
Description: "Description of the policy.",
80+
},
4581
"name": schema.StringAttribute{
4682
Computed: true,
4783
Description: "Name of the policy.",
@@ -53,10 +89,32 @@ func (d *PoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaReques
5389
"ppl": schema.StringAttribute{
5490
Computed: true,
5591
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.",
56110
},
57111
},
58112
},
59113
},
114+
"total_count": schema.Int64Attribute{
115+
Optional: true,
116+
Description: "Total number of policies.",
117+
},
60118
},
61119
}
62120
}
@@ -78,10 +136,23 @@ func (d *PoliciesDataSource) Configure(_ context.Context, req datasource.Configu
78136
d.client = client
79137
}
80138

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) {
82140
var data PoliciesDataSourceModel
83141

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.NamespaceID.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)
85156
if err != nil {
86157
resp.Diagnostics.AddError("Error reading policies", err.Error())
87158
return
@@ -99,5 +170,6 @@ func (d *PoliciesDataSource) Read(ctx context.Context, _ datasource.ReadRequest,
99170
}
100171

101172
data.Policies = policies
173+
data.TotalCount = types.Int64Value(policiesResp.GetTotalCount())
102174
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
103175
}

internal/provider/policy_data_source.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-framework/datasource"
88
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
910

1011
client "github.com/pomerium/enterprise-client-go"
1112
"github.com/pomerium/enterprise-client-go/pb"
@@ -33,6 +34,10 @@ func (d *PolicyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
3334
Required: true,
3435
Description: "Unique identifier for the policy.",
3536
},
37+
"description": schema.StringAttribute{
38+
Computed: true,
39+
Description: "Description of the policy.",
40+
},
3641
"name": schema.StringAttribute{
3742
Computed: true,
3843
Description: "Name of the policy.",
@@ -44,6 +49,24 @@ func (d *PolicyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
4449
"ppl": schema.StringAttribute{
4550
Computed: true,
4651
Description: "Policy Policy Language (PPL) string.",
52+
CustomType: PolicyLanguageType{},
53+
},
54+
"rego": schema.ListAttribute{
55+
Computed: true,
56+
Description: "Rego policies.",
57+
ElementType: types.StringType,
58+
},
59+
"enforced": schema.BoolAttribute{
60+
Computed: true,
61+
Description: "Whether the policy is enforced within the namespace hierarchy.",
62+
},
63+
"explanation": schema.StringAttribute{
64+
Computed: true,
65+
Description: "Explanation of the policy.",
66+
},
67+
"remediation": schema.StringAttribute{
68+
Computed: true,
69+
Description: "Remediation of the policy.",
4770
},
4871
},
4972
}
@@ -82,11 +105,13 @@ func (d *PolicyDataSource) Read(ctx context.Context, req datasource.ReadRequest,
82105
return
83106
}
84107

85-
diags := ConvertPolicyFromPB(&data, policyResp.Policy)
108+
var out PolicyModel
109+
110+
diags := ConvertPolicyFromPB(&out, policyResp.Policy)
86111
resp.Diagnostics.Append(diags...)
87112
if resp.Diagnostics.HasError() {
88113
return
89114
}
90115

91-
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
116+
resp.Diagnostics.Append(resp.State.Set(ctx, &out)...)
92117
}

0 commit comments

Comments
 (0)