Skip to content

new datasource: ovh_dedicated_server_boots #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions ovh/data_source_ovh_dedicated_server_boots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ovh

import (
"fmt"
"net/url"
"sort"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceDedicatedServerBoots() *schema.Resource {
return &schema.Resource{
Read: dataSourceDedicatedServerBootsRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "The internal name of your dedicated server.",
Required: true,
},

"boot_type": {
Type: schema.TypeString,
Optional: true,
Description: "Filter the value of bootType property",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := validateBootType(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},

// Computed
"result": {
Type: schema.TypeList,
Computed: true,
Description: "Server compatibles netboots ids",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
}
}

func dataSourceDedicatedServerBootsRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)

ids := []int{}

endpoint := fmt.Sprintf(
"/dedicated/server/%s/boot",
url.PathEscape(serviceName),
)

if bootType, ok := d.GetOk("boot_type"); ok {
endpoint = fmt.Sprintf(
"%s?bootType=%s",
endpoint,
url.PathEscape(bootType.(string)),
)
}

if err := config.OVHClient.Get(endpoint, &ids); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}

// setting id by computing a hashcode of all the ids
idsStr := make([]string, len(ids))
for i, id := range ids {
idsStr[i] = strconv.Itoa(id)
}

// sort.Strings sorts in place, returns nothing
sort.Strings(idsStr)

d.SetId(hashcode.Strings(idsStr))
d.Set("result", ids)
return nil
}
70 changes: 70 additions & 0 deletions ovh/data_source_ovh_dedicated_server_boots_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ovh

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDedicatedServerBootsDataSource_basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckCredentials(t)
testAccPreCheckDedicatedServer(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDedicatedServerBootsDatasourceConfig(""),
Check: resource.TestCheckOutput("test", "true"),
},
{
Config: testAccDedicatedServerBootsDatasourceConfig("harddisk"),
Check: resource.TestCheckOutput("test", "true"),
},
{
Config: testAccDedicatedServerBootsDatasourceConfig("rescue"),
Check: resource.TestCheckOutput("test", "true"),
},
{
Config: testAccDedicatedServerBootsDatasourceConfig("network"),
Check: resource.TestCheckOutput("test", "true"),
},
},
})
}

func testAccDedicatedServerBootsDatasourceConfig(filter string) string {
dedicated_server := os.Getenv("OVH_DEDICATED_SERVER")
if filter == "" {
return fmt.Sprintf(
testAccDedicatedServerBootsDatasourceConfig_Basic,
dedicated_server,
)
}
return fmt.Sprintf(
testAccDedicatedServerBootsDatasourceConfig_Filter,
dedicated_server,
filter,
)

}

const testAccDedicatedServerBootsDatasourceConfig_Basic = `
data "ovh_dedicated_server_boots" "boots" {
service_name = "%s"
}

output test { value = tostring(length(data.ovh_dedicated_server_boots.boots.result) > 0 )}
`
const testAccDedicatedServerBootsDatasourceConfig_Filter = `
data "ovh_dedicated_server_boots" "boots" {
service_name = "%s"
boot_type = "%s"
}

output test { value = tostring(length(data.ovh_dedicated_server_boots.boots.result) > 0 )}
`
10 changes: 10 additions & 0 deletions ovh/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func validateStringEnum(value string, enum []string) error {
return nil
}

func validateBootType(value string) error {
return validateStringEnum(value, []string{
"harddisk",
"internal",
"ipxeCustomerScript",
"network",
"rescue",
})
}

func getNilBoolPointer(val interface{}) *bool {
if val == nil {
return nil
Expand Down
3 changes: 2 additions & 1 deletion ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ func Provider() terraform.ResourceProvider {
"ovh_cloud_region": dataSourcePublicCloudRegion(),
"ovh_cloud_regions": dataSourcePublicCloudRegions(),
"ovh_dedicated_installation_templates": dataSourceDedicatedInstallationTemplates(),
"ovh_dedicated_servers": dataSourceDedicatedServers(),
"ovh_dedicated_server": dataSourceDedicatedServer(),
"ovh_dedicated_server_boots": dataSourceDedicatedServerBoots(),
"ovh_dedicated_servers": dataSourceDedicatedServers(),
"ovh_domain_zone": dataSourceDomainZone(),
"ovh_iploadbalancing": dataSourceIpLoadbalancing(),
"ovh_me_paymentmean_bankaccount": dataSourceMePaymentmeanBankaccount(),
Expand Down
32 changes: 32 additions & 0 deletions website/docs/d/dedicated_server_boots.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
layout: "ovh"
page_title: "OVH: dedicated_server_boots"
sidebar_current: "docs-ovh-datasource-dedicated-server-boots"
description: |-
Get the list of compatible netboots for a dedicated server associated with your OVH Account.
---

# ovh_me_dedicated_server_boots

Use this data source to get the list of compatible netboots for a dedicated server associated with your OVH Account.

## Example Usage

```hcl
data "ovh_dedicated_server_boots" "netboots" {
service_name = "myserver"
boot_type = "harddisk"
}
```

## Argument Reference

* `service_name` - (Required) The internal name of your dedicated server.

* `boot_type` - (Optional) Filter the value of bootType property (harddisk, rescue, ipxeCustomerScript, internal, network)

## Attributes Reference

The following attributes are exported:

* `result` - The list of dedicated server netboots.
12 changes: 7 additions & 5 deletions website/ovh.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% wrap_layout :inner do %>
<% wrap_layout :inner do %>
<% content_for :sidebar do %>
<div class="docs-sidebar hidden-print affix-top" role="complementary">
<ul class="nav docs-sidenav">
Expand All @@ -22,12 +22,15 @@
<li<%= sidebar_current("docs-ovh-datasource-dedicated-installation-templates") %>>
<a href="/docs/providers/ovh/d/dedicated_installation_templates.html">ovh_dedicated_installation_templates</a>
</li>
<li<%= sidebar_current("docs-ovh-datasource-dedicated-servers") %>>
<a href="/docs/providers/ovh/d/dedicated_servers.html">ovh_dedicated_servers</a>
</li>
<li<%= sidebar_current("docs-ovh-datasource-dedicated-server-x") %>>
<a href="/docs/providers/ovh/d/dedicated_server.html">ovh_dedicated_server</a>
</li>
<li<%= sidebar_current("docs-ovh-datasource-dedicated-server-boots") %>>
<a href="/docs/providers/ovh/d/dedicated_server_boots.html">ovh_dedicated_server_boots</a>
</li>
<li<%= sidebar_current("docs-ovh-datasource-dedicated-servers") %>>
<a href="/docs/providers/ovh/d/dedicated_servers.html">ovh_dedicated_servers</a>
</li>
<li<%= sidebar_current("docs-ovh-datasource-domain-zone") %>>
<a href="/docs/providers/ovh/d/domain_zone.html">ovh_domain_zone</a>
</li>
Expand Down Expand Up @@ -77,7 +80,6 @@
<li<%= sidebar_current("docs-ovh-resource-cloud-user") %>>
<a href="/docs/providers/ovh/r/cloud_user.html">ovh_cloud_user</a>
</li>

<li<%= sidebar_current("docs-ovh-resource-publiccloud-private-network-x") %>>
<a href="/docs/providers/ovh/r/publiccloud_private_network.html">ovh_publiccloud_private_network</a>
</li>
Expand Down