From 4ee480928481b883b1592e4432be7492e89a2285 Mon Sep 17 00:00:00 2001 From: Arthur Outhenin-Chalandre Date: Fri, 24 Jun 2022 16:08:21 +0200 Subject: [PATCH] auth: add data source Signed-off-by: Arthur Outhenin-Chalandre --- ceph/data_source_auth.go | 77 +++++++++++++++++++++++++++++++++++++++ ceph/provider.go | 4 +- ceph/resource_auth.go | 18 ++++----- docs/data-sources/auth.md | 29 +++++++++++++++ docs/resources/auth.md | 2 +- examples/simple/main.tf | 7 ++++ 6 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 ceph/data_source_auth.go create mode 100644 docs/data-sources/auth.md diff --git a/ceph/data_source_auth.go b/ceph/data_source_auth.go new file mode 100644 index 0000000..e9fabb8 --- /dev/null +++ b/ceph/data_source_auth.go @@ -0,0 +1,77 @@ +package ceph + +import ( + "context" + "encoding/json" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceAuth() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceAuthRead, + + Schema: map[string]*schema.Schema{ + "entity": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The entity name (i.e.: client.admin)", + }, + + "caps": { + Type: schema.TypeMap, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Computed: true, + Description: "The caps of the entity", + }, + + "keyring": { + Type: schema.TypeString, + + Computed: true, + Description: "The cephx keyring of the entity", + }, + + "key": { + Type: schema.TypeString, + Computed: true, + Description: "The cephx key of the entity", + }, + }, + } +} + +func dataSourceAuthRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn, err := meta.(*Config).GetCephConnection() + if err != nil { + return diag.Errorf("Unable to connect to Ceph: %s", err) + } + entity := d.Get("entity").(string) + + command, err := json.Marshal(map[string]interface{}{ + "prefix": "auth get", + "format": "json", + "entity": entity, + }) + if err != nil { + return diag.Errorf("Error data_source_auth unable to create get JSON command: %s", err) + } + + buf, _, err := conn.MonCommand(command) + if err != nil { + return diag.Errorf("Error data_source_auth on get command: %s", err) + } + + var authResponses []authResponse + err = json.Unmarshal(buf, &authResponses) + if err != nil { + return diag.Errorf("Error data_source auth unmarshal on response: %s", err) + } + + d.SetId(entity) + return setAuthResourceData(d, authResponses) +} diff --git a/ceph/provider.go b/ceph/provider.go index a0d1eac..0631af3 100644 --- a/ceph/provider.go +++ b/ceph/provider.go @@ -39,7 +39,9 @@ func Provider() *schema.Provider { Description: "List of mon to connect to Ceph.", }, }, - DataSourcesMap: map[string]*schema.Resource{}, + DataSourcesMap: map[string]*schema.Resource{ + "ceph_auth": dataSourceAuth(), + }, ResourcesMap: map[string]*schema.Resource{ "ceph_wait_online": resourceWaitOnline(), "ceph_auth": resourceAuth(), diff --git a/ceph/resource_auth.go b/ceph/resource_auth.go index 4c2dcfb..738545c 100644 --- a/ceph/resource_auth.go +++ b/ceph/resource_auth.go @@ -39,7 +39,7 @@ func resourceAuth() *schema.Resource { Type: schema.TypeString, }, Optional: true, - Description: "The caps wanted for the entity", + Description: "The caps of the entity", }, "keyring": { @@ -61,7 +61,7 @@ func resourceAuth() *schema.Resource { const clientKeyringFormat = `[%s] %s` -func setResourceData(d *schema.ResourceData, authResponses []authResponse) diag.Diagnostics { +func setAuthResourceData(d *schema.ResourceData, authResponses []authResponse) diag.Diagnostics { if len(authResponses) == 0 { return diag.Errorf("No data returned by ceph auth command") } @@ -105,7 +105,7 @@ func resourceAuthCreate(ctx context.Context, d *schema.ResourceData, meta interf "caps": toCapsArray(d.Get("caps").(map[string]interface{})), }) if err != nil { - return diag.Errorf("Unable resource_auth unable to create get-or-create JSON command: %s", err) + return diag.Errorf("Error resource_auth unable to create get-or-create JSON command: %s", err) } buf, _, err := conn.MonCommand(command) @@ -116,11 +116,11 @@ func resourceAuthCreate(ctx context.Context, d *schema.ResourceData, meta interf var authResponses []authResponse err = json.Unmarshal(buf, &authResponses) if err != nil { - return diag.Errorf("Error unmarshal on get-or-create response: %s", err) + return diag.Errorf("Error resource_auth unmarshal on get-or-create response: %s", err) } d.SetId(entity) - return setResourceData(d, authResponses) + return setAuthResourceData(d, authResponses) } func resourceAuthRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { @@ -136,7 +136,7 @@ func resourceAuthRead(ctx context.Context, d *schema.ResourceData, meta interfac "entity": entity, }) if err != nil { - return diag.Errorf("Unable resource_auth unable to create get JSON command: %s", err) + return diag.Errorf("Error resource_auth unable to create get JSON command: %s", err) } buf, _, err := conn.MonCommand(command) @@ -147,10 +147,10 @@ func resourceAuthRead(ctx context.Context, d *schema.ResourceData, meta interfac var authResponses []authResponse err = json.Unmarshal(buf, &authResponses) if err != nil { - return diag.Errorf("Error unmarshal on get response: %s", err) + return diag.Errorf("Error resource_auth unmarshal on get response: %s", err) } - return setResourceData(d, authResponses) + return setAuthResourceData(d, authResponses) } func resourceAuthUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { @@ -167,7 +167,7 @@ func resourceAuthUpdate(ctx context.Context, d *schema.ResourceData, meta interf "caps": toCapsArray(d.Get("caps").(map[string]interface{})), }) if err != nil { - return diag.Errorf("Unable resource_auth unable to create caps JSON command: %s", err) + return diag.Errorf("Error resource_auth unable to create caps JSON command: %s", err) } _, _, err = conn.MonCommand(command) diff --git a/docs/data-sources/auth.md b/docs/data-sources/auth.md new file mode 100644 index 0000000..f6b312f --- /dev/null +++ b/docs/data-sources/auth.md @@ -0,0 +1,29 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "ceph_auth Data Source - terraform-provider-ceph" +subcategory: "" +description: |- + +--- + +# ceph_auth (Data Source) + + + + + + +## Schema + +### Required + +- `entity` (String) The entity name (i.e.: client.admin) + +### Read-Only + +- `caps` (Map of String) The caps of the entity +- `id` (String) The ID of this resource. +- `key` (String) The cephx key of the entity +- `keyring` (String) The cephx keyring of the entity + + diff --git a/docs/resources/auth.md b/docs/resources/auth.md index 50d8a21..357dc29 100644 --- a/docs/resources/auth.md +++ b/docs/resources/auth.md @@ -21,7 +21,7 @@ description: |- ### Optional -- `caps` (Map of String) The caps wanted for the entity +- `caps` (Map of String) The caps of the entity ### Read-Only diff --git a/examples/simple/main.tf b/examples/simple/main.tf index 927756e..e8fe86b 100644 --- a/examples/simple/main.tf +++ b/examples/simple/main.tf @@ -27,3 +27,10 @@ resource "ceph_auth" "test" { ceph_wait_online.test ] } + +data "ceph_auth" "test-data" { + entity = "client.test" + depends_on = [ + ceph_auth.test + ] +}