Skip to content

Commit

Permalink
wait_online: add data source
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 452be9c commit fe8157f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
41 changes: 41 additions & 0 deletions ceph/data_source_wait_online.go
Original file line number Diff line number Diff line change
@@ -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")
}
8 changes: 5 additions & 3 deletions ceph/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
22 changes: 22 additions & 0 deletions docs/data-sources/wait_online.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## Schema

### Read-Only

- `id` (String) The ID of this resource.


0 comments on commit fe8157f

Please sign in to comment.