Skip to content

Commit

Permalink
project: add provider config to connect to a ceph cluster
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 23, 2022
1 parent 1c82205 commit eaaaa1f
Show file tree
Hide file tree
Showing 6 changed files with 677 additions and 0 deletions.
83 changes: 83 additions & 0 deletions ceph/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ceph

import (
"fmt"
"io/ioutil"
"os"

"github.com/ceph/go-ceph/rados"
)

type Config struct {
ConfigPath string
Username string
Cluster string
Keyring string
MonHost string
}

func (config Config) GetCephConnection() (*rados.Conn, error) {
var conn *rados.Conn
var err error

if config.Username != "" && config.Cluster != "" {
conn, err = rados.NewConnWithClusterAndUser(config.Cluster, config.Username)
} else if config.Username != "" {
conn, err = rados.NewConnWithUser(config.Username)
} else {
conn, err = rados.NewConn()
}
if err != nil {
conn.Shutdown()
return nil, err
}

configPath := config.ConfigPath
if config.Keyring != "" {
if config.MonHost == "" {
conn.Shutdown()
return nil, fmt.Errorf("Error creating Ceph connection: keyring specified while mon_host is not")
}

keyringFile, err := ioutil.TempFile("", "terraform-provider-ceph")
if err != nil {
return nil, err
}
defer os.Remove(keyringFile.Name())
_, err = keyringFile.WriteString(config.Keyring)
if err != nil {
return nil, err
}

cephConfigFile, err := ioutil.TempFile("", "terraform-provider-ceph")
if err != nil {
return nil, err
}
defer os.Remove(cephConfigFile.Name())
cephConfig := fmt.Sprintf(`
[global]
mon host = %s
keyring = %s
`, config.MonHost, keyringFile.Name())
_, err = cephConfigFile.WriteString(cephConfig)
if err != nil {
return nil, err
}
}

if configPath != "" {
err = conn.ReadConfigFile(configPath)
} else {
err = conn.ReadDefaultConfigFile()
}
if err != nil {
conn.Shutdown()
return nil, err
}

err = conn.Connect()
if err != nil {
conn.Shutdown()
}
return conn, err
}
53 changes: 53 additions & 0 deletions ceph/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ceph

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"config_path": {
Type: schema.TypeString,
Optional: true,
Description: "Path to the ceph config",
},
"username": {
Type: schema.TypeString,
Optional: true,
Description: "The cephx username to use to connect to Ceph (i.e.: client.admin).",
},
"cluster": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the Ceph cluster to use.",
},
"keyring": {
Type: schema.TypeString,
Optional: true,
Description: "The actual keyring (not a path to a file), to use to connect to Ceph. " +
"Using this ignore `config_path` and you must also specify `mon_host`",
},
"mon_host": {
Type: schema.TypeString,
Optional: true,
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,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := &Config{
ConfigPath: d.Get("config_path").(string),
Username: d.Get("username").(string),
Cluster: d.Get("cluster").(string),
Keyring: d.Get("keyring").(string),
MonHost: d.Get("mon_host").(string),
}

return config, nil
}
24 changes: 24 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "ceph Provider"
subcategory: ""
description: |-
---

# ceph Provider





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

### Optional

- `cluster` (String) The name of the Ceph cluster to use.
- `config_path` (String) Path to the ceph config
- `keyring` (String) The actual keyring (not a path to a file), to use to connect to Ceph. Using this ignore `config_path` and you must also specify `mon_host`
- `mon_host` (String) List of mon to connect to Ceph. This is only used with `keyring`, otherwise it is ignored.
- `username` (String) The cephx username to use to connect to Ceph (i.e.: client.admin).
70 changes: 70 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module gitlab.cern.ch/ceph/terraform/terraform-provider-ceph

go 1.17

require (
github.com/ceph/go-ceph v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.13.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.3 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.5.0 // indirect
github.com/hashicorp/hc-install v0.3.2 // indirect
github.com/hashicorp/hcl/v2 v2.11.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.16.1 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-docs v0.10.1 // indirect
github.com/hashicorp/terraform-plugin-go v0.8.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.3.0 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20220131103327-5c1c5e123275 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/cli v1.1.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)
Loading

0 comments on commit eaaaa1f

Please sign in to comment.