feat: add support for UniFi Switch (USW) settings with unifi_setting_usw resource (#53)
This commit is contained in:
committed by
GitHub
parent
34c495021f
commit
7440febd10
47
internal/provider/acctest/resource_setting_usw_test.go
Normal file
47
internal/provider/acctest/resource_setting_usw_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-testing/plancheck"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var settingUswLock = &sync.Mutex{}
|
||||
|
||||
func TestAccSettingUsw(t *testing.T) {
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Lock: settingUswLock,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingUswConfig(true),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttrSet("unifi_setting_usw.test", "id"),
|
||||
resource.TestCheckResourceAttr("unifi_setting_usw.test", "site", "default"),
|
||||
resource.TestCheckResourceAttr("unifi_setting_usw.test", "dhcp_snoop", "true"),
|
||||
),
|
||||
ConfigPlanChecks: pt.CheckResourceActions("unifi_setting_usw.test", plancheck.ResourceActionCreate),
|
||||
},
|
||||
pt.ImportStepWithSite("unifi_setting_usw.test"),
|
||||
{
|
||||
Config: testAccSettingUswConfig(false),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttrSet("unifi_setting_usw.test", "id"),
|
||||
resource.TestCheckResourceAttr("unifi_setting_usw.test", "site", "default"),
|
||||
resource.TestCheckResourceAttr("unifi_setting_usw.test", "dhcp_snoop", "false"),
|
||||
),
|
||||
ConfigPlanChecks: pt.CheckResourceActions("unifi_setting_usw.test", plancheck.ResourceActionUpdate),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccSettingUswConfig(dhcpSnoop bool) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "unifi_setting_usw" "test" {
|
||||
dhcp_snoop = %t
|
||||
}
|
||||
`, dhcpSnoop)
|
||||
}
|
||||
@@ -187,6 +187,7 @@ func (p *unifiProvider) Resources(_ context.Context) []func() resource.Resource
|
||||
settings.NewSslInspectionResource,
|
||||
settings.NewTeleportResource,
|
||||
settings.NewUsgResource,
|
||||
settings.NewUswResource,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
83
internal/provider/settings/resource_setting_usw.go
Normal file
83
internal/provider/settings/resource_setting_usw.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"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/types"
|
||||
)
|
||||
|
||||
type uswModel struct {
|
||||
base.Model
|
||||
DHCPSnoop types.Bool `tfsdk:"dhcp_snoop"`
|
||||
}
|
||||
|
||||
func (d *uswModel) AsUnifiModel(ctx context.Context) (interface{}, diag.Diagnostics) {
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
model := &unifi.SettingUsw{
|
||||
ID: d.ID.ValueString(),
|
||||
DHCPSnoop: d.DHCPSnoop.ValueBool(),
|
||||
}
|
||||
|
||||
return model, diags
|
||||
}
|
||||
|
||||
func (d *uswModel) Merge(ctx context.Context, other interface{}) diag.Diagnostics {
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
model, ok := other.(*unifi.SettingUsw)
|
||||
if !ok {
|
||||
diags.AddError("Cannot merge", "Cannot merge type that is not *unifi.SettingUsw")
|
||||
return diags
|
||||
}
|
||||
|
||||
d.ID = types.StringValue(model.ID)
|
||||
d.DHCPSnoop = types.BoolValue(model.DHCPSnoop)
|
||||
|
||||
return diags
|
||||
}
|
||||
|
||||
var (
|
||||
_ base.ResourceModel = &uswModel{}
|
||||
_ resource.Resource = &uswResource{}
|
||||
_ resource.ResourceWithConfigure = &uswResource{}
|
||||
_ resource.ResourceWithImportState = &uswResource{}
|
||||
)
|
||||
|
||||
type uswResource struct {
|
||||
*base.GenericResource[*uswModel]
|
||||
}
|
||||
|
||||
func (r *uswResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: "Manages UniFi Switch (USW) settings for a UniFi site. These settings control global switch behaviors such as DHCP snooping.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": base.ID(),
|
||||
"site": base.SiteAttribute(),
|
||||
"dhcp_snoop": schema.BoolAttribute{
|
||||
MarkdownDescription: "Whether DHCP snooping is enabled. DHCP snooping is a security feature that filters untrusted DHCP messages and builds a binding database of valid hosts.",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewUswResource() resource.Resource {
|
||||
r := &uswResource{}
|
||||
r.GenericResource = NewSettingResource(
|
||||
"unifi_setting_usw",
|
||||
func() *uswModel { return &uswModel{} },
|
||||
func(ctx context.Context, client *base.Client, site string) (interface{}, error) {
|
||||
return client.GetSettingUsw(ctx, site)
|
||||
},
|
||||
func(ctx context.Context, client *base.Client, site string, body interface{}) (interface{}, error) {
|
||||
return client.UpdateSettingUsw(ctx, site, body.(*unifi.SettingUsw))
|
||||
},
|
||||
)
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user