Skip to content

Commit

Permalink
Merge branch 'master' into fix/243-ip-ranges-cannot-detect-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdemendonca authored Feb 21, 2025
2 parents a288a5a + 66e7959 commit b27cf5b
Show file tree
Hide file tree
Showing 21 changed files with 676 additions and 69 deletions.
1 change: 0 additions & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ To run the tests:
Note that you may need to specify a specific environment variables for some tests to pass, for example machine or fabric ids. Add these to your `env.sh` file before sourcing it again, if required:
```bash
export TF_ACC=1
export TF_ACC_FABRIC=<fabric_id> # e.g. 8
export TF_ACC_NETWORK_INTERFACE_MACHINE=<machine_id> # e.g. b68rn4
export TF_ACC_TAG_MACHINES=<machine_id> # e.g. b68rn4
```
Expand Down
40 changes: 40 additions & 0 deletions docs/data-sources/boot_source_selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "maas_boot_source_selection Data Source - terraform-provider-maas"
subcategory: ""
description: |-
Provides a resource to fetch a MAAS boot source selection.
---

# maas_boot_source_selection (Data Source)

Provides a resource to fetch a MAAS boot source selection.

## Example Usage

```terraform
data "maas_boot_source" "default" {}
data "maas_boot_source_selection" "default" {
boot_source = data.maas_boot_source.default.id
os = "ubuntu"
release = "noble"
}
```

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

### Required

- `boot_source` (Number) The boot source database ID this resource is associated with.
- `os` (String) The operating system for this selection.
- `release` (String) The specific release of the operating system for this selection.

### Read-Only

- `arches` (Set of String) The architecture list for this selection.
- `id` (String) The ID of this resource.
- `labels` (Set of String) The label list for this selection.
- `subarches` (Set of String) The list of subarches for this selection.
54 changes: 54 additions & 0 deletions docs/resources/boot_source_selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "maas_boot_source_selection Resource - terraform-provider-maas"
subcategory: ""
description: |-
Provides a resource to manage a MAAS boot source selection.
---

# maas_boot_source_selection (Resource)

Provides a resource to manage a MAAS boot source selection.

## Example Usage

```terraform
resource "maas_boot_source" "test_boot_source" {
url = "http://images.maas.io/ephemeral-v3/candidate/"
}
resource "maas_boot_source_selection" "test" {
boot_source = maas_boot_source.test_boot_source.id
os = "ubuntu"
release = "jammy"
}
```

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

### Required

- `boot_source` (Number) The boot source database ID this selection is associated with.
- `os` (String) The operating system for this selection.
- `release` (String) The specific release of the operating system for this selection.

### Optional

- `arches` (Set of String) The architecture list for this selection.
- `labels` (Set of String) The label list for this selection.
- `subarches` (Set of String) The list of subarches for this selection.

### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# A boot resource selection can be imported using the boot source ID and the boot source selection ID. e.g.
$ terraform import maas_boot_source.example boot_source:id
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data "maas_boot_source" "default" {}

data "maas_boot_source_selection" "default" {
boot_source = data.maas_boot_source.default.id

os = "ubuntu"
release = "noble"
}
2 changes: 2 additions & 0 deletions examples/resources/maas_boot_source_selection/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# A boot resource selection can be imported using the boot source ID and the boot source selection ID. e.g.
$ terraform import maas_boot_source.example boot_source:id
10 changes: 10 additions & 0 deletions examples/resources/maas_boot_source_selection/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "maas_boot_source" "test_boot_source" {
url = "http://images.maas.io/ephemeral-v3/candidate/"
}

resource "maas_boot_source_selection" "test" {
boot_source = maas_boot_source.test_boot_source.id

os = "ubuntu"
release = "jammy"
}
102 changes: 102 additions & 0 deletions maas/data_source_maas_boot_source_selection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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 dataSourceMaasBootSourceSelection() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMaasBootSourceSelectionRead,
Description: "Provides a resource to fetch a MAAS boot source selection.",

Schema: map[string]*schema.Schema{
"arches": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "The architecture list for this selection.",
},
"boot_source": {
Type: schema.TypeInt,
Required: true,
Description: "The boot source database ID this resource is associated with.",
},
"labels": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "The label list for this selection.",
},
"os": {
Type: schema.TypeString,
Required: true,
Description: "The operating system for this selection.",
},
"release": {
Type: schema.TypeString,
Required: true,
Description: "The specific release of the operating system for this selection.",
},
"subarches": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "The list of subarches for this selection.",
},
},
}
}

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

bootsourceselection, err := getBootSourceSelectionByRelease(client, d.Get("boot_source").(int), d.Get("os").(string), d.Get("release").(string))
if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%v", bootsourceselection.ID))

tfState := map[string]interface{}{
"arches": bootsourceselection.Arches,
"boot_source": bootsourceselection.BootSourceID,
"labels": bootsourceselection.Labels,
"os": bootsourceselection.OS,
"release": bootsourceselection.Release,
"subarches": bootsourceselection.Subarches,
}
if err := setTerraformState(d, tfState); err != nil {
return diag.FromErr(err)
}

return nil
}

func getBootSourceSelectionByRelease(client *client.Client, boot_source int, os string, release string) (*entity.BootSourceSelection, error) {
bootsourceselection, err := findBootSourceSelection(client, boot_source, os, release)
if err != nil {
return nil, err
}
if bootsourceselection == nil {
return nil, fmt.Errorf("boot source selection (%s %s) was not found", os, release)
}
return bootsourceselection, nil
}

func findBootSourceSelection(client *client.Client, boot_source int, os string, release string) (*entity.BootSourceSelection, error) {
bootsourceselections, err := client.BootSourceSelections.Get(boot_source)
if err != nil {
return nil, err
}
for _, d := range bootsourceselections {
if d.OS == os && d.Release == release {
return &d, nil
}
}
return nil, nil
}
55 changes: 55 additions & 0 deletions maas/data_source_maas_boot_source_selection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package maas_test

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceMaasBootSourceSelection_basic(t *testing.T) {
os := "ubuntu"
release := "mantic"
arches := []string{"amd64"}
subarches := []string{"*"}
labels := []string{"*"}

checks := []resource.TestCheckFunc{
resource.TestCheckResourceAttrSet("data.maas_boot_source_selection.test", "boot_source"),
resource.TestCheckResourceAttr("data.maas_boot_source_selection.test", "os", os),
resource.TestCheckResourceAttr("maas_boot_source_selection.test", "release", release),
resource.TestCheckResourceAttr("maas_boot_source_selection.test", "arches.#", "1"),
resource.TestCheckResourceAttr("maas_boot_source_selection.test", "arches.0", arches[0]),
resource.TestCheckResourceAttr("maas_boot_source_selection.test", "subarches.#", "1"),
resource.TestCheckResourceAttr("maas_boot_source_selection.test", "subarches.0", subarches[0]),
resource.TestCheckResourceAttr("maas_boot_source_selection.test", "labels.#", "1"),
resource.TestCheckResourceAttr("maas_boot_source_selection.test", "labels.0", labels[0]),
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.TestAccProviders,
CheckDestroy: testAccCheckMAASBootSourceSelectionDestroy,
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
{
Config: testAccDataSourceMaasBootSourceSelection(os, release, arches, subarches, labels),
Check: resource.ComposeTestCheckFunc(checks...),
},
},
})
}

func testAccDataSourceMaasBootSourceSelection(os string, release string, arches []string, subarches []string, labels []string) string {
return fmt.Sprintf(`
%s
data "maas_boot_source_selection" "test" {
boot_source = maas_boot_source_selection.test.boot_source
os = maas_boot_source_selection.test.os
release = maas_boot_source_selection.test.release
}
`, testAccMAASBootSourceSelection(os, release, arches, subarches, labels))
}
17 changes: 11 additions & 6 deletions maas/data_source_maas_boot_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package maas_test

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

Expand All @@ -21,18 +22,22 @@ func TestAccDataSourceMaasBootSource_basic(t *testing.T) {
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.TestAccProviders,
ErrorCheck: func(err error) error { return err },
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.TestAccProviders,
CheckDestroy: testAccCheckMAASBootSourceDestroy,
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
{
Config: testAccDataSourceMaasBootSource(),
Config: testAccDataSourceMaasBootSource(url, keyring_path),
Check: resource.ComposeTestCheckFunc(checks...),
},
},
})
}

func testAccDataSourceMaasBootSource() string {
return `data "maas_boot_source" "test" {}`
func testAccDataSourceMaasBootSource(url string, keyring_path string) string {
return fmt.Sprintf(`
%s
data "maas_boot_source" "test" {}`, testAccMAASBootSource(url, keyring_path))
}
2 changes: 1 addition & 1 deletion maas/data_source_maas_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccDataSourceMaasDevice_basic(t *testing.T) {
domain := acctest.RandomWithPrefix("tf-domain-")
hostname := acctest.RandomWithPrefix("tf-device-")
zone := "default"
mac_address := "12:23:45:67:89:fa"
mac_address := testutils.RandomMAC()

checks := []resource.TestCheckFunc{
testAccMaasDeviceCheckExists("maas_device.test", &device),
Expand Down
2 changes: 2 additions & 0 deletions maas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"maas_boot_source_selection": resourceMAASBootSourceSelection(),
"maas_boot_source": resourceMAASBootSource(),
"maas_device": resourceMaasDevice(),
"maas_instance": resourceMaasInstance(),
Expand All @@ -77,6 +78,7 @@ func Provider() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"maas_boot_source": dataSourceMaasBootSource(),
"maas_boot_source_selection": dataSourceMaasBootSourceSelection(),
"maas_fabric": dataSourceMaasFabric(),
"maas_vlan": dataSourceMaasVlan(),
"maas_subnet": dataSourceMaasSubnet(),
Expand Down
Loading

0 comments on commit b27cf5b

Please sign in to comment.