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

Dev/aamstutz/add instances #809

Merged
merged 3 commits into from
Jan 24, 2025
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
13 changes: 13 additions & 0 deletions .cds/terraform-provider-ovh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,18 @@ workflow:
pipeline: terraform-provider-ovh-testacc
when:
- success
Tests_TestAccCloudProjectInstance:
application: terraform-provider-ovh
depends_on:
- terraform-provider-ovh-pre-sweepers
environment: acctests
one_at_a_time: true
parameters:
testargs: -run CloudProjectInstance
skipthispipeline: "{{.workflow.terraform-provider-ovh.pip.skipthistest.cloudproject}}"
pipeline: terraform-provider-ovh-testacc
when:
- success
Tests_TestAccCloudProjectWorkflowBackup:
application: terraform-provider-ovh
depends_on:
Expand Down Expand Up @@ -807,6 +819,7 @@ workflow:
- Tests_CloudProjectUser
- Tests_CloudProjectLoadBalancer
- Tests_CloudProjectVolume
- Tests_TestAccCloudProjectInstance
- Tests_DbaasLogs
- Tests_DedicatedCeph
- Tests_TestAccDedicatedInstallationTemplate
Expand Down
145 changes: 145 additions & 0 deletions ovh/data_cloud_project_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
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,
},
"instance_id": {
Type: schema.TypeString,
Description: "Instance id",
Required: true,
},
// computed
"addresses": {
Type: schema.TypeSet,
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.TypeSet,
Computed: true,
Description: "Volumes attached to the instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
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: "Instance name",
Computed: true,
},
"image_id": {
Type: schema.TypeString,
Description: "Image id",
Computed: true,
},
"ssh_key": {
Type: schema.TypeString,
Description: "SSH Key pair name",
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)

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)
}

addresses := make([]map[string]interface{}, 0, len(res.Addresses))
for _, addr := range res.Addresses {
address := make(map[string]interface{})
address["ip"] = addr.Ip
address["version"] = addr.Version
addresses = append(addresses, address)
}

attachedVolumes := make([]map[string]interface{}, 0, len(res.AttachedVolumes))
for _, volume := range res.AttachedVolumes {
attachedVolume := make(map[string]interface{})
attachedVolume["id"] = volume.Id
attachedVolumes = append(attachedVolumes, attachedVolume)
}

d.Set("addresses", addresses)
d.Set("flavor_id", res.FlavorId)
d.Set("flavor_name", res.FlavorName)
d.SetId(res.Id)
d.Set("image_id", res.ImageId)
d.Set("instance_id", res.Id)
d.Set("name", res.Name)
d.Set("ssh_key", res.SshKey)
d.Set("task_state", res.TaskState)
d.Set("attached_volumes", attachedVolumes)

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 TestAccDataSourceCloudProjectInstance_basic(t *testing.T) {
config := fmt.Sprintf(`
data "ovh_cloud_project_instance" "test" {
service_name = "%s"
region = "%s"
instance_id = "%s"
}
`,
os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_INSTANCE_ID_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", "flavor_id"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "id"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "image_id"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "name"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "ssh_key"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_instance.test", "region"),
),
},
},
})
}
140 changes: 140 additions & 0 deletions ovh/data_cloud_project_instances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package ovh

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

"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,
},
// computed
"instances": {
Type: schema.TypeSet,
Description: "List of instances",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"addresses": {
Type: schema.TypeSet,
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{
"id": {
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: "Instance 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: "SSH Key pair name",
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{}, 0, len(res))
ids := make([]string, 0, len(res))
for _, instance := range res {
instances = append(instances, instance.ToMap())
ids = append(ids, instance.Id)
}
sort.Strings(ids)

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

log.Printf("[DEBUG] Read instances: %s", ids)
return nil
}
Loading