Skip to content

Commit

Permalink
add plugin default function support
Browse files Browse the repository at this point in the history
  • Loading branch information
BBBmau committed Mar 27, 2024
1 parent dad8803 commit 4d25c84
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
6 changes: 2 additions & 4 deletions internal/framework/provider/corev1/generate_namespace.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
// autocrud_options {
// wait_for_deletion = true

// }
// autocrud_hooks{
// before_create = false
// after_create = false
// }
// }

// default_value_attributes = ["metadata.name"]


// ignored_attributes = [
// "api_version",
Expand Down
25 changes: 12 additions & 13 deletions internal/framework/provider/corev1/generate_secret.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,22 @@ computed_attributes = [
"type"
]

// default_values = {
// "id" = "testing"
// }
default_value_attributes = ["metadata.name", "immutable", "metadata.generation"]


generate {
schema = true
model = true
autocrud = true
autocrud_options {
hooks{
before{
create = true
}
after{

}
}
}
// autocrud_options {
// hooks{
// before{
// create = true
// }
// after{

// }
// }
// }
}
}
7 changes: 0 additions & 7 deletions internal/framework/provider/corev1/secret_crud_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ func (r *Secret) Create(ctx context.Context, req resource.CreateRequest, resp *r
return
}

r.BeforeCreate(ctx, req, resp, &dataModel)

err := autocrud.Create(ctx, r.clientGetter, r.APIVersion, r.Kind, &dataModel)
if err != nil {
resp.Diagnostics.AddError("Error creating resource", err.Error())
return
}

diags := resp.State.Set(ctx, &dataModel)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -41,7 +38,6 @@ func (r *Secret) Read(ctx context.Context, req resource.ReadRequest, resp *resou
resp.Diagnostics.AddError("Error reading resource", err.Error())
return
}

diags := resp.State.Set(ctx, &dataModel)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -63,7 +59,6 @@ func (r *Secret) Update(ctx context.Context, req resource.UpdateRequest, resp *r
resp.Diagnostics.AddError("Error updating resource", err.Error())
return
}

diags := resp.State.Set(ctx, &dataModel)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -73,13 +68,11 @@ func (r *Secret) Update(ctx context.Context, req resource.UpdateRequest, resp *r

func (r *Secret) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
waitForDeletion := false

err := autocrud.Delete(ctx, r.clientGetter, r.Kind, r.APIVersion, req, waitForDeletion)
if err != nil {
resp.Diagnostics.AddError("Error deleting resource", err.Error())
return
}

}

func (r *Secret) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
Expand Down
9 changes: 8 additions & 1 deletion internal/framework/provider/corev1/secret_schema_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/types"
)
Expand All @@ -17,7 +19,6 @@ func (r *Secret) Schema(ctx context.Context, req resource.SchemaRequest, resp *r
MarkdownDescription: `The unique ID for this terraform resource`,
Optional: true,
Computed: true,
Default: stringdefault.StaticString("str"),
},
"data": schema.MapAttribute{
MarkdownDescription: `Data contains the secret data. Each key must consist of alphanumeric characters, '-', '_' or '.'. The serialized form of the secret data is a base64 encoded string, representing the arbitrary (possibly non-string) data value here. Described in https://tools.ietf.org/html/rfc4648#section-4`,
Expand All @@ -27,6 +28,8 @@ func (r *Secret) Schema(ctx context.Context, req resource.SchemaRequest, resp *r
"immutable": schema.BoolAttribute{
MarkdownDescription: `Immutable, if set to true, ensures that data stored in the Secret cannot be updated (only object metadata can be modified). If not set to true, the field can be modified at any time. Defaulted to nil.`,
Optional: true,
Default: booldefault.StaticBool(false), //TODO change to default value

},
"metadata": schema.SingleNestedAttribute{
MarkdownDescription: `Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata`,
Expand All @@ -49,6 +52,8 @@ Applied only if Name is not specified. More info: https://git.k8s.io/community/c
MarkdownDescription: `A sequence number representing a specific generation of the desired state. Populated by the system. Read-only.`,
Optional: true,
Computed: true,
Default: int64default.StaticInt64(0), //TODO change to default value

},
"labels": schema.MapAttribute{
MarkdownDescription: `Map of string keys and values that can be used to organize and categorize (scope and select) objects. May match selectors of replication controllers and services. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels`,
Expand All @@ -59,6 +64,8 @@ Applied only if Name is not specified. More info: https://git.k8s.io/community/c
MarkdownDescription: `Name must be unique within a namespace. Is required when creating resources, although some resources may allow a client to request the generation of an appropriate name automatically. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names`,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""), // TODO: add default value

},
"namespace": schema.StringAttribute{
MarkdownDescription: `Namespace defines the space within which each name must be unique. An empty namespace is equivalent to the "default" namespace, but "default" is the canonical representation. Not all objects are required to be scoped to a namespace - the value of this field for those objects will be empty.
Expand Down
2 changes: 1 addition & 1 deletion internal/framework/provider/resources_list_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d25c84

Please sign in to comment.