diff --git a/docs/data-sources/principal.md b/docs/data-sources/principal.md index e79574ea8..a6117d321 100644 --- a/docs/data-sources/principal.md +++ b/docs/data-sources/principal.md @@ -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 diff --git a/rancher2/data_source_rancher2_principal.go b/rancher2/data_source_rancher2_principal.go index e7c80b2ed..31cfb86f8 100644 --- a/rancher2/data_source_rancher2_principal.go +++ b/rancher2/data_source_rancher2_principal.go @@ -32,6 +32,11 @@ func dataSourceRancher2Principal() *schema.Resource { Default: principalTypeUser, ValidateFunc: validation.StringInSlice(principalTypes, true), }, + "exact_match": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, }, } } @@ -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 } @@ -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]) }