-
Notifications
You must be signed in to change notification settings - Fork 78
feat(git): add ability for users to update git resources #1634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tomkillen
wants to merge
2
commits into
stackitcloud:main
Choose a base branch
from
tomkillen:feat-git-update-resource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,10 +8,11 @@ import ( | |||||
| "strings" | ||||||
|
|
||||||
| "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/attr" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||||||
|
|
@@ -23,7 +24,6 @@ import ( | |||||
|
|
||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features" | ||||||
| gitUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/git/utils" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||||||
|
|
@@ -36,18 +36,31 @@ var ( | |||||
| _ resource.ResourceWithImportState = &gitResource{} | ||||||
| ) | ||||||
|
|
||||||
| // Default to an open access-control-list unless otherwise specified | ||||||
| var defaultAclValue = types.ListValueMust( | ||||||
| types.StringType, | ||||||
| []attr.Value{types.StringValue("0.0.0.0/0")}, | ||||||
| ) | ||||||
|
|
||||||
| // Model represents the schema for the git resource. | ||||||
| type Model struct { | ||||||
| Id types.String `tfsdk:"id"` // Required by Terraform | ||||||
| ACL types.List `tfsdk:"acl"` | ||||||
| ConsumedDisk types.String `tfsdk:"consumed_disk"` | ||||||
| ConsumedObjectStorage types.String `tfsdk:"consumed_object_storage"` | ||||||
| Id types.String `tfsdk:"id"` // Required by Terraform | ||||||
|
|
||||||
| ProjectId types.String `tfsdk:"project_id"` | ||||||
| InstanceId types.String `tfsdk:"instance_id"` | ||||||
|
|
||||||
| // Requires replacement on change | ||||||
| Name types.String `tfsdk:"name"` | ||||||
| Flavor types.String `tfsdk:"flavor"` | ||||||
|
|
||||||
| // Updateable fields | ||||||
| ACL types.List `tfsdk:"acl"` | ||||||
|
|
||||||
| // Read-only fields | ||||||
| Created types.String `tfsdk:"created"` | ||||||
| Flavor types.String `tfsdk:"flavor"` | ||||||
| InstanceId types.String `tfsdk:"instance_id"` | ||||||
| Name types.String `tfsdk:"name"` | ||||||
| ProjectId types.String `tfsdk:"project_id"` | ||||||
| Url types.String `tfsdk:"url"` | ||||||
| ConsumedDisk types.String `tfsdk:"consumed_disk"` | ||||||
| ConsumedObjectStorage types.String `tfsdk:"consumed_object_storage"` | ||||||
| Version types.String `tfsdk:"version"` | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -63,16 +76,17 @@ type gitResource struct { | |||||
|
|
||||||
| // descriptions for the attributes in the Schema | ||||||
| var descriptions = map[string]string{ | ||||||
| "main": "Git Instance resource schema.", | ||||||
| "id": "Terraform's internal resource ID, structured as \"`project_id`,`instance_id`\".", | ||||||
| "acl": "Restricted ACL for instance access.", | ||||||
| "consumed_disk": "How many bytes of disk space is consumed.", | ||||||
| "consumed_object_storage": "How many bytes of Object Storage is consumed.", | ||||||
| "created": "Instance creation timestamp in RFC3339 format.", | ||||||
| "flavor": "Instance flavor. If not provided, defaults to git-100. For a list of available flavors, refer to our API documentation: `https://docs.api.stackit.cloud/documentation/git/version/v1beta`", | ||||||
| "project_id": "STACKIT project ID to which the git instance is associated.", | ||||||
| "instance_id": "ID linked to the git instance.", | ||||||
| "name": "Unique name linked to the git instance.", | ||||||
| "project_id": "STACKIT project ID to which the git instance is associated.", | ||||||
| "flavor": "Instance flavor. If not provided, defaults to git-100. For a list of available flavors, refer to our API documentation: `https://docs.api.stackit.cloud/documentation/git/version/v1beta`", | ||||||
| "created": "Instance creation timestamp in RFC3339 format.", | ||||||
| "url": "Url linked to the git instance.", | ||||||
| "acl": "Restricted ACL for instance access.", | ||||||
| "consumed_disk": "How many bytes of disk space is consumed.", | ||||||
| "consumed_object_storage": "How many bytes of Object Storage is consumed.", | ||||||
| "version": "Version linked to the git instance.", | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -83,7 +97,6 @@ func (g *gitResource) Configure(ctx context.Context, req resource.ConfigureReque | |||||
| return | ||||||
| } | ||||||
|
|
||||||
| features.CheckBetaResourcesEnabled(ctx, &providerData, &resp.Diagnostics, "stackit_git", "resource") | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
|
|
@@ -104,16 +117,14 @@ func (g *gitResource) Metadata(_ context.Context, req resource.MetadataRequest, | |||||
| // Schema defines the schema for the resource. | ||||||
| func (g *gitResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||||||
| resp.Schema = schema.Schema{ | ||||||
| MarkdownDescription: fmt.Sprintf( | ||||||
| "%s %s", | ||||||
| features.AddBetaDescription("Git Instance resource schema.", core.Resource), | ||||||
| "This resource currently does not support updates. Changing the ACLs, flavor, or name will trigger resource recreation. Update functionality will be added soon. In the meantime, please proceed with caution. To update these attributes, please open a support ticket.", | ||||||
| ), | ||||||
| Description: "Git Instance resource schema.", | ||||||
| Description: descriptions["main"], | ||||||
| Attributes: map[string]schema.Attribute{ | ||||||
| "id": schema.StringAttribute{ | ||||||
| Description: descriptions["id"], | ||||||
| Computed: true, | ||||||
| PlanModifiers: []planmodifier.String{ | ||||||
| stringplanmodifier.UseStateForUnknown(), | ||||||
| }, | ||||||
| }, | ||||||
| "project_id": schema.StringAttribute{ | ||||||
| Description: descriptions["project_id"], | ||||||
|
|
@@ -133,15 +144,16 @@ func (g *gitResource) Schema(_ context.Context, _ resource.SchemaRequest, resp * | |||||
| validate.UUID(), | ||||||
| validate.NoSeparator(), | ||||||
| }, | ||||||
| PlanModifiers: []planmodifier.String{ | ||||||
| stringplanmodifier.UseStateForUnknown(), | ||||||
| }, | ||||||
| }, | ||||||
| "acl": schema.ListAttribute{ | ||||||
| Description: descriptions["acl"], | ||||||
| PlanModifiers: []planmodifier.List{ | ||||||
| listplanmodifier.RequiresReplace(), | ||||||
| }, | ||||||
| ElementType: types.StringType, | ||||||
| Optional: true, | ||||||
| Computed: true, | ||||||
| Default: listdefault.StaticValue(defaultAclValue), | ||||||
| }, | ||||||
| "consumed_disk": schema.StringAttribute{ | ||||||
| Description: descriptions["consumed_disk"], | ||||||
|
|
@@ -154,6 +166,9 @@ func (g *gitResource) Schema(_ context.Context, _ resource.SchemaRequest, resp * | |||||
| "created": schema.StringAttribute{ | ||||||
| Description: descriptions["created"], | ||||||
| Computed: true, | ||||||
| PlanModifiers: []planmodifier.String{ | ||||||
| stringplanmodifier.UseStateForUnknown(), | ||||||
| }, | ||||||
| }, | ||||||
| "flavor": schema.StringAttribute{ | ||||||
| Description: descriptions["flavor"], | ||||||
|
|
@@ -176,6 +191,9 @@ func (g *gitResource) Schema(_ context.Context, _ resource.SchemaRequest, resp * | |||||
| "url": schema.StringAttribute{ | ||||||
| Description: descriptions["url"], | ||||||
| Computed: true, | ||||||
| PlanModifiers: []planmodifier.String{ | ||||||
| stringplanmodifier.UseStateForUnknown(), | ||||||
| }, | ||||||
| }, | ||||||
| "version": schema.StringAttribute{ | ||||||
| Description: descriptions["version"], | ||||||
|
|
@@ -299,15 +317,63 @@ func (g *gitResource) Read(ctx context.Context, req resource.ReadRequest, resp * | |||||
| tflog.Info(ctx, fmt.Sprintf("read git instance %s", instanceId)) | ||||||
| } | ||||||
|
|
||||||
| // Update attempts to update the resource. In this case, git instances cannot be updated. | ||||||
| // Note: This method is intentionally left without update logic because changes | ||||||
| // to 'project_id' or 'name' require the resource to be entirely replaced. | ||||||
| // As a result, the Update function is redundant since any modifications will | ||||||
| // automatically trigger a resource recreation through Terraform's built-in | ||||||
| // lifecycle management. | ||||||
| func (g *gitResource) Update(ctx context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { // nolint:gocritic // function signature required by Terraform | ||||||
| // git instances cannot be updated, so we log an error. | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating git instance", "Git Instance can't be updated") | ||||||
| // Updates the git instance. | ||||||
| func (g *gitResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { // nolint:gocritic // function signature required by Terraform | ||||||
| // Retrieve the planned values for the resource. | ||||||
| var model Model | ||||||
| diags := req.Plan.Get(ctx, &model) | ||||||
| resp.Diagnostics.Append(diags...) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| ctx = core.InitProviderContext(ctx) | ||||||
|
|
||||||
| projectId := model.ProjectId.ValueString() | ||||||
| instanceId := model.InstanceId.ValueString() | ||||||
| ctx = tflog.SetField(ctx, "project_id", projectId) | ||||||
| ctx = tflog.SetField(ctx, "instance_id", instanceId) | ||||||
|
|
||||||
| payload, diags := toPatchPayload(ctx, &model) | ||||||
| resp.Diagnostics.Append(diags...) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| tflog.Info(ctx, "updating instance", map[string]interface{}{ | ||||||
| "project_id": projectId, | ||||||
| "instanceId": instanceId, | ||||||
| "payload": payload, | ||||||
| }) | ||||||
|
|
||||||
| gitInstanceResp, err := g.client.DefaultAPI.PatchInstance(ctx, projectId, instanceId). | ||||||
| PatchInstancePayload(payload). | ||||||
| Execute() | ||||||
| if err != nil { | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating git instance", fmt.Sprintf("Calling API: %v", err)) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| // Wait for update | ||||||
| _, err = wait.UpdateGitInstanceWaitHandler(ctx, g.client.DefaultAPI, projectId, instanceId).WaitWithContext(ctx) | ||||||
| if err != nil { | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating git instance", fmt.Sprintf("Git instance update waiting: %v", err)) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| err = mapFields(ctx, gitInstanceResp, &model) | ||||||
| if err != nil { | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating git instance", fmt.Sprintf("Processing API response: %v", err)) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| // Set the updated state. | ||||||
| diags = resp.State.Set(ctx, model) | ||||||
| resp.Diagnostics.Append(diags...) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
| tflog.Info(ctx, "Git instance updated") | ||||||
| } | ||||||
|
|
||||||
| // Delete deletes the git instance and removes it from the Terraform state on success. | ||||||
|
|
@@ -435,3 +501,25 @@ func toCreatePayload(ctx context.Context, model *Model) (git.CreateInstancePaylo | |||||
|
|
||||||
| return payload, diags | ||||||
| } | ||||||
|
|
||||||
| // toPatchPayload creates the payload to update a git instance | ||||||
| func toPatchPayload(ctx context.Context, model *Model) (git.PatchInstancePayload, diag.Diagnostics) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Your function is named If you need help making a decision, |
||||||
| diags := diag.Diagnostics{} | ||||||
|
|
||||||
| if model == nil { | ||||||
| return git.PatchInstancePayload{}, diags | ||||||
| } | ||||||
|
|
||||||
| payload := git.PatchInstancePayload{} | ||||||
|
|
||||||
| if !(model.ACL.IsNull() || model.ACL.IsUnknown()) { | ||||||
| var acl []string | ||||||
| aclDiags := model.ACL.ElementsAs(ctx, &acl, false) | ||||||
| diags.Append(aclDiags...) | ||||||
| if !aclDiags.HasError() { | ||||||
| payload.Acl = acl | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return payload, diags | ||||||
| } | ||||||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short note without any deep review: We're still consuming the
v1betaSTACKIT git API here, so this must stay marked as beta