Skip to content

Commit

Permalink
Merge pull request #39 from ans-group/config
Browse files Browse the repository at this point in the history
Allow config context to be overridden in provider config
  • Loading branch information
0x4c6565 authored Dec 1, 2022
2 parents 4a81f1f + b54b2ff commit 6e4ae3e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
9 changes: 7 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ provider "ecloud" {
}
resource "ecloud_vpc" "vpc-1" {
region_id = "reg-abcdef12"
region_id = "reg-abcdef12"
}
```

## Argument Reference

* `api_key`: UKFast API key - read/write permissions for `ecloud` service required. If omitted, will use `ANS_API_KEY` environment variable value
* `context`: Config context to use (overrides current context)
* `api_key`: API key - read/write permissions for `ecloud` service required

## Configuration

If `api_key` is omitted from the provider config, the provider will fall back to the default configuration (file / environment). Documentation for the configuration file / environment variables can be found within the [SDK repository](https://github.com/ans-group/sdk-go#configuration-file)
35 changes: 21 additions & 14 deletions ecloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/ans-group/sdk-go/pkg/config"
"github.com/ans-group/sdk-go/pkg/connection"
"github.com/ans-group/sdk-go/pkg/logging"
ecloudservice "github.com/ans-group/sdk-go/pkg/service/ecloud"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ukfast/terraform-provider-ecloud/pkg/logger"
)
Expand All @@ -17,6 +16,11 @@ const userAgent = "terraform-provider-ecloud"
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"context": {
Type: schema.TypeString,
Optional: true,
Description: "Config context to use",
},
"api_key": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -99,18 +103,15 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
logging.SetLogger(&logger.ProviderLogger{})
}

return getService(d.Get("api_key").(string))
}

func getConnection() (connection.Connection, error) {
connFactory := connection.NewDefaultConnectionFactory(
connection.WithDefaultConnectionUserAgent(userAgent),
)

return connFactory.NewConnection()
}
context := d.Get("context").(string)
if len(context) > 0 {
err := config.SwitchCurrentContext(context)
if err != nil {
return nil, err
}
}

func getService(apiKey string) (ecloudservice.ECloudService, error) {
apiKey := d.Get("api_key").(string)
if len(apiKey) > 0 {
config.Set(config.GetCurrentContextName(), "api_key", apiKey)
}
Expand All @@ -120,7 +121,13 @@ func getService(apiKey string) (ecloudservice.ECloudService, error) {
return nil, err
}

return nil, fmt.Errorf("Test: %s", config.GetString("api_key"))

return client.NewClient(conn).ECloudService(), nil
}

func getConnection() (connection.Connection, error) {
connFactory := connection.NewDefaultConnectionFactory(
connection.WithDefaultConnectionUserAgent(userAgent),
)

return connFactory.NewConnection()
}
10 changes: 5 additions & 5 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ func (l *ProviderLogger) Error(msg string) {
}

func (l *ProviderLogger) Warn(msg string) {
l.log("ERROR", msg)
l.log("WARN", msg)
}

func (l *ProviderLogger) Info(msg string) {
l.log("ERROR", msg)
l.log("INFO", msg)
}

func (l *ProviderLogger) Debug(msg string) {
l.log("ERROR", msg)
l.log("DEBUG", msg)
}

func (l *ProviderLogger) Trace(msg string) {
l.log("ERROR", msg)
l.log("TRACE", msg)
}

func (l *ProviderLogger) log(level string, msg string) {
log.Print("[%s] %s", level, msg)
log.Printf("[%s] %s", level, msg)
}

0 comments on commit 6e4ae3e

Please sign in to comment.