Skip to content

Commit a85a0e5

Browse files
authored
service-accounts: move jwt from shared model (#32)
For service account data sources, the `JWT` field only exists for resources not for the data model, so move it. Also add support for querying by namespace.
1 parent 5027f4d commit a85a0e5

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

internal/provider/models.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ type ServiceAccountModel struct {
1919
Description types.String `tfsdk:"description"`
2020
UserID types.String `tfsdk:"user_id"`
2121
ExpiresAt types.String `tfsdk:"expires_at"`
22-
JWT types.String `tfsdk:"jwt"`
2322
}
2423

2524
func ConvertServiceAccountToPB(_ context.Context, src *ServiceAccountResourceModel) (*pb.PomeriumServiceAccount, diag.Diagnostics) {
@@ -42,7 +41,7 @@ func ConvertServiceAccountToPB(_ context.Context, src *ServiceAccountResourceMod
4241
return pbServiceAccount, diags
4342
}
4443

45-
func ConvertServiceAccountFromPB(dst *ServiceAccountResourceModel, src *pb.PomeriumServiceAccount) diag.Diagnostics {
44+
func ConvertServiceAccountFromPB(dst *ServiceAccountModel, src *pb.PomeriumServiceAccount) diag.Diagnostics {
4645
var diagnostics diag.Diagnostics
4746

4847
dst.ID = types.StringValue(src.Id)

internal/provider/service_account.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ type ServiceAccountResource struct {
3131
client *client.Client
3232
}
3333

34-
type ServiceAccountResourceModel = ServiceAccountModel
34+
type ServiceAccountResourceModel struct {
35+
ServiceAccountModel
36+
JWT types.String `tfsdk:"jwt"`
37+
}
3538

3639
func (r *ServiceAccountResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
3740
resp.TypeName = req.ProviderTypeName + "_service_account"
@@ -127,7 +130,7 @@ func (r *ServiceAccountResource) Create(ctx context.Context, req resource.Create
127130
return
128131
}
129132

130-
diags = ConvertServiceAccountFromPB(&plan, respServiceAccount.ServiceAccount)
133+
diags = ConvertServiceAccountFromPB(&plan.ServiceAccountModel, respServiceAccount.ServiceAccount)
131134
resp.Diagnostics.Append(diags...)
132135
if resp.Diagnostics.HasError() {
133136
return
@@ -163,7 +166,7 @@ func (r *ServiceAccountResource) Read(ctx context.Context, req resource.ReadRequ
163166
return
164167
}
165168

166-
diags := ConvertServiceAccountFromPB(&state, respServiceAccount.ServiceAccount)
169+
diags := ConvertServiceAccountFromPB(&state.ServiceAccountModel, respServiceAccount.ServiceAccount)
167170
resp.Diagnostics.Append(diags...)
168171
if resp.Diagnostics.HasError() {
169172
return

internal/provider/service_accounts_data_source.go

+16-3
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"
@@ -22,6 +23,7 @@ type ServiceAccountsDataSource struct {
2223
}
2324

2425
type ServiceAccountsDataSourceModel struct {
26+
NamespaceID types.String `tfsdk:"namespace_id"`
2527
ServiceAccounts []ServiceAccountModel `tfsdk:"service_accounts"`
2628
}
2729

@@ -32,8 +34,11 @@ func (d *ServiceAccountsDataSource) Metadata(_ context.Context, req datasource.M
3234
func (d *ServiceAccountsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
3335
resp.Schema = schema.Schema{
3436
MarkdownDescription: "List all service accounts",
35-
3637
Attributes: map[string]schema.Attribute{
38+
"namespace_id": schema.StringAttribute{
39+
Optional: true,
40+
Description: "Namespace of the service accounts.",
41+
},
3742
"service_accounts": schema.ListNestedAttribute{
3843
Computed: true,
3944
NestedObject: schema.NestedAttributeObject{
@@ -86,10 +91,18 @@ func (d *ServiceAccountsDataSource) Configure(_ context.Context, req datasource.
8691
d.client = client
8792
}
8893

89-
func (d *ServiceAccountsDataSource) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) {
94+
func (d *ServiceAccountsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
9095
var data ServiceAccountsDataSourceModel
9196

92-
serviceAccountsResp, err := d.client.PomeriumServiceAccountService.ListPomeriumServiceAccounts(ctx, &pb.ListPomeriumServiceAccountsRequest{})
97+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
98+
if resp.Diagnostics.HasError() {
99+
return
100+
}
101+
102+
listReq := &pb.ListPomeriumServiceAccountsRequest{
103+
Namespace: data.NamespaceID.ValueString(),
104+
}
105+
serviceAccountsResp, err := d.client.PomeriumServiceAccountService.ListPomeriumServiceAccounts(ctx, listReq)
93106
if err != nil {
94107
resp.Diagnostics.AddError("Error reading service accounts", err.Error())
95108
return

0 commit comments

Comments
 (0)