Skip to content

Commit

Permalink
Merge pull request rancher#6 from rawmind0/master
Browse files Browse the repository at this point in the history
Fix rancher2_cluster_role_template_binding resource saving. Updated acceptance tests.
  • Loading branch information
cloudnautique authored Nov 12, 2018
2 parents 1461010 + ca7fecb commit 2433ff3
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 63 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ To update vendor dependencies, edit `vendor.conf` file and execute trash
$ trash
```

Acceptance tests
----------------

For execute acceptance tests, a running rancher HA system and a rancher API key are needed.

To run acceptance tests, export `RANCHER_URL` with rancher url and `RANCHER_TOKEN_KEY` with bearer token or `RANCHER_ACCESS_KEY` with rancher acces key and `RANCHER_SECRET_KEY` with rancher secret key and execute

```sh
$ export RANCHER_URL=<URL>
$ export RANCHER_TOKEN_KEY=<TOKEN>
$ scripts/testacc
```

38 changes: 38 additions & 0 deletions rancher2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,41 @@ func (c *Config) GetAuthConfig(in *managementClient.AuthConfig) (interface{}, er
func (c *Config) UpdateAuthConfig(url string, createObj interface{}, respObject interface{}) error {
return c.Client.Management.Ops.DoModify("PUT", url, createObj, respObject)
}

func (c *Config) GetUserByName(name string) (*managementClient.User, error) {
if name == "" {
return nil, fmt.Errorf("[ERROR] Username is nil")
}

client, err := c.ManagementClient()
if err != nil {
return nil, err
}

filters := map[string]interface{}{"username": name}
listOpts := NewListOpts(filters)

users, err := client.User.List(listOpts)
if err != nil {
return nil, err
}

for _, user := range users.Data {
if user.Username == name {
return &user, nil
}
}
return nil, fmt.Errorf("[ERROR] Username %s not found", name)
}

func (c *Config) GetUserIDByName(name string) (string, error) {
if name == "" {
return "", nil
}

user, err := c.GetUserByName(name)
if err != nil {
return "", err
}
return user.ID, nil
}
11 changes: 10 additions & 1 deletion rancher2/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ func TestProvider_impl(t *testing.T) {
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("RANCHER_URL"); v == "" {
url := os.Getenv("RANCHER_URL")
token := os.Getenv("RANCHER_TOKEN_KEY")
accessKey := os.Getenv("RANCHER_ACCESS_KEY")
secretKey := os.Getenv("RANCHER_SECRET_KEY")

if url == "" {
t.Fatal("RANCHER_URL must be set for acceptance tests")
}

if token == "" && (accessKey == "" || secretKey == "") {
t.Fatal("RANCHER_TOKEN_KEY or RANCHER_ACCESS_KEY and RANCHER_SECRET_KEY must be set for acceptance tests")
}
}
12 changes: 6 additions & 6 deletions rancher2/resource_rancher2_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ const (
resource "rancher2_catalog" "foo" {
name = "foo"
url = "http://foo.com:8080"
description= "Foo catalog test"
description= "Terraform catalog acceptance test"
}
`

testAccRancher2CatalogUpdateConfig = `
resource "rancher2_catalog" "foo" {
name = "foo"
url = "http://foo.updated.com:8080"
description= "Foo catalog test - updated"
description= "Terraform catalog acceptance test - updated"
}
`

testAccRancher2CatalogRecreateConfig = `
resource "rancher2_catalog" "foo" {
name = "foo"
url = "http://foo.com:8080"
description= "Foo catalog test"
description= "Terraform catalog acceptance test"
}
`
)
Expand All @@ -50,7 +50,7 @@ func TestAccRancher2Catalog_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo", catalog),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "name", "foo"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "description", "Foo catalog test"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "description", "Terraform catalog acceptance test"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "url", "http://foo.com:8080"),
),
},
Expand All @@ -59,7 +59,7 @@ func TestAccRancher2Catalog_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo", catalog),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "name", "foo"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "description", "Foo catalog test - updated"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "description", "Terraform catalog acceptance test - updated"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "url", "http://foo.updated.com:8080"),
),
},
Expand All @@ -68,7 +68,7 @@ func TestAccRancher2Catalog_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo", catalog),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "name", "foo"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "description", "Foo catalog test"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "description", "Terraform catalog acceptance test"),
resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo", "url", "http://foo.com:8080"),
),
},
Expand Down
39 changes: 30 additions & 9 deletions rancher2/resource_rancher2_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,38 @@ func resourceRancher2ClusterUpdate(d *schema.ResourceData, meta interface{}) err
return err
}

rkeConfig, err := expandRkeConfig(d.Get("rke_config").([]interface{}))
if err != nil {
return err
update := map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"annotations": toMapString(d.Get("annotations").(map[string]interface{})),
"labels": toMapString(d.Get("labels").(map[string]interface{})),
}

update := map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"rancherKubernetesEngineConfig": rkeConfig,
"annotations": toMapString(d.Get("annotations").(map[string]interface{})),
"labels": toMapString(d.Get("labels").(map[string]interface{})),
switch kind := d.Get("kind").(string); kind {
case "rke":
rkeConfig, err := expandRkeConfig(d.Get("rke_config").([]interface{}))
if err != nil {
return err
}
update["rancherKubernetesEngineConfig"] = rkeConfig
case "eks":
eksConfig, err := expandEksConfig(d.Get("eks_config").([]interface{}))
if err != nil {
return err
}
update["amazonElasticContainerServiceConfig"] = eksConfig
case "aks":
aksConfig, err := expandAksConfig(d.Get("aks_config").([]interface{}))
if err != nil {
return err
}
update["azureKubernetesServiceConfig"] = aksConfig
case "gke":
gkeConfig, err := expandGkeConfig(d.Get("gke_config").([]interface{}))
if err != nil {
return err
}
update["googleKubernetesEngineConfig"] = gkeConfig
}

newCluster, err := meta.(*Config).UpdateClusterByID(cluster, update)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func resourceRancher2ClusterRoleTemplateBindingCreate(d *schema.ResourceData, me
"[ERROR] waiting for cluster role template binding (%s) to be created: %s", newClusterRole.ID, waitErr)
}

err = flattenClusterRoleTemplateBinding(d, clusterRole)
err = flattenClusterRoleTemplateBinding(d, newClusterRole)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ resource "rancher2_cluster_role_template_binding" "foo" {
resource "rancher2_cluster_role_template_binding" "foo" {
name = "foo"
cluster_id = "local"
role_template_id = "project-member"
user_id = "u-q2wg7"
role_template_id = "project-owner"
}
`

Expand Down Expand Up @@ -61,8 +60,7 @@ func TestAccRancher2ClusterRoleTemplateBinding_basic(t *testing.T) {
testAccCheckRancher2ClusterRoleTemplateBindingExists(testAccRancher2ClusterRoleTemplateBindingType+".foo", clusterRole),
resource.TestCheckResourceAttr(testAccRancher2ClusterRoleTemplateBindingType+".foo", "name", "foo"),
resource.TestCheckResourceAttr(testAccRancher2ClusterRoleTemplateBindingType+".foo", "cluster_id", "local"),
resource.TestCheckResourceAttr(testAccRancher2ClusterRoleTemplateBindingType+".foo", "role_template_id", "project-member"),
resource.TestCheckResourceAttr(testAccRancher2ClusterRoleTemplateBindingType+".foo", "user_id", "u-q2wg7"),
resource.TestCheckResourceAttr(testAccRancher2ClusterRoleTemplateBindingType+".foo", "role_template_id", "project-owner"),
),
},
resource.TestStep{
Expand Down
Loading

0 comments on commit 2433ff3

Please sign in to comment.