Skip to content

Commit

Permalink
Changed resource structure to force use of ID for both applicationRef…
Browse files Browse the repository at this point in the history
… and definitionRef in API requests that the provider makes, optimized struct references
  • Loading branch information
HenryEstberg committed Apr 5, 2024
1 parent c0f76da commit 09868a3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 280 deletions.
36 changes: 4 additions & 32 deletions docs/data-sources/waypoint_add_on.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,19 @@ The Waypoint Add-on data source retrieves information on a given Add-on.

### Read-Only

- `application` (Attributes) The Application for which this Add-on was created. (see [below for nested schema](#nestedatt--application))
- `application_id` (String) The ID of the Application that this Add-on is created for.
- `created_by` (String) The user who created the Add-on.
- `definition` (Attributes) The Add-on Definition from which this Add-on was created. (see [below for nested schema](#nestedatt--definition))
- `definition_id` (String) The ID of the Add-on Definition that this Add-on is created from.
- `description` (String) A longer description of the Add-on.
- `install_count` (Number) The number of installed Add-ons for the same Application that share the same Add-on Definition.
- `labels` (List of String) List of labels attached to this Add-on.
- `output_values` (Attributes List) The output values of the Terraform run for the Add-on, sensitive values have type and value omitted. (see [below for nested schema](#nestedatt--output_values))
- `organization_id` (String) The ID of the HCP organization where the Waypoint AddOn is located.
- `project_id` (String) The ID of the HCP project where the Waypoint AddOn is located.
- `readme_markdown` (String) Instructions for using the Add-on (markdown format supported).
- `status` (Number) The status of the Terraform run for the Add-on.
- `summary` (String) A short summary of the Add-on.
- `terraform_no_code_module` (Attributes) Terraform Cloud no-code Module details. (see [below for nested schema](#nestedatt--terraform_no_code_module))

<a id="nestedatt--application"></a>
### Nested Schema for `application`

Read-Only:

- `id` (String) The ID of the Application.
- `name` (String) The name of the Application.


<a id="nestedatt--definition"></a>
### Nested Schema for `definition`

Read-Only:

- `id` (String) The ID of the Add-on Definition.
- `name` (String) The name of the Add-on Definition.


<a id="nestedatt--output_values"></a>
### Nested Schema for `output_values`

Read-Only:

- `name` (String) The name of the output value.
- `sensitive` (Boolean) Whether the output value is sensitive.
- `type` (String) The type of the output value.
- `value` (String) The value of the output value.


<a id="nestedatt--terraform_no_code_module"></a>
### Nested Schema for `terraform_no_code_module`

Expand Down
22 changes: 2 additions & 20 deletions docs/resources/waypoint_add_on.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Waypoint Add-on resource

### Required

- `application` (Attributes) The Application for which this Add-on was created. At least one of ID or Name must be set. (see [below for nested schema](#nestedatt--application))
- `definition` (Attributes) The Add-on Definition from which this Add-on was created. At least one of ID or Name must be set. (see [below for nested schema](#nestedatt--definition))
- `application_id` (String) The ID of the Application that this Add-on is created for.
- `definition_id` (String) The ID of the Add-on Definition that this Add-on is created from.
- `name` (String) The name of the Add-on.

### Optional
Expand All @@ -37,24 +37,6 @@ Waypoint Add-on resource
- `summary` (String) A short summary of the Add-on.
- `terraform_no_code_module` (Attributes) Terraform Cloud no-code Module details. (see [below for nested schema](#nestedatt--terraform_no_code_module))

<a id="nestedatt--application"></a>
### Nested Schema for `application`

Optional:

- `id` (String) The ID of the Application.
- `name` (String) The name of the Application.


<a id="nestedatt--definition"></a>
### Nested Schema for `definition`

Optional:

- `id` (String) The ID of the Add-on Definition.
- `name` (String) The name of the Add-on Definition.


<a id="nestedatt--terraform_no_code_module"></a>
### Nested Schema for `terraform_no_code_module`

Expand Down
126 changes: 35 additions & 91 deletions internal/provider/waypoint/data_source_waypoint_add_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,6 @@ type DataSourceAddOn struct {
client *clients.Client
}

type DataSourceAddOnModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Summary types.String `tfsdk:"summary"`
Labels types.List `tfsdk:"labels"`
Description types.String `tfsdk:"description"`
ReadmeMarkdown types.String `tfsdk:"readme_markdown"`
CreatedBy types.String `tfsdk:"created_by"`
Count types.Int64 `tfsdk:"install_count"`
Status types.Int64 `tfsdk:"status"`
OutputValues types.List `tfsdk:"output_values"`

Application *addOnApplicationRef `tfsdk:"application"`
Definition *addOnDefinitionRef `tfsdk:"definition"`
TerraformNoCodeModule *tfcNoCodeModule `tfsdk:"terraform_no_code_module"`
}

type addOnDefinitionRef struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}

type addOnApplicationRef struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}

func NewAddOnDataSource() datasource.DataSource {
return &DataSourceAddOn{}
}
Expand All @@ -85,6 +58,15 @@ func (d *DataSourceAddOn) Schema(ctx context.Context, req datasource.SchemaReque
Computed: true,
Optional: true,
},

"organization_id": schema.StringAttribute{
Description: "The ID of the HCP organization where the Waypoint AddOn is located.",
Computed: true,
},
"project_id": schema.StringAttribute{
Description: "The ID of the HCP project where the Waypoint AddOn is located.",
Computed: true,
},
"summary": schema.StringAttribute{
Description: "A short summary of the Add-on.",
Computed: true,
Expand All @@ -111,33 +93,13 @@ func (d *DataSourceAddOn) Schema(ctx context.Context, req datasource.SchemaReque
"Add-on Definition.",
Computed: true,
},
"definition": schema.SingleNestedAttribute{
"definition_id": schema.StringAttribute{
Computed: true,
Description: "The Add-on Definition from which this Add-on was created.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the Add-on Definition.",
},
"id": schema.StringAttribute{
Computed: true,
Description: "The ID of the Add-on Definition.",
},
},
Description: "The ID of the Add-on Definition that this Add-on is created from.",
},
"application": schema.SingleNestedAttribute{
"application_id": schema.StringAttribute{
Computed: true,
Description: "The Application for which this Add-on was created.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the Application.",
},
"id": schema.StringAttribute{
Computed: true,
Description: "The ID of the Application.",
},
},
Description: "The ID of the Application that this Add-on is created for.",
},
"terraform_no_code_module": &schema.SingleNestedAttribute{
Computed: true,
Expand All @@ -157,7 +119,7 @@ func (d *DataSourceAddOn) Schema(ctx context.Context, req datasource.SchemaReque
Computed: true,
Description: "The status of the Terraform run for the Add-on.",
},
"output_values": schema.ListNestedAttribute{
/*"output_values": schema.ListNestedAttribute{
Computed: true,
Description: "The output values of the Terraform run for the Add-on, sensitive values have type " +
"and value omitted.",
Expand All @@ -181,7 +143,7 @@ func (d *DataSourceAddOn) Schema(ctx context.Context, req datasource.SchemaReque
},
},
},
},
},*/
},
}
}
Expand All @@ -202,9 +164,9 @@ func (d *DataSourceAddOn) Configure(ctx context.Context, req datasource.Configur
d.client = client
}

// TODO: Convert count from uint64 to int64, add support for application, definition, created at, status, created by, (output values?)
// TODO: Output values?
func (d *DataSourceAddOn) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state DataSourceAddOnModel
var state AddOnResourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)

client := d.client
Expand Down Expand Up @@ -242,14 +204,6 @@ func (d *DataSourceAddOn) Read(ctx context.Context, req datasource.ReadRequest,

state.Summary = types.StringValue(addOn.Summary)

if addOn.TerraformNocodeModule != nil {
tfcNoCode := &tfcNoCodeModule{
Source: types.StringValue(addOn.TerraformNocodeModule.Source),
Version: types.StringValue(addOn.TerraformNocodeModule.Version),
}
state.TerraformNoCodeModule = tfcNoCode
}

labels, diags := types.ListValueFrom(ctx, types.StringType, addOn.Labels)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -260,6 +214,18 @@ func (d *DataSourceAddOn) Read(ctx context.Context, req datasource.ReadRequest,
}
state.Labels = labels

if addOn.TerraformNocodeModule != nil {
tfcNoCode := &tfcNoCodeModule{
Source: types.StringValue(addOn.TerraformNocodeModule.Source),
Version: types.StringValue(addOn.TerraformNocodeModule.Version),
}
state.TerraformNoCodeModule, diags = types.ObjectValueFrom(ctx, tfcNoCode.attrTypes(), tfcNoCode)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// set plan.description if it's not null or addOn.description is not empty
state.Description = types.StringValue(addOn.Description)
if addOn.Description == "" {
Expand Down Expand Up @@ -291,40 +257,18 @@ func (d *DataSourceAddOn) Read(ctx context.Context, req datasource.ReadRequest,
state.Count = types.Int64Value(installedCount)
}

// Display the reference to the Definition in the plan,
// prioritizing displaying Name because it is more user-friendly
// Display the reference to the Definition in the state
if addOn.Definition != nil {
definitionRef := &addOnDefinitionRef{}
if addOn.Definition.Name != "" {
definitionRef.Name = types.StringValue(addOn.Definition.Name)
} else if addOn.Definition.ID != "" {
definitionRef.ID = types.StringValue(addOn.Definition.ID)
} else {
// One of the above cases has to be true, this should not happen
resp.Diagnostics.AddError(
"error reading definition ref",
"the definition reference was missing, no ID or Name found")
return
if addOn.Definition.ID != "" {
state.DefinitionID = types.StringValue(addOn.Definition.ID)
}
state.Definition = definitionRef
}

// Display the reference to the Application in the plan,
// prioritizing displaying Name because it is more user-friendly
// Display the reference to the Application in the state
if addOn.Application != nil {
applicationRef := &addOnApplicationRef{}
if addOn.Application.Name != "" {
applicationRef.Name = types.StringValue(addOn.Application.Name)
} else if addOn.Application.ID != "" {
applicationRef.ID = types.StringValue(addOn.Application.ID)
} else {
// One of the above cases has to be true, this should not happen
resp.Diagnostics.AddError(
"error reading application ref",
"the application reference was missing, no ID or Name found")
return
if addOn.Application.ID != "" {
state.ApplicationID = types.StringValue(addOn.Application.ID)
}
state.Application = applicationRef
}

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down
Loading

0 comments on commit 09868a3

Please sign in to comment.