Skip to content

Commit

Permalink
add more fields to the policy resource (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
wasaga authored Jan 2, 2025
1 parent 6ac235f commit 1f9ae99
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ resource "pomerium_settings" "settings" {
resource "pomerium_policy" "test_policy" {
name = "test-policy"
namespace_id = pomerium_namespace.test_namespace.id
description = "test policy"
enforced = false
explanation = "test policy explanation"
remediation = "test policy remediation"
ppl = <<EOF
- allow:
and:
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (r *PolicyResource) Schema(_ context.Context, _ resource.SchemaRequest, res
stringplanmodifier.UseStateForUnknown(),
},
},
"description": schema.StringAttribute{
Description: "Description of the policy.",
Optional: true,
},
"name": schema.StringAttribute{
Description: "Name of the policy.",
Required: true,
Expand All @@ -64,6 +68,23 @@ func (r *PolicyResource) Schema(_ context.Context, _ resource.SchemaRequest, res
Required: true,
CustomType: PolicyLanguageType{},
},
"rego": schema.ListAttribute{
Description: "Rego policies.",
Optional: true,
ElementType: types.StringType,
},
"enforced": schema.BoolAttribute{
Description: "Whether the policy is enforced within the namespace hierarchy.",
Optional: true,
},
"explanation": schema.StringAttribute{
Description: "Explanation of the policy.",
Optional: true,
},
"remediation": schema.StringAttribute{
Description: "Remediation of the policy.",
Optional: true,
},
},
}
}
Expand Down
17 changes: 16 additions & 1 deletion internal/provider/policy_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ import (
type PolicyModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
NamespaceID types.String `tfsdk:"namespace_id"`
PPL PolicyLanguage `tfsdk:"ppl"`
Rego types.List `tfsdk:"rego"`
Enforced types.Bool `tfsdk:"enforced"`
Explanation types.String `tfsdk:"explanation"`
Remediation types.String `tfsdk:"remediation"`
}

func ConvertPolicyToPB(_ context.Context, src *PolicyResourceModel) (*pb.Policy, diag.Diagnostics) {
func ConvertPolicyToPB(ctx context.Context, src *PolicyResourceModel) (*pb.Policy, diag.Diagnostics) {
var diagnostics diag.Diagnostics

pbPolicy := &pb.Policy{
Id: src.ID.ValueString(),
Name: src.Name.ValueString(),
Description: src.Description.ValueString(),
NamespaceId: src.NamespaceID.ValueString(),
Ppl: string(src.PPL.PolicyJSON),
Enforced: src.Enforced.ValueBool(),
Explanation: src.Explanation.ValueString(),
Remediation: src.Remediation.ValueString(),
}
diagnostics.Append(src.Rego.ElementsAs(ctx, &pbPolicy.Rego, false)...)

return pbPolicy, diagnostics
}
Expand All @@ -34,7 +44,12 @@ func ConvertPolicyFromPB(dst *PolicyResourceModel, src *pb.Policy) diag.Diagnost

dst.ID = types.StringValue(src.Id)
dst.Name = types.StringValue(src.Name)
dst.Description = types.StringValue(src.Description)
dst.NamespaceID = types.StringValue(src.NamespaceId)
dst.Enforced = types.BoolValue(src.Enforced)
dst.Explanation = types.StringValue(src.Explanation)
dst.Remediation = types.StringValue(src.Remediation)
dst.Rego = FromStringSlice(src.Rego)
ppl, err := PolicyLanguageType{}.Parse(types.StringValue(src.Ppl))
if err != nil {
diagnostics.AddError("converting PPL", err.Error())
Expand Down

0 comments on commit 1f9ae99

Please sign in to comment.