Skip to content

Commit 6b25df9

Browse files
committed
(feat) add data source for all namespaces.
1 parent a2fba68 commit 6b25df9

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
11+
client "github.com/pomerium/enterprise-client-go"
12+
"github.com/pomerium/enterprise-client-go/pb"
13+
)
14+
15+
var _ datasource.DataSource = &NamespacesDataSource{}
16+
17+
func NewNamespacesDataSource() datasource.DataSource {
18+
return &NamespacesDataSource{}
19+
}
20+
21+
type NamespacesDataSource struct {
22+
client *client.Client
23+
}
24+
25+
type NamespaceModel struct {
26+
ID types.String `tfsdk:"id"`
27+
Name types.String `tfsdk:"name"`
28+
ParentID types.String `tfsdk:"parent_id"`
29+
}
30+
31+
type NamespacesDataSourceModel struct {
32+
Namespaces []NamespaceModel `tfsdk:"namespaces"`
33+
}
34+
35+
func (d *NamespacesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
36+
resp.TypeName = req.ProviderTypeName + "_namespaces"
37+
}
38+
39+
func (d *NamespacesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
40+
resp.Schema = schema.Schema{
41+
MarkdownDescription: "List all namespaces",
42+
43+
Attributes: map[string]schema.Attribute{
44+
"namespaces": schema.ListNestedAttribute{
45+
Computed: true,
46+
NestedObject: schema.NestedAttributeObject{
47+
Attributes: map[string]schema.Attribute{
48+
"id": schema.StringAttribute{
49+
Computed: true,
50+
Description: "Unique identifier for the namespace.",
51+
},
52+
"name": schema.StringAttribute{
53+
Computed: true,
54+
Description: "Name of the namespace.",
55+
},
56+
"parent_id": schema.StringAttribute{
57+
Computed: true,
58+
Description: "ID of the parent namespace.",
59+
},
60+
},
61+
},
62+
},
63+
},
64+
}
65+
}
66+
67+
func (d *NamespacesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
68+
if req.ProviderData == nil {
69+
return
70+
}
71+
72+
client, ok := req.ProviderData.(*client.Client)
73+
if !ok {
74+
resp.Diagnostics.AddError(
75+
"Unexpected Data Source Configure Type",
76+
fmt.Sprintf("Expected *client.Client, got: %T.", req.ProviderData),
77+
)
78+
return
79+
}
80+
81+
d.client = client
82+
}
83+
84+
func (d *NamespacesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
85+
var data NamespacesDataSourceModel
86+
87+
namespacesResp, err := d.client.NamespaceService.ListNamespaces(ctx, &pb.ListNamespacesRequest{})
88+
if err != nil {
89+
resp.Diagnostics.AddError("Error reading namespaces", err.Error())
90+
return
91+
}
92+
93+
namespaces := make([]NamespaceModel, 0, len(namespacesResp.Namespaces))
94+
for _, ns := range namespacesResp.Namespaces {
95+
var namespace NamespaceModel
96+
namespace.ID = types.StringValue(ns.Id)
97+
namespace.Name = types.StringValue(ns.Name)
98+
if ns.ParentId != "" {
99+
namespace.ParentID = types.StringValue(ns.ParentId)
100+
} else {
101+
namespace.ParentID = types.StringNull()
102+
}
103+
namespaces = append(namespaces, namespace)
104+
}
105+
106+
data.Namespaces = namespaces
107+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
108+
}

internal/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func (p *PomeriumProvider) DataSources(_ context.Context) []func() datasource.Da
132132
NewRouteDataSource,
133133
NewNamespaceDataSource,
134134
NewPolicyDataSource,
135+
NewNamespacesDataSource,
135136
}
136137
}
137138

0 commit comments

Comments
 (0)