Skip to content

Commit

Permalink
add zoom_phone_external_contact resource
Browse files Browse the repository at this point in the history
Signed-off-by: krrrr38 <k.kaizu38@gmail.com>
  • Loading branch information
krrrr38 committed Aug 29, 2024
1 parent c5d0608 commit c94a374
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 4 deletions.
70 changes: 70 additions & 0 deletions docs/resources/phone_external_contact.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "zoom_phone_external_contact Resource - zoom"
subcategory: ""
description: |-
External contact's information.
API Permissions
The following API permissions are required in order to use this resource.
This resource requires the phone:read:external_contact:admin, phone:write:external_contact:admin, phone:update:external_contact:admin, phone:delete:external_contact:admin.
---

# zoom_phone_external_contact (Resource)

External contact's information.

## API Permissions
The following API permissions are required in order to use this resource.
This resource requires the `phone:read:external_contact:admin`, `phone:write:external_contact:admin`, `phone:update:external_contact:admin`, `phone:delete:external_contact:admin`.

## Example Usage

```terraform
resource "zoom_phone_external_contact" "example" {
id = "4gAYcVCwTUGGDpYWdFpXxx"
name = "Example"
description = "Description"
email = "example@example.com"
phone_numbers = [
"+0123456789",
]
auto_call_recorded = true
}
resource "zoom_phone_external_contact" "other" {
name = "Other"
phone_numbers = [
"+0123456789",
]
}
```

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

### Required

- `name` (String) The external contact's username or extension display name.
- `phone_numbers` (Set of String) The external contact's phone numbers. This value must be in [E.164](https://en.wikipedia.org/wiki/E.164) format. If you do not provide an extension number, you must provide this value.

### Optional

- `auto_call_recorded` (Boolean) Whether to allow the automatic call recording.
- `description` (String) The external contact's description.
- `email` (String) The external contact's email address.
- `extension_number` (String) The external contact's extension number.
- `id` (String) The customer-configured external contact ID. It is recommended that you use a primary key from the original phone system. If you do not use this parameter, the API automatically generates an `external_contact_id`.
- `routing_path` (String) The external contact's SIP group, to define the call routing path. This is for customers that use SIP trunking.

### Read-Only

- `external_contact_id` (String) The Zoom-generated external contact ID.

## Import

Import is supported using the following syntax:

```shell
# ${external_contact_id}
terraform import zoom_phone_external_contact.example lSq8jyDORe6tmbaUkOVhXx
```
2 changes: 2 additions & 0 deletions examples/resources/zoom_phone_external_contact/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ${external_contact_id}
terraform import zoom_phone_external_contact.example lSq8jyDORe6tmbaUkOVhXx
22 changes: 22 additions & 0 deletions examples/resources/zoom_phone_external_contact/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
terraform {
required_providers {
zoom = {
source = "folio-sec/zoom"
version = "~> 0.0.0"
}
}
}

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
}
17 changes: 17 additions & 0 deletions examples/resources/zoom_phone_external_contact/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "zoom_phone_external_contact" "example" {
id = "4gAYcVCwTUGGDpYWdFpXxx"
name = "Example"
description = "Description"
email = "example@example.com"
phone_numbers = [
"+0123456789",
]
auto_call_recorded = true
}

resource "zoom_phone_external_contact" "other" {
name = "Other"
phone_numbers = [
"+0123456789",
]
}
19 changes: 18 additions & 1 deletion generated/api/zoomphone/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions generated/api/zoomphone/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/callqueue"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/callqueuemember"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/callqueuephonenumber"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/externalcontact"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/phonenumbers"
sharedlinegroupgroup "github.com/folio-sec/terraform-provider-zoom/internal/services/phone/sharedlinegroup"
sharedlinegroupgroupmembers "github.com/folio-sec/terraform-provider-zoom/internal/services/phone/sharedlinegroupmember"
sharedlinegroupgroupphonenumbers "github.com/folio-sec/terraform-provider-zoom/internal/services/phone/sharedlinegroupphonenumber"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/sharedlinegroup"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/sharedlinegroupmember"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/sharedlinegroupphonenumber"
phoneuser "github.com/folio-sec/terraform-provider-zoom/internal/services/phone/user"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/usercallingplans"
"github.com/folio-sec/terraform-provider-zoom/internal/services/phone/userphonenumber"
Expand Down Expand Up @@ -216,6 +217,7 @@ func (p *zoomProvider) Resources(_ context.Context) []func() resource.Resource {
callqueue.NewPhoneCallQueueResource,
callqueuemember.NewPhoneCallQueueMembersResource,
callqueuephonenumber.NewPhoneCallQueuePhoneNumbersResource,
externalcontact.NewPhoneExternalContactResource,
sharedlinegroupgroup.NewPhoneSharedLineGroupResource,
sharedlinegroupgroupmembers.NewPhoneSharedLineGroupMembersResource,
sharedlinegroupgroupphonenumbers.NewPhoneSharedLineGroupPhoneNumbersResource,
Expand Down
118 changes: 118 additions & 0 deletions internal/services/phone/externalcontact/external_contact_crud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package externalcontact

import (
"context"
"errors"
"fmt"
"github.com/samber/lo"

"github.com/folio-sec/terraform-provider-zoom/generated/api/zoomphone"
"github.com/folio-sec/terraform-provider-zoom/internal/util"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func newCrud(client *zoomphone.Client) *crud {
return &crud{
client: client,
}
}

type crud struct {
client *zoomphone.Client
}

func (c *crud) read(ctx context.Context, externalContactID types.String) (*readDto, error) {
detail, err := c.client.GetAExternalContact(ctx, zoomphone.GetAExternalContactParams{
ExternalContactId: externalContactID.ValueString(),
})
if err != nil {
var status *zoomphone.ErrorResponseStatusCode
if errors.As(err, &status) {
if status.StatusCode == 400 && status.Response.Code.Value == 300 {
return nil, nil // already deleted
}
}
return nil, fmt.Errorf("unable to read phone external contact: %v", err)
}

return &readDto{
description: util.FromOptString(detail.Description),
email: util.FromOptString(detail.Email),
extensionNumber: util.FromOptString(detail.ExtensionNumber),
externalContactID: util.FromOptString(detail.ExternalContactID),
id: util.FromOptString(detail.ID),
name: util.FromOptString(detail.Name),
phoneNumbers: lo.Map(detail.PhoneNumbers, func(item string, index int) types.String {
return types.StringValue(item)
}),
routingPath: util.FromOptString(detail.RoutingPath),
autoCallRecorded: util.FromOptBool(detail.AutoCallRecorded),
}, nil
}

func (c *crud) create(ctx context.Context, dto *createDto) (*createdDto, error) {
res, err := c.client.AddExternalContact(ctx, zoomphone.OptAddExternalContactReq{
Value: zoomphone.AddExternalContactReq{
Description: util.ToPhoneOptString(dto.description),
Email: util.ToPhoneOptString(dto.email),
ExtensionNumber: util.ToPhoneOptString(dto.extensionNumber),
ID: util.ToPhoneOptString(dto.id),
Name: dto.name.ValueString(),
PhoneNumbers: lo.Map(dto.phoneNumbers, func(item types.String, index int) string {
return item.ValueString()
}),
RoutingPath: util.ToPhoneOptString(dto.routingPath),
AutoCallRecorded: util.ToPhoneOptBool(dto.autoCallRecorded),
},
Set: true,
})
if err != nil {
return nil, fmt.Errorf("error creating phone external contact: %v", err)
}

return &createdDto{
externalContactID: util.FromOptString(res.ExternalContactID),
}, nil
}

func (c *crud) update(ctx context.Context, dto *updateDto) error {
err := c.client.UpdateExternalContact(ctx, zoomphone.OptUpdateExternalContactReq{
Value: zoomphone.UpdateExternalContactReq{
Description: util.ToPhoneOptString(dto.description),
Email: util.ToPhoneOptString(dto.email),
ExtensionNumber: util.ToPhoneOptString(dto.extensionNumber),
ID: util.ToPhoneOptString(dto.id),
Name: util.ToPhoneOptString(dto.name),
PhoneNumbers: lo.Map(dto.phoneNumbers, func(item types.String, index int) string {
return item.ValueString()
}),
RoutingPath: util.ToPhoneOptString(dto.routingPath),
AutoCallRecorded: util.ToPhoneOptBool(dto.autoCallRecorded),
},
Set: true,
}, zoomphone.UpdateExternalContactParams{
ExternalContactId: dto.externalContactID.ValueString(),
})
if err != nil {
return fmt.Errorf("error updating phone external contact: %v", err)
}

return nil
}

func (c *crud) delete(ctx context.Context, externalContactId types.String) error {
err := c.client.DeleteAExternalContact(ctx, zoomphone.DeleteAExternalContactParams{
ExternalContactId: externalContactId.ValueString(),
})
if err != nil {
var status *zoomphone.ErrorResponseStatusCode
if errors.As(err, &status) {
if status.StatusCode == 400 && status.Response.Code.Value == 300 {
return nil
}
}
return fmt.Errorf("error deleting phone external contact: %v", err)
}

return nil
}
43 changes: 43 additions & 0 deletions internal/services/phone/externalcontact/external_contact_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package externalcontact

import "github.com/hashicorp/terraform-plugin-framework/types"

type readDto struct {
description types.String
email types.String
extensionNumber types.String
externalContactID types.String
id types.String
name types.String
phoneNumbers []types.String
routingPath types.String
autoCallRecorded types.Bool
}

type createDto struct {
description types.String
email types.String
extensionNumber types.String
externalContactID types.String
id types.String
name types.String
phoneNumbers []types.String
routingPath types.String
autoCallRecorded types.Bool
}

type createdDto struct {
externalContactID types.String
}

type updateDto struct {
externalContactID types.String
description types.String
email types.String
extensionNumber types.String
id types.String
name types.String
phoneNumbers []types.String
routingPath types.String
autoCallRecorded types.Bool
}
Loading

0 comments on commit c94a374

Please sign in to comment.