-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aiven_organization_user_list): add datasource
- Loading branch information
Showing
6 changed files
with
344 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "aiven_organization_user_list Data Source - terraform-provider-aiven" | ||
subcategory: "" | ||
description: |- | ||
List of users of the organization | ||
--- | ||
|
||
# aiven_organization_user_list (Data Source) | ||
|
||
List of users of the organization | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `id` (String) Organization id. Example: `org12345678`. | ||
- `name` (String) Organization name. Example: `aiven`. | ||
|
||
### Read-Only | ||
|
||
- `users` (List of Object) List of users of the organization (see [below for nested schema](#nestedatt--users)) | ||
|
||
<a id="nestedatt--users"></a> | ||
### Nested Schema for `users` | ||
|
||
Read-Only: | ||
|
||
- `is_super_admin` (Boolean) | ||
- `join_time` (String) | ||
- `last_activity_time` (String) | ||
- `user_id` (String) | ||
- `user_info` (List of Object) (see [below for nested schema](#nestedobjatt--users--user_info)) | ||
|
||
<a id="nestedobjatt--users--user_info"></a> | ||
### Nested Schema for `users.user_info` | ||
|
||
Read-Only: | ||
|
||
- `city` (String) | ||
- `country` (String) | ||
- `create_time` (String) | ||
- `department` (String) | ||
- `is_application_user` (Boolean) | ||
- `job_title` (String) | ||
- `managed_by_scim` (Boolean) | ||
- `managing_organization_id` (String) | ||
- `real_name` (String) | ||
- `state` (String) | ||
- `user_email` (String) |
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
178 changes: 178 additions & 0 deletions
178
internal/sdkprovider/service/organization/organization_user_list_data_source.go
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,178 @@ | ||
package organization | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
avngen "github.com/aiven/go-client-codegen" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/aiven/terraform-provider-aiven/internal/common" | ||
"github.com/aiven/terraform-provider-aiven/internal/schemautil" | ||
) | ||
|
||
func datasourceOrganizationUserListSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Organization id. Example: `org12345678`.", | ||
ConflictsWith: []string{"name"}, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Organization name. Example: `aiven`.", | ||
ConflictsWith: []string{"id"}, | ||
}, | ||
"users": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "List of users of the organization", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"is_super_admin": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Super admin state of the organization user", | ||
}, | ||
"join_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Join time", | ||
}, | ||
"last_activity_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Last activity time", | ||
}, | ||
"user_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "User ID", | ||
}, | ||
"user_info": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"city": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "City", | ||
}, | ||
"country": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Country", | ||
}, | ||
"create_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Creation time", | ||
}, | ||
"department": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Department", | ||
}, | ||
"is_application_user": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Is Application User", | ||
}, | ||
"job_title": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Job Title", | ||
}, | ||
"managed_by_scim": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Managed By Scim", | ||
}, | ||
"managing_organization_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Managing Organization ID", | ||
}, | ||
"real_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Real Name", | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "State", | ||
}, | ||
"user_email": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "User Email", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func DatasourceOrganizationUserList() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: common.WithGenClient(datasourceOrganizationUserListRead), | ||
Description: "List of users of the organization", | ||
Schema: datasourceOrganizationUserListSchema(), | ||
} | ||
} | ||
|
||
func datasourceOrganizationUserListRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error { | ||
organizationID := d.Get("id").(string) | ||
if organizationID == "" { | ||
name := d.Get("name").(string) | ||
if name == "" { | ||
return fmt.Errorf("either id or name must be specified") | ||
} | ||
|
||
id, err := GetOrganizationByName(ctx, client, name) | ||
if err != nil { | ||
return err | ||
} | ||
organizationID = id | ||
} | ||
|
||
list, err := client.OrganizationUserList(ctx, organizationID) | ||
if err != nil { | ||
return fmt.Errorf("cannot get organization %q user list: %w", organizationID, err) | ||
} | ||
|
||
d.SetId(organizationID) | ||
users := map[string]any{"users": list} | ||
return schemautil.ResourceDataSet(datasourceOrganizationUserListSchema(), d, users) | ||
} | ||
|
||
func GetOrganizationByName(ctx context.Context, client avngen.Client, name string) (string, error) { | ||
ids := make([]string, 0) | ||
list, err := client.UserOrganizationsList(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, o := range list { | ||
// Organization name is not unique | ||
if o.OrganizationName == name { | ||
ids = append(ids, o.OrganizationId) | ||
} | ||
} | ||
|
||
switch len(ids) { | ||
case 0: | ||
return "", fmt.Errorf("organization %q not found", name) | ||
case 1: | ||
return ids[0], nil | ||
} | ||
return "", fmt.Errorf("multiple organizations %q found, ids: %s", name, strings.Join(ids, ", ")) | ||
} |
Oops, something went wrong.