-
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.
- Loading branch information
Showing
7 changed files
with
408 additions
and
2 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
internal/framework/provider/rbacv1/generate_role_binding.hcl
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,50 @@ | ||
resource "kubernetes_role_binding_v1_gen" { | ||
package = "rbacv1" | ||
|
||
api_version = "rbac.authorization.k8s.io/v1" | ||
kind = "RoleBinding" | ||
|
||
description = "rolebindings are used to grant permissions at the namespace level" | ||
|
||
output_filename_prefix = "role_binding" | ||
|
||
openapi { | ||
filename = "./codegen/data/kubernetes-v1.28.3/api/openapi-spec/v3/apis__rbac.authorization.k8s.io__v1_openapi.json" | ||
create_path = "/apis/rbac.authorization.k8s.io/v1/clusterrolebindings" | ||
read_path = "/apis/rbac.authorization.k8s.io/v1/clusterrolebindings/{name}" | ||
} | ||
|
||
generate { | ||
schema = true | ||
model = true | ||
autocrud = true | ||
} | ||
|
||
ignored_attributes = [ | ||
"api_version", | ||
"kind", | ||
"metadata.namespace", | ||
"metadata.finalizers", | ||
"metadata.managed_fields", | ||
"metadata.owner_references", | ||
"metadata.self_link", | ||
"metadata.creation_timestamp", | ||
"metadata.deletion_timestamp", | ||
"metadata.deletion_grace_period_seconds", | ||
] | ||
|
||
required_attributes = [ | ||
"metadata" | ||
] | ||
|
||
computed_attributes = [ | ||
"metadata.uid", | ||
"metadata.resource_version", | ||
"metadata.generation", | ||
"metadata.name", | ||
] | ||
|
||
immutable_attributes = [ | ||
"metadata.name" | ||
] | ||
} |
152 changes: 152 additions & 0 deletions
152
internal/framework/provider/rbacv1/role_binding_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,152 @@ | ||
package rbacv1 | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
|
||
"github.com/hashicorp/terraform-plugin-codegen-kubernetes/autocrud" | ||
) | ||
|
||
func (r *RoleBinding) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var dataModel RoleBindingModel | ||
|
||
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 *RoleBinding) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var dataModel RoleBindingModel | ||
|
||
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() | ||
|
||
err = autocrud.Read(ctx, r.clientGetter, r.Kind, r.APIVersion, req, &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 *RoleBinding) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var dataModel RoleBindingModel | ||
|
||
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 *RoleBinding) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
waitForDeletion := false | ||
|
||
var dataModel RoleBindingModel | ||
|
||
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 *RoleBinding) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) | ||
} |
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 rbacv1 | ||
|
||
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 = &RoleBinding{} | ||
var _ resource.ResourceWithImportState = &RoleBinding{} | ||
|
||
func NewRoleBinding() resource.Resource { | ||
return &RoleBinding{ | ||
Kind: "RoleBinding", | ||
APIVersion: "rbac.authorization.k8s.io/v1", | ||
} | ||
} | ||
|
||
type RoleBinding struct { | ||
APIVersion string | ||
Kind string | ||
|
||
clientGetter client.KubernetesClientGetter | ||
} | ||
|
||
func (r *RoleBinding) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = "kubernetes_role_binding_v1_gen" | ||
} | ||
|
||
func (r *RoleBinding) 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 | ||
} |
32 changes: 32 additions & 0 deletions
32
internal/framework/provider/rbacv1/role_binding_model_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,32 @@ | ||
package rbacv1 | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type RoleBindingModel struct { | ||
Timeouts timeouts.Value `tfsdk:"timeouts"` | ||
|
||
ID types.String `tfsdk:"id" manifest:""` | ||
Metadata struct { | ||
Annotations map[string]types.String `tfsdk:"annotations" manifest:"annotations"` | ||
GenerateName types.String `tfsdk:"generate_name" manifest:"generateName"` | ||
Generation types.Int64 `tfsdk:"generation" manifest:"generation"` | ||
Labels map[string]types.String `tfsdk:"labels" manifest:"labels"` | ||
Name types.String `tfsdk:"name" manifest:"name"` | ||
ResourceVersion types.String `tfsdk:"resource_version" manifest:"resourceVersion"` | ||
UID types.String `tfsdk:"uid" manifest:"uid"` | ||
} `tfsdk:"metadata" manifest:"metadata"` | ||
RoleRef struct { | ||
ApiGroup types.String `tfsdk:"api_group" manifest:"apiGroup"` | ||
Kind types.String `tfsdk:"kind" manifest:"kind"` | ||
Name types.String `tfsdk:"name" manifest:"name"` | ||
} `tfsdk:"role_ref" manifest:"roleRef"` | ||
Subjects []struct { | ||
ApiGroup types.String `tfsdk:"api_group" manifest:"apiGroup"` | ||
Kind types.String `tfsdk:"kind" manifest:"kind"` | ||
Name types.String `tfsdk:"name" manifest:"name"` | ||
Namespace types.String `tfsdk:"namespace" manifest:"namespace"` | ||
} `tfsdk:"subjects" manifest:"subjects"` | ||
} |
Oops, something went wrong.