Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data-sources/principal.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The following arguments are supported:

* `name` - (Required) The full name of the principal (string)
* `type` - (Optional) The type of the identity (string). Defaults to `user`. Only `user` and `group` values are supported (string)
* `exact_match` - (Optional) If set to `true`, only the exactly matched result is returned. Defaults to `false`, which means a partially matched result can be returned (for example: `foo2` also matches for `foo` search input) (bool)


## Attributes Reference
Expand Down
21 changes: 20 additions & 1 deletion rancher2/data_source_rancher2_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func dataSourceRancher2Principal() *schema.Resource {
Default: principalTypeUser,
ValidateFunc: validation.StringInSlice(principalTypes, true),
},
"exact_match": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand All @@ -44,8 +49,9 @@ func dataSourceRancher2PrincipalRead(d *schema.ResourceData, meta interface{}) e

name := d.Get("name").(string)
principalType := d.Get("type").(string)
exactMatch := d.Get("exact_match").(bool)

collection, err := client.Principal.List(nil)
collection, err := client.Principal.ListAll(nil)
if err != nil {
return err
}
Expand All @@ -63,6 +69,19 @@ func dataSourceRancher2PrincipalRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("[ERROR] principal \"%s\" of type \"%s\" not found", name, principalType)
}

// We always had at least one result here, let's find which can match exactly with the inputted name
if exactMatch {
for _, v := range principals.Data {
if v.Name == name {
return flattenDataSourcePrincipal(d, &v)
}
}
// This situation will be almost never happened, but we still ensure for the special case
return fmt.Errorf(
"[ERROR] principal \"%s\" of type \"%s\" not found. Try again with \"exact_match=false\" for partially matched result",
name, principalType)
}

return flattenDataSourcePrincipal(d, &principals.Data[0])
}

Expand Down