-
Notifications
You must be signed in to change notification settings - Fork 979
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate boilerplate code for apps/v1 and batch/v1 resources (#2610)
- Loading branch information
Showing
26 changed files
with
36,409 additions
and
1 deletion.
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
internal/framework/provider/appsv1/daemon_set_crud_gen.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package appsv1 | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-codegen-kubernetes/autocrud" | ||
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func (r *DaemonSet) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var dataModel DaemonSetModel | ||
|
||
diag := req.Config.Get(ctx, &dataModel) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
|
||
defaultTimeout, err := time.ParseDuration("20m") | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error parsing timeout", err.Error()) | ||
return | ||
} | ||
timeout, diag := dataModel.Timeouts.Create(ctx, defaultTimeout) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
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() { | ||
return | ||
} | ||
} | ||
|
||
func (r *DaemonSet) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var dataModel DaemonSetModel | ||
|
||
diag := req.State.Get(ctx, &dataModel) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
|
||
defaultTimeout, err := time.ParseDuration("20m") | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error parsing timeout", err.Error()) | ||
return | ||
} | ||
timeout, diag := dataModel.Timeouts.Read(ctx, defaultTimeout) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
var id string | ||
req.State.GetAttribute(ctx, path.Root("id"), &id) | ||
err = autocrud.Read(ctx, r.clientGetter, r.Kind, r.APIVersion, id, &dataModel) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error reading resource", err.Error()) | ||
return | ||
} | ||
|
||
diags := resp.State.Set(ctx, &dataModel) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *DaemonSet) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var dataModel DaemonSetModel | ||
|
||
diag := req.Config.Get(ctx, &dataModel) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
|
||
defaultTimeout, err := time.ParseDuration("20m") | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error parsing timeout", err.Error()) | ||
return | ||
} | ||
timeout, diag := dataModel.Timeouts.Update(ctx, defaultTimeout) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
err = autocrud.Update(ctx, r.clientGetter, r.Kind, r.APIVersion, &dataModel) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error updating resource", err.Error()) | ||
return | ||
} | ||
|
||
diags := resp.State.Set(ctx, &dataModel) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *DaemonSet) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
waitForDeletion := true | ||
|
||
var dataModel DaemonSetModel | ||
|
||
diag := req.State.Get(ctx, &dataModel) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
|
||
defaultTimeout, err := time.ParseDuration("20m") | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error parsing timeout", err.Error()) | ||
return | ||
} | ||
timeout, diag := dataModel.Timeouts.Delete(ctx, defaultTimeout) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
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 *DaemonSet) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
var dataModel DaemonSetModel | ||
|
||
err := autocrud.Read(ctx, r.clientGetter, r.Kind, r.APIVersion, req.ID, &dataModel) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error importing resource", err.Error()) | ||
return | ||
} | ||
|
||
// awkward timeouts/types.Object issue https://github.com/hashicorp/terraform-plugin-framework-timeouts/issues/46 & https://github.com/hashicorp/terraform-plugin-framework/issues/716 | ||
dataModel.Timeouts = timeouts.Value{ | ||
Object: types.ObjectNull(map[string]attr.Type{ | ||
"create": types.StringType, | ||
"delete": types.StringType, | ||
"read": types.StringType, | ||
"update": types.StringType, | ||
}), | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &dataModel)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package appsv1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-provider-kubernetes/internal/framework/provider/client" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ resource.Resource = &DaemonSet{} | ||
var _ resource.ResourceWithImportState = &DaemonSet{} | ||
|
||
func NewDaemonSet() resource.Resource { | ||
return &DaemonSet{ | ||
Kind: "DaemonSet", | ||
APIVersion: "apps/v1", | ||
} | ||
} | ||
|
||
type DaemonSet struct { | ||
APIVersion string | ||
Kind string | ||
|
||
clientGetter client.KubernetesClientGetter | ||
} | ||
|
||
func (r *DaemonSet) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = "kubernetes_daemon_set_v1_gen" | ||
} | ||
|
||
func (r *DaemonSet) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
clientGetter, ok := req.ProviderData.(client.KubernetesClientGetter) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Resource Configure Type", | ||
fmt.Sprintf("Expected KubernetesClientGetter, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
return | ||
} | ||
|
||
r.clientGetter = clientGetter | ||
} |
Oops, something went wrong.