From 185f8a27c1662d306802dd87ea6b0d167a6f8c57 Mon Sep 17 00:00:00 2001 From: Adrian Lai Date: Wed, 30 Apr 2025 11:09:05 +0100 Subject: [PATCH] Add CA Account ID to output for product option data source --- docs/data-sources/ca_product.md | 1 + internal/provider/ca_product_data_source.go | 8 +++++++- internal/tlspc/tlspc.go | 12 ++++++------ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/docs/data-sources/ca_product.md b/docs/data-sources/ca_product.md index e5ce5db..13b8fed 100644 --- a/docs/data-sources/ca_product.md +++ b/docs/data-sources/ca_product.md @@ -41,4 +41,5 @@ data "tlspc_ca_product" "built_in" { ### Read-Only +- `account_id` (String) The ID of the CA Account - `id` (String) The ID of this resource. diff --git a/internal/provider/ca_product_data_source.go b/internal/provider/ca_product_data_source.go index fc386d4..22858b7 100644 --- a/internal/provider/ca_product_data_source.go +++ b/internal/provider/ca_product_data_source.go @@ -64,6 +64,10 @@ func (d *caProductDataSource) Schema(_ context.Context, _ datasource.SchemaReque "id": schema.StringAttribute{ Computed: true, }, + "account_id": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "The ID of the CA Account", + }, "type": schema.StringAttribute{ Required: true, MarkdownDescription: `Type of Certificate Authority, valid values include: @@ -92,6 +96,7 @@ func (d *caProductDataSource) Schema(_ context.Context, _ datasource.SchemaReque type caProductDataSourceModel struct { ID types.String `tfsdk:"id"` + AccountID types.String `tfsdk:"account_id"` Type types.String `tfsdk:"type"` CAName types.String `tfsdk:"ca_name"` ProductOption types.String `tfsdk:"product_option"` @@ -106,7 +111,7 @@ func (d *caProductDataSource) Read(ctx context.Context, req datasource.ReadReque return } - caProduct, err := d.client.GetCAProductOption(model.Type.ValueString(), model.CAName.ValueString(), model.ProductOption.ValueString()) + caProduct, caAcct, err := d.client.GetCAProductOption(model.Type.ValueString(), model.CAName.ValueString(), model.ProductOption.ValueString()) if err != nil { resp.Diagnostics.AddError( "Error retrieving CA Product", @@ -115,6 +120,7 @@ func (d *caProductDataSource) Read(ctx context.Context, req datasource.ReadReque return } model.ID = types.StringValue(caProduct.ID) + model.AccountID = types.StringValue(caAcct.ID) diags = resp.State.Set(ctx, &model) resp.Diagnostics.Append(diags...) } diff --git a/internal/tlspc/tlspc.go b/internal/tlspc/tlspc.go index dc2fb6d..af1511b 100644 --- a/internal/tlspc/tlspc.go +++ b/internal/tlspc/tlspc.go @@ -544,21 +544,21 @@ type caAccount struct { ProductOptions []CAProductOption `json:"productOptions"` } -func (c *Client) GetCAProductOption(kind, name, option string) (*CAProductOption, error) { +func (c *Client) GetCAProductOption(kind, name, option string) (*CAProductOption, *CAAccount, error) { path := c.Path(`%s/v1/certificateauthorities/` + kind + "/accounts") resp, err := c.Get(path) if err != nil { - return nil, fmt.Errorf("Error getting ca product: %s", err) + return nil, nil, fmt.Errorf("Error getting ca product: %s", err) } body, err := io.ReadAll(resp.Body) if err != nil { - return nil, fmt.Errorf("Error reading response body: %s", err) + return nil, nil, fmt.Errorf("Error reading response body: %s", err) } var accounts caAccounts err = json.Unmarshal(body, &accounts) if err != nil { - return nil, fmt.Errorf("Error decoding response: %s", string(body)) + return nil, nil, fmt.Errorf("Error decoding response: %s", string(body)) } for _, acc := range accounts.Accounts { acct := acc.Account @@ -567,12 +567,12 @@ func (c *Client) GetCAProductOption(kind, name, option string) (*CAProductOption } for _, opt := range acc.ProductOptions { if opt.Name == option { - return &opt, nil + return &opt, &acct, nil } } } - return nil, fmt.Errorf("Specified CA product option not found.") + return nil, nil, fmt.Errorf("Specified CA product option not found.") } func (c *Client) GetCAProductOptionByID(kind, option_id string) (*CAProductOption, error) {