Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add discovery rules resource and webhooks #189

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add resources webhook & webhook_template
  • Loading branch information
Dirk committed Jan 28, 2025
commit 253c9dccbf8d24891873836495e599d9f690c2af
59 changes: 59 additions & 0 deletions docs/resources/foreman_webhook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# foreman_webhook

Webhooks provide integration to 3rd parties via web services with configurable payload (via foreman_webhooktemplate).

## Example Usage

```terraform
resource "foreman_webhook" "example_webhook_01" {
name = "Example Webhook"
target_url = "https://example-webhook.local:8080"
http_method = "GET"
http_content_type = "application/json"
http_headers = "{\n\"X-Shellhook-Arg-1\":\"value\"\n}"
event = "build_entered.event.foreman"
enabled = true
verify_ssl = false
ssl_ca_certs = ""
proxy_authorization = false
user = "foo"
password = "bar"
webhook_template_id = 1
}
```

## Argument Reference

The following arguments are supported:

- `name` - (Required) The name of the webhook.
- `target_url` - (Required) The URL for the webhook.
- `http_method` - (Optional) Must be one of: POST, GET, PUT, DELETE, PATCH.
- `http_content_type` - (Optional) Content Type Header.
- `http_headers` - (Optional) Additional Headers to send. Must be a json object.
- `event` - (Required) An string specifying the event type for which the webhook is triggered.
- `enabled` - (Optional) A boolean value indicating whether the webhook is enabled. When set to `true`, the rule is active and will be evaluated.
- `verify_ssl` - (Optional) A boolean value indicating if SSL certs should be verified.
- `ssl_ca_certs` - (Optional) X509 Certification Authorities for verification concatenated in PEM format.
- `proxy_authorization` - (Optional) Indicating whether to authorize with Foreman client certificate and validate smart-proxy CA from Settings
- `user` - (Optional) User name for basic authentication.
- `password` - (Optional) Password for basic authentication.
- `webhook_template_id` - (Optional) ID of the webhook template containing the payload.

## Attributes Reference

The following attributes are exported:

- `name` - The name of the webhook.
- `target_url` - The URL for the webhook.
- `http_method` - Must be one of: POST, GET, PUT, DELETE, PATCH.
- `http_content_type` - Content Type Header.
- `http_headers` - Additional Headers to send. Must be a json object.
- `event` - An string specifying the event type for which the webhook is triggered.
- `enabled` - A boolean value indicating whether the webhook is enabled. When set to `true`, the rule is active and will be evaluated.
- `verify_ssl` - A boolean value indicating if SSL certs should be verified.
- `ssl_ca_certs` - X509 Certification Authorities for verification concatenated in PEM format.
- `proxy_authorization` - Indicating whether to authorize with Foreman client certificate and validate smart-proxy CA from Settings.
- `user` - User name for basic authentication.
- `password_set` - If a password is set for basic authentication.
- `webhook_template` - Webhook template containing the payload.
47 changes: 47 additions & 0 deletions docs/resources/foreman_webhooktemplate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# foreman_webhooktemplate

Webhook templates allow to configure a payload to send via webhook.

## Example Usage

```terraform
resource "foreman_webhooktemplate" "example_webhooktemplate_01" {
name = "Example Webhook Template"
template = "<%=\npayload({\n id: @object.id\n})\n-%>\n"
snippet = false
audit_comment = ""
locked = true
default = true
description = "This template is used to define default content of payload for a webhook."
location_ids = [2]
organization_ids = [1]
}
```

## Argument Reference

The following arguments are supported:

- `name` - (Required) The name of the webhook template.
- `template` - (Required) The content of the webhook template.
- `snippet` - (Optional) Specifies if webhook template is a snippet.
- `audit_comment` - (Optional) Comment for audits.
- `locked` - (Optional) Whether the template is locked for editing.
- `default` - (Optional) Whether the template is automatically added to new organizations and locations.
- `description` - (Optional) Webhook Template description.
- `location_ids` - (Optional) A list of location IDs where the discovered hosts will be assigned.
- `organization_ids` - (Optional) A list of organization IDs where the discovered hosts will be assigned.

## Attributes Reference

The following attributes are exported:

- `name` - The name of the webhook template.
- `template` - The content of the webhook template.
- `snippet` - Specifies if webhook template is a snippet.
- `audit_comment` - Comment for audits.
- `locked` - Whether the template is locked for editing.
- `default` - Whether the template is automatically added to new organizations and locations.
- `description` - Webhook Template description.
- `locations` - A list of locations where the discovered hosts will be assigned.
- `organizations` - A list of organizations where the discovered hosts will be assigned.
25 changes: 25 additions & 0 deletions examples/webhook/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
provider "foreman" {
server_hostname = "192.168.1.118"
server_protocol = "https"

client_tls_insecure = true

client_username = "${var.client_username}"
client_password = "${var.client_password}"
}

resource "foreman_webhook" "example_webhook_01" {
name = "Example Webhook"
target_url = "https://example-webhook.local:8080"
http_method = "GET"
http_content_type = "application/json"
http_headers = "{\n\"X-Shellhook-Arg-1\":\"value\"\n}"
event = "build_entered.event.foreman"
enabled = true
verify_ssl = false
ssl_ca_certs = ""
proxy_authorization = false
user = "foo"
password = "bar"
webhook_template_id = 1
}
21 changes: 21 additions & 0 deletions examples/webhook_template/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
provider "foreman" {
server_hostname = "192.168.1.118"
server_protocol = "https"

client_tls_insecure = true

client_username = "${var.client_username}"
client_password = "${var.client_password}"
}

resource "foreman_webhooktemplate" "example_webhooktemplate_01" {
name = "Example Webhook Template"
template = "<%=\npayload({\n id: @object.id\n})\n-%>\n"
snippet = false
audit_comment = ""
locked = true
default = true
description = "This template is used to define default content of payload for a webhook."
location_ids = [2]
organization_ids = [1]
}
8 changes: 8 additions & 0 deletions foreman/api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,11 @@ type QueryResponsePuppet struct {
// Foreman API objects that matched the search criteria for the query.
Results map[string]interface{} `json:"results"`
}

// Organizations and Locations response
type EntityResponse struct {
ID int `json:"id"`
Name string `json:"name"`
Title string `json:"title"`
Description any `json:"description"`
}
7 changes: 0 additions & 7 deletions foreman/api/discovery_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ type ForemanDiscoveryRuleResponse struct {
Organizations []EntityResponse `json:"organizations,omitempty"`
}

type EntityResponse struct {
ID int `json:"id"`
Name string `json:"name"`
Title string `json:"title"`
Description any `json:"description"`
}

// CreateDiscoveryRule creates a new ForemanDiscoveryRule
func (c *Client) CreateDiscoveryRule(ctx context.Context, d *ForemanDiscoveryRule) (*ForemanDiscoveryRule, error) {
log.Tracef("foreman/api/discovery_rule.go#Create")
Expand Down
Loading