Skip to content

Commit

Permalink
go generate role_binding
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhouston committed Apr 28, 2024
1 parent f67fd55 commit 877e9ab
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/framework/provider/provider_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func (p *KubernetesProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
if os.Getenv("TF_X_KUBERNETES_CODEGEN_PLUGIN6") == "1" {
if os.Getenv("TF_X_KUBERNETES_CODEGEN_PLUGIN6") != "1" {
// NOTE don't configure the client unless the plugin6 experiment is enabled
return
}
Expand Down
50 changes: 50 additions & 0 deletions internal/framework/provider/rbacv1/generate_role_binding.hcl
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 internal/framework/provider/rbacv1/role_binding_crud_gen.go
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)
}
49 changes: 49 additions & 0 deletions internal/framework/provider/rbacv1/role_binding_gen.go
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 internal/framework/provider/rbacv1/role_binding_model_gen.go
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"`
}
Loading

0 comments on commit 877e9ab

Please sign in to comment.