Skip to content

Commit

Permalink
Added block of code to reserve n number of IP's (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
saiprasannasastry authored and jkraj committed May 4, 2018
1 parent 2b89ec5 commit 41e0ff4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 42 deletions.
21 changes: 0 additions & 21 deletions infoblox/resourceIPAllocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ func resourceIPAllocation() *schema.Resource {
DefaultFunc: schema.EnvDefaultFunc("tenantID", nil),
Description: "Unique identifier of your tenant in cloud.",
},
"gateway": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("tenantID", nil),
Description: "gateway ip address of your network block.First IPv4 address.",
Computed: true,
},
},
}
}
Expand All @@ -83,30 +76,16 @@ func resourceIPAllocationRequest(d *schema.ResourceData, m interface{}) error {
macAddr := d.Get("mac_addr").(string)
vmID := d.Get("vm_id").(string)
tenantID := d.Get("tenant_id").(string)
gateway := d.Get("gateway").(string)
connector := m.(*ibclient.Connector)

objMgr := ibclient.NewObjectManager(connector, "terraform", tenantID)

// Check whether gateway or ip address already allocated
gatewayIP, err := objMgr.GetFixedAddress(networkViewName, cidr, gateway, "")
if err == nil && gatewayIP != nil {
fmt.Printf("Gateway alreadt created")
} else if gatewayIP == nil {
gatewayIP, err = objMgr.AllocateIP(networkViewName, cidr, gateway, "00:00:00:00:00:00", "")
if err != nil {
return fmt.Errorf("Gateway Creation failed in network block(%s) error: %s", cidr, err)
}
}

hostrecordObj, err := objMgr.CreateHostRecordWithoutDNS(recordName, networkViewName, cidr, ipAddr, macAddr, vmID)
if err != nil {
return fmt.Errorf("Error allocating IP from network block(%s): %s", cidr, err)
}

d.Set("gateway", gatewayIP.IPAddress)
d.Set("ip_addr", hostrecordObj.Ipv4Addrs[0].Ipv4Addr)
// TODO what happens in case of a VM have 2 network interfaces.
d.SetId(hostrecordObj.Ref)

log.Printf("[DEBUG] %s:completing Request of IP from required network block", resourceIPAllocationIDString(d))
Expand Down
57 changes: 36 additions & 21 deletions infoblox/resourceIPAssociation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/infobloxopen/infoblox-go-client"
"log"
"strings"
)

func resourceIPAssociation() *schema.Resource {
Expand Down Expand Up @@ -70,33 +71,18 @@ func resourceIPAssociation() *schema.Resource {
func resourceIPAssociationCreate(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] %s: Beginning Association of IP address in specified network block", resourceIPAssociationIDString(d))

networkViewName := d.Get("network_view_name").(string)
recordName := d.Get("host_name").(string)
ipAddr := d.Get("ip_addr").(string)
cidr := d.Get("cidr").(string)
macAddr := d.Get("mac_addr").(string)
tenantID := d.Get("tenant_id").(string)
vmID := d.Get("vm_id").(string)
resource(d, m)

connector := m.(*ibclient.Connector)

objMgr := ibclient.NewObjectManager(connector, "terraform", tenantID)

hostRecordObj, err := objMgr.GetHostRecordWithoutDNS(recordName, networkViewName, cidr, ipAddr)
if err != nil {
return fmt.Errorf("GetHostAddress error from network block(%s):%s", cidr, err)
}
_, err = objMgr.UpdateHostRecordWithoutDNS(hostRecordObj.Ref, ipAddr, macAddr, vmID)
if err != nil {
return fmt.Errorf("UpdateHostAddress error from network block(%s):%s", cidr, err)
}

d.SetId(hostRecordObj.Ref)
log.Printf("[DEBUG] %s:completing Association of IP address in specified network block", resourceIPAssociationIDString(d))
return nil
}

func resourceIPAssociationUpdate(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] %s:update operation on Association of IP address in specified network block", resourceIPAssociationIDString(d))

resource(d, m)

log.Printf("[DEBUG] %s:completing updation on Association of IP address in specified network block", resourceIPAssociationIDString(d))
return nil
}

Expand Down Expand Up @@ -140,3 +126,32 @@ func resourceIPAssociationIDString(d resourceIPAssociationIDStringInterface) str
}
return fmt.Sprintf("infoblox_mac_allocation (ID = %s)", id)
}

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

networkViewName := d.Get("network_view_name").(string)
recordName := d.Get("host_name").(string)
ipAddr := d.Get("ip_addr").(string)
cidr := d.Get("cidr").(string)
macAddr := d.Get("mac_addr").(string)
tenantID := d.Get("tenant_id").(string)
vmID := d.Get("vm_id").(string)

connector := m.(*ibclient.Connector)

objMgr := ibclient.NewObjectManager(connector, "terraform", tenantID)
//conversion from bit reversed EUI-48 format to hexadecimal EUI-48 format
macAddr = strings.Replace(macAddr, "-", ":", -1)

hostRecordObj, err := objMgr.GetHostRecordWithoutDNS(recordName, networkViewName, cidr, ipAddr)
if err != nil {
return fmt.Errorf("GetHostAddress error from network block(%s):%s", cidr, err)
}
_, err = objMgr.UpdateHostRecordWithoutDNS(hostRecordObj.Ref, ipAddr, macAddr, vmID)
if err != nil {
return fmt.Errorf("UpdateHostAddress error from network block(%s):%s", cidr, err)
}

d.SetId(hostRecordObj.Ref)
return nil
}
37 changes: 37 additions & 0 deletions infoblox/resourceNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ func resourceNetwork() *schema.Resource {
DefaultFunc: schema.EnvDefaultFunc("tenantID", nil),
Description: "Unique identifier of your tenant in cloud.",
},
"reserve_ip": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("reserveIP", 0),
Description: "The no of IP's you want to reserve.",
},
"gateway": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("gateway", nil),
Description: "gateway ip address of your network block.By default first
IPv4 address is set as gateway address.",
Computed: true,
},
},
}
}
Expand All @@ -49,6 +63,8 @@ func resourceNetworkCreate(d *schema.ResourceData, m interface{}) error {
networkViewName := d.Get("network_view_name").(string)
cidr := d.Get("cidr").(string)
networkName := d.Get("network_name").(string)
reserveIP := d.Get("reserve_ip").(int)
gateway := d.Get("gateway").(string)
tenantID := d.Get("tenant_id").(string)
connector := m.(*ibclient.Connector)

Expand All @@ -58,6 +74,25 @@ func resourceNetworkCreate(d *schema.ResourceData, m interface{}) error {
if err != nil {
return fmt.Errorf("Creation of network block failed in network view (%s) : %s", networkViewName, err)
}

gatewayIP, err := objMgr.GetFixedAddress(networkViewName, cidr, gateway, "")
if err == nil && gatewayIP != nil {
fmt.Printf("Gateway already created")
} else if gatewayIP == nil {
gatewayIP, err = objMgr.AllocateIP(networkViewName, cidr, gateway, "00:00:00:00:00:00", "")
if err != nil {
return fmt.Errorf("Gateway Creation failed in network block(%s) error: %s", cidr, err)
}
}

for i := 1; i <= reserveIP; i++ {
_, err = objMgr.AllocateIP(networkViewName, cidr, gateway, "00:00:00:00:00:00", "")
if err != nil {
return fmt.Errorf("Reservation in network block failed in network view(%s):%s", networkViewName, err)
}
}

d.Set("gateway", gatewayIP.IPAddress)
d.SetId(nwname.Ref)

log.Printf("[DEBUG] %s: Creation on network block complete", resourceNetworkIDString(d))
Expand Down Expand Up @@ -115,3 +150,5 @@ func resourceNetworkIDString(d resourceNetworkIDStringInterface) string {
}
return fmt.Sprintf("infoblox_ip_allocation (ID = %s)", id)
}

// Check whether gateway or ip address already allocated

0 comments on commit 41e0ff4

Please sign in to comment.