Skip to content

Commit

Permalink
Merge pull request #106 from maksym-nazarenko/feature/next-pool-field
Browse files Browse the repository at this point in the history
[resource] add 'next-pool' field support to Pool
  • Loading branch information
ddelnano authored Sep 28, 2022
2 parents 80ec84a + 3dd6e60 commit 7cbf55a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
9 changes: 5 additions & 4 deletions client/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
)

type Pool struct {
Id string `mikrotik:".id"`
Name string `mikrotik:"name"`
Ranges string `mikrotik:"ranges"`
Comment string `mikrotik:"comment"`
Id string `mikrotik:".id"`
Name string `mikrotik:"name"`
Ranges string `mikrotik:"ranges"`
NextPool string `mikrotik:"next-pool"`
Comment string `mikrotik:"comment"`
}

func (client Mikrotik) AddPool(p *Pool) (*Pool, error) {
Expand Down
1 change: 1 addition & 0 deletions docs/resources/pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ resource "mikrotik_pool" "pool" {
### Optional

- `comment` (String) The comment of the IP Pool to be created.
- `next_pool` (String) The IP pool to pick next address from if current is exhausted.

### Read-Only

Expand Down
24 changes: 21 additions & 3 deletions mikrotik/resource_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ func resourcePool() *schema.Resource {
Required: true,
Description: "The IP range(s) of the pool. Multiple ranges can be specified, separated by commas: `172.16.0.6-172.16.0.12,172.16.0.50-172.16.0.60`.",
},
"next_pool": {
Type: schema.TypeString,
Optional: true,
StateFunc: func(i interface{}) string {
v := i.(string)
// handle special case for 'none' string:
// it behaves the same as an empty string - unsets the value
// and MikroTik API will return an empty string, but we don't wont diff on '' != 'none'
if v == "none" {
return ""
}

return v
},
Description: "The IP pool to pick next address from if current is exhausted.",
},
"comment": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -99,9 +115,10 @@ func resourcePoolDelete(ctx context.Context, d *schema.ResourceData, m interface

func poolToData(pool *client.Pool, d *schema.ResourceData) diag.Diagnostics {
values := map[string]interface{}{
"name": pool.Name,
"ranges": pool.Ranges,
"comment": pool.Comment,
"name": pool.Name,
"ranges": pool.Ranges,
"next_pool": pool.NextPool,
"comment": pool.Comment,
}

d.SetId(pool.Id)
Expand All @@ -121,6 +138,7 @@ func preparePool(d *schema.ResourceData) *client.Pool {
pool := new(client.Pool)

pool.Name = d.Get("name").(string)
pool.NextPool = d.Get("next_pool").(string)
pool.Ranges = d.Get("ranges").(string)
pool.Comment = d.Get("comment").(string)

Expand Down
50 changes: 50 additions & 0 deletions mikrotik/resource_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,40 @@ func TestAccMikrotikPool_create(t *testing.T) {
})
}

func TestAccMikrotikPool_createNextPool(t *testing.T) {
name := acctest.RandomWithPrefix("pool-create")
ranges := fmt.Sprintf("%s,%s", internal.GetNewIpAddrRange(10), internal.GetNewIpAddr())

resourceName := "mikrotik_pool.bar"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMikrotikPoolDestroy,
Steps: []resource.TestStep{
{
Config: testAccPoolWithNextPool(name, ranges, "next_ip_pool", "next_ip_pool"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccPoolExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "ranges", ranges),
resource.TestCheckResourceAttr(resourceName, "next_pool", "next_ip_pool"),
),
},
{
Config: testAccPoolWithNextPool(name, ranges, "none", "next_ip_pool"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccPoolExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "ranges", ranges),
resource.TestCheckResourceAttr(resourceName, "next_pool", ""),
),
},
},
})
}

func TestAccMikrotikPool_createAndPlanWithNonExistantPool(t *testing.T) {
name := acctest.RandomWithPrefix("pool-plan")
ranges := fmt.Sprintf("%s,%s", internal.GetNewIpAddrRange(10), internal.GetNewIpAddr())
Expand Down Expand Up @@ -155,6 +189,22 @@ resource "mikrotik_pool" "bar" {
`, name, ranges)
}

func testAccPoolWithNextPool(name, ranges, nextPoolToUse, nextPoolName string) string {
return fmt.Sprintf(`
resource "mikrotik_pool" "bar" {
name = %q
ranges = %q
next_pool = %q
depends_on = [mikrotik_pool.next_pool]
}
resource "mikrotik_pool" "next_pool" {
name = %q
ranges = "10.10.10.10-10.10.10.20"
}
`, name, ranges, nextPoolToUse, nextPoolName)
}

func testAccPoolWithComment(name, ranges, comment string) string {
return fmt.Sprintf(`
resource "mikrotik_pool" "bar" {
Expand Down

0 comments on commit 7cbf55a

Please sign in to comment.