-
Notifications
You must be signed in to change notification settings - Fork 47
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
peterctl
wants to merge
4
commits into
canonical:master
Choose a base branch
from
peterctl:master
base: master
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23a0d92
feat: add resource and data source for MAAS zones
peterctl 1761603
add docs and fix code for the Zone resource and datasource
peterctl b283b11
add missing pre-check and test for Zone resources
peterctl 04adaca
use Zone ID instead of Name as Terraform ID
peterctl 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 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 @@ | ||
--- | ||
# 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. |
This file contains 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,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. |
This file contains 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,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 | ||
} |
This file contains 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,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)) | ||
} |
This file contains 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 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,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) | ||
|
||
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 | ||
} |
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.
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.
This is needed since we want to save the database ID. Then
id
should be given toZone.Update
instead ofd.Id()
.