Skip to content

Commit

Permalink
auth: 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 6065bf9 commit 4ee4809
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 11 deletions.
77 changes: 77 additions & 0 deletions ceph/data_source_auth.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 3 additions & 1 deletion ceph/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
18 changes: 9 additions & 9 deletions ceph/resource_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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")
}
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions docs/data-sources/auth.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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


2 changes: 1 addition & 1 deletion docs/resources/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
}

0 comments on commit 4ee4809

Please sign in to comment.