Skip to content

Commit

Permalink
[Provider] Rancher (hashicorp#9173)
Browse files Browse the repository at this point in the history
* Vendor Rancher Go library.

* Implement Rancher Provider.

Starting implementation taken from
https://github.com/platanus/terraform-provider-rancher

Commits from jidonoso@gmail.com and raphael.pinson@camptocamp.com
  • Loading branch information
johnrengelman authored and Gustavo Mateus committed Dec 6, 2016
1 parent 8a120d8 commit 26043a9
Show file tree
Hide file tree
Showing 241 changed files with 53,807 additions and 0 deletions.
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,
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

0 comments on commit 26043a9

Please sign in to comment.