Skip to content

Commit

Permalink
Add resource pool resources
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jan 11, 2024
1 parent fc35082 commit 4dd989e
Show file tree
Hide file tree
Showing 14 changed files with 480 additions and 18 deletions.
36 changes: 36 additions & 0 deletions docs/data-sources/resource_pool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "maas_resource_pool Data Source - terraform-provider-maas"
subcategory: ""
description: |-
Provides details about an existing MAAS resource pool.
---

# maas_resource_pool (Data Source)

Provides details about an existing MAAS resource pool.

## Example Usage

```terraform
resource "maas_resource_pool" "test_resource_pool" {
description = "Test description"
name = "test-resource-pool"
}
data "maas_resource_pool" "test_resource_pool" {
name = maas_resource_pool.test_resource_pool.name
}
```

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

### Required

- `name` (String) The name of the resource pool.

### Read-Only

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

# maas_resource_pool (Resource)

Provides a resource to manage MAAS resource pools.

## Example Usage

```terraform
resource "maas_resource_pool" "test_resource_pool" {
description = "Test description"
name = "test-resource-pool"
}
```

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

### Required

- `name` (String) The name of the resource pool.

### Optional

- `description` (String) The description of the resource pool.

### Read-Only

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

## Import

Import is supported using the following syntax:

```shell
# Resource pools can be imported using their ID or name. e.g.
$ terraform import maas_resource_pool.test_resource_pool test-resource-pool
```
8 changes: 8 additions & 0 deletions examples/data-sources/maas_resource_pool/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "maas_resource_pool" "test_resource_pool" {
description = "Test description"
name = "test-resource-pool"
}

data "maas_resource_pool" "test_resource_pool" {
name = maas_resource_pool.test_resource_pool.name
}
2 changes: 2 additions & 0 deletions examples/resources/maas_resource_pool/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Resource pools can be imported using their ID or name. e.g.
$ terraform import maas_resource_pool.test_resource_pool test-resource-pool
4 changes: 4 additions & 0 deletions examples/resources/maas_resource_pool/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "maas_resource_pool" "test_resource_pool" {
description = "Test description"
name = "test-resource-pool"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
github.com/maas/gomaasclient v0.1.0
github.com/maas/gomaasclient v0.2.0
github.com/stretchr/testify v1.8.4
)

Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/lunixbochs/vtclean v0.0.0-20160125035106-4fbf7632a2c6/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/maas/gomaasclient v0.1.0 h1:YGTsEiBB7mskJ+PKvxwo68z/dzzwFNIz0CYvrFrOxxk=
github.com/maas/gomaasclient v0.1.0/go.mod h1:egIWxFQv8Q4uSaLJVba30FmI55Vg8CMzUo0lw+VpjKI=
github.com/maas/gomaasclient v0.2.0 h1:V+MxdQbxTW2ub1THLLmaN3Z3JBmrmhfWg7Vp55HD5Hw=
github.com/maas/gomaasclient v0.2.0/go.mod h1:egIWxFQv8Q4uSaLJVba30FmI55Vg8CMzUo0lw+VpjKI=
github.com/masterzen/azure-sdk-for-go v3.2.0-beta.0.20161014135628-ee4f0065d00c+incompatible/go.mod h1:mf8fjOu33zCqxUjuiU3I8S1lJMyEAlH+0F2+M5xl3hE=
github.com/masterzen/simplexml v0.0.0-20160608183007-4572e39b1ab9/go.mod h1:kCEbxUJlNDEBNbdQMkPSp6yaKcRXVI6f4ddk8Riv4bc=
github.com/masterzen/simplexml v0.0.0-20190410153822-31eea3082786/go.mod h1:kCEbxUJlNDEBNbdQMkPSp6yaKcRXVI6f4ddk8Riv4bc=
Expand Down Expand Up @@ -350,6 +350,7 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
27 changes: 14 additions & 13 deletions maas/data_source_maas_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"terraform-provider-maas/maas/testutils"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/maas/gomaasclient/entity"
)
Expand All @@ -13,24 +14,24 @@ func TestAccDataSourceMaasDevice_basic(t *testing.T) {

var device entity.Device
description := "Test description"
domain := "test-data-domain"
hostname := "test-data-device"
domain := acctest.RandomWithPrefix("tf-domain-")
hostname := acctest.RandomWithPrefix("tf-device-")
zone := "default"
mac_address := "12:23:45:67:89:fa"

checks := []resource.TestCheckFunc{
testAccMaasDeviceCheckExists("maas_device.test", &device),
resource.TestCheckResourceAttr("maas_device.test", "description", description),
resource.TestCheckResourceAttr("maas_device.test", "domain", domain),
resource.TestCheckResourceAttr("maas_device.test", "fqdn", fmt.Sprintf("%s.%s", hostname, domain)),
resource.TestCheckResourceAttr("maas_device.test", "hostname", hostname),
resource.TestCheckResourceAttr("maas_device.test", "zone", zone),
resource.TestCheckResourceAttr("maas_device.test", "ip_addresses.#", "0"),
resource.TestCheckResourceAttr("maas_device.test", "network_interfaces.#", "1"),
resource.TestCheckResourceAttrSet("maas_device.test", "network_interfaces.0.id"),
resource.TestCheckResourceAttr("maas_device.test", "network_interfaces.0.mac_address", mac_address),
resource.TestCheckResourceAttr("maas_device.test", "network_interfaces.0.name", "eth0"),
resource.TestCheckResourceAttrSet("maas_device.test", "owner"),
resource.TestCheckResourceAttr("data.maas_device.test", "description", description),
resource.TestCheckResourceAttr("data.maas_device.test", "domain", domain),
resource.TestCheckResourceAttr("data.maas_device.test", "fqdn", fmt.Sprintf("%s.%s", hostname, domain)),
resource.TestCheckResourceAttr("data.maas_device.test", "hostname", hostname),
resource.TestCheckResourceAttr("data.maas_device.test", "zone", zone),
resource.TestCheckResourceAttr("data.maas_device.test", "ip_addresses.#", "0"),
resource.TestCheckResourceAttr("data.maas_device.test", "network_interfaces.#", "1"),
resource.TestCheckResourceAttrSet("data.maas_device.test", "network_interfaces.0.id"),
resource.TestCheckResourceAttr("data.maas_device.test", "network_interfaces.0.mac_address", mac_address),
resource.TestCheckResourceAttr("data.maas_device.test", "network_interfaces.0.name", "eth0"),
resource.TestCheckResourceAttrSet("data.maas_device.test", "owner"),
}

resource.ParallelTest(t, resource.TestCase{
Expand Down
46 changes: 46 additions & 0 deletions maas/data_source_maas_resource_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package maas

import (
"context"
"fmt"

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

func dataSourceMaasResourcePool() *schema.Resource {
return &schema.Resource{
Description: "Provides details about an existing MAAS resource pool.",
ReadContext: dataSourceResourcePoolRead,

Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the resource pool.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the resource pool.",
},
},
}
}

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

resourcePool, err := getResourcePool(client, d.Get("name").(string))
if err != nil {
return diag.FromErr(err)
}

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

d.Set("description", resourcePool.Description)
d.Set("name", resourcePool.Name)

return nil
}
47 changes: 47 additions & 0 deletions maas/data_source_maas_resource_pool_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/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/maas/gomaasclient/entity"
)

func TestAccDataSourceMaasResourcePool_basic(t *testing.T) {

var resourcePool entity.ResourcePool
description := "Test description"
name := acctest.RandomWithPrefix("tf-resource-pool-")

checks := []resource.TestCheckFunc{
testAccMaasResourcePoolCheckExists("maas_resource_pool.test", &resourcePool),
resource.TestCheckResourceAttr("data.maas_resource_pool.test", "description", description),
resource.TestCheckResourceAttr("data.maas_resource_pool.test", "name", name),
}

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

func testAccDataSourceMaasResourcePool(description string, name string) string {
return fmt.Sprintf(`
%s
data "maas_resource_pool" "test" {
name = maas_resource_pool.test.name
}
`, testAccMaasResourcePool(description, name))
}
2 changes: 2 additions & 0 deletions maas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Provider() *schema.Provider {
"maas_block_device": resourceMaasBlockDevice(),
"maas_tag": resourceMaasTag(),
"maas_user": resourceMaasUser(),
"maas_resource_pool": resourceMaasResourcePool(),
},
DataSourcesMap: map[string]*schema.Resource{
"maas_fabric": dataSourceMaasFabric(),
Expand All @@ -69,6 +70,7 @@ func Provider() *schema.Provider {
"maas_machine": dataSourceMaasMachine(),
"maas_network_interface_physical": dataSourceMaasNetworkInterfacePhysical(),
"maas_device": dataSourceMaasDevice(),
"maas_resource_pool": dataSourceMaasResourcePool(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
5 changes: 3 additions & 2 deletions maas/resource_maas_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"terraform-provider-maas/maas/testutils"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/maas/gomaasclient/client"
Expand All @@ -16,8 +17,8 @@ func TestAccResourceMaasDevice_basic(t *testing.T) {

var device entity.Device
description := "Test description"
domain := "test-domain"
hostname := "test-device"
domain := acctest.RandomWithPrefix("tf-domain-")
hostname := acctest.RandomWithPrefix("tf-device-")
zone := "default"
mac_address := "12:23:45:67:89:de"

Expand Down
Loading

0 comments on commit 4dd989e

Please sign in to comment.