Skip to content

Commit e9eb04d

Browse files
authored
Merge pull request #141 from yanndegat/feat/me/ipxeScript
feat: add me/ipxeScript datasources and resource
2 parents 4024cb9 + bf25e75 commit e9eb04d

14 files changed

+583
-26
lines changed

ovh/data_source_ovh_me_ipxe_script.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
)
9+
10+
func dataSourceMeIpxeScript() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceMeIpxeScriptRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"name": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
Description: "Name of your script",
19+
},
20+
"script": {
21+
Type: schema.TypeString,
22+
Computed: true,
23+
Description: "Content of your IPXE script",
24+
},
25+
},
26+
}
27+
}
28+
29+
// Common function with the datasource
30+
func dataSourceMeIpxeScriptRead(d *schema.ResourceData, meta interface{}) error {
31+
config := meta.(*Config)
32+
33+
ipxeScript := &MeIpxeScriptResponse{}
34+
35+
name := d.Get("name").(string)
36+
err := config.OVHClient.Get(
37+
fmt.Sprintf("/me/ipxeScript/%s", url.PathEscape(name)),
38+
ipxeScript,
39+
)
40+
if err != nil {
41+
return fmt.Errorf("Unable to find IpxeScript named %s:\n\t %q", name, err)
42+
}
43+
44+
d.SetId(ipxeScript.Name)
45+
d.Set("name", ipxeScript.Name)
46+
d.Set("script", ipxeScript.Script)
47+
return nil
48+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccMeIpxeScriptDataSource_basic(t *testing.T) {
12+
scriptName := acctest.RandomWithPrefix(test_prefix)
13+
config := fmt.Sprintf(testAccMeIpxeScriptDatasourceConfig, scriptName)
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheckCredentials(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: config,
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttr(
23+
"data.ovh_me_ipxe_script.script", "name", scriptName),
24+
resource.TestCheckResourceAttr(
25+
"data.ovh_me_ipxe_script.script", "script", "test"),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
const testAccMeIpxeScriptDatasourceConfig = `
33+
resource "ovh_me_ipxe_script" "script" {
34+
name = "%s"
35+
script = "test"
36+
}
37+
38+
data "ovh_me_ipxe_script" "script" {
39+
name = ovh_me_ipxe_script.script.name
40+
}
41+
`
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func dataSourceMeIpxeScripts() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceMeIpxeScriptsRead,
14+
Schema: map[string]*schema.Schema{
15+
// Computed
16+
"result": {
17+
Type: schema.TypeList,
18+
Computed: true,
19+
Elem: &schema.Schema{
20+
Type: schema.TypeString,
21+
},
22+
},
23+
},
24+
}
25+
}
26+
27+
// Common function with the datasource
28+
func dataSourceMeIpxeScriptsRead(d *schema.ResourceData, meta interface{}) error {
29+
config := meta.(*Config)
30+
31+
ids := []string{}
32+
err := config.OVHClient.Get("/me/ipxeScript", &ids)
33+
34+
if err != nil {
35+
return fmt.Errorf("Error calling /me/ipxeScript:\n\t %q", err)
36+
}
37+
38+
// sort.Strings sorts in place, returns nothing
39+
sort.Strings(ids)
40+
41+
d.SetId(hashcode.Strings(ids))
42+
d.Set("result", ids)
43+
return nil
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccMeIpxeScriptsDataSource_basic(t *testing.T) {
12+
scriptName := acctest.RandomWithPrefix(test_prefix)
13+
presetup := fmt.Sprintf(
14+
testAccMeIpxeScriptsDatasourceConfig_presetup,
15+
scriptName,
16+
)
17+
config := fmt.Sprintf(
18+
testAccMeIpxeScriptsDatasourceConfig_Basic,
19+
scriptName,
20+
scriptName,
21+
)
22+
23+
resource.Test(t, resource.TestCase{
24+
PreCheck: func() { testAccPreCheckCredentials(t) },
25+
Providers: testAccProviders,
26+
Steps: []resource.TestStep{
27+
{
28+
Config: presetup,
29+
Check: resource.TestCheckResourceAttr(
30+
"ovh_me_ipxe_script.script",
31+
"name",
32+
scriptName,
33+
),
34+
},
35+
{
36+
Config: config,
37+
Check: resource.ComposeTestCheckFunc(
38+
resource.TestCheckResourceAttrSet(
39+
"data.ovh_me_ipxe_scripts.scripts",
40+
"result.#",
41+
),
42+
resource.TestCheckOutput(
43+
"check",
44+
"true",
45+
),
46+
),
47+
},
48+
},
49+
})
50+
}
51+
52+
const testAccMeIpxeScriptsDatasourceConfig_presetup = `
53+
resource "ovh_me_ipxe_script" "script" {
54+
name = "%s"
55+
script = "test"
56+
}
57+
`
58+
59+
const testAccMeIpxeScriptsDatasourceConfig_Basic = `
60+
resource "ovh_me_ipxe_script" "script" {
61+
name = "%s"
62+
script = "test"
63+
}
64+
65+
data "ovh_me_ipxe_scripts" "scripts" {}
66+
67+
output check {
68+
value = tostring(contains(data.ovh_me_ipxe_scripts.scripts.result, "%s"))
69+
}
70+
`

ovh/import_me_ipxe_script_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccMeIpxeScript_importBasic(t *testing.T) {
12+
resourceName := "ovh_me_ipxe_script.script"
13+
script := acctest.RandomWithPrefix(test_prefix)
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheckCredentials(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: fmt.Sprintf(testAccMeIpxeScriptConfig_import, script),
21+
},
22+
{
23+
ResourceName: resourceName,
24+
ImportState: true,
25+
ImportStateVerify: true,
26+
},
27+
},
28+
})
29+
}
30+
31+
const testAccMeIpxeScriptConfig_import = `
32+
resource "ovh_me_ipxe_script" "script" {
33+
name = "%s"
34+
script = "test"
35+
}
36+
`

ovh/provider.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func Provider() terraform.ResourceProvider {
5353
"ovh_iploadbalancing_vrack_networks": dataSourceIpLoadbalancingVrackNetworks(),
5454
"ovh_me_installation_template": dataSourceMeInstallationTemplate(),
5555
"ovh_me_installation_templates": dataSourceMeInstallationTemplates(),
56+
"ovh_me_ipxe_script": dataSourceMeIpxeScript(),
57+
"ovh_me_ipxe_scripts": dataSourceMeIpxeScripts(),
5658
"ovh_me_paymentmean_bankaccount": dataSourceMePaymentmeanBankaccount(),
5759
"ovh_me_paymentmean_creditcard": dataSourceMePaymentmeanCreditcard(),
5860
"ovh_me_ssh_key": dataSourceMeSshKey(),
@@ -71,26 +73,27 @@ func Provider() terraform.ResourceProvider {
7173
"ovh_cloud_network_private": resourceCloudNetworkPrivate(),
7274
"ovh_cloud_network_private_subnet": resourceCloudNetworkPrivateSubnet(),
7375
"ovh_cloud_user": resourceCloudUser(),
74-
"ovh_dedicated_server_update": resourceDedicatedServerUpdate(),
7576
"ovh_dedicated_server_install_task": resourceDedicatedServerInstallTask(),
7677
"ovh_dedicated_server_reboot_task": resourceDedicatedServerRebootTask(),
78+
"ovh_dedicated_server_update": resourceDedicatedServerUpdate(),
7779
"ovh_domain_zone_record": resourceOvhDomainZoneRecord(),
7880
"ovh_domain_zone_redirection": resourceOvhDomainZoneRedirection(),
7981
"ovh_ip_reverse": resourceOvhIpReverse(),
80-
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
81-
"ovh_iploadbalancing_tcp_farm_server": resourceIpLoadbalancingTcpFarmServer(),
82-
"ovh_iploadbalancing_tcp_frontend": resourceIpLoadbalancingTcpFrontend(),
8382
"ovh_iploadbalancing_http_farm": resourceIpLoadbalancingHttpFarm(),
8483
"ovh_iploadbalancing_http_farm_server": resourceIpLoadbalancingHttpFarmServer(),
8584
"ovh_iploadbalancing_http_frontend": resourceIpLoadbalancingHttpFrontend(),
8685
"ovh_iploadbalancing_http_route": resourceIPLoadbalancingRouteHTTP(),
8786
"ovh_iploadbalancing_http_route_rule": resourceIPLoadbalancingRouteHTTPRule(),
8887
"ovh_iploadbalancing_refresh": resourceIPLoadbalancingRefresh(),
88+
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
89+
"ovh_iploadbalancing_tcp_farm_server": resourceIpLoadbalancingTcpFarmServer(),
90+
"ovh_iploadbalancing_tcp_frontend": resourceIpLoadbalancingTcpFrontend(),
8991
"ovh_iploadbalancing_vrack_network": resourceIPLoadbalancingVrackNetwork(),
9092
"ovh_me_installation_template": resourceMeInstallationTemplate(),
9193
"ovh_me_installation_template_partition_scheme": resourceMeInstallationTemplatePartitionScheme(),
92-
"ovh_me_installation_template_partition_scheme_partition": resourceMeInstallationTemplatePartitionSchemePartition(),
9394
"ovh_me_installation_template_partition_scheme_hardware_raid": resourceMeInstallationTemplatePartitionSchemeHardwareRaid(),
95+
"ovh_me_installation_template_partition_scheme_partition": resourceMeInstallationTemplatePartitionSchemePartition(),
96+
"ovh_me_ipxe_script": resourceMeIpxeScript(),
9497
"ovh_me_ssh_key": resourceMeSshKey(),
9598
"ovh_vrack_cloudproject": resourceVrackCloudProject(),
9699
"ovh_vrack_dedicated_server": resourceVrackDedicatedServer(),

ovh/resource_ovh_me_ipxe_script.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/url"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func resourceMeIpxeScript() *schema.Resource {
12+
return &schema.Resource{
13+
Create: resourceMeIpxeScriptCreate,
14+
Read: resourceMeIpxeScriptRead,
15+
Delete: resourceMeIpxeScriptDelete,
16+
17+
Importer: &schema.ResourceImporter{
18+
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
19+
return []*schema.ResourceData{d}, nil
20+
},
21+
},
22+
23+
Schema: map[string]*schema.Schema{
24+
"name": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
ForceNew: true,
28+
Description: "Name of your script",
29+
},
30+
"description": {
31+
Type: schema.TypeString,
32+
Optional: true,
33+
Computed: true,
34+
Description: "For documentation purpose only. This attribute is not passed to the OVH API as it cannot be retrieved back. Instead a fake description ('$name auto description') is passed at creation time.",
35+
},
36+
"script": {
37+
Type: schema.TypeString,
38+
Required: true,
39+
ForceNew: true,
40+
Description: "Content of your IPXE script",
41+
},
42+
},
43+
}
44+
}
45+
46+
// Common function with the datasource
47+
func resourceMeIpxeScriptRead(d *schema.ResourceData, meta interface{}) error {
48+
config := meta.(*Config)
49+
50+
ipxeScript := &MeIpxeScriptResponse{}
51+
52+
id := d.Id()
53+
err := config.OVHClient.Get(
54+
fmt.Sprintf("/me/ipxeScript/%s", url.PathEscape(id)),
55+
ipxeScript,
56+
)
57+
if err != nil {
58+
return fmt.Errorf("Unable to find IpxeScript named %s:\n\t %q", id, err)
59+
}
60+
61+
d.Set("name", ipxeScript.Name)
62+
d.Set("script", ipxeScript.Script)
63+
64+
return nil
65+
}
66+
67+
func resourceMeIpxeScriptCreate(d *schema.ResourceData, meta interface{}) error {
68+
config := meta.(*Config)
69+
70+
name := d.Get("name").(string)
71+
script := d.Get("script").(string)
72+
73+
params := &MeIpxeScriptCreateOpts{
74+
Description: fmt.Sprintf("%s auto description", name),
75+
Name: name,
76+
Script: script,
77+
}
78+
79+
response := &MeIpxeScriptResponse{}
80+
81+
log.Printf("[DEBUG] Will create IpxeScript: %s", params)
82+
83+
err := config.OVHClient.Post("/me/ipxeScript", params, response)
84+
if err != nil {
85+
return fmt.Errorf("Error creating IpxeScript with params %s:\n\t %q", params, err)
86+
}
87+
88+
d.SetId(response.Name)
89+
90+
return resourceMeIpxeScriptRead(d, meta)
91+
}
92+
93+
func resourceMeIpxeScriptDelete(d *schema.ResourceData, meta interface{}) error {
94+
config := meta.(*Config)
95+
96+
err := config.OVHClient.Delete(
97+
fmt.Sprintf("/me/ipxeScript/%s", url.PathEscape(d.Id())),
98+
nil,
99+
)
100+
if err != nil {
101+
return fmt.Errorf("Unable to delete IpxeScript named %s:\n\t %q", d.Id(), err)
102+
}
103+
104+
log.Printf("[DEBUG] Deleted IpxeScript %s", d.Id())
105+
d.SetId("")
106+
return nil
107+
}

0 commit comments

Comments
 (0)