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

[Draft] Add plugin framework conflics_with and at_least_one_of to instance #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
75 changes: 51 additions & 24 deletions internal/instance/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -116,6 +117,21 @@ func (r InstanceResource) Schema(_ context.Context, _ resource.SchemaRequest, re
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the formatting with gofmt. In Go indentation is done with tab (and not spaces).

stringvalidator.ConflictsWith(
path.Expressions{
path.MatchRoot("source_instance"),
path.MatchRoot("source_file"),
}...
),
stringvalidator.AtLeastOneOf(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation enforces atMostOneOf, which is not the same as AtLeastOneOf.

path.Expressions{
path.MatchRoot("image"),
path.MatchRoot("source_instance"),
path.MatchRoot("source_file"),
}...
),
},
},

"ephemeral": schema.BoolAttribute{
Expand Down Expand Up @@ -205,16 +221,49 @@ func (r InstanceResource) Schema(_ context.Context, _ resource.SchemaRequest, re
PlanModifiers: []planmodifier.Object{
objectplanmodifier.RequiresReplace(),
},
Validators: []validator.Object{
objectvalidator.ConflictsWith(
path.Expressions{
path.MatchRoot("image"),
path.MatchRoot("source_file"),
}...),
objectvalidator.AtLeastOneOf(
path.Expressions{
path.MatchRoot("image"),
path.MatchRoot("source_instance"),
path.MatchRoot("source_file"),
}...
),
},
},

"source_file": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
stringvalidator.ConflictsWith(
path.Expressions{
path.MatchRoot("image"),
path.MatchRoot("source_instance"),
path.MatchRoot("description"),
path.MatchRoot("type"),
path.MatchRoot("ephemeral"),
path.MatchRoot("profiles"),
path.MatchRoot("file"),
path.MatchRoot("config"),
}...
),
stringvalidator.AtLeastOneOf(
path.Expressions{
path.MatchRoot("image"),
path.MatchRoot("source_instance"),
path.MatchRoot("source_file"),
}...
),
},
},

// Computed.
Expand Down Expand Up @@ -383,29 +432,7 @@ func (r InstanceResource) ValidateConfig(ctx context.Context, req resource.Valid
)
}

if !atMostOneOf(config.Image, config.SourceFile, config.SourceInstance) {
resp.Diagnostics.AddError(
"Invalid Configuration",
"At most one of image, source_file and source_instance can be set.",
)
return
}

if !config.SourceFile.IsNull() {
// Instances from source_file are mutually exclusive with a series of other attributes.
if !config.Description.IsNull() ||
!config.Type.IsNull() ||
!config.Ephemeral.IsNull() ||
!config.Profiles.IsNull() ||
!config.Files.IsNull() ||
!config.Config.IsNull() {
resp.Diagnostics.AddError(
"Invalid Configuration",
"Attribute source_file is mutually exclusive with description, type, ephemeral, profiles, file and config.",
)
return
}

// With `incus import`, a storage pool can be provided optionally.
// In order to support the same behavior with source_file,
// a single device entry of type `disk` is allowed with exactly two properties
Expand Down
Loading