Skip to content

Commit

Permalink
Merge pull request rancher#61 from rawmind0/user
Browse files Browse the repository at this point in the history
User and global_role_binding resources and data sources
  • Loading branch information
rawmind0 authored Jul 12, 2019
2 parents bb3ae8f + a6ccb4e commit 5853153
Show file tree
Hide file tree
Showing 24 changed files with 1,699 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ FEATURES:
* **New Data Source:** `rancher2_cloud_credential`
* **New Data Source:** `rancher2_cluster`
* **New Data Source:** `rancher2_etcd_backup`
* **New Data Source:** `rancher2_global_role_binding`
* **New Data Source:** `rancher2_namespace`
* **New Data Source:** `rancher2_node_pool`
* **New Data Source:** `rancher2_user`
* **New Resource:** `rancher2_global_role_binding`
* **New Resource:** `rancher2_user`

ENHANCEMENTS:

Expand Down
22 changes: 22 additions & 0 deletions rancher2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,28 @@ func (c *Config) ProjectExist(id string) error {
return nil
}

func (c *Config) GetGlobalRoleByID(id string) (*managementClient.GlobalRole, error) {
if id == "" {
return nil, fmt.Errorf("Global role id is nil")
}

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

return client.GlobalRole.ByID(id)
}

func (c *Config) GlobalRoleExist(id string) error {
_, err := c.GetGlobalRoleByID(id)
if err != nil {
return err
}

return nil
}

func (c *Config) GetRoleTemplateByID(id string) (*managementClient.RoleTemplate, error) {
if id == "" {
return nil, fmt.Errorf("Role template id is nil")
Expand Down
70 changes: 70 additions & 0 deletions rancher2/data_source_rancher2_global_role_binding.go
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])
}
52 changes: 52 additions & 0 deletions rancher2/data_source_rancher2_global_role_binding_test.go
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"),
),
},
},
})
}
72 changes: 72 additions & 0 deletions rancher2/data_source_rancher2_user.go
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])
}
47 changes: 47 additions & 0 deletions rancher2/data_source_rancher2_user_test.go
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"),
),
},
},
})
}
14 changes: 14 additions & 0 deletions rancher2/import_rancher2_global_role_binding.go
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
}
14 changes: 14 additions & 0 deletions rancher2/import_rancher2_user.go
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
}
20 changes: 12 additions & 8 deletions rancher2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func Provider() terraform.ResourceProvider {
"rancher2_cluster_logging": resourceRancher2ClusterLogging(),
"rancher2_cluster_role_template_binding": resourceRancher2ClusterRoleTemplateBinding(),
"rancher2_etcd_backup": resourceRancher2EtcdBackup(),
"rancher2_global_role_binding": resourceRancher2GlobalRoleBinding(),
"rancher2_node_driver": resourceRancher2NodeDriver(),
"rancher2_node_pool": resourceRancher2NodePool(),
"rancher2_node_template": resourceRancher2NodeTemplate(),
Expand All @@ -95,17 +96,20 @@ func Provider() terraform.ResourceProvider {
"rancher2_project_role_template_binding": resourceRancher2ProjectRoleTemplateBinding(),
"rancher2_namespace": resourceRancher2Namespace(),
"rancher2_setting": resourceRancher2Setting(),
"rancher2_user": resourceRancher2User(),
},

DataSourcesMap: map[string]*schema.Resource{
"rancher2_catalog": dataSourceRancher2Catalog(),
"rancher2_cloud_credential": dataSourceRancher2CloudCredential(),
"rancher2_cluster": dataSourceRancher2Cluster(),
"rancher2_etcd_backup": dataSourceRancher2EtcdBackup(),
"rancher2_namespace": dataSourceRancher2Namespace(),
"rancher2_node_pool": dataSourceRancher2NodePool(),
"rancher2_project": dataSourceRancher2Project(),
"rancher2_setting": dataSourceRancher2Setting(),
"rancher2_catalog": dataSourceRancher2Catalog(),
"rancher2_cloud_credential": dataSourceRancher2CloudCredential(),
"rancher2_cluster": dataSourceRancher2Cluster(),
"rancher2_etcd_backup": dataSourceRancher2EtcdBackup(),
"rancher2_global_role_binding": dataSourceRancher2GlobalRoleBinding(),
"rancher2_namespace": dataSourceRancher2Namespace(),
"rancher2_node_pool": dataSourceRancher2NodePool(),
"rancher2_project": dataSourceRancher2Project(),
"rancher2_setting": dataSourceRancher2Setting(),
"rancher2_user": dataSourceRancher2User(),
},

ConfigureFunc: providerConfigure,
Expand Down
Loading

0 comments on commit 5853153

Please sign in to comment.