-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
stack72
merged 2 commits into
hashicorp:master
from
johnrengelman:rancher-provider-merged
Dec 5, 2016
Merged
[Provider] Rancher #9173
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
builtin/providers/rancher/import_rancher_environment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
28 changes: 28 additions & 0 deletions
28
builtin/providers/rancher/import_rancher_registration_token_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
30 changes: 30 additions & 0 deletions
30
builtin/providers/rancher/import_rancher_registry_credential_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.