Skip to content

Add CA Account ID to output for product option data source #64

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/data-sources/ca_product.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 7 additions & 1 deletion internal/provider/ca_product_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"`
Expand All @@ -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",
Expand All @@ -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...)
}
12 changes: 6 additions & 6 deletions internal/tlspc/tlspc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Loading