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 Oct 9, 2024
1 parent ada25ad commit 1af54b1
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 168 deletions.
42 changes: 28 additions & 14 deletions ovh/data_cloud_project_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ func dataSourceCloudProjectInstance() *schema.Resource {
Type: schema.TypeString,
Description: "Instance region",
Required: true,
ForceNew: true,
},
"instance_id": {
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 +50,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 +107,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 +119,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)

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

for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
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)

log.Printf("[DEBUG] Read instance: %+v", res)
return nil
}
2 changes: 1 addition & 1 deletion ovh/data_cloud_project_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAccDataSourceCloudProjecInstance_basic(t *testing.T) {
testAccDataSourceCloudProjectInstance,
os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_INSTANCE_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_INSTANCE_ID_TEST"),
)

resource.Test(t, resource.TestCase{
Expand Down
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
54 changes: 26 additions & 28 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,10 +90,9 @@ func resourceCloudProjectInstance() *schema.Resource {
ForceNew: true,
},
"flavor": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
Description: "Flavor information",
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"flavor_id": {
Expand All @@ -103,7 +104,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 +125,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 +134,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 +166,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 +182,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 +201,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 +247,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 +257,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 +333,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
12 changes: 9 additions & 3 deletions ovh/resource_cloud_project_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestAccCloudProjectInstance_basic(t *testing.T) {
var testCreateLoadBalancerLogSubscription = fmt.Sprintf(`
var testCreateInstance = fmt.Sprintf(`
resource "ovh_cloud_project_instance" "instance" {
service_name = "%s"
region = "%s"
Expand All @@ -20,7 +20,7 @@ func TestAccCloudProjectInstance_basic(t *testing.T) {
flavor {
flavor_id = "%s"
}
name = "haproxy"
name = "%s"
ssh_key {
name = "%s"
}
Expand All @@ -33,6 +33,7 @@ func TestAccCloudProjectInstance_basic(t *testing.T) {
os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_IMAGE_ID_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_FLAVOR_ID_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_INSTANCE_NAME_TEST"),
os.Getenv("OVH_CLOUD_PROJECT_SSH_NAME_TEST"))

resource.Test(t, resource.TestCase{
Expand All @@ -43,9 +44,14 @@ func TestAccCloudProjectInstance_basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testCreateLoadBalancerLogSubscription,
Config: testCreateInstance,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("ovh_cloud_project_instance.instance", "id"),
resource.TestCheckResourceAttrSet("ovh_cloud_project_instance.instance", "flavor_name"),
resource.TestCheckResourceAttr("ovh_cloud_project_instance.instance", "flavor_id", os.Getenv("OVH_CLOUD_PROJECT_FLAVOR_ID_TEST")),
resource.TestCheckResourceAttr("ovh_cloud_project_instance.instance", "image_id", os.Getenv("OVH_CLOUD_PROJECT_IMAGE_ID_TEST")),
resource.TestCheckResourceAttr("ovh_cloud_project_instance.instance", "region", os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST")),
resource.TestCheckResourceAttr("ovh_cloud_project_instance.instance", "name", os.Getenv("OVH_CLOUD_PROJECT_INSTANCE_NAME_TEST")),
),
},
},
Expand Down
Loading

0 comments on commit 1af54b1

Please sign in to comment.