diff --git a/ceph/data_source_wait_online.go b/ceph/data_source_wait_online.go new file mode 100644 index 0000000..fc63a19 --- /dev/null +++ b/ceph/data_source_wait_online.go @@ -0,0 +1,41 @@ +package ceph + +import ( + "context" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +const ( + maxDuration = time.Hour + sleepDuration = time.Minute +) + +func dataSourceWaitOnline() *schema.Resource { + return &schema.Resource{ + Description: "This dummy resource is waiting to Ceph to be online for up to 1 hour. " + + "This is useful for example on a boostrap procedure.", + ReadContext: dataSourceWaitOnlineRead, + Schema: map[string]*schema.Schema{}, + } +} + +func dataSourceWaitOnlineRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + config := meta.(*Config) + startTime := time.Now() + + for time.Since(startTime) < maxDuration { + _, err := config.GetCephConnection() + log.Printf("[DEBUG] Cannot connect to Ceph on ceph_wait_online: %s", err) + if err == nil { + log.Printf("[DEBUG] Ceph online on ceph_wait_online") + return nil + } + time.Sleep(sleepDuration) + } + + return diag.Errorf("Error time out after trying to connect to Ceph many times") +} diff --git a/ceph/provider.go b/ceph/provider.go index cb38e99..c7fda87 100644 --- a/ceph/provider.go +++ b/ceph/provider.go @@ -34,9 +34,11 @@ func Provider() *schema.Provider { Description: "List of mon to connect to Ceph. This is only used with `keyring`, otherwise it is ignored.", }, }, - DataSourcesMap: map[string]*schema.Resource{}, - ResourcesMap: map[string]*schema.Resource{}, - ConfigureFunc: providerConfigure, + DataSourcesMap: map[string]*schema.Resource{ + "ceph_wait_online": dataSourceWaitOnline(), + }, + ResourcesMap: map[string]*schema.Resource{}, + ConfigureFunc: providerConfigure, } } diff --git a/docs/data-sources/wait_online.md b/docs/data-sources/wait_online.md new file mode 100644 index 0000000..f438572 --- /dev/null +++ b/docs/data-sources/wait_online.md @@ -0,0 +1,22 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "ceph_wait_online Data Source - terraform-provider-ceph" +subcategory: "" +description: |- + This dummy resource is waiting to Ceph to be online for up to 1 hour. This is useful for example on a boostrap procedure. +--- + +# ceph_wait_online (Data Source) + +This dummy resource is waiting to Ceph to be online for up to 1 hour. This is useful for example on a boostrap procedure. + + + + +## Schema + +### Read-Only + +- `id` (String) The ID of this resource. + +