diff --git a/docs/resources/integration_zendesk.md b/docs/resources/integration_zendesk.md new file mode 100644 index 0000000..b0cd4cb --- /dev/null +++ b/docs/resources/integration_zendesk.md @@ -0,0 +1,76 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "mondoo_integration_zendesk Resource - terraform-provider-mondoo" +subcategory: "" +description: |- + Zendesk integration to keep track of security tasks and add Zendesk tickets directly from within the Mondoo Console. +--- + +# mondoo_integration_zendesk (Resource) + +Zendesk integration to keep track of security tasks and add Zendesk tickets directly from within the Mondoo Console. + +## Example Usage + +```terraform +variable "zendesk_token" { + description = "The GitHub Token" + type = string + sensitive = true +} + +provider "mondoo" { + space = "hungry-poet-123456" +} + +# Setup the zendesk integration +resource "mondoo_integration_zendesk" "zendesk_integration" { + name = "My Zendesk Integration" + subdomain = "your-subdomain" + email = "zendeskowner@email.com" + api_token = var.zendesk_token + + custom_fields = [ + { + id = "123456" + value = "custom_value_1" + }, + { + id = "123457" + value = "custom_value_2" + } + ] + + auto_create = true + auto_close = true +} +``` + + +## Schema + +### Required + +- `api_token` (String, Sensitive) Zendesk API token. +- `email` (String) Zendesk email. +- `name` (String) Name of the integration. +- `subdomain` (String) Zendesk subdomain. + +### Optional + +- `auto_close` (Boolean) Automatically close tickets. +- `auto_create` (Boolean) Automatically create tickets. +- `custom_fields` (Attributes List) Custom fields to be added to the Zendesk ticket. (see [below for nested schema](#nestedatt--custom_fields)) +- `space_id` (String) Mondoo Space Identifier. If it is not provided, the provider space is used. + +### Read-Only + +- `mrn` (String) Integration identifier + + +### Nested Schema for `custom_fields` + +Required: + +- `id` (Number) Custom field ID. +- `value` (String) Custom field value. diff --git a/examples/resources/mondoo_integration_zendesk/main.tf b/examples/resources/mondoo_integration_zendesk/main.tf new file mode 100644 index 0000000..24d24a1 --- /dev/null +++ b/examples/resources/mondoo_integration_zendesk/main.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + mondoo = { + source = "mondoohq/mondoo" + version = ">= 0.19" + } + } +} \ No newline at end of file diff --git a/examples/resources/mondoo_integration_zendesk/resource.tf b/examples/resources/mondoo_integration_zendesk/resource.tf new file mode 100644 index 0000000..b0f66f4 --- /dev/null +++ b/examples/resources/mondoo_integration_zendesk/resource.tf @@ -0,0 +1,31 @@ +variable "zendesk_token" { + description = "The GitHub Token" + type = string + sensitive = true +} + +provider "mondoo" { + space = "hungry-poet-123456" +} + +# Setup the zendesk integration +resource "mondoo_integration_zendesk" "zendesk_integration" { + name = "My Zendesk Integration" + subdomain = "your-subdomain" + email = "zendeskowner@email.com" + api_token = var.zendesk_token + + custom_fields = [ + { + id = "123456" + value = "custom_value_1" + }, + { + id = "123457" + value = "custom_value_2" + } + ] + + auto_create = true + auto_close = true +} \ No newline at end of file diff --git a/internal/provider/integration_zendesk_resource.go b/internal/provider/integration_zendesk_resource.go new file mode 100644 index 0000000..fa60678 --- /dev/null +++ b/internal/provider/integration_zendesk_resource.go @@ -0,0 +1,288 @@ +package provider + +import ( + "context" + "fmt" + "regexp" + + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + mondoov1 "go.mondoo.com/mondoo-go" +) + +var _ resource.Resource = (*integrationZendeskResource)(nil) + +func NewIntegrationZendeskResource() resource.Resource { + return &integrationZendeskResource{} +} + +type integrationZendeskResource struct { + client *ExtendedGqlClient +} + +type integrationZendeskResourceModel struct { + // scope + SpaceID types.String `tfsdk:"space_id"` + + // integration details + Mrn types.String `tfsdk:"mrn"` + Name types.String `tfsdk:"name"` + Subdomain types.String `tfsdk:"subdomain"` + Email types.String `tfsdk:"email"` + APIToken types.String `tfsdk:"api_token"` + + // (Optional.) + AutoClose types.Bool `tfsdk:"auto_close"` + AutoCreate types.Bool `tfsdk:"auto_create"` + CustomFields *[]integrationZendeskCustomFieldModel `tfsdk:"custom_fields"` +} + +type integrationZendeskCustomFieldModel struct { + ID types.Int64 `tfsdk:"id"` + Value types.String `tfsdk:"value"` +} + +func (r *integrationZendeskResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_integration_zendesk" +} + +func (r *integrationZendeskResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: `Zendesk integration to keep track of security tasks and add Zendesk tickets directly from within the Mondoo Console.`, + Attributes: map[string]schema.Attribute{ + "space_id": schema.StringAttribute{ + MarkdownDescription: "Mondoo Space Identifier. If it is not provided, the provider space is used.", + Optional: true, + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "mrn": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "Integration identifier", + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "Name of the integration.", + Required: true, + Validators: []validator.String{ + stringvalidator.LengthAtMost(250), + }, + }, + "subdomain": schema.StringAttribute{ + MarkdownDescription: "Zendesk subdomain.", + Required: true, + }, + "email": schema.StringAttribute{ + MarkdownDescription: "Zendesk email.", + Required: true, + Validators: []validator.String{ + stringvalidator.RegexMatches( + regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`), + "must be a valid email", + ), + }, + }, + "api_token": schema.StringAttribute{ + MarkdownDescription: "Zendesk API token.", + Required: true, + Sensitive: true, + }, + "auto_close": schema.BoolAttribute{ + MarkdownDescription: "Automatically close tickets.", + Optional: true, + }, + "auto_create": schema.BoolAttribute{ + MarkdownDescription: "Automatically create tickets.", + Optional: true, + }, + "custom_fields": schema.ListNestedAttribute{ + MarkdownDescription: "Custom fields to be added to the Zendesk ticket.", + Optional: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "id": schema.Int64Attribute{ + MarkdownDescription: "Custom field ID.", + Required: true, + }, + "value": schema.StringAttribute{ + MarkdownDescription: "Custom field value.", + Required: true, + }, + }, + }, + }, + }, + } +} + +func (r *integrationZendeskResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + // Prevent panic if the provider has not been configured. + if req.ProviderData == nil { + return + } + + client, ok := req.ProviderData.(*ExtendedGqlClient) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected Resource Configure Type", + fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + + return + } + + r.client = client +} + +func convertCustomFields(recipients *[]integrationZendeskCustomFieldModel) *[]mondoov1.ZendeskCustomFieldInput { + if recipients == nil { + return nil + } + var result []mondoov1.ZendeskCustomFieldInput + for _, r := range *recipients { + result = append(result, mondoov1.ZendeskCustomFieldInput{ + ID: mondoov1.Int(r.ID.ValueInt64()), + Value: mondoov1.String(r.Value.ValueString()), + }) + } + return &result +} + +func (r *integrationZendeskResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data integrationZendeskResourceModel + + // Read Terraform plan data into the model + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + // Compute and validate the space + space, err := r.client.ComputeSpace(data.SpaceID) + if err != nil { + resp.Diagnostics.AddError("Invalid Configuration", err.Error()) + return + } + ctx = tflog.SetField(ctx, "space_mrn", space.MRN()) + + // Do GraphQL request to API to create the resource. + tflog.Debug(ctx, "Creating integration") + integration, err := r.client.CreateIntegration(ctx, + space.MRN(), + data.Name.ValueString(), + mondoov1.ClientIntegrationTypeTicketSystemZendesk, + mondoov1.ClientIntegrationConfigurationInput{ + ZendeskConfigurationOptions: &mondoov1.ZendeskConfigurationOptionsInput{ + Subdomain: mondoov1.String(data.Subdomain.ValueString()), + Email: mondoov1.String(data.Email.ValueString()), + APIToken: mondoov1.String(data.APIToken.ValueString()), + AutoCloseTickets: mondoov1.Boolean(data.AutoClose.ValueBool()), + AutoCreateTickets: mondoov1.Boolean(data.AutoCreate.ValueBool()), + CustomFields: convertCustomFields(data.CustomFields), + }, + }) + if err != nil { + resp.Diagnostics. + AddError("Client Error", + fmt.Sprintf("Unable to create Zendesk integration, got error: %s", err), + ) + return + } + + // Save space mrn into the Terraform state. + data.Mrn = types.StringValue(string(integration.Mrn)) + data.Name = types.StringValue(string(integration.Name)) + data.SpaceID = types.StringValue(space.ID()) + + // Save data into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func (r *integrationZendeskResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data integrationZendeskResourceModel + + // Read Terraform prior state data into the model + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + // Read API call logic + + // Save updated data into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func (r *integrationZendeskResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data integrationZendeskResourceModel + + // Read Terraform plan data into the model + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + // Do GraphQL request to API to update the resource. + opts := mondoov1.ClientIntegrationConfigurationInput{ + ZendeskConfigurationOptions: &mondoov1.ZendeskConfigurationOptionsInput{ + Subdomain: mondoov1.String(data.Subdomain.ValueString()), + Email: mondoov1.String(data.Email.ValueString()), + APIToken: mondoov1.String(data.APIToken.ValueString()), + AutoCloseTickets: mondoov1.Boolean(data.AutoClose.ValueBool()), + AutoCreateTickets: mondoov1.Boolean(data.AutoCreate.ValueBool()), + CustomFields: convertCustomFields(data.CustomFields), + }, + } + + _, err := r.client.UpdateIntegration(ctx, + data.Mrn.ValueString(), + data.Name.ValueString(), + mondoov1.ClientIntegrationTypeTicketSystemZendesk, + opts, + ) + if err != nil { + resp.Diagnostics. + AddError("Client Error", + fmt.Sprintf("Unable to update Zendesk integration, got error: %s", err), + ) + return + } + + // Save updated data into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func (r *integrationZendeskResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var data integrationZendeskResourceModel + + // Read Terraform prior state data into the model + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + // Do GraphQL request to API to update the resource. + _, err := r.client.DeleteIntegration(ctx, data.Mrn.ValueString()) + if err != nil { + resp.Diagnostics. + AddError("Client Error", + fmt.Sprintf("Unable to delete Zendesk integration, got error: %s", err), + ) + return + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index fe54ab6..18f31a8 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -204,6 +204,7 @@ func (p *MondooProvider) Resources(ctx context.Context) []func() resource.Resour NewIntegrationShodanResource, NewFrameworkAssignmentResource, NewCustomFrameworkResource, + NewIntegrationZendeskResource, } }