forked from rancher/norman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rancher#61 from rawmind0/user
User and global_role_binding resources and data sources
- Loading branch information
Showing
24 changed files
with
1,699 additions
and
8 deletions.
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
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
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,70 @@ | ||
package rancher2 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceRancher2GlobalRoleBinding() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceRancher2GlobalRoleBindingRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"global_role_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"user_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"annotations": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
"labels": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceRancher2GlobalRoleBindingRead(d *schema.ResourceData, meta interface{}) error { | ||
client, err := meta.(*Config).ManagementClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
globalRole := d.Get("global_role_id").(string) | ||
name := d.Get("name").(string) | ||
|
||
filters := map[string]interface{}{ | ||
"name": name, | ||
} | ||
if len(globalRole) > 0 { | ||
filters["globalRoleId"] = globalRole | ||
} | ||
listOpts := NewListOpts(filters) | ||
|
||
globalRoleBindings, err := client.GlobalRoleBinding.List(listOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
count := len(globalRoleBindings.Data) | ||
if count <= 0 { | ||
return fmt.Errorf("[ERROR] global role binding with name \"%s\" not found", name) | ||
} | ||
if count > 1 { | ||
return fmt.Errorf("[ERROR] found %d global role binding with name \"%s\"", count, name) | ||
} | ||
|
||
return flattenGlobalRoleBinding(d, &globalRoleBindings.Data[0]) | ||
} |
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,52 @@ | ||
package rancher2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
const ( | ||
testAccRancher2GlobalRoleBindingDataSourceType = "rancher2_global_role_binding" | ||
) | ||
|
||
var ( | ||
testAccCheckRancher2GlobalRoleBindingDataSourceConfig string | ||
) | ||
|
||
func init() { | ||
testAccCheckRancher2GlobalRoleBindingDataSourceConfig = ` | ||
resource "rancher2_user" "foo" { | ||
name = "Terraform user acceptance test" | ||
username = "foo" | ||
password = "changeme" | ||
} | ||
resource "rancher2_global_role_binding" "foo" { | ||
name = "foo" | ||
global_role_id = "user-base" | ||
user_id = "${rancher2_user.foo.id}" | ||
} | ||
data "` + testAccRancher2GlobalRoleBindingDataSourceType + `" "foo" { | ||
name = "${rancher2_global_role_binding.foo.name}" | ||
global_role_id = "${rancher2_global_role_binding.foo.global_role_id}" | ||
} | ||
` | ||
} | ||
|
||
func TestAccRancher2GlobalRoleBindingDataSource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckRancher2GlobalRoleBindingDataSourceConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data."+testAccRancher2GlobalRoleBindingDataSourceType+".foo", "name", "foo"), | ||
resource.TestCheckResourceAttr("data."+testAccRancher2GlobalRoleBindingDataSourceType+".foo", "global_role_id", "user-base"), | ||
resource.TestCheckResourceAttr("data."+testAccRancher2GlobalRoleBindingDataSourceType+".foo", "labels.cattle.io/creator", "norman"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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,72 @@ | ||
package rancher2 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceRancher2User() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceRancher2UserRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"username": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"enabled": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"principal_ids": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"annotations": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
"labels": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceRancher2UserRead(d *schema.ResourceData, meta interface{}) error { | ||
client, err := meta.(*Config).ManagementClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
username := d.Get("username").(string) | ||
|
||
filters := map[string]interface{}{ | ||
"username": username, | ||
} | ||
listOpts := NewListOpts(filters) | ||
|
||
users, err := client.User.List(listOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
count := len(users.Data) | ||
if count <= 0 { | ||
return fmt.Errorf("[ERROR] username \"%s\" not found", username) | ||
} | ||
if count > 1 { | ||
return fmt.Errorf("[ERROR] found %d username \"%s\"", count, username) | ||
} | ||
|
||
return flattenUser(d, &users.Data[0]) | ||
} |
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,47 @@ | ||
package rancher2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
const ( | ||
testAccRancher2UserDataSourceType = "rancher2_user" | ||
) | ||
|
||
var ( | ||
testAccCheckRancher2UserDataSourceConfig string | ||
) | ||
|
||
func init() { | ||
testAccCheckRancher2UserDataSourceConfig = ` | ||
resource "rancher2_user" "foo" { | ||
name = "Terraform user acceptance test" | ||
username = "foo" | ||
password = "changeme" | ||
enabled = "true" | ||
} | ||
data "` + testAccRancher2UserDataSourceType + `" "foo" { | ||
username = "${rancher2_user.foo.username}" | ||
} | ||
` | ||
} | ||
|
||
func TestAccRancher2UserDataSource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckRancher2UserDataSourceConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data."+testAccRancher2UserDataSourceType+".foo", "username", "foo"), | ||
resource.TestCheckResourceAttr("data."+testAccRancher2UserDataSourceType+".foo", "name", "Terraform user acceptance test"), | ||
resource.TestCheckResourceAttr("data."+testAccRancher2UserDataSourceType+".foo", "enabled", "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,14 @@ | ||
package rancher2 | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceRancher2GlobalRoleBindingImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
err := resourceRancher2GlobalRoleBindingRead(d, meta) | ||
if err != nil { | ||
return []*schema.ResourceData{}, err | ||
} | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} |
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,14 @@ | ||
package rancher2 | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceRancher2UserImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
err := resourceRancher2UserRead(d, meta) | ||
if err != nil { | ||
return []*schema.ResourceData{}, err | ||
} | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} |
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
Oops, something went wrong.