Skip to content

Commit

Permalink
Implemented Name Server Group Stub resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ppwarwick authored Aug 30, 2017
1 parent 17b86a9 commit 4ad3266
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 8 deletions.
1 change: 1 addition & 0 deletions infoblox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Provider() terraform.ResourceProvider {
"infoblox_zone_forward": resourceZoneForward(),
"infoblox_ns_group_delegation": resourceNSGroupDelegation(),
"infoblox_ns_group_forward": resourceNSGroupForward(),
"infoblox_ns_group_stub": resourceNSGroupStub(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
152 changes: 152 additions & 0 deletions infoblox/resource_nsgroup_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package infoblox

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/sky-uk/skyinfoblox"
"github.com/sky-uk/skyinfoblox/api/nsgroupstub"
"github.com/sky-uk/terraform-provider-infoblox/infoblox/util"
"net/http"
)

func resourceNSGroupStub() *schema.Resource {
return &schema.Resource{
Create: resourceNSGroupStubCreate,
Read: resourceNSGroupStubRead,
Update: resourceNSGroupStubUpdate,
Delete: resourceNSGroupStubDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the name server group",
Required: true,
ValidateFunc: util.CheckLeadingTrailingSpaces,
},
"comment": {
Type: schema.TypeString,
Description: "Comment field",
Optional: true,
ValidateFunc: util.CheckLeadingTrailingSpaces,
},
// Note the lead, stealth, grid_replicate, preferred_primaries, override_preferred_primaries attributes are ignored by Infoblox when set for this object type.
"stub_members": util.MemberServerListSchema(),
},
}
}

func resourceNSGroupStubCreate(d *schema.ResourceData, m interface{}) error {

var nsGroupStubObject nsgroupstub.NSGroupStub
client := m.(*skyinfoblox.InfobloxClient)

if v, ok := d.GetOk("name"); ok && v != "" {
nsGroupStubObject.Name = v.(string)
}
if v, ok := d.GetOk("comment"); ok && v != "" {
nsGroupStubObject.Comment = v.(string)
}
if v, ok := d.GetOk("stub_members"); ok && v != nil {
stubMemberList := util.GetMapList(v.([]interface{}))
nsGroupStubObject.StubMembers = util.BuildMemberServerListFromT(stubMemberList)
}

createNSGroupStubAPI := nsgroupstub.NewCreate(nsGroupStubObject)
err := client.Do(createNSGroupStubAPI)
httpStatus := createNSGroupStubAPI.StatusCode()
if err != nil || httpStatus < http.StatusOK || httpStatus >= http.StatusBadRequest {
return fmt.Errorf("Infoblox NS Group Stub create for %s failed with status code %d and error: %+v", nsGroupStubObject.Name, httpStatus, string(createNSGroupStubAPI.RawResponse()))
}

nsGroupStubObject.Reference = *createNSGroupStubAPI.ResponseObject().(*string)
d.SetId(nsGroupStubObject.Reference)
return resourceNSGroupStubRead(d, m)
}

func resourceNSGroupStubRead(d *schema.ResourceData, m interface{}) error {

reference := d.Id()
client := m.(*skyinfoblox.InfobloxClient)

getNSGroupStubAPI := nsgroupstub.NewGet(reference, nsgroupstub.RequestReturnFields)
err := client.Do(getNSGroupStubAPI)
httpStatus := getNSGroupStubAPI.StatusCode()
if httpStatus == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil || httpStatus < http.StatusOK || httpStatus >= http.StatusBadRequest {
return fmt.Errorf("Infoblox NS Group Stub read for %s failed with status code %d and error: %+v", reference, httpStatus, string(getNSGroupStubAPI.RawResponse()))
}
response := *getNSGroupStubAPI.ResponseObject().(*nsgroupstub.NSGroupStub)
d.SetId(response.Reference)
d.Set("name", response.Name)
d.Set("comment", response.Comment)
d.Set("stub_members", util.BuildMemberServerListFromIBX(response.StubMembers))

return nil
}

func resourceNSGroupStubUpdate(d *schema.ResourceData, m interface{}) error {

var nsGroupStubObject nsgroupstub.NSGroupStub
hasChanges := false

if d.HasChange("name") {
if v, ok := d.GetOk("name"); ok && v != "" {
nsGroupStubObject.Name = v.(string)
}
hasChanges = true
}
if d.HasChange("comment") {
if v, ok := d.GetOk("comment"); ok && v != "" {
nsGroupStubObject.Comment = v.(string)
}
hasChanges = true
}
if d.HasChange("stub_members") {
if v, ok := d.GetOk("stub_members"); ok && v != nil {
stubMemberList := util.GetMapList(v.([]interface{}))
nsGroupStubObject.StubMembers = util.BuildMemberServerListFromT(stubMemberList)
}
hasChanges = true
}

if hasChanges {
nsGroupStubObject.Reference = d.Id()
client := m.(*skyinfoblox.InfobloxClient)

updateNSGroupStubAPI := nsgroupstub.NewUpdate(nsGroupStubObject, nsgroupstub.RequestReturnFields)
err := client.Do(updateNSGroupStubAPI)
httpStatus := updateNSGroupStubAPI.StatusCode()
if err != nil || httpStatus < http.StatusOK || httpStatus >= http.StatusBadRequest {
return fmt.Errorf("Infoblox NS Group Stub update for %s failed with status code %d and error: %+v", nsGroupStubObject.Reference, httpStatus, string(updateNSGroupStubAPI.RawResponse()))
}
response := *updateNSGroupStubAPI.ResponseObject().(*nsgroupstub.NSGroupStub)
d.SetId(response.Reference)
d.Set("name", response.Name)
d.Set("comment", response.Comment)
d.Set("stub_members", util.BuildMemberServerListFromIBX(response.StubMembers))
}

return resourceNSGroupStubRead(d, m)
}

func resourceNSGroupStubDelete(d *schema.ResourceData, m interface{}) error {

client := m.(*skyinfoblox.InfobloxClient)
reference := d.Id()

deleteNSGroupStubAPI := nsgroupstub.NewDelete(reference)
err := client.Do(deleteNSGroupStubAPI)
httpStatus := deleteNSGroupStubAPI.StatusCode()
if httpStatus == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil || httpStatus < http.StatusOK || httpStatus >= http.StatusBadRequest {
return fmt.Errorf("Infoblox NS Group Stub delete for %s failed with status code %d and error: %+v", reference, httpStatus, string(deleteNSGroupStubAPI.RawResponse()))
}
d.SetId("")
return nil
}
183 changes: 183 additions & 0 deletions infoblox/resource_nsgroup_stub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package infoblox

/*
Note: test is commented out until we have a proper test environment
import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/sky-uk/skyinfoblox"
"github.com/sky-uk/skyinfoblox/api/nsgroupstub"
"regexp"
"testing"
)
func TestAccInfobloxNSGroupStubBasic(t *testing.T) {
randomInt := acctest.RandInt()
nsGroupStubName := fmt.Sprintf("acctest-infoblox-ns-group-stub-%d", randomInt)
nsGroupStubNameUpdated := fmt.Sprintf("%s-updated", nsGroupStubName)
nsGroupStubResourceInstance := "infoblox_ns_group_stub.acctest"
fmt.Printf("\n\nAcceptance Test NS Group Stub is %s\n\n", nsGroupStubName)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccInfobloxNSGroupStubCheckDestroy(state, nsGroupStubName)
},
Steps: []resource.TestStep{
{
Config: testAccInfobloxNSGroupStubNoNameTemplate(),
ExpectError: regexp.MustCompile(`required field is not set`),
},
{
Config: testAccInfobloxNSGroupStubCommentLeadingTrailingSpaces(nsGroupStubName),
ExpectError: regexp.MustCompile(`must not contain trailing or leading white space`),
},
{
Config: testAccInfobloxNSGroupStubCreateTemplate(nsGroupStubName),
Check: resource.ComposeTestCheckFunc(
testAccInfobloxNSGroupStubCheckExists(nsGroupStubName, nsGroupStubResourceInstance),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "name", nsGroupStubName),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "comment", "Infoblox Terraform Acceptance test"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.#", "3"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.0.name", "grid-member01.example.com"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.1.name", "grid-member02.example.com"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.2.name", "grid-member03.example.com"),
),
},
{
Config: testAccInfobloxNSGroupStubUpdateTemplate(nsGroupStubNameUpdated),
Check: resource.ComposeTestCheckFunc(
testAccInfobloxNSGroupStubCheckExists(nsGroupStubNameUpdated, nsGroupStubResourceInstance),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "name", nsGroupStubNameUpdated),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "comment", "Infoblox Terraform Acceptance test - updated"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.#", "4"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.0.name", "grid-member01.example.com"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.1.name", "grid-member02.example.com"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.2.name", "grid-member03.example.com"),
resource.TestCheckResourceAttr(nsGroupStubResourceInstance, "stub_members.3.name", "grid-member04.example.com"),
),
},
},
})
}
func testAccInfobloxNSGroupStubCheckDestroy(state *terraform.State, name string) error {
client := testAccProvider.Meta().(*skyinfoblox.InfobloxClient)
for _, rs := range state.RootModule().Resources {
if rs.Type != "infoblox_ns_group_stub" {
continue
}
if id, ok := rs.Primary.Attributes["id"]; ok && id == "" {
return nil
}
api := nsgroupstub.NewGetAll()
err := client.Do(api)
if err != nil {
return fmt.Errorf("Infoblox - error occurred whilst retrieving a list of NS Group Stub")
}
for _, nsGroupStub := range *api.ResponseObject().(*[]nsgroupstub.NSGroupStub) {
if nsGroupStub.Name == name {
return fmt.Errorf("Infoblox NS Group Stub %s still exists", name)
}
}
}
return nil
}
func testAccInfobloxNSGroupStubCheckExists(name, resourceName string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("\nInfoblox NS Group Stub %s wasn't found in resources", name)
}
if rs.Primary.ID == "" {
return fmt.Errorf("\nInfoblox NS Group Stub ID not set for %s in resources", name)
}
client := testAccProvider.Meta().(*skyinfoblox.InfobloxClient)
api := nsgroupstub.NewGetAll()
err := client.Do(api)
if err != nil {
return fmt.Errorf("Infoblox NS Group Stub - error whilst retrieving a list of NS Group Stub: %+v", err)
}
for _, nsGroupStub := range *api.ResponseObject().(*[]nsgroupstub.NSGroupStub) {
if nsGroupStub.Name == name {
return nil
}
}
return fmt.Errorf("Infoblox NS Group Stub %s wasn't found on remote Infoblox server", name)
}
}
func testAccInfobloxNSGroupStubNoNameTemplate() string {
return fmt.Sprintf(`
resource "infoblox_ns_group_stub" "acctest" {
comment = "Infoblox Terraform Acceptance test"
}
`)
}
func testAccInfobloxNSGroupStubCommentLeadingTrailingSpaces(name string) string {
return fmt.Sprintf(`
resource "infoblox_ns_group_stub" "acctest" {
name = "%s"
comment = " Infoblox Terraform Acceptance test "
}
`, name)
}
func testAccInfobloxNSGroupStubCreateTemplate(name string) string {
return fmt.Sprintf(`
resource "infoblox_ns_group_stub" "acctest" {
name = "%s"
comment = "Infoblox Terraform Acceptance test"
stub_members = [
{
name = "grid-member01.example.com"
},
{
name = "grid-member02.example.com"
},
{
name = "grid-member03.example.com"
},
]
}
`, name)
}
func testAccInfobloxNSGroupStubUpdateTemplate(name string) string {
return fmt.Sprintf(`
resource "infoblox_ns_group_stub" "acctest" {
name = "%s"
comment = "Infoblox Terraform Acceptance test - updated"
stub_members = [
{
name = "grid-member01.example.com"
},
{
name = "grid-member02.example.com"
},
{
name = "grid-member03.example.com"
},
{
name = "grid-member04.example.com"
},
]
}
`, name)
}
*/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4ad3266

Please sign in to comment.