// Copyright (c) EZSCALE. // SPDX-License-Identifier: MPL-2.0 package provider import ( "context" "fmt" "terraform-provider-virtfusion/internal/client" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" ) // Ensure provider-defined types fully satisfy framework interfaces. var ( _ resource.Resource = &SelfServiceResourceGroupProfileResource{} _ resource.ResourceWithConfigure = &SelfServiceResourceGroupProfileResource{} ) // NewSelfServiceResourceGroupProfileResource creates a new self-service resource group profile resource. func NewSelfServiceResourceGroupProfileResource() resource.Resource { return &SelfServiceResourceGroupProfileResource{} } // SelfServiceResourceGroupProfileResource defines the resource implementation. type SelfServiceResourceGroupProfileResource struct { client *client.Client } // SelfServiceResourceGroupProfileResourceModel describes the resource data model. type SelfServiceResourceGroupProfileResourceModel struct { ID types.String `tfsdk:"id"` UserID types.Int64 `tfsdk:"user_id"` GroupID types.Int64 `tfsdk:"group_id"` ProfileID types.Int64 `tfsdk:"profile_id"` } func (r *SelfServiceResourceGroupProfileResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_self_service_resource_group_profile" } func (r *SelfServiceResourceGroupProfileResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Associates a resource group profile with a user in VirtFusion self-service. Changing any attribute forces recreation of the association.", Attributes: map[string]schema.Attribute{ "id": schema.StringAttribute{ MarkdownDescription: "The composite identifier for this resource group profile association.", Computed: true, PlanModifiers: []planmodifier.String{ stringplanmodifier.UseStateForUnknown(), }, }, "user_id": schema.Int64Attribute{ MarkdownDescription: "The ID of the user.", Required: true, PlanModifiers: []planmodifier.Int64{ int64planmodifier.RequiresReplace(), }, }, "group_id": schema.Int64Attribute{ MarkdownDescription: "The ID of the resource group.", Required: true, PlanModifiers: []planmodifier.Int64{ int64planmodifier.RequiresReplace(), }, }, "profile_id": schema.Int64Attribute{ MarkdownDescription: "The ID of the profile.", Required: true, PlanModifiers: []planmodifier.Int64{ int64planmodifier.RequiresReplace(), }, }, }, } } func (r *SelfServiceResourceGroupProfileResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { if req.ProviderData == nil { return } c, ok := req.ProviderData.(*client.Client) if !ok { resp.Diagnostics.AddError( "Unexpected Resource Configure Type", fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), ) return } r.client = c } func (r *SelfServiceResourceGroupProfileResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { var data SelfServiceResourceGroupProfileResourceModel resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } body := map[string]int64{ "userId": data.UserID.ValueInt64(), "groupId": data.GroupID.ValueInt64(), "profileId": data.ProfileID.ValueInt64(), } _, err := r.client.Post(ctx, "/selfService/resourceGroupProfile", body) if err != nil { resp.Diagnostics.AddError( "Error Creating Resource Group Profile Association", fmt.Sprintf("Could not create resource group profile association (user=%d, group=%d, profile=%d): %s", data.UserID.ValueInt64(), data.GroupID.ValueInt64(), data.ProfileID.ValueInt64(), err), ) return } data.ID = types.StringValue(fmt.Sprintf("%d-%d-%d", data.UserID.ValueInt64(), data.GroupID.ValueInt64(), data.ProfileID.ValueInt64())) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } func (r *SelfServiceResourceGroupProfileResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var data SelfServiceResourceGroupProfileResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } // Return stored state as-is. The API does not provide a direct read endpoint for this association. resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } func (r *SelfServiceResourceGroupProfileResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { // All attributes have RequiresReplace, so Update should never be called. var data SelfServiceResourceGroupProfileResourceModel resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } func (r *SelfServiceResourceGroupProfileResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { var data SelfServiceResourceGroupProfileResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } apiPath := fmt.Sprintf("/selfService/resourceGroupProfile/%d/%d/%d", data.UserID.ValueInt64(), data.GroupID.ValueInt64(), data.ProfileID.ValueInt64()) _, err := r.client.Delete(ctx, apiPath) if err != nil { resp.Diagnostics.AddError( "Error Deleting Resource Group Profile Association", fmt.Sprintf("Could not delete resource group profile association (user=%d, group=%d, profile=%d): %s", data.UserID.ValueInt64(), data.GroupID.ValueInt64(), data.ProfileID.ValueInt64(), err), ) return } } // ValidateConfig validates the resource configuration. func (r *SelfServiceResourceGroupProfileResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { var data SelfServiceResourceGroupProfileResourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } // Validate user_id is positive. if !data.UserID.IsNull() && !data.UserID.IsUnknown() && data.UserID.ValueInt64() <= 0 { resp.Diagnostics.AddAttributeError( path.Root("user_id"), "Invalid User ID", "user_id must be a positive integer.", ) } // Validate group_id is positive. if !data.GroupID.IsNull() && !data.GroupID.IsUnknown() && data.GroupID.ValueInt64() <= 0 { resp.Diagnostics.AddAttributeError( path.Root("group_id"), "Invalid Group ID", "group_id must be a positive integer.", ) } // Validate profile_id is positive. if !data.ProfileID.IsNull() && !data.ProfileID.IsUnknown() && data.ProfileID.ValueInt64() <= 0 { resp.Diagnostics.AddAttributeError( path.Root("profile_id"), "Invalid Profile ID", "profile_id must be a positive integer.", ) } }