Skip to content
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

Add resource & data source instance #696

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
135 changes: 135 additions & 0 deletions ovh/data_cloud_project_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package ovh

import (
"fmt"
"log"
"net/url"

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

func dataSourceCloudProjectInstance() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectInstanceRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
},
"region": {
Type: schema.TypeString,
Description: "Instance region",
Required: true,
ForceNew: true,
amstuta marked this conversation as resolved.
Show resolved Hide resolved
},
"instance_id": {
Type: schema.TypeString,
Description: "Instance id",
Required: true,
ForceNew: true,
amstuta marked this conversation as resolved.
Show resolved Hide resolved
},
// computed
"addresses": {
Type: schema.TypeList,
amstuta marked this conversation as resolved.
Show resolved Hide resolved
Computed: true,
Description: "Instance IP addresses",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Description: "IP address",
Computed: true,
},
"version": {
Type: schema.TypeInt,
Description: "IP version",
Computed: true,
},
},
},
},
"attached_volumes": {
Type: schema.TypeList,
amstuta marked this conversation as resolved.
Show resolved Hide resolved
Computed: true,
Description: " Volumes attached to the instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
amstuta marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Description: "Volume Id",
Computed: true,
},
},
},
},
"flavor_id": {
Type: schema.TypeString,
Description: "Flavor id",
Computed: true,
},
"flavor_name": {
Type: schema.TypeString,
Description: "Flavor name",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "Flavor name",
Computed: true,
},
"id": {
Type: schema.TypeString,
Description: "Instance id",
Computed: true,
},
"image_id": {
Type: schema.TypeString,
Description: "Image id",
Computed: true,
},
"ssh_key": {
Type: schema.TypeString,
Description: "Instance task state",
Computed: true,
},
"task_state": {
Type: schema.TypeString,
Description: "Instance task state",
Computed: true,
},
},
}
}

func dataSourceCloudProjectInstanceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
region := d.Get("region").(string)
instanceId := d.Get("instance_id").(string)
log.Printf("[DEBUG] SCROUTCH")
amstuta marked this conversation as resolved.
Show resolved Hide resolved
endpoint := fmt.Sprintf("/cloud/project/%s/region/%s/instance/%s",
url.PathEscape(serviceName),
url.PathEscape(region),
url.PathEscape(instanceId),
)
var res CloudProjectInstanceResponse

log.Printf("[DEBUG] Will read instance %s from project %s in region %s", instanceId, serviceName, region)
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}

for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}

log.Printf("[DEBUG] Read instance: %+v", res)
return nil
}
46 changes: 46 additions & 0 deletions ovh/data_cloud_project_instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ovh

import (
"fmt"
"os"
"testing"

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

func TestAccDataSourceCloudProjecInstance_basic(t *testing.T) {

config := fmt.Sprintf(
testAccDataSourceCloudProjectInstance,
os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_INSTANCE_TEST"),
)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckCloud(t)
testAccCheckCloudProjectExists(t)
},

Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "flavor_name"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "id"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "image_id"),
),
},
},
})
}

var testAccDataSourceCloudProjectInstance = `
data "ovh_cloud_project_instance" "test" {
service_name = "%s"
region = "%s"
instance_id = "%s"
}
`
139 changes: 139 additions & 0 deletions ovh/data_cloud_project_instances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package ovh

import (
"fmt"
"log"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)

func dataSourceCloudProjectInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectInstancesRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
},
"region": {
Type: schema.TypeString,
Description: "Instance region",
Required: true,
ForceNew: true,
},
// computed
"instances": {
Type: schema.TypeList,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Type: schema.TypeList,
Type: schema.TypeSet,

(unless you know it's sorted)

Description: "List of instances",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"addresses": {
Type: schema.TypeList,
Computed: true,
Description: "Instance IP addresses",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Description: "IP address",
Computed: true,
},
"version": {
Type: schema.TypeInt,
Description: "IP version",
Computed: true,
},
},
},
},
"attached_volumes": {
Type: schema.TypeList,
Computed: true,
Description: " Volumes attached to the instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
amstuta marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Description: "Volume Id",
Computed: true,
},
},
},
},
"flavor_id": {
Type: schema.TypeString,
Description: "Flavor id",
Computed: true,
},
"flavor_name": {
Type: schema.TypeString,
Description: "Flavor name",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "Flavor name",
Computed: true,
},
"id": {
Type: schema.TypeString,
Description: "Instance id",
Computed: true,
},
"image_id": {
Type: schema.TypeString,
Description: "Image id",
Computed: true,
},
"ssh_key": {
Type: schema.TypeString,
Description: "Instance task state",
Computed: true,
},
"task_state": {
Type: schema.TypeString,
Description: "Instance task state",
Computed: true,
},
},
},
},
},
}
}

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

endpoint := fmt.Sprintf("/cloud/project/%s/region/%s/instance",
url.PathEscape(serviceName),
url.PathEscape(region),
)
var res []CloudProjectInstanceResponse

log.Printf("[DEBUG] Will read instances from project %s in region %s", serviceName, region)
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
instances := make([]map[string]interface{}, len(res))
ids := make([]string, len(instances))

for i, instance := range res {
instances[i] = instance.ToMap()
ids = append(ids, instance.Id)
}

d.SetId(hashcode.Strings(ids))
d.Set("instances", instances)

log.Printf("[DEBUG] Read instances: %+v", res)
Copy link
Contributor

Choose a reason for hiding this comment

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

that could end up being a long log, maybe we should just log the ids ?

return nil
}
47 changes: 47 additions & 0 deletions ovh/data_cloud_project_instances_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ovh

import (
"fmt"
"os"
"testing"

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

const testAccDataSourceCloudProjecInstancesConfig_basic = `
data "ovh_cloud_project_instances" "instances" {
service_name = "%s"
region = "%s"
}
`

func TestAccDataSourceCloudProjecInstances_basic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
region := os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST")

config := fmt.Sprintf(
testAccDataSourceCloudProjecInstancesConfig_basic,
serviceName,
region,
)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckCloud(t)
testAccCheckCloudProjectExists(t)
},

Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.ovh_cloud_project_instances.instances",
"instances.#",
),
),
},
},
})
}
3 changes: 3 additions & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func Provider() *schema.Provider {
"ovh_cloud_project_database_user": dataSourceCloudProjectDatabaseUser(),
"ovh_cloud_project_database_users": dataSourceCloudProjectDatabaseUsers(),
"ovh_cloud_project_failover_ip_attach": dataSourceCloudProjectFailoverIpAttach(),
"ovh_cloud_project_instance": dataSourceCloudProjectInstance(),
"ovh_cloud_project_instances": dataSourceCloudProjectInstances(),
"ovh_cloud_project_kube": dataSourceCloudProjectKube(),
"ovh_cloud_project_kube_iprestrictions": dataSourceCloudProjectKubeIPRestrictions(),
"ovh_cloud_project_kube_nodepool_nodes": dataSourceCloudProjectKubeNodepoolNodes(),
Expand Down Expand Up @@ -196,6 +198,7 @@ func Provider() *schema.Provider {
"ovh_cloud_project_database_user": resourceCloudProjectDatabaseUser(),
"ovh_cloud_project_failover_ip_attach": resourceCloudProjectFailoverIpAttach(),
"ovh_cloud_project_gateway": resourceCloudProjectGateway(),
"ovh_cloud_project_instance": resourceCloudProjectInstance(),
"ovh_cloud_project_kube": resourceCloudProjectKube(),
"ovh_cloud_project_kube_nodepool": resourceCloudProjectKubeNodePool(),
"ovh_cloud_project_kube_oidc": resourceCloudProjectKubeOIDC(),
Expand Down
Loading