-
Notifications
You must be signed in to change notification settings - Fork 135
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 #234 from albeego/master
Adding failover IP address data and resource blocks for lookup and at…
- Loading branch information
Showing
12 changed files
with
528 additions
and
1 deletion.
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,14 @@ | ||
package ovh | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceCloudProjectFailoverIpAttach() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: func(d *schema.ResourceData, meta interface{}) error { | ||
return resourceCloudProjectFailoverIpAttachRead(d, meta) | ||
}, | ||
Schema: resourceCloudProjectFailoverIpAttachSchema(), | ||
} | ||
} |
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,57 @@ | ||
package ovh | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const testAccDataSourceCloudProjectFailoverIpAttach = ` | ||
data "ovh_cloud_project_failover_ip_attach" "myfailoverip" { | ||
service_name = "%s" | ||
ip = "%s" | ||
} | ||
` | ||
|
||
func TestAccDataSourceCloudProjectFailoverIpAttach(t *testing.T) { | ||
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST") | ||
ipAddress := os.Getenv("OVH_CLOUD_PROJECT_FAILOVER_IP_TEST") | ||
config := fmt.Sprintf( | ||
testAccDataSourceCloudProjectFailoverIpAttach, | ||
serviceName, | ||
ipAddress, | ||
) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheckFailoverIpAttach(t) }, | ||
|
||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"data.ovh_cloud_project_failover_ip_attach.myfailoverip", | ||
"service_name", | ||
serviceName, | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.ovh_cloud_project_failover_ip_attach.myfailoverip", | ||
"ip", | ||
ipAddress, | ||
), | ||
resource.TestCheckResourceAttrSet( | ||
"data.ovh_cloud_project_failover_ip_attach.myfailoverip", | ||
"id", | ||
), | ||
resource.TestCheckResourceAttrSet( | ||
"data.ovh_cloud_project_failover_ip_attach.myfailoverip", | ||
"routed_to", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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,216 @@ | ||
package ovh | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/ovh/terraform-provider-ovh/ovh/helpers" | ||
"log" | ||
"net/url" | ||
) | ||
|
||
func resourceCloudProjectFailoverIpAttach() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCloudProjectFailoverIpAttachCreate, | ||
Read: resourceCloudProjectFailoverIpAttachRead, | ||
Delete: resourceCloudProjectFailoverIpAttachDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
return []*schema.ResourceData{d}, nil | ||
}, | ||
}, | ||
|
||
Schema: resourceCloudProjectFailoverIpAttachSchema(), | ||
} | ||
} | ||
|
||
func resourceCloudProjectFailoverIpAttachSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"service_name": { | ||
Type: schema.TypeString, | ||
Description: "The service name", | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
|
||
"block": { | ||
Type: schema.TypeString, | ||
Description: "IP block", | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
err := helpers.ValidateIp(v.(string)) | ||
if err != nil { | ||
errors = append(errors, err) | ||
} | ||
return | ||
}, | ||
}, | ||
"continent_code": { | ||
Type: schema.TypeString, | ||
Description: "Ip continent", | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"geo_loc": { | ||
Type: schema.TypeString, | ||
Description: "Ip location", | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "Ip id", | ||
Computed: true, | ||
}, | ||
"ip": { | ||
Type: schema.TypeString, | ||
Description: "Ip", | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
err := helpers.ValidateIp(v.(string)) | ||
if err != nil { | ||
errors = append(errors, err) | ||
} | ||
return | ||
}, | ||
}, | ||
"progress": { | ||
Type: schema.TypeInt, | ||
Description: "Current operation progress in percent", | ||
Computed: true, | ||
}, | ||
"routed_to": { | ||
Type: schema.TypeString, | ||
Description: "Instance where ip is routed to", | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Description: "Ip status", | ||
Computed: true, | ||
}, | ||
"sub_type": { | ||
Type: schema.TypeString, | ||
Description: "IP sub type", | ||
Computed: true, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCloudProjectFailoverIpAttachRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
serviceName := d.Get("service_name").(string) | ||
|
||
log.Printf("[DEBUG] Will read cloud project ip addresses %s", serviceName) | ||
endpoint := fmt.Sprintf("/cloud/project/%s/ip/failover", | ||
url.PathEscape(serviceName), | ||
) | ||
|
||
ips := []FailoverIp{} | ||
if err := config.OVHClient.Get(endpoint, &ips); err != nil { | ||
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err) | ||
} | ||
|
||
match := false | ||
for _, ip := range ips { | ||
if ip.Ip == d.Get("ip").(string) { | ||
for k, v := range ip.ToMap() { | ||
match = true | ||
if k == "id" { | ||
d.SetId(v.(string)) | ||
} else { | ||
err := d.Set(k, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if !match { | ||
return fmt.Errorf("your query returned no results, " + | ||
"please change your search criteria and try again") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudProjectFailoverIpAttachCreate(d *schema.ResourceData, meta interface{}) error { | ||
|
||
serviceName := d.Get("service_name").(string) | ||
|
||
config := meta.(*Config) | ||
|
||
//Fetch Failover IP address to populate ID field | ||
log.Printf("[DEBUG] Will read cloud project ip addresses %s", serviceName) | ||
endpoint := fmt.Sprintf("/cloud/project/%s/ip/failover", | ||
url.PathEscape(serviceName), | ||
) | ||
|
||
ips := []FailoverIp{} | ||
if err := config.OVHClient.Get(endpoint, &ips); err != nil { | ||
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err) | ||
} | ||
|
||
match := false | ||
for _, ip := range ips { | ||
if ip.Ip == d.Get("ip").(string) { | ||
for k, v := range ip.ToMap() { | ||
match = true | ||
if k == "id" { | ||
d.SetId(v.(string)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if !match { | ||
return fmt.Errorf("your query returned no results, " + | ||
"please change your search criteria and try again") | ||
} | ||
|
||
id := d.Get("id").(string) | ||
|
||
log.Printf("[DEBUG] Will attach failover ip to an instance: %s", serviceName) | ||
opts := (&ProjectIpFailoverAttachCreation{}).FromResource(d) | ||
endpoint = fmt.Sprintf("/cloud/project/%s/ip/failover/%s/attach", | ||
url.PathEscape(serviceName), | ||
url.PathEscape(id), | ||
) | ||
|
||
ip := &FailoverIp{} | ||
if err := config.OVHClient.Post(endpoint, opts, ip); err != nil { | ||
return fmt.Errorf("calling Put %s: %q", endpoint, err) | ||
} | ||
|
||
for k, v := range ip.ToMap() { | ||
match = true | ||
if k != "id" { | ||
err := d.Set(k, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
for d.Get("status").(string) == "operationPending" { | ||
if err := resourceCloudProjectFailoverIpAttachRead(d, meta); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudProjectFailoverIpAttachDelete(d *schema.ResourceData, meta interface{}) error { | ||
// Failover IPs cannot be deleted, the best that can be done in this instance is to check it exists | ||
if err := resourceCloudProjectFailoverIpAttachRead(d, meta); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.