Skip to content
Open
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
61 changes: 61 additions & 0 deletions ovh/data_vrack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ovh

import (
"fmt"

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

func dataSourceVrack() *schema.Resource {
return &schema.Resource{
Read: dataSourceVrackRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
},
// Here come all the computed items
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"iam": {
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
},
}
}

func dataSourceVrackRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
vrack := &Vrack{}
err := config.OVHClient.Get(
fmt.Sprintf(
"/vrack/%s",
serviceName,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serviceName,
url.PathEscape(serviceName),

),
&vrack,
)

if err != nil {
return fmt.Errorf("Error calling /vrack/%s:\n\t %q", serviceName, err)
}

d.SetId(serviceName)
d.Set("name", vrack.Name)
d.Set("description", vrack.Description)
iam := make(map[string]string)
iam["id"] = vrack.IamResourceDetails.Id
iam["urn"] = vrack.IamResourceDetails.URN
d.Set("iam", iam)
return nil
}
42 changes: 42 additions & 0 deletions ovh/data_vrack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ovh

import (
"fmt"
"os"
"testing"

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

func TestAccVrackDataSource_basic(t *testing.T) {
vrack := os.Getenv("OVH_VRACK_SERVICE_TEST")
config := fmt.Sprintf(testAccVrackDatasourceConfig_Basic, vrack)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckVrack(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ovh_vrack.vrack", "id", vrack),
resource.TestCheckResourceAttr(
"data.ovh_vrack.vrack", "service_name", vrack),
resource.TestCheckResourceAttrSet(
"data.ovh_vrack.vrack", "iam"),
resource.TestCheckResourceAttrSet(
"data.ovh_vrack.vrack", "name"),
resource.TestCheckResourceAttrSet(
"data.ovh_vrack.vrack", "description"),
),
},
},
})
}

const testAccVrackDatasourceConfig_Basic = `
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move this definition in the test func itself to avoid the global variable ?

data "ovh_vrack" "vrack" {
service_name = "%s"
}
`
1 change: 1 addition & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func Provider() *schema.Provider {
"ovh_vps": dataSourceVPS(),
"ovh_vpss": dataSourceVPSs(),
"ovh_vracks": dataSourceVracks(),
"ovh_vrack": dataSourceVrack(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
5 changes: 5 additions & 0 deletions ovh/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@ func testAccPreCheckOkmsCredential(t *testing.T) {
checkEnvOrSkip(t, "OVH_OKMS_CREDENTIAL")
}

func testAccPreCheckVrack(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed, there is already the testAccPreCheckVRack func that does the same thing

testAccPreCheckCredentials(t)
checkEnvOrSkip(t, "OVH_VRACK_SERVICE_TEST")
}

func testAccCheckVRackExists(t *testing.T) {
type vrackResponse struct {
Name string `json:"name"`
Expand Down