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

[Provider] Rancher #9173

Merged
merged 2 commits into from
Dec 5, 2016
Merged
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
15 changes: 15 additions & 0 deletions builtin/bins/provider-rancher/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"github.com/hashicorp/terraform/builtin/providers/rancher"
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return rancher.Provider()
},
})
}
76 changes: 76 additions & 0 deletions builtin/providers/rancher/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package rancher

import (
"log"

rancherClient "github.com/rancher/go-rancher/client"
"github.com/raphink/go-rancher/catalog"
)

type Config struct {
*rancherClient.RancherClient
APIURL string
AccessKey string
SecretKey string
}

// Create creates a generic Rancher client
func (c *Config) CreateClient() error {
client, err := rancherClient.NewRancherClient(&rancherClient.ClientOpts{
Url: c.APIURL,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
})
if err != nil {
return err
}

log.Printf("[INFO] Rancher Client configured for url: %s", c.APIURL)

c.RancherClient = client

return nil
}

func (c *Config) EnvironmentClient(env string) (*rancherClient.RancherClient, error) {

url := c.APIURL + "/projects/" + env + "/schemas"
client, err := rancherClient.NewRancherClient(&rancherClient.ClientOpts{
Url: url,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
})
if err != nil {
return nil, err
}

log.Printf("[INFO] Rancher Client configured for url: %s", url)

return client, nil
}

func (c *Config) RegistryClient(id string) (*rancherClient.RancherClient, error) {
reg, err := c.Registry.ById(id)
if err != nil {
return nil, err
}

return c.EnvironmentClient(reg.AccountId)
}

func (c *Config) CatalogClient() (*catalog.RancherClient, error) {

url := c.APIURL + "-catalog/schemas"
client, err := catalog.NewRancherClient(&catalog.ClientOpts{
Url: url,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
})
if err != nil {
return nil, err
}

log.Printf("[INFO] Rancher Catalog Client configured for url: %s", url)

return client, nil
}
28 changes: 28 additions & 0 deletions builtin/providers/rancher/import_rancher_environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rancher

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccRancherEnvironment_importBasic(t *testing.T) {
resourceName := "rancher_environment.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancherEnvironmentDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancherEnvironmentConfig,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rancher

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccRancherRegistrationToken_importBasic(t *testing.T) {
resourceName := "rancher_registration_token.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancherRegistrationTokenDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancherRegistrationTokenConfig,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package rancher

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccRancherRegistryCredential_importBasic(t *testing.T) {
resourceName := "rancher_registry_credential.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancherRegistryCredentialDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancherRegistryCredentialConfig,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"secret_value"},
},
},
})
}
28 changes: 28 additions & 0 deletions builtin/providers/rancher/import_rancher_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rancher

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccRancherRegistry_importBasic(t *testing.T) {
resourceName := "rancher_registry.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancherRegistryDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancherRegistryConfig,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
28 changes: 28 additions & 0 deletions builtin/providers/rancher/import_rancher_stack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rancher

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccRancherStack_importBasic(t *testing.T) {
resourceName := "rancher_stack.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancherStackDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancherStackConfig,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
66 changes: 66 additions & 0 deletions builtin/providers/rancher/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package rancher

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"api_url": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("RANCHER_URL", nil),
Description: descriptions["api_url"],
},
"access_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("RANCHER_ACCESS_KEY", ""),
Description: descriptions["access_key"],
},
"secret_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, makes total sense if we can use them from the environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point here is that Rancher doesn't require API keys. You could have a rancher server that does not have access control enabled, so it wouldn't make sense to force the user to provide these values then.

DefaultFunc: schema.EnvDefaultFunc("RANCHER_SECRET_KEY", ""),
Description: descriptions["secret_key"],
},
},

ResourcesMap: map[string]*schema.Resource{
"rancher_environment": resourceRancherEnvironment(),
"rancher_registration_token": resourceRancherRegistrationToken(),
"rancher_registry": resourceRancherRegistry(),
"rancher_registry_credential": resourceRancherRegistryCredential(),
"rancher_stack": resourceRancherStack(),
},

ConfigureFunc: providerConfigure,
}
}

var descriptions map[string]string

func init() {
descriptions = map[string]string{
"access_key": "API Key used to authenticate with the rancher server",

"secret_key": "API secret used to authenticate with the rancher server",

"api_url": "The URL to the rancher API",
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := &Config{
APIURL: d.Get("api_url").(string) + "/v1",
AccessKey: d.Get("access_key").(string),
SecretKey: d.Get("secret_key").(string),
}

err := config.CreateClient()

return config, err
}
35 changes: 35 additions & 0 deletions builtin/providers/rancher/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package rancher

import (
"os"
"testing"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"rancher": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("RANCHER_URL"); v == "" {
t.Fatal("RANCHER_URL must be set for acceptance tests")
}
}
Loading