Implement unifi_setting_mgmt resource
This commit is contained in:
38
docs/resources/setting_mgmt.md
Normal file
38
docs/resources/setting_mgmt.md
Normal file
@@ -0,0 +1,38 @@
|
||||
---
|
||||
# generated by https://github.com/hashicorp/terraform-plugin-docs
|
||||
page_title: "unifi_setting_mgmt Resource - terraform-provider-unifi"
|
||||
subcategory: ""
|
||||
description: |-
|
||||
unifi_setting_mgmt manages settings for a unifi site.
|
||||
---
|
||||
|
||||
# unifi_setting_mgmt (Resource)
|
||||
|
||||
`unifi_setting_mgmt` manages settings for a unifi site.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```terraform
|
||||
resource "unifi_site" "example" {
|
||||
description = "example"
|
||||
}
|
||||
|
||||
resource "unifi_setting_mgmt" "example" {
|
||||
site = unifi_site.example.name
|
||||
auto_upgrade = true
|
||||
}
|
||||
```
|
||||
|
||||
<!-- schema generated by tfplugindocs -->
|
||||
## Schema
|
||||
|
||||
### Optional
|
||||
|
||||
- **auto_upgrade** (Boolean) Automatically upgrade device firmware.
|
||||
- **site** (String) The name of the site to associate the settings with.
|
||||
|
||||
### Read-Only
|
||||
|
||||
- **id** (String) The ID of the settings.
|
||||
|
||||
|
||||
8
examples/resources/unifi_setting_mgmt/resource.tf
Normal file
8
examples/resources/unifi_setting_mgmt/resource.tf
Normal file
@@ -0,0 +1,8 @@
|
||||
resource "unifi_site" "example" {
|
||||
description = "example"
|
||||
}
|
||||
|
||||
resource "unifi_setting_mgmt" "example" {
|
||||
site = unifi_site.example.name
|
||||
auto_upgrade = true
|
||||
}
|
||||
@@ -494,3 +494,15 @@ func (c *lazyClient) UpdateDynamicDNS(ctx context.Context, site string, d *unifi
|
||||
}
|
||||
return c.inner.UpdateDynamicDNS(ctx, site, d)
|
||||
}
|
||||
func (c *lazyClient) GetSettingMgmt(ctx context.Context, site string) (*unifi.SettingMgmt, error) {
|
||||
if err := c.init(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.inner.GetSettingMgmt(ctx, site)
|
||||
}
|
||||
func (c *lazyClient) UpdateSettingMgmt(ctx context.Context, site string, d *unifi.SettingMgmt) (*unifi.SettingMgmt, error) {
|
||||
if err := c.init(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.inner.UpdateSettingMgmt(ctx, site, d)
|
||||
}
|
||||
|
||||
@@ -94,6 +94,7 @@ func New(version string) func() *schema.Provider {
|
||||
"unifi_user_group": resourceUserGroup(),
|
||||
"unifi_user": resourceUser(),
|
||||
"unifi_wlan": resourceWLAN(),
|
||||
"unifi_setting_mgmt": resourceSettingMgmt(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -208,6 +209,9 @@ type unifiClient interface {
|
||||
DeleteDynamicDNS(ctx context.Context, site, id string) error
|
||||
CreateDynamicDNS(ctx context.Context, site string, d *unifi.DynamicDNS) (*unifi.DynamicDNS, error)
|
||||
UpdateDynamicDNS(ctx context.Context, site string, d *unifi.DynamicDNS) (*unifi.DynamicDNS, error)
|
||||
|
||||
GetSettingMgmt(ctx context.Context, id string) (*unifi.SettingMgmt, error)
|
||||
UpdateSettingMgmt(ctx context.Context, site string, d *unifi.SettingMgmt) (*unifi.SettingMgmt, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
|
||||
123
internal/provider/resource_setting_mgmt.go
Normal file
123
internal/provider/resource_setting_mgmt.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/paultyng/go-unifi/unifi"
|
||||
)
|
||||
|
||||
func resourceSettingMgmt() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_setting_mgmt` manages settings for a unifi site.",
|
||||
|
||||
Create: resourceSettingMgmtCreate,
|
||||
Read: resourceSettingMgmtRead,
|
||||
Update: resourceSettingMgmtUpdate,
|
||||
Delete: resourceSettingMgmtDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: importSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"id": {
|
||||
Description: "The ID of the settings.",
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"site": {
|
||||
Description: "The name of the site to associate the settings with.",
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"auto_upgrade": {
|
||||
Description: "Automatically upgrade device firmware.",
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceSettingMgmtGetResourceData(d *schema.ResourceData, meta interface{}) (*unifi.SettingMgmt, error) {
|
||||
return &unifi.SettingMgmt{
|
||||
AutoUpgrade: d.Get("auto_upgrade").(bool),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func resourceSettingMgmtCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
c := meta.(*client)
|
||||
|
||||
req, err := resourceSettingMgmtGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
site = c.site
|
||||
}
|
||||
|
||||
resp, err := c.c.UpdateSettingMgmt(context.TODO(), site, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId(resp.ID)
|
||||
|
||||
return resourceSettingMgmtSetResourceData(resp, d, meta, site)
|
||||
}
|
||||
|
||||
func resourceSettingMgmtSetResourceData(resp *unifi.SettingMgmt, d *schema.ResourceData, meta interface{}, site string) error {
|
||||
d.Set("site", site)
|
||||
d.Set("auto_upgrade", resp.AutoUpgrade)
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceSettingMgmtRead(d *schema.ResourceData, meta interface{}) error {
|
||||
c := meta.(*client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
site = c.site
|
||||
}
|
||||
|
||||
resp, err := c.c.GetSettingMgmt(context.TODO(), site)
|
||||
if _, ok := err.(*unifi.NotFoundError); ok {
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceSettingMgmtSetResourceData(resp, d, meta, site)
|
||||
}
|
||||
|
||||
func resourceSettingMgmtUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
c := meta.(*client)
|
||||
|
||||
req, err := resourceSettingMgmtGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.ID = d.Id()
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
site = c.site
|
||||
}
|
||||
|
||||
resp, err := c.c.UpdateSettingMgmt(context.TODO(), site, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceSettingMgmtSetResourceData(resp, d, meta, site)
|
||||
}
|
||||
|
||||
func resourceSettingMgmtDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
return nil
|
||||
}
|
||||
59
internal/provider/resource_setting_mgmt_test.go
Normal file
59
internal/provider/resource_setting_mgmt_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccSettingMgmt_basic(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
ProviderFactories: providerFactories,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingMgmtConfig_basic(),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
importStep("unifi_setting_mgmt.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccSettingMgmt_site(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
ProviderFactories: providerFactories,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingMgmtConfig_site(),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
{
|
||||
ResourceName: "unifi_setting_mgmt.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_setting_mgmt.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccSettingMgmtConfig_basic() string {
|
||||
return `
|
||||
resource "unifi_setting_mgmt" "test" {
|
||||
auto_upgrade = true
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func testAccSettingMgmtConfig_site() string {
|
||||
return `
|
||||
resource "unifi_site" "test" {
|
||||
description = "test"
|
||||
}
|
||||
|
||||
resource "unifi_setting_mgmt" "test" {
|
||||
site = unifi_site.test.name
|
||||
auto_upgrade = true
|
||||
}
|
||||
`
|
||||
}
|
||||
Reference in New Issue
Block a user