-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from src-d/resource-failover-ip
Add resource online_failover_ip
- Loading branch information
Showing
6 changed files
with
390 additions
and
2 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
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,43 @@ | ||
package online | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func (c *client) EditFailoverIP(source, destination string) error { | ||
target := fmt.Sprintf("%s/failover/edit", serverEndPoint) | ||
_, err := c.doPOST(target, map[string]string{ | ||
"source": source, | ||
"destination": destination, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) GenerateMACFailoverIP(address, macType string) (string, error) { | ||
target := fmt.Sprintf("%s/failover/generateMac", serverEndPoint) | ||
body, err := c.doPOST(target, map[string]string{ | ||
"address": address, | ||
"type": macType, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(body), nil | ||
} | ||
|
||
func (c *client) DeleteMACFailoverIP(address string) error { | ||
target := fmt.Sprintf("%s/failover/deleteMac", serverEndPoint) | ||
_, err := c.doPOST(target, map[string]string{ | ||
"address": address, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package provider | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/src-d/terraform-provider-online-net/online" | ||
) | ||
|
||
func resourceFailoverIP() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: resourceFailoverIPRead, | ||
Create: resourceFailoverIPCreate, | ||
Delete: resourceFailoverIPDelete, | ||
Update: resourceFailoverIPUpdate, | ||
Schema: map[string]*schema.Schema{ | ||
"ip": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "the failover IP to modify", | ||
}, | ||
"destination_server_id": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Description: "The ID of the server to route the failover IP to", | ||
ConflictsWith: []string{"destination_server_ip"}, | ||
}, | ||
"destination_server_ip": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The ID of the server to route the failover IP to", | ||
ConflictsWith: []string{"destination_server_id"}, | ||
}, | ||
"generate_mac": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Description: "should a virtual mac be created for the IP", | ||
}, | ||
"generate_mac_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "should a virtual mac be created for the IP", | ||
Default: "kvm", | ||
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { | ||
v := val.(string) | ||
if v != "vmware" && v != "xen" && v != "kvm" && v != "" { | ||
errs = append(errs, errors.New("generate_mac_type must be either vmware, xen or kvm")) | ||
} | ||
return | ||
}, | ||
}, | ||
"mac": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
Description: "the generated mac", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceFailoverIPRead(d *schema.ResourceData, meta interface{}) error { | ||
ip := d.Get("ip").(string) | ||
d.SetId(ip) | ||
|
||
return nil | ||
} | ||
|
||
func resourceFailoverIPCreate(d *schema.ResourceData, meta interface{}) error { | ||
c := meta.(online.Client) | ||
|
||
ip := d.Get("ip").(string) | ||
serverIDInterface, hasServerID := d.GetOk("destination_server_id") | ||
serverIPInterface, hasServerIP := d.GetOk("destination_server_ip") | ||
generateMac := d.Get("generate_mac").(bool) | ||
macType := d.Get("generate_mac_type").(string) | ||
|
||
dstIP := "" | ||
|
||
if hasServerIP { | ||
dstIP = serverIPInterface.(string) | ||
} | ||
|
||
if hasServerID { | ||
server, err := c.Server(serverIDInterface.(int)) | ||
if err != nil { | ||
return err | ||
} | ||
dstIP = server.InterfaceByType(online.Public).Address | ||
} | ||
|
||
err := c.EditFailoverIP(ip, dstIP) | ||
if err != nil && !strings.Contains(err.Error(), "Address already provisioned") { | ||
return err | ||
} | ||
|
||
if generateMac { | ||
mac, err := c.GenerateMACFailoverIP(ip, macType) | ||
if err != nil { | ||
return err | ||
} | ||
d.Set("mac", mac) | ||
} | ||
|
||
d.SetId(ip) | ||
return nil | ||
} | ||
|
||
func resourceFailoverIPDelete(d *schema.ResourceData, meta interface{}) error { | ||
ip := d.Get("ip").(string) | ||
_, macExists := d.GetOkExists("mac") | ||
c := meta.(online.Client) | ||
|
||
if macExists { | ||
err := c.DeleteMACFailoverIP(ip) | ||
if err != nil { | ||
return err | ||
} | ||
d.Set("mac", "") | ||
} | ||
|
||
err := c.EditFailoverIP(ip, "") | ||
return err | ||
} | ||
|
||
func resourceFailoverIPUpdate(d *schema.ResourceData, meta interface{}) error { | ||
ip := d.Get("ip").(string) | ||
c := meta.(online.Client) | ||
|
||
hasNewMACRequest := d.HasChange("generate_mac") | ||
|
||
hasNewServerID := d.HasChange("destination_server_id") | ||
hasNewServerIP := d.HasChange("destination_server_ip") | ||
|
||
if hasNewServerID && hasNewServerIP { | ||
// we switched here! | ||
_, hasdID := d.GetOkExists("destination_server_id") | ||
_, hasIP := d.GetOkExists("destination_server_ip") | ||
serverID := d.Get("destination_server_id").(int) | ||
|
||
// make sure the next steps will use the correct key | ||
if hasdID && serverID != 0 { | ||
hasNewServerIP = false | ||
} else if hasIP { | ||
hasNewServerID = false | ||
} | ||
} | ||
|
||
if hasNewServerID { | ||
serverID := d.Get("destination_server_id").(int) | ||
server, err := c.Server(serverID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dstIP := server.InterfaceByType(online.Public).Address | ||
err = c.EditFailoverIP(ip, dstIP) | ||
if err != nil { | ||
return err | ||
} | ||
} else if hasNewServerIP { | ||
dstIP := d.Get("destination_server_ip").(string) | ||
err := c.EditFailoverIP(ip, dstIP) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if hasNewMACRequest { | ||
// we need to enable or disable the generated MAC | ||
_, newValue := d.GetChange("generate_mac") | ||
if newValue.(bool) { | ||
macType := d.Get("generate_mac_type").(string) | ||
mac, err := c.GenerateMACFailoverIP(ip, macType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set("mac", mac) | ||
} else { | ||
err := c.DeleteMACFailoverIP(ip) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.