Skip to content

Commit

Permalink
wait_online: convert to resource to properly use retry
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@cern.ch>
  • Loading branch information
MrFreezeex committed Jun 24, 2022
1 parent cd95566 commit 8a7df0b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 67 deletions.
41 changes: 0 additions & 41 deletions ceph/data_source_wait_online.go

This file was deleted.

7 changes: 3 additions & 4 deletions ceph/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ func Provider() *schema.Provider {
Description: "List of mon to connect to Ceph.",
},
},
DataSourcesMap: map[string]*schema.Resource{
"ceph_wait_online": dataSourceWaitOnline(),
},
DataSourcesMap: map[string]*schema.Resource{},
ResourcesMap: map[string]*schema.Resource{
"ceph_auth": resourceAuth(),
"ceph_wait_online": resourceWaitOnline(),
"ceph_auth": resourceAuth(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
65 changes: 65 additions & 0 deletions ceph/resource_wait_online.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ceph

import (
"context"
"fmt"
"log"
"time"

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

func resourceWaitOnline() *schema.Resource {
return &schema.Resource{
Description: "This dummy resource is waiting to Ceph to be online at creation time for up to 1 hour. " +
"This is useful for example on a boostrap procedure.",
CreateContext: resourceWaitOnlineCreate,
ReadContext: resourceWaitOnlineDummy,
DeleteContext: resourceWaitOnlineDummy,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(time.Hour),
},
Schema: map[string]*schema.Schema{
"cluster_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "That's a workaround to actually have an id, set this to something unique (i.e.: the cluster name).",
},

"online": {
Type: schema.TypeBool,
Computed: true,
Description: "If the cluster is online, only checked at creationg time (always true)",
},
},
}
}

func resourceWaitOnlineCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
log.Printf("[DEBUG] Ceph starting ceph_wait_online")

err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
_, err := config.GetCephConnection()
if err == nil {
log.Printf("[DEBUG] Ceph online on ceph_wait_online")
d.SetId(d.Get("cluster_name").(string))
if err := d.Set("online", true); err != nil {
return resource.NonRetryableError(fmt.Errorf("Unable to set online: %s", err))
}
return nil
}

log.Printf("[DEBUG] Cannot connect to Ceph on ceph_wait_online: %s", err)
return resource.RetryableError(err)
})

return diag.FromErr(err)
}

func resourceWaitOnlineDummy(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
22 changes: 0 additions & 22 deletions docs/data-sources/wait_online.md

This file was deleted.

38 changes: 38 additions & 0 deletions docs/resources/wait_online.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "ceph_wait_online Resource - terraform-provider-ceph"
subcategory: ""
description: |-
This dummy resource is waiting to Ceph to be online at creation time for up to 1 hour. This is useful for example on a boostrap procedure.
---

# ceph_wait_online (Resource)

This dummy resource is waiting to Ceph to be online at creation time for up to 1 hour. This is useful for example on a boostrap procedure.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `cluster_name` (String) That's a workaround to actually have an id, set this to something unique (i.e.: the cluster name).

### Optional

- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `id` (String) The ID of this resource.
- `online` (Boolean) If the cluster is online, only checked at creationg time (always true)

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String)


0 comments on commit 8a7df0b

Please sign in to comment.