-
-
Notifications
You must be signed in to change notification settings - Fork 210
feat(sdn): add custom resource to apply sdn_*
configurations
#2127
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f6ce32e
feat: added custom resource to apply sdn configurations
MacherelR 77b17a7
fix: remove development test
MacherelR ac71a94
fix(sdn): proper handling of pending state
bpg 17c8702
chore(sdn): update docs
bpg a1ce406
Merge branch 'main' into feature/sdn_applier
bpg f9892cb
Update fwprovider/cluster/sdn/applier/resource.go
bpg d0ebf51
Update docs/resources/virtual_environment_sdn_applier.md
bpg 2473d0c
Update docs/resources/virtual_environment_sdn_applier.md
bpg 77b44a2
fix(docs): add an example to docs
bpg 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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
layout: page | ||
title: proxmox_virtual_environment_sdn_applier | ||
parent: Resources | ||
subcategory: Virtual Environment | ||
description: |- | ||
EXPERIMENTAL Triggers Proxmox's SDN Apply (equivalent to PUT /cluster/sdn).Intended to be used with replace_triggered_by so it runs after SDN objects change. | ||
--- | ||
|
||
# Resource: proxmox_virtual_environment_sdn_applier | ||
|
||
**EXPERIMENTAL** Triggers Proxmox's SDN **Apply** (equivalent to `PUT /cluster/sdn`).Intended to be used with `replace_triggered_by` so it runs after SDN objects change. | ||
bpg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "proxmox_virtual_environment_sdn_zone_simple" "test_zone_1" { | ||
id = "tZone1" | ||
nodes = [data.proxmox_virtual_environment_nodes.example.names] | ||
mtu = 1496 | ||
|
||
depends_on = [ | ||
proxmox_virtual_environment_sdn_applier.finalizer | ||
] | ||
} | ||
|
||
resource "proxmox_virtual_environment_sdn_zone_simple" "test_zone_2" { | ||
id = "tZone2" | ||
nodes = [data.proxmox_virtual_environment_nodes.example.names] | ||
mtu = 1496 | ||
|
||
depends_on = [ | ||
proxmox_virtual_environment_sdn_applier.finalizer | ||
] | ||
} | ||
|
||
resource "proxmox_virtual_environment_sdn_applier" "applier" { | ||
lifecycle { | ||
replace_triggered_by = [ | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_1, | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_2, | ||
] | ||
} | ||
|
||
depends_on = [ | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_1, | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_2, | ||
] | ||
} | ||
|
||
resource "proxmox_virtual_environment_sdn_applier" "finalizer" { | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `id` (String) Opaque identifier set to the Unix timestamp (milliseconds) when the apply was executed. |
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
resource "proxmox_virtual_environment_sdn_zone_simple" "test_zone_1" { | ||
id = "tZone1" | ||
nodes= [data.proxmox_virtual_environment_nodes.example.names] | ||
mtu = 1496 | ||
} | ||
|
||
resource "proxmox_virtual_environment_sdn_zone_simple" "test_zone_2" { | ||
id = "tZone2" | ||
nodes= [data.proxmox_virtual_environment_nodes.example.names] | ||
mtu = 1496 | ||
} | ||
|
||
resource "proxmox_virtual_environment_sdn_applier" "apply" { | ||
lifecycle { | ||
replace_triggered_by = [ | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_1, | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_2, | ||
] | ||
} | ||
|
||
depends_on = [ | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_1, | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_2, | ||
] | ||
} |
36 changes: 36 additions & 0 deletions
36
examples/resources/proxmox_virtual_environment_sdn_applier/resource.tf
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
resource "proxmox_virtual_environment_sdn_zone_simple" "test_zone_1" { | ||
id = "tZone1" | ||
nodes = [data.proxmox_virtual_environment_nodes.example.names] | ||
mtu = 1496 | ||
|
||
depends_on = [ | ||
proxmox_virtual_environment_sdn_applier.finalizer | ||
] | ||
} | ||
|
||
resource "proxmox_virtual_environment_sdn_zone_simple" "test_zone_2" { | ||
id = "tZone2" | ||
nodes = [data.proxmox_virtual_environment_nodes.example.names] | ||
mtu = 1496 | ||
|
||
depends_on = [ | ||
proxmox_virtual_environment_sdn_applier.finalizer | ||
] | ||
} | ||
|
||
resource "proxmox_virtual_environment_sdn_applier" "applier" { | ||
lifecycle { | ||
replace_triggered_by = [ | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_1, | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_2, | ||
] | ||
} | ||
|
||
depends_on = [ | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_1, | ||
proxmox_virtual_environment_sdn_zone_simple.test_zone_2, | ||
] | ||
} | ||
|
||
resource "proxmox_virtual_environment_sdn_applier" "finalizer" { | ||
} |
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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package applier | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/bpg/terraform-provider-proxmox/fwprovider/config" | ||
"github.com/bpg/terraform-provider-proxmox/proxmox/cluster/sdn/applier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var ( | ||
_ resource.Resource = &Resource{} | ||
_ resource.ResourceWithConfigure = &Resource{} | ||
) | ||
|
||
type model struct { | ||
// Opaque ID set timestamp at creation time. | ||
ID types.String `tfsdk:"id"` | ||
} | ||
|
||
type Resource struct { | ||
client *applier.Client | ||
} | ||
|
||
func NewResource() resource.Resource { | ||
return &Resource{} | ||
} | ||
|
||
func (r *Resource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_sdn_applier" | ||
} | ||
|
||
func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Applies pending Proxmox SDN configuration (cluster-wide).", | ||
MarkdownDescription: "**EXPERIMENTAL** Triggers Proxmox's SDN **Apply** (equivalent to `PUT /cluster/sdn`)." + | ||
"Intended to be used with `replace_triggered_by` so it runs after SDN objects change.", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Opaque identifier set to the Unix timestamp (milliseconds) when the apply was executed.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *Resource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
cfg, ok := req.ProviderData.(config.Resource) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Resource Configure Type", | ||
fmt.Sprintf("Expected config.Resource, got: %T", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
r.client = cfg.Client.Cluster().SDNApplier() | ||
} | ||
|
||
func (r *Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
if err := r.client.ApplyConfig(ctx); err != nil { | ||
resp.Diagnostics.AddError("Unable to Apply SDN Configuration", err.Error()) | ||
return | ||
} | ||
|
||
state := &model{ | ||
ID: types.StringValue(strconv.FormatInt(time.Now().UTC().UnixMilli(), 10)), | ||
} | ||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...) | ||
} | ||
|
||
func (r *Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
// Nothing to refresh | ||
} | ||
|
||
func (r *Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
// We expect replacements only. But if someone does in-place Update, | ||
// we just re-run apply for safety and bump the ID timestamp. | ||
if err := r.client.ApplyConfig(ctx); err != nil { | ||
resp.Diagnostics.AddError("Unable to Re-Apply SDN Configuration", err.Error()) | ||
return | ||
} | ||
|
||
var plan model | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
plan.ID = types.StringValue(strconv.FormatInt(time.Now().UTC().UnixMilli(), 10)) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) | ||
} | ||
|
||
func (r *Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
if err := r.client.ApplyConfig(ctx); err != nil { | ||
resp.Diagnostics.AddError("Unable to Re-Apply SDN Configuration", err.Error()) | ||
return | ||
} | ||
} |
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.