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

fix(accounts): fix the account domains payload #393

Merged
merged 2 commits into from
Feb 28, 2025
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
10 changes: 9 additions & 1 deletion internal/api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// AccountsClient is a client for working with accounts.
type AccountsClient interface {
Get(ctx context.Context) (*Account, error)
GetDomains(ctx context.Context) (*AccountDomainsUpdate, error)
GetDomains(ctx context.Context) ([]*AccountDomain, error)
Update(ctx context.Context, data AccountUpdate) error
UpdateSettings(ctx context.Context, data AccountSettingsUpdate) error
UpdateDomains(ctx context.Context, data AccountDomainsUpdate) error
Expand Down Expand Up @@ -54,6 +54,14 @@ type AccountSettingsUpdate struct {
AccountSettings `json:"settings"`
}

// AccountDomain is the data retrieved when getting an account's domain names.
type AccountDomain struct {
Name string `json:"name"`

// The fields below are present in the response but are currently ignored.
// id, created, updated, account_id
}

// AccountDomainsUpdate is the data sent when updating an account's domain names.
type AccountDomainsUpdate struct {
DomainNames []string `json:"domain_names,omitempty"`
Expand Down
12 changes: 6 additions & 6 deletions internal/client/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *AccountsClient) Get(ctx context.Context) (*api.Account, error) {
}

// GetDomains returns domain names for an account by ID.
func (c *AccountsClient) GetDomains(ctx context.Context) (*api.AccountDomainsUpdate, error) {
func (c *AccountsClient) GetDomains(ctx context.Context) ([]*api.AccountDomain, error) {
cfg := requestConfig{
method: http.MethodGet,
url: c.routePrefix + "domains",
Expand All @@ -69,12 +69,12 @@ func (c *AccountsClient) GetDomains(ctx context.Context) (*api.AccountDomainsUpd
successCodes: successCodesStatusOK,
}

var accountDomains api.AccountDomainsUpdate
if err := requestWithDecodeResponse(ctx, c.hc, cfg, &accountDomains.DomainNames); err != nil {
return nil, fmt.Errorf("failed to get account domains: %w", err)
var accountDomains []*api.AccountDomain
if err := requestWithDecodeResponse(ctx, c.hc, cfg, &accountDomains); err != nil {
return nil, fmt.Errorf("hey failed to get account domains: %w", err)
}

return &accountDomains, nil
return accountDomains, nil
}

// Update modifies an existing account by ID.
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c *AccountsClient) UpdateDomains(ctx context.Context, data api.AccountDoma
cfg := requestConfig{
method: http.MethodPatch,
url: c.routePrefix + "domains",
body: data.DomainNames,
body: data,
apiKey: c.apiKey,
basicAuthKey: c.basicAuthKey,
successCodes: successCodesStatusNoContent,
Expand Down
21 changes: 14 additions & 7 deletions internal/provider/datasources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,6 @@ func (d *AccountDataSource) Read(ctx context.Context, req datasource.ReadRequest
return
}

accountDomains, err := client.GetDomains(ctx)
if err != nil {
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account domains", "get", err))

return
}
model.ID = customtypes.NewUUIDValue(account.ID)
model.Created = customtypes.NewTimestampPointerValue(account.Created)
model.Updated = customtypes.NewTimestampPointerValue(account.Updated)
Expand All @@ -194,7 +188,20 @@ func (d *AccountDataSource) Read(ctx context.Context, req datasource.ReadRequest

model.Settings = settingsObject

domainNames, diags := types.ListValueFrom(ctx, types.StringType, accountDomains.DomainNames)
domains, err := client.GetDomains(ctx)
if err != nil {
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account domains", "get", err))

return
}

// Convert the list of AccountDomain to a list of names as strings.
names := make([]string, 0, len(domains))
for _, name := range domains {
names = append(names, name.Name)
}

domainNames, diags := types.ListValueFrom(ctx, types.StringType, names)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/datasources/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func TestAccDatasource_account(t *testing.T) {
testutils.ExpectKnownValue(datasourceName, "id", os.Getenv("PREFECT_CLOUD_ACCOUNT_ID")),
testutils.ExpectKnownValueNotNull(datasourceName, "name"),
testutils.ExpectKnownValueNotNull(datasourceName, "handle"),

// These domain names were manually added to the account, because we're using a pre-existing account
// due to the fact that accounts cannot be created with the API/Terraform.
testutils.ExpectKnownValueList(datasourceName, "domain_names", []string{"example.com", "foobar.com"}),
},
},
},
Expand Down
10 changes: 8 additions & 2 deletions internal/provider/resources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,14 @@ func copyAccountToModel(_ context.Context, account *api.Account, tfModel *Accoun

// copyAccountDomainsToModel maps an API response to a model that is saved in Terraform state.
// A model can be a Terraform Plan, State, or Config object.
func copyAccountDomainsToModel(ctx context.Context, accountDomains *api.AccountDomainsUpdate, tfModel *AccountResourceModel) diag.Diagnostics {
domainNames, diags := types.ListValueFrom(ctx, types.StringType, accountDomains.DomainNames)
func copyAccountDomainsToModel(ctx context.Context, accountDomains []*api.AccountDomain, tfModel *AccountResourceModel) diag.Diagnostics {
// Convert the list of AccountDomain to a list of names as strings.
names := make([]string, 0, len(accountDomains))
for _, name := range accountDomains {
names = append(names, name.Name)
}

domainNames, diags := types.ListValueFrom(ctx, types.StringType, names)
if diags.HasError() {
return diags
}
Expand Down
Loading