Skip to content

Commit

Permalink
Merge pull request #44 from mittwald/bugfix/app-deps-databases-computed
Browse files Browse the repository at this point in the history
Databases + Dependencies of an app may be computed
  • Loading branch information
martin-helmich authored Apr 15, 2024
2 parents 8a82e45 + e014ecf commit b9b1a3b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
12 changes: 10 additions & 2 deletions docs/resources/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,16 @@ resource "mittwald_app" "custom_php" {

### Optional

- `databases` (Attributes Set) The databases the app uses (see [below for nested schema](#nestedatt--databases))
- `dependencies` (Attributes Map) The dependencies of the app (see [below for nested schema](#nestedatt--dependencies))
- `databases` (Attributes Set) The databases the app uses.

You can use this field to link databases to the app. The database resources must be created before the app resource, and the database resources must have the same project ID as the app resource.

This is only necessary if the specific app is not implicitly linked to a database by the backend. This is the case for apps like WordPress or TYPO3, which are always linked to a database. In these cases, you can (or should, even) omit the `databases` attribute. You can still retrieve the linked databases from the observed state, but you should not manage them manually. (see [below for nested schema](#nestedatt--databases))
- `dependencies` (Attributes Map) The dependencies of the app.

You can omit these to use the suggested dependencies for the app (in which case you can later select the dependencies from the resource state).

If you specify dependencies, you must specify the exact version of the dependency. To select a version using a semantic versioning constraint, use the `mittwald_systemsoftware` data source. (see [below for nested schema](#nestedatt--dependencies))
- `description` (String) The description of the app
- `document_root` (String) The document root of the app
- `user_inputs` (Map of String) The user inputs of the app
Expand Down
28 changes: 23 additions & 5 deletions internal/provider/resource/appresource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -68,8 +70,11 @@ func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *res
},
},
"databases": schema.SetNestedAttribute{
MarkdownDescription: "The databases the app uses",
Optional: true,
MarkdownDescription: "The databases the app uses.\n\n" +
" You can use this field to link databases to the app. The database resources must be created before the app resource, and the database resources must have the same project ID as the app resource.\n\n" +
" This is only necessary if the specific app is not implicitly linked to a database by the backend. This is the case for apps like WordPress or TYPO3, which are always linked to a database. In these cases, you can (or should, even) omit the `databases` attribute. You can still retrieve the linked databases from the observed state, but you should not manage them manually.",
Optional: true,
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Expand All @@ -90,6 +95,9 @@ func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *res
},
},
},
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
},
},
"app": schema.StringAttribute{
MarkdownDescription: "The name of the app",
Expand Down Expand Up @@ -134,8 +142,11 @@ func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *res
ElementType: types.StringType,
},
"dependencies": schema.MapNestedAttribute{
MarkdownDescription: "The dependencies of the app",
Optional: true,
MarkdownDescription: "The dependencies of the app.\n\n" +
" You can omit these to use the suggested dependencies for the app (in which case you can later select the dependencies from the resource state).\n\n" +
" If you specify dependencies, you must specify the exact version of the dependency. To select a version using a semantic versioning constraint, use the `mittwald_systemsoftware` data source.",
Optional: true,
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"version": schema.StringAttribute{
Expand All @@ -148,6 +159,9 @@ func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *res
},
},
},
PlanModifiers: []planmodifier.Map{
mapplanmodifier.UseStateForUnknown(),
},
},
},
}
Expand All @@ -162,7 +176,11 @@ func (r *Resource) Create(ctx context.Context, req resource.CreateRequest, resp
databases := make([]DatabaseModel, 0)

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
resp.Diagnostics.Append(data.Databases.ElementsAs(ctx, &databases, false)...)

// Databases may be unknown, in cases where linked database resources are determined by backend logic
if !data.Databases.IsUnknown() {
resp.Diagnostics.Append(data.Databases.ElementsAs(ctx, &databases, false)...)
}

appClient := r.client.App()
appInput, appUpdaters := data.ToCreateRequestWithUpdaters(ctx, resp.Diagnostics, appClient)
Expand Down

0 comments on commit b9b1a3b

Please sign in to comment.