Skip to content

Commit

Permalink
fix: requested changes
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Theuermann <[email protected]>
  • Loading branch information
mati007thm committed Dec 9, 2024
1 parent 30d3099 commit 805df29
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 17 deletions.
8 changes: 4 additions & 4 deletions docs/resources/integration_email.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "mondoo_integration_email Resource - terraform-provider-mondoo"
subcategory: ""
description: |-
Send an email message to your ticket system (or any destination).
Send an email to your ticket system, or any recipient.
---

# mondoo_integration_email (Resource)

Send an email message to your ticket system (or any destination).
Send an email to your ticket system, or any recipient.

## Example Usage

Expand Down Expand Up @@ -51,8 +51,8 @@ resource "mondoo_integration_email" "email_integration" {

### Optional

- `auto_close` (Boolean) Auto close tickets.
- `auto_create` (Boolean) Auto create tickets.
- `auto_close` (Boolean) Auto close tickets (defaults to false).
- `auto_create` (Boolean) Auto create tickets (defaults to false).
- `space_id` (String) Mondoo Space Identifier. If it is not provided, the provider space is used.

### Read-Only
Expand Down
14 changes: 14 additions & 0 deletions internal/provider/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,19 @@ type ShodanConfigurationOptions struct {
Targets []string
}

type EmailConfigurationOptions struct {
Recipients []EmailRecipient
AutoCreateTickets bool
AutoCloseTickets bool
}

type EmailRecipient struct {
Name string
Email string
IsDefault bool
ReferenceURL string
}

type ClientIntegrationConfigurationOptions struct {
AzureConfigurationOptions AzureConfigurationOptions `graphql:"... on AzureConfigurationOptions"`
HostConfigurationOptions HostConfigurationOptions `graphql:"... on HostConfigurationOptions"`
Expand All @@ -618,6 +631,7 @@ type ClientIntegrationConfigurationOptions struct {
GithubConfigurationOptions GithubConfigurationOptions `graphql:"... on GithubConfigurationOptions"`
HostedAwsConfigurationOptions HostedAwsConfigurationOptions `graphql:"... on HostedAwsConfigurationOptions"`
ShodanConfigurationOptions ShodanConfigurationOptions `graphql:"... on ShodanConfigurationOptions"`
EmailConfigurationOptions EmailConfigurationOptions `graphql:"... on EmailConfigurationOptions"`
// Add other configuration options here
}

Expand Down
56 changes: 43 additions & 13 deletions internal/provider/integration_email_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ type integrationEmailRecipientInput struct {
ReferenceURL types.String `tfsdk:"reference_url"`
}

func (m integrationEmailResourceModel) GetConfigurationOptions() *mondoov1.EmailConfigurationOptionsInput {
opts := &mondoov1.EmailConfigurationOptionsInput{
Recipients: convertRecipients(m.Recipients),
AutoCreateTickets: mondoov1.NewBooleanPtr(mondoov1.Boolean(m.AutoCreateTickets.ValueBool())),
AutoCloseTickets: mondoov1.NewBooleanPtr(mondoov1.Boolean(m.AutoCloseTickets.ValueBool())),
}

return opts
}

func (r *integrationEmailResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_integration_email"
}
Expand Down Expand Up @@ -190,7 +200,7 @@ func NewAutoCreateValidator() validator.Bool {

func (r *integrationEmailResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Send an email message to your ticket system (or any destination).",
MarkdownDescription: "Send an email to your ticket system, or any recipient.",
Attributes: map[string]schema.Attribute{
"space_id": schema.StringAttribute{
MarkdownDescription: "Mondoo Space Identifier. If it is not provided, the provider space is used.",
Expand Down Expand Up @@ -248,14 +258,14 @@ func (r *integrationEmailResource) Schema(ctx context.Context, req resource.Sche
},
},
"auto_create": schema.BoolAttribute{
MarkdownDescription: "Auto create tickets.",
MarkdownDescription: "Auto create tickets (defaults to false).",
Optional: true,
Validators: []validator.Bool{
NewAutoCreateValidator(),
},
},
"auto_close": schema.BoolAttribute{
MarkdownDescription: "Auto close tickets.",
MarkdownDescription: "Auto close tickets (defaults to false).",
Optional: true,
},
},
Expand Down Expand Up @@ -322,11 +332,7 @@ func (r *integrationEmailResource) Create(ctx context.Context, req resource.Crea
data.Name.ValueString(),
mondoov1.ClientIntegrationTypeTicketSystemEmail,
mondoov1.ClientIntegrationConfigurationInput{
EmailConfigurationOptions: &mondoov1.EmailConfigurationOptionsInput{
Recipients: convertRecipients(data.Recipients),
AutoCreateTickets: mondoov1.NewBooleanPtr(mondoov1.Boolean(data.AutoCreateTickets.ValueBool())),
AutoCloseTickets: mondoov1.NewBooleanPtr(mondoov1.Boolean(data.AutoCloseTickets.ValueBool())),
},
EmailConfigurationOptions: data.GetConfigurationOptions(),
})
if err != nil {
resp.Diagnostics.
Expand Down Expand Up @@ -373,11 +379,7 @@ func (r *integrationEmailResource) Update(ctx context.Context, req resource.Upda

// Do GraphQL request to API to update the resource.
opts := mondoov1.ClientIntegrationConfigurationInput{
EmailConfigurationOptions: &mondoov1.EmailConfigurationOptionsInput{
Recipients: convertRecipients(data.Recipients),
AutoCreateTickets: mondoov1.NewBooleanPtr(mondoov1.Boolean(data.AutoCreateTickets.ValueBool())),
AutoCloseTickets: mondoov1.NewBooleanPtr(mondoov1.Boolean(data.AutoCloseTickets.ValueBool())),
},
EmailConfigurationOptions: data.GetConfigurationOptions(),
}

_, err := r.client.UpdateIntegration(ctx,
Expand Down Expand Up @@ -418,3 +420,31 @@ func (r *integrationEmailResource) Delete(ctx context.Context, req resource.Dele
return
}
}

func (r *integrationEmailResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
integration, ok := r.client.ImportIntegration(ctx, req, resp)
if !ok {
return
}

var recipients []integrationEmailRecipientInput
for _, recipient := range integration.ConfigurationOptions.EmailConfigurationOptions.Recipients {
recipients = append(recipients, integrationEmailRecipientInput{
Name: types.StringValue(recipient.Name),
Email: types.StringValue(recipient.Email),
IsDefault: types.BoolValue(recipient.IsDefault),
ReferenceURL: types.StringValue(recipient.ReferenceURL),
})
}

model := integrationEmailResourceModel{
Mrn: types.StringValue(integration.Mrn),
Name: types.StringValue(integration.Name),
SpaceID: types.StringValue(integration.SpaceID()),
AutoCreateTickets: types.BoolValue(integration.ConfigurationOptions.EmailConfigurationOptions.AutoCreateTickets),
AutoCloseTickets: types.BoolValue(integration.ConfigurationOptions.EmailConfigurationOptions.AutoCloseTickets),
Recipients: &recipients,
}

resp.State.Set(ctx, &model)
}
96 changes: 96 additions & 0 deletions internal/provider/integration_email_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccEmailIntegrationResource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing
{
Config: testAccEmailIntegrationResourceConfig(accSpace.ID(), "one", []map[string]interface{}{
{"name": "John Doe", "email": "[email protected]", "is_default": true, "reference_url": "https://example.com"},
{"name": "Alice Doe", "email": "[email protected]", "is_default": false, "reference_url": "https://example.com"},
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("mondoo_integration_email.email_integration", "name", "one"),
resource.TestCheckResourceAttr("mondoo_integration_email.email_integration", "space_id", accSpace.ID()),
),
},
{
Config: testAccEmailIntegrationResourceWithSpaceInProviderConfig(accSpace.ID(), "two", true, true),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("mondoo_integration_email.email_integration", "name", "two"),
resource.TestCheckResourceAttr("mondoo_integration_email.email_integration", "space_id", accSpace.ID()),
),
},
// Update and Read testing
{
Config: testAccEmailIntegrationResourceConfig(accSpace.ID(), "three", []map[string]interface{}{
{"name": "John Doe", "email": "[email protected]", "is_default": true, "reference_url": "https://newurl.com"},
{"name": "Alice Doe", "email": "[email protected]", "is_default": false, "reference_url": "https://newurl.com"},
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("mondoo_integration_email.email_integration", "name", "three"),
resource.TestCheckResourceAttr("mondoo_integration_email.email_integration", "space_id", accSpace.ID()),
),
},
{
Config: testAccEmailIntegrationResourceWithSpaceInProviderConfig(accSpace.ID(), "four", false, false),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("mondoo_integration_email.email_integration", "name", "four"),
resource.TestCheckResourceAttr("mondoo_integration_email.email_integration", "space_id", accSpace.ID()),
),
},
// Delete testing automatically occurs in TestCase
},
})
}

func testAccEmailIntegrationResourceConfig(spaceID, intName string, recipients []map[string]interface{}) string {
return fmt.Sprintf(`
resource "mondoo_integration_email" "email_integration" {
space_id = %[1]q
name = %[2]q
recipients = %[3]q
auto_create = true
auto_close = true
}
`, spaceID, intName, recipients)
}

func testAccEmailIntegrationResourceWithSpaceInProviderConfig(spaceID, intName string, autoCreate, autoClose bool) string {
return fmt.Sprintf(`
provider "mondoo" {
space = %[1]q
}
resource "mondoo_integration_email" "email_integration" {
name = %[2]q
recipients = [
{
name = "John Doe"
email = "[email protected]"
is_default = true
reference_url = "https://example.com"
},
{
name = "Alice Doe"
email = "[email protected]"
is_default = false
reference_url = "https://example.com"
}
]
auto_create = %[3]t
auto_close = %[4]t
}
`, spaceID, intName, autoCreate, autoClose)
}

0 comments on commit 805df29

Please sign in to comment.