Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shared credential information from gscloud #176

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module github.com/terraform-providers/terraform-provider-gridscale
require (
github.com/gridscale/gsclient-go/v3 v3.6.3
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/sirupsen/logrus v1.8.1 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
gopkg.in/yaml.v2 v2.4.0
)

go 1.16
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfE
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.11.2 h1:MiK62aErc3gIiVEtyzKfeOHgW7atJb5g/KNX5m3c2nQ=
github.com/klauspost/compress v1.11.2/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
Expand Down Expand Up @@ -588,6 +590,8 @@ gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRN
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
31 changes: 31 additions & 0 deletions gridscale/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package gridscale

import (
"context"
"errors"
"log"
"os"

"github.com/gridscale/gsclient-go/v3"
"gopkg.in/yaml.v2"
)

//Arrays can't be constants in Go, but these will be used as constants
Expand Down Expand Up @@ -76,3 +78,32 @@ func (c *Config) Client() (*gsclient.Client, error) {

return client, err
}

// GSCloudAccountEntry represents a single account in the config file of gscloud.
type GSCloudAccountEntry struct {
Name string `yaml:"name"`
UserID string `yaml:"userId"`
Token string `yaml:"token"`
URL string `yaml:"url"`
}

// GSCloudConfig are all configuration settings parsed from a configuration file of gscloud.
type GSCloudConfig struct {
Accounts []GSCloudAccountEntry `yaml:"accounts"`
}

func getGSCloudConfigFromPath(configFile string) (GSCloudConfig, error) {
config := GSCloudConfig{}
fileContent, err := os.ReadFile(configFile)
if err != nil {
return GSCloudConfig{}, err
}
err = yaml.Unmarshal(fileContent, &config)
if err != nil {
return GSCloudConfig{}, err
}
if len(config.Accounts) == 0 {
return GSCloudConfig{}, errors.New("no configuration in the config file.")
}
return config, nil
}
35 changes: 32 additions & 3 deletions gridscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package gridscale

import (
"fmt"
"log"
"runtime"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/kirsle/configdir"
)

var (
Expand Down Expand Up @@ -34,6 +36,11 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("GRIDSCALE_URL", nil),
Description: "the url for the gridscale API.",
},
"config_file": {
Type: schema.TypeString,
Optional: true,
Description: "Path of the compatible config file, e.g., gscloud's config file. Note: Only the first configured account is used.",
},
"http_headers": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -114,10 +121,32 @@ func Provider() *schema.Provider {
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
headers := convertStrToHeaderMap(d.Get("http_headers").(string))
headers["User-Agent"] = fmt.Sprintf("terraform-provider-gridscale/%s-%s-%s", version, commit, runtime.GOOS)
uuid := d.Get("uuid").(string)
token := d.Get("token").(string)
apiURL := d.Get("api_url").(string)
if uuid == "" && token == "" {
log.Println("uuid, token are not set. Getting authentication information from gscloud config file...")
var configFile string
configFileInf, ok := d.GetOk("config_file")
if !ok {
configFile = configdir.LocalConfig("gscloud") + "/config.yaml"
} else {
configFile = configFileInf.(string)
}
log.Printf("Reading gscloud config from \"%s\"\n", configFile)
gscloudCfg, err := getGSCloudConfigFromPath(configFile)
if err != nil {
return nil, err
}
// get the 1st gscloud account config to use for tf config.
firstGSCloudAccount := gscloudCfg.Accounts[0]
uuid, token, apiURL = firstGSCloudAccount.UserID, firstGSCloudAccount.Token, firstGSCloudAccount.URL
}

config := Config{
UserUUID: d.Get("uuid").(string),
APIToken: d.Get("token").(string),
APIUrl: d.Get("api_url").(string),
UserUUID: uuid,
APIToken: token,
APIUrl: apiURL,
DelayIntMs: d.Get("request_delay_interval").(int),
MaxNRetries: d.Get("max_n_retries").(int),
HTTPHeaders: headers,
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/kirsle/configdir/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions vendor/github.com/kirsle/configdir/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/kirsle/configdir/config_darwin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/kirsle/configdir/config_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/kirsle/configdir/config_xdg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading