-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data source and tests - looks like not a lot of work was necessary to get it working as we are just re-using the resource's read function - we we have copied over and renamed.
- Loading branch information
Chris Marchesi
committed
Mar 20, 2017
1 parent
27c3b0d
commit 60e4a2d
Showing
5 changed files
with
104 additions
and
51 deletions.
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 |
---|---|---|
@@ -1 +1,45 @@ | ||
package phpipam | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/paybyphone/phpipam-sdk-go/controllers/subnets" | ||
) | ||
|
||
func dataSourcePHPIPAMSubnet() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourcePHPIPAMSubnetRead, | ||
Schema: dataSourceSubnetSchema(), | ||
} | ||
} | ||
|
||
func dataSourcePHPIPAMSubnetRead(d *schema.ResourceData, meta interface{}) error { | ||
c := meta.(*ProviderPHPIPAMClient).subnetsController | ||
var out subnets.Subnet | ||
// We need to determine how to get the subnet. An ID search takes priority, | ||
// and after that subnets. | ||
if id := d.Get("subnet_id").(int); id != 0 { | ||
var err error | ||
out, err = c.GetSubnetByID(id) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
v, err := c.GetSubnetsByCIDR(fmt.Sprintf("%s/%d", d.Get("subnet_address"), d.Get("subnet_mask"))) | ||
if err != nil { | ||
return err | ||
} | ||
// This should not happen, as a CIDR search from what I have seen so far | ||
// generally only returns 1 subnet ever. Nontheless, the API spec and the | ||
// function return a slice of subnets, so we need to assert that the search | ||
// has only retuned a single match. | ||
if len(v) != 1 { | ||
return errors.New("CIDR search returned either zero or multiple results. Please correct your search and try again") | ||
} | ||
out = v[0] | ||
} | ||
flattenSubnet(out, d) | ||
return nil | ||
} |
35 changes: 35 additions & 0 deletions
35
plugin/providers/phpipam/data_source_phpipam_subnet_test.go
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,35 @@ | ||
package phpipam | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
const testAccDataSourcePHPIPAMSubnetConfig = ` | ||
data "phpipam_subnet" "subnet_by_cidr" { | ||
subnet_address = "10.10.2.0" | ||
subnet_mask = 24 | ||
} | ||
data "phpipam_subnet" "subnet_by_id" { | ||
subnet_id = "${data.phpipam_subnet.subnet_by_cidr.subnet_id}" | ||
} | ||
` | ||
|
||
func TestAccDataSourcePHPIPAMSubnet(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccDataSourcePHPIPAMSubnetConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair("data.phpipam_subnet.subnet_by_cidr", "subnet_id", "data.phpipam_subnet.subnet_by_id", "subnet_id"), | ||
resource.TestCheckResourceAttrPair("data.phpipam_subnet.subnet_by_cidr", "subnet_address", "data.phpipam_subnet.subnet_by_id", "subnet_address"), | ||
resource.TestCheckResourceAttrPair("data.phpipam_subnet.subnet_by_cidr", "subnet_mask", "data.phpipam_subnet.subnet_by_id", "subnet_mask"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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
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