Skip to content
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

feat: add resource and data source for MAAS zones #258

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/data-sources/zone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "maas_zone Data Source - terraform-provider-maas"
subcategory: ""
description: |-
Provides details about an existing MAAS zone.
---

# maas_zone (Data Source)

Provides details about an existing MAAS zone.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The zone's name.

### Read-Only

- `description` (String) A brief description of the zone.
- `id` (String) The ID of this resource.
28 changes: 28 additions & 0 deletions docs/resources/zone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "maas_zone Resource - terraform-provider-maas"
subcategory: ""
description: |-
Provides a resource to manage MAAS zones.
---

# maas_zone (Resource)

Provides a resource to manage MAAS zones.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of the new zone.

### Optional

- `description` (String) A brief description of the new zone.

### Read-Only

- `id` (String) The ID of this resource.
49 changes: 49 additions & 0 deletions maas/data_source_maas_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package maas

import (
"context"
"fmt"

"github.com/canonical/gomaasclient/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceMaasZone() *schema.Resource {
return &schema.Resource{
Description: "Provides details about an existing MAAS zone.",
ReadContext: dataSourceZoneRead,

Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
Description: "A brief description of the zone.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The zone's name.",
},
},
}
}

func dataSourceZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

zone, err := getZone(client, d.Get("name").(string))
if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%v", zone.ID))
tfstate := map[string]interface{}{
"name": zone.Name,
"description": zone.Description,
}
if err := setTerraformState(d, tfstate); err != nil {
return diag.FromErr(err)
}

return nil
}
47 changes: 47 additions & 0 deletions maas/data_source_maas_zone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package maas_test

import (
"fmt"
"terraform-provider-maas/maas/testutils"
"testing"

"github.com/canonical/gomaasclient/entity"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceMaasZone_basic(t *testing.T) {

var zone entity.Zone
description := "Test description"
name := acctest.RandomWithPrefix("tf-zone-")

checks := []resource.TestCheckFunc{
testAccMaasZoneCheckExists("maas_zone.test", &zone),
resource.TestCheckResourceAttr("data.maas_zone.test", "description", description),
resource.TestCheckResourceAttr("data.maas_zone.test", "name", name),
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.TestAccProviders,
CheckDestroy: testAccCheckMaasZoneDestroy,
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
{
Config: testAccDataSourceMaasZone(description, name),
Check: resource.ComposeTestCheckFunc(checks...),
},
},
})
}

func testAccDataSourceMaasZone(description string, name string) string {
return fmt.Sprintf(`
%s

data "maas_zone" "test" {
name = maas_zone.test.name
}
`, testAccMaasZone(name, description))
}
2 changes: 2 additions & 0 deletions maas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func Provider() *schema.Provider {
"maas_tag": resourceMaasTag(),
"maas_user": resourceMaasUser(),
"maas_resource_pool": resourceMaasResourcePool(),
"maas_zone": resourceMaasZone(),
},
DataSourcesMap: map[string]*schema.Resource{
"maas_boot_source": dataSourceMaasBootSource(),
Expand All @@ -76,6 +77,7 @@ func Provider() *schema.Provider {
"maas_device": dataSourceMaasDevice(),
"maas_resource_pool": dataSourceMaasResourcePool(),
"maas_rack_controller": dataSourceMaasRackController(),
"maas_zone": dataSourceMaasZone(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
142 changes: 142 additions & 0 deletions maas/resource_maas_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package maas

import (
"context"
"fmt"

"github.com/canonical/gomaasclient/client"
"github.com/canonical/gomaasclient/entity"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceMaasZone() *schema.Resource {
return &schema.Resource{
Description: "Provides a resource to manage MAAS zones.",
CreateContext: resourceZoneCreate,
ReadContext: resourceZoneRead,
UpdateContext: resourceZoneUpdate,
DeleteContext: resourceZoneDelete,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*client.Client)
zone, err := getZone(client, d.Id())
if err != nil {
return nil, err
}
d.SetId(fmt.Sprintf("%v", zone.ID))
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
Description: "A brief description of the new zone.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the new zone.",
},
},
}
}

func resourceZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

zone, err := getZone(client, d.Id())
if err != nil {
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf("%v", zone.ID))

tfstate := map[string]any{
"name": zone.Name,
"description": zone.Description,
}

if err := setTerraformState(d, tfstate); err != nil {
return diag.FromErr(err)
}

return nil
}

func resourceZoneCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

params := getZoneParams(d)
zone, err := client.Zones.Create(params)
if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%v", zone.ID))

return resourceZoneRead(ctx, d, meta)
}

func resourceZoneUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed since we want to save the database ID. Then id should be given to Zone.Update instead of d.Id().

Suggested change
id, err := strconv.Atoi(d.Id())
if err != nil {
return diag.FromErr(err)
}

params := getZoneParams(d)
zone, err := getZone(client, d.Id())
if err != nil {
return diag.FromErr(err)
}
zone, err = client.Zone.Update(zone.Name, params)
if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%v", zone.ID))

return resourceZoneRead(ctx, d, meta)
}

func resourceZoneDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

zone, err := getZone(client, d.Id())
if err != nil {
return diag.FromErr(err)
}
if err := client.Zone.Delete(zone.Name); err != nil {
return diag.FromErr(err)
}

return nil
}

func getZoneParams(d *schema.ResourceData) *entity.ZoneParams {
return &entity.ZoneParams{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
}

func findZone(client *client.Client, identifier string) (*entity.Zone, error) {
zones, err := client.Zones.Get()
if err != nil {
return nil, err
}
for _, z := range zones {
if fmt.Sprintf("%v", z.ID) == identifier || z.Name == identifier {
return &z, nil
}
}
return nil, nil
}

func getZone(client *client.Client, identifier string) (*entity.Zone, error) {
zone, err := findZone(client, identifier)
if err != nil {
return nil, err
}
if zone == nil {
return nil, fmt.Errorf("zone (%s) was not found", identifier)
}
return zone, nil
}
Loading