Skip to content

Commit

Permalink
New data-source: add zoom_phone_users data source (#40)
Browse files Browse the repository at this point in the history
Signed-off-by: krrrr38 <[email protected]>
  • Loading branch information
krrrr38 authored Aug 29, 2024
1 parent dbb8908 commit c5d0608
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ build:
.PHONY: build_override
build_override: build
mkdir -p ${PLUGINS}
rm -rf ${PLUGINS}/* || true
mv dist/${BIN} ${PLUGINS}/${BIN}

# Run go build. Move artifact to terraform plugins dir. Output override config for ~/.terraformrc
Expand Down
97 changes: 97 additions & 0 deletions docs/data-sources/phone_users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "zoom_phone_users Data Source - zoom"
subcategory: ""
description: |-
A list of all of an account's users who are assigned a Zoom Phone license.
API Permissions
The following API permissions are required in order to use this resource.
This resource requires the phone:read:list_users:admin.
---

# zoom_phone_users (Data Source)

A list of all of an account's users who are assigned a Zoom Phone license.

## API Permissions
The following API permissions are required in order to use this resource.
This resource requires the `phone:read:list_users:admin`.

## Example Usage

```terraform
data "zoom_phone_users" "example" {
query = {
status = "activate"
}
}
output "users" {
value = data.zoom_phone_users.example.users
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `query` (Attributes) The query parameters for listing users. (see [below for nested schema](#nestedatt--query))

### Read-Only

- `users` (Attributes Set) A list of users. Each user object provides the attributes documented below. (see [below for nested schema](#nestedatt--users))

<a id="nestedatt--query"></a>
### Nested Schema for `query`

Optional:

- `calling_type` (Number) The [type](https://marketplace.zoom.us/docs/api-reference/other-references/plans#zoom-phone-calling-plans) of calling plan.
- `cost_center` (String) The cost center where the user belongs.
- `department` (String) The department where the user belongs.
- `keyword` (String) The partial string of user's name, extension number, or phone number.
- `site_id` (String) The unique identifier of the site. Get it from the [List Phone Sites](https://marketplace.zoom.us/docs/api-reference/phone/methods#operation/listPhoneSites) API.
- `status` (String) The status of the Zoom Phone user.
- pending: The users have been assigned the Zoom Workplace license, but not been assigned Zoom Phone feature.
- Allowed: activate ┃ deactivate ┃ pending


<a id="nestedatt--users"></a>
### Nested Schema for `users`

Read-Only:

- `calling_plans` (Attributes Set) (see [below for nested schema](#nestedatt--users--calling_plans))
- `cost_center` (String) The cost center where the user belongs.
- `department` (String) The department where the user belongs.
- `email` (String) The email address of the user.
- `extension_id` (String) The extension ID.
- `extension_number` (Number) The extension number assigned to the user's Zoom phone number.
- `name` (String) The name of the user.
- `phone_numbers` (Attributes Set) (see [below for nested schema](#nestedatt--users--phone_numbers))
- `phone_user_id` (String) The ID of the Phone user.
- `site_id` (String) The unique identifier of the [site](https://support.zoom.us/hc/en-us/articles/360020809672z) where the user should be moved or assigned.
- `status` (String) The status of the user's Zoom Phone license. The value can be either of the following:
- activate: Active Zoom phone user.
- deactivate: User with Zoom phone license disabled. This type of user can't make or receive calls.
- `user_id` (String) The ID of the Zoom user.

<a id="nestedatt--users--calling_plans"></a>
### Nested Schema for `users.calling_plans`

Read-Only:

- `billing_account_id` (String) The billing account ID. It displays when the user is located in India.
- `billing_account_name` (String) The billing account name. It displays when the user is located in India.
- `name` (String) The name of the user's calling plan.
- `type` (Number) The type of calling plan where the user is enrolled.


<a id="nestedatt--users--phone_numbers"></a>
### Nested Schema for `users.phone_numbers`

Read-Only:

- `id` (String) The phone number ID.
- `number` (String) The phone number.
9 changes: 9 additions & 0 deletions examples/data-sources/zoom_phone_users/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "zoom_phone_users" "example" {
query = {
status = "activate"
}
}

output "users" {
value = data.zoom_phone_users.example.users
}
21 changes: 21 additions & 0 deletions examples/data-sources/zoom_phone_users/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
terraform {
required_providers {
zoom = {
source = "registry.terraform.io/folio-sec/zoom"
}
}
}

provider "zoom" {
account_id = var.zoom_account_id
client_id = var.zoom_client_id
client_secret = var.zoom_client_secret
}

variable "zoom_account_id" {}

variable "zoom_client_id" {}

variable "zoom_client_secret" {
sensitive = true
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func (p *zoomProvider) DataSources(_ context.Context) []func() datasource.DataSo
blockedlist.NewPhoneBlockedListDataSource,
callqueue.NewPhoneCallQueueDataSource,
phonenumbers.NewPhonePhoneNumbersDataSource,
phoneuser.NewPhoneUsersDataSource,
sharedlinegroupgroup.NewPhoneSharedLineGroupDataSource,
user.NewUsersDataSource,
}
Expand Down
66 changes: 66 additions & 0 deletions internal/services/phone/user/user_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,72 @@ type crud struct {
userClient *zoomuser.Client
}

func (c *crud) list(ctx context.Context, dto listQueryDto) (*listDto, error) {
var users []listDtoUser
nextPageToken := zoomphone.OptString{}

for {
ret, err := c.phoneClient.ListPhoneUsers(ctx, zoomphone.ListPhoneUsersParams{
PageSize: zoomphone.NewOptInt(100), // Max 100
NextPageToken: nextPageToken,
SiteID: util.ToPhoneOptString(dto.siteID),
CallingType: util.ToPhoneOptInt(dto.callingType),
Status: util.ToPhoneOptString(dto.status),
Department: util.ToPhoneOptString(dto.department),
CostCenter: util.ToPhoneOptString(dto.costCenter),
Keyword: util.ToPhoneOptString(dto.keyword),
})
if err != nil {
return nil, fmt.Errorf("unable to list users: %v", err)
}

users = append(users, lo.Map(ret.Users, func(item zoomphone.ListPhoneUsersOKUsersItem, _ int) listDtoUser {
return listDtoUser{
callingPlans: lo.Map(item.CallingPlans, func(item zoomphone.ListPhoneUsersOKUsersItemCallingPlansItem, index int) *listDtoUserCallingPlan {
return &listDtoUserCallingPlan{
name: util.FromOptString(item.Name),
typ: util.FromOptInt(item.Type),
billingAccountID: util.FromOptString(item.BillingAccountID),
billingAccountName: util.FromOptString(item.BillingAccountName),
}
}),
email: util.FromOptString(item.Email),
extensionID: util.FromOptString(item.Email),
extensionNumber: util.FromOptInt64(item.ExtensionNumber),
userID: util.FromOptString(item.ID),
name: util.FromOptString(item.Name),
phoneUserID: util.FromOptString(item.PhoneUserID),
site: lo.TernaryF(item.Site.IsSet(), func() *listDtoUserSite {
return &listDtoUserSite{
id: util.FromOptString(item.Site.Value.ID),
name: util.FromOptString(item.Site.Value.Name),
}
}, func() *listDtoUserSite {
return nil
}),
status: util.FromOptString(item.Status),
phoneNumbers: lo.Map(item.PhoneNumbers, func(item zoomphone.ListPhoneUsersOKUsersItemPhoneNumbersItem, index int) *listDtoUserPhoneNumber {
return &listDtoUserPhoneNumber{
id: util.FromOptString(item.ID),
number: util.FromOptString(item.Number),
}
}),
department: util.FromOptString(item.Department),
costCenter: util.FromOptString(item.CostCenter),
}
})...)

if !ret.NextPageToken.IsSet() || ret.NextPageToken.Value == "" {
break
}
nextPageToken = ret.NextPageToken
}

return &listDto{
users: users,
}, nil
}

func (c *crud) read(ctx context.Context, zoomUserID types.String) (*readDto, error) {
detail, err := c.phoneClient.PhoneUser(ctx, zoomphone.PhoneUserParams{
UserId: zoomUserID.ValueString(),
Expand Down
45 changes: 45 additions & 0 deletions internal/services/phone/user/user_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,51 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

type listQueryDto struct {
siteID types.String
callingType types.Int32
status types.String
department types.String
costCenter types.String
keyword types.String
}

type listDto struct {
users []listDtoUser
}

type listDtoUser struct {
userID types.String
phoneUserID types.String
name types.String
email types.String
extensionID types.String
extensionNumber types.Int64
status types.String
department types.String
costCenter types.String
site *listDtoUserSite
phoneNumbers []*listDtoUserPhoneNumber
callingPlans []*listDtoUserCallingPlan
}

type listDtoUserSite struct {
id types.String
name types.String
}

type listDtoUserPhoneNumber struct {
id types.String
number types.String
}

type listDtoUserCallingPlan struct {
name types.String
typ types.Int32
billingAccountID types.String
billingAccountName types.String
}

type readDto struct {
callingPlans []readDtoCallingPlan
costCenter types.String
Expand Down
Loading

0 comments on commit c5d0608

Please sign in to comment.