Skip to content

Commit

Permalink
datasource & resource instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaut Di Prima committed Sep 30, 2024
1 parent ada25ad commit 808b25c
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 161 deletions.
41 changes: 28 additions & 13 deletions ovh/data_cloud_project_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ func dataSourceCloudProjectInstance() *schema.Resource {
Type: schema.TypeString,
Description: "Instance id",
Required: true,
ForceNew: true,
},
// computed
"addresses": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Description: "Instance IP addresses",
Elem: &schema.Resource{
Expand All @@ -52,12 +51,12 @@ func dataSourceCloudProjectInstance() *schema.Resource {
},
},
"attached_volumes": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Description: " Volumes attached to the instance",
Description: "Volumes attached to the instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
"id": {
Type: schema.TypeString,
Description: "Volume Id",
Computed: true,
Expand Down Expand Up @@ -109,7 +108,7 @@ func dataSourceCloudProjectInstanceRead(d *schema.ResourceData, meta interface{}
serviceName := d.Get("service_name").(string)
region := d.Get("region").(string)
instanceId := d.Get("instance_id").(string)
log.Printf("[DEBUG] SCROUTCH")

endpoint := fmt.Sprintf("/cloud/project/%s/region/%s/instance/%s",
url.PathEscape(serviceName),
url.PathEscape(region),
Expand All @@ -121,15 +120,31 @@ func dataSourceCloudProjectInstanceRead(d *schema.ResourceData, meta interface{}
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
log.Printf("[DEBUG] Read instance: %+v", res)

for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
addresses := make([]map[string]interface{}, 0)
for i := range res.Addresses {
address := make(map[string]interface{})
address["ip"] = res.Addresses[i].Ip
address["version"] = res.Addresses[i].Version
addresses = append(addresses, address)
}

log.Printf("[DEBUG] Read instance: %+v", res)
attachedVolumes := make([]map[string]interface{}, 0)
for i := range res.AttachedVolumes {
attachedVolume := make(map[string]interface{})
attachedVolume["id"] = res.AttachedVolumes[i].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)

return nil
}
11 changes: 6 additions & 5 deletions ovh/data_cloud_project_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/url"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
Expand All @@ -24,7 +25,6 @@ func dataSourceCloudProjectInstances() *schema.Resource {
Type: schema.TypeString,
Description: "Instance region",
Required: true,
ForceNew: true,
},
// computed
"instances": {
Expand All @@ -34,7 +34,7 @@ func dataSourceCloudProjectInstances() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"addresses": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Description: "Instance IP addresses",
Elem: &schema.Resource{
Expand All @@ -58,7 +58,7 @@ func dataSourceCloudProjectInstances() *schema.Resource {
Description: " Volumes attached to the instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
"id": {
Type: schema.TypeString,
Description: "Volume Id",
Computed: true,
Expand Down Expand Up @@ -123,13 +123,14 @@ func dataSourceCloudProjectInstancesRead(d *schema.ResourceData, meta interface{
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))

instances := make([]map[string]interface{}, len(res))
ids := make([]string, len(res))
for i, instance := range res {
instances[i] = instance.ToMap()
ids = append(ids, instance.Id)
}
sort.Strings(ids)

d.SetId(hashcode.Strings(ids))
d.Set("instances", instances)
Expand Down
53 changes: 26 additions & 27 deletions ovh/resource_cloud_project_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

func resourceCloudProjectInstance() *schema.Resource {
Expand All @@ -34,7 +35,7 @@ func resourceCloudProjectInstance() *schema.Resource {
ForceNew: true,
},
"auto_backup": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "Create an autobackup workflow after instance start up",
ForceNew: true,
Expand All @@ -43,26 +44,27 @@ func resourceCloudProjectInstance() *schema.Resource {
"cron": {
Type: schema.TypeString,
Description: "Unix cron pattern",
Optional: true,
ForceNew: true,
Required: true,
},
"rotation": {
Type: schema.TypeInt,
Description: "Number of backup to keep",
Optional: true,
ForceNew: true,
Required: true,
},
},
},
},
"billing_period": {
Type: schema.TypeString,
Description: "Number of backup to keep",
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Billing period - hourly | monthly ",
Required: true,
ForceNew: true,
ValidateFunc: helpers.ValidateEnum([]string{"monthly", "hourly"}),
},
"boot_from": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
Description: "Boot the instance from an image or a volume",
ForceNew: true,
Expand All @@ -88,7 +90,7 @@ func resourceCloudProjectInstance() *schema.Resource {
ForceNew: true,
},
"flavor": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
Description: "Flavor information",
ForceNew: true,
Expand All @@ -103,7 +105,7 @@ func resourceCloudProjectInstance() *schema.Resource {
},
},
"group": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "Start instance in group",
ForceNew: true,
Expand All @@ -124,7 +126,7 @@ func resourceCloudProjectInstance() *schema.Resource {
ForceNew: true,
},
"ssh_key": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "Existing SSH Keypair",
ForceNew: true,
Expand All @@ -133,27 +135,27 @@ func resourceCloudProjectInstance() *schema.Resource {
"name": {
Type: schema.TypeString,
Description: "SSH Keypair name",
Optional: true,
Required: true,
},
},
},
},
"ssh_key_create": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "Start instance in group",
Description: "Creatting SSH Keypair",
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "SSH Keypair name",
Optional: true,
Required: true,
},
"public_key": {
Type: schema.TypeString,
Description: "SSH Public key",
Optional: true,
Description: "Group id",
Required: true,
},
},
},
Expand All @@ -165,17 +167,12 @@ func resourceCloudProjectInstance() *schema.Resource {
ForceNew: true,
},
"network": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Description: "Create network interfaces",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"private": {
Type: schema.TypeString,
Description: "Private network information",
Optional: true,
},
"public": {
Type: schema.TypeBool,
Description: "Set the new instance as public",
Expand All @@ -186,7 +183,7 @@ func resourceCloudProjectInstance() *schema.Resource {
},
// computed
"addresses": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Description: "Instance IP addresses",
Elem: &schema.Resource{
Expand All @@ -205,7 +202,7 @@ func resourceCloudProjectInstance() *schema.Resource {
},
},
"attached_volumes": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Description: " Volumes attached to the instance",
Elem: &schema.Resource{
Expand Down Expand Up @@ -251,7 +248,8 @@ func resourceCloudProjectInstanceCreate(ctx context.Context, d *schema.ResourceD
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
region := d.Get("region").(string)
params := (&CloudProjectInstanceCreateOpts{}).FromResource(d)
params := new(CloudProjectInstanceCreateOpts)
params.FromResource(d)

r := &CloudProjectOperation{}

Expand All @@ -260,6 +258,8 @@ func resourceCloudProjectInstanceCreate(ctx context.Context, d *schema.ResourceD
url.PathEscape(region),
)

log.Printf("[DEBUG] params ------- %+vv", params)

if err := config.OVHClient.Post(endpoint, params, r); err != nil {
return diag.Errorf("calling %s with params %v:\n\t %q", endpoint, params, err)
}
Expand Down Expand Up @@ -334,7 +334,6 @@ func resourceCloudProjectInstanceRead(ctx context.Context, d *schema.ResourceDat
d.Set("region", r.Region)
d.Set("task_state", r.TaskState)
d.Set("name", d.Get("name").(string))
// d.Set("name", r.Name)
d.Set("id", r.Id)

addresses := make([]map[string]interface{}, 0)
Expand Down
4 changes: 4 additions & 0 deletions ovh/resource_cloud_project_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func TestAccCloudProjectInstance_basic(t *testing.T) {
Config: testCreateLoadBalancerLogSubscription,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("ovh_cloud_project_instance.instance", "id"),
resource.TestCheckResourceAttrSet("ovh_cloud_project_instance.instance", "flavor_name"),
resource.TestCheckResourceAttrSet("ovh_cloud_project_instance.instance", "flavor_id"),
resource.TestCheckResourceAttrSet("ovh_cloud_project_instance.instance", "image_id"),
resource.TestCheckResourceAttrSet("ovh_cloud_project_instance.instance", "name"),
),
},
},
Expand Down
Loading

0 comments on commit 808b25c

Please sign in to comment.