Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

policies: data fields #31

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ go.work.sum
.env
.DS_Store
bin

.vscode
78 changes: 75 additions & 3 deletions internal/provider/policies_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

client "github.com/pomerium/enterprise-client-go"
"github.com/pomerium/enterprise-client-go/pb"
Expand All @@ -22,7 +25,13 @@ type PoliciesDataSource struct {
}

type PoliciesDataSourceModel struct {
Policies []PolicyModel `tfsdk:"policies"`
NamespaceID types.String `tfsdk:"namespace_id"`
Query types.String `tfsdk:"query"`
Offset types.Int64 `tfsdk:"offset"`
Limit types.Int64 `tfsdk:"limit"`
OrderBy types.String `tfsdk:"order_by"`
Policies []PolicyModel `tfsdk:"policies"`
TotalCount types.Int64 `tfsdk:"total_count"`
}

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

Attributes: map[string]schema.Attribute{
"namespace_id": schema.StringAttribute{
Optional: true,
Description: "Namespace to list policies in.",
},
"query": schema.StringAttribute{
Optional: true,
Description: "Query for policies.",
},
"offset": schema.Int64Attribute{
Optional: true,
Description: "List offset.",
},
"limit": schema.Int64Attribute{
Optional: true,
Description: "List limit.",
},
"order_by": schema.StringAttribute{
Optional: true,
Description: "List order by.",
Validators: []validator.String{
stringvalidator.OneOf("newest", "oldest", "name"),
},
},
"policies": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Expand All @@ -42,6 +74,10 @@ func (d *PoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Computed: true,
Description: "Unique identifier for the policy.",
},
"description": schema.StringAttribute{
Computed: true,
Description: "Description of the policy.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Name of the policy.",
Expand All @@ -53,10 +89,32 @@ func (d *PoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaReques
"ppl": schema.StringAttribute{
Computed: true,
Description: "Policy Policy Language (PPL) string.",
CustomType: PolicyLanguageType{},
},
"rego": schema.ListAttribute{
Computed: true,
Description: "Rego policies.",
ElementType: types.StringType,
},
"enforced": schema.BoolAttribute{
Computed: true,
Description: "Whether the policy is enforced within the namespace hierarchy.",
},
"explanation": schema.StringAttribute{
Computed: true,
Description: "Explanation of the policy.",
},
"remediation": schema.StringAttribute{
Computed: true,
Description: "Remediation of the policy.",
},
},
},
},
"total_count": schema.Int64Attribute{
Optional: true,
Description: "Total number of policies.",
},
},
}
}
Expand All @@ -78,10 +136,23 @@ func (d *PoliciesDataSource) Configure(_ context.Context, req datasource.Configu
d.client = client
}

func (d *PoliciesDataSource) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) {
func (d *PoliciesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data PoliciesDataSourceModel

policiesResp, err := d.client.PolicyService.ListPolicies(ctx, &pb.ListPoliciesRequest{})
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

listReq := &pb.ListPoliciesRequest{
Namespace: data.NamespaceID.ValueString(),
Query: data.Query.ValueStringPointer(),
Offset: data.Offset.ValueInt64Pointer(),
Limit: data.Limit.ValueInt64Pointer(),
OrderBy: data.OrderBy.ValueStringPointer(),
}

policiesResp, err := d.client.PolicyService.ListPolicies(ctx, listReq)
if err != nil {
resp.Diagnostics.AddError("Error reading policies", err.Error())
return
Expand All @@ -99,5 +170,6 @@ func (d *PoliciesDataSource) Read(ctx context.Context, _ datasource.ReadRequest,
}

data.Policies = policies
data.TotalCount = types.Int64Value(policiesResp.GetTotalCount())
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
29 changes: 27 additions & 2 deletions internal/provider/policy_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

client "github.com/pomerium/enterprise-client-go"
"github.com/pomerium/enterprise-client-go/pb"
Expand Down Expand Up @@ -33,6 +34,10 @@ func (d *PolicyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
Required: true,
Description: "Unique identifier for the policy.",
},
"description": schema.StringAttribute{
Computed: true,
Description: "Description of the policy.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Name of the policy.",
Expand All @@ -44,6 +49,24 @@ func (d *PolicyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
"ppl": schema.StringAttribute{
Computed: true,
Description: "Policy Policy Language (PPL) string.",
CustomType: PolicyLanguageType{},
},
"rego": schema.ListAttribute{
Computed: true,
Description: "Rego policies.",
ElementType: types.StringType,
},
"enforced": schema.BoolAttribute{
Computed: true,
Description: "Whether the policy is enforced within the namespace hierarchy.",
},
"explanation": schema.StringAttribute{
Computed: true,
Description: "Explanation of the policy.",
},
"remediation": schema.StringAttribute{
Computed: true,
Description: "Remediation of the policy.",
},
},
}
Expand Down Expand Up @@ -82,11 +105,13 @@ func (d *PolicyDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return
}

diags := ConvertPolicyFromPB(&data, policyResp.Policy)
var out PolicyModel

diags := ConvertPolicyFromPB(&out, policyResp.Policy)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &out)...)
}