Skip to content

Commit

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

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

func resourceNSGroupForward() *schema.Resource {
return &schema.Resource{
Create: resourceNSGroupForwardCreate,
Read: resourceNSGroupForwardRead,
Update: resourceNSGroupForwardUpdate,
Delete: resourceNSGroupForwardDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the name server group",
Required: true,
},
"comment": {
Type: schema.TypeString,
Description: "Comment field",
Optional: true,
ValidateFunc: util.CheckLeadingTrailingSpaces,
},
"forwarding_servers": util.ForwardingMemberServerListSchema(),
},
}
}

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

var nsGroupForwardObject nsgroupfwd.NSGroupFwd
client := m.(*skyinfoblox.InfobloxClient)

if v, ok := d.GetOk("name"); ok && v != "" {
nsGroupForwardObject.Name = v.(string)
}
if v, ok := d.GetOk("comment"); ok && v != "" {
nsGroupForwardObject.Comment = v.(string)
}
if v, ok := d.GetOk("forwarding_servers"); ok && v != nil {
forwardingServerList := util.GetMapList(v.([]interface{}))
nsGroupForwardObject.ForwardingServers = util.BuildForwardingMemberServerListFromT(forwardingServerList)
}

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

nsGroupForwardObject.Reference = *createNSGroupForwardAPI.ResponseObject().(*string)
d.SetId(nsGroupForwardObject.Reference)
return resourceNSGroupForwardRead(d, m)
}

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

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

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

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

var nsGroupForwardObject nsgroupfwd.NSGroupFwd
hasChanges := false

if d.HasChange("name") {
if v, ok := d.GetOk("name"); ok && v != "" {
nsGroupForwardObject.Name = v.(string)
}
hasChanges = true
}
if d.HasChange("comment") {
if v, ok := d.GetOk("comment"); ok && v != "" {
nsGroupForwardObject.Comment = v.(string)
}
hasChanges = true
}
if d.HasChange("forwarding_servers") {
if v, ok := d.GetOk("forwarding_servers"); ok && v != nil {
forwardingServerList := util.GetMapList(v.([]interface{}))
nsGroupForwardObject.ForwardingServers = util.BuildForwardingMemberServerListFromT(forwardingServerList)
}
hasChanges = true
}

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

nsGroupForwardUpdateAPI := nsgroupfwd.NewUpdate(nsGroupForwardObject, nsgroupfwd.RequestReturnFields)
err := client.Do(nsGroupForwardUpdateAPI)
httpStatus := nsGroupForwardUpdateAPI.StatusCode()
if err != nil || httpStatus < http.StatusOK || httpStatus >= http.StatusBadRequest {
return fmt.Errorf("Infoblox NS Group Forward update for %s failed with status code %d and error: %+v", nsGroupForwardObject.Name, httpStatus, string(nsGroupForwardUpdateAPI.RawResponse()))
}
response := *nsGroupForwardUpdateAPI.ResponseObject().(*nsgroupfwd.NSGroupFwd)
d.SetId(response.Reference)
d.Set("name", response.Name)
d.Set("comment", response.Comment)
d.Set("forwarding_servers", util.BuildForwardingMemberServerListFromIBX(response.ForwardingServers))
}

return resourceNSGroupForwardRead(d, m)
}

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

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

nsGroupForwardAPI := nsgroupfwd.NewDelete(reference)
err := client.Do(nsGroupForwardAPI)
httpStatus := nsGroupForwardAPI.StatusCode()
if httpStatus == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil || httpStatus < http.StatusOK || httpStatus >= http.StatusBadRequest {
return fmt.Errorf("Infoblox NS Group Forward delete for %s failed with status code %d and error: %+v", reference, httpStatus, nsGroupForwardAPI.RawResponse())
}
d.SetId("")
return nil
}
Loading

0 comments on commit 17b86a9

Please sign in to comment.