Skip to content

Commit

Permalink
Add Konnect Terraform tab to plugin examples (#7244)
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap committed Apr 16, 2024
1 parent c4c2c0e commit 3aa883d
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 1 deletion.
27 changes: 27 additions & 0 deletions app/_includes/plugins/create-consumer/terraform.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% if include.hub_example.auth_plugin? %}

As this is an auth plugin, you need to create a consumer and attach a credential to it.

Create a consumer:

```bash
resource "konnect_gateway_consumer" "alex" {
username = "alex"
custom_id = "alex-custom"
control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
}
```

Create a {{ include.hub_example.plugin_name }} credential attached to this consumer:

{% assign plugin_name = include.hub_example.plugin_name | replace: "-","_" %}
```hcl
resource "konnect_gateway_{{ plugin_name }}" "my_{{ plugin_name }}" {
{%- for field in include.hub_example.auth_fields %}
{{ field[0] }} = {{ field[1] | quote }}{% endfor %}

consumer_id = konnect_gateway_consumer.alex.id
control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
}
```
{% endif %}
10 changes: 10 additions & 0 deletions app/_includes/plugins/hub-examples/consumer.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@
{% endnavtab %}
{% endif %}
{% endunless %}

{% unless page.terraform_examples == false %}
{% if hub_example.render_terraform? %}
{% navtab Konnect Terraform %}

{% include_cached plugins/hub-examples/terraform.html example=hub_example parent="consumer" %}

{% endnavtab %}
{% endif %}
{% endunless %}
{% endnavtabs %}
11 changes: 11 additions & 0 deletions app/_includes/plugins/hub-examples/consumer_groups.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,15 @@
{% endnavtab %}
{% endif %}
{% endunless %}

{% unless page.terraform_examples == false %}
{% if hub_example.render_terraform? %}
{% navtab Konnect Terraform %}

{% include_cached plugins/hub-examples/terraform.html example=hub_example parent="consumer_group" %}

{% endnavtab %}
{% endif %}
{% endunless %}

{% endnavtabs %}
13 changes: 13 additions & 0 deletions app/_includes/plugins/hub-examples/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,18 @@
{% endnavtab %}
{% endif %}
{% endunless %}

{% unless page.terraform_examples == false %}
{% if hub_example.render_terraform? %}
{% navtab Konnect Terraform %}

{% include_cached plugins/hub-examples/terraform.html example=hub_example %}

{% include_cached plugins/create-consumer/terraform.html hub_example=hub_example %}

{% endnavtab %}
{% endif %}
{% endunless %}

{% endnavtabs %}
{% endif %}
13 changes: 13 additions & 0 deletions app/_includes/plugins/hub-examples/route.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,17 @@
{% endnavtab %}
{% endif %}
{% endunless %}

{% unless page.terraform_examples == false %}
{% if hub_example.render_terraform? %}
{% navtab Konnect Terraform %}

{% include_cached plugins/hub-examples/terraform.html example=hub_example parent="route" %}

{% include_cached plugins/create-consumer/terraform.html hub_example=hub_example %}

{% endnavtab %}
{% endif %}
{% endunless %}

{% endnavtabs %}
12 changes: 12 additions & 0 deletions app/_includes/plugins/hub-examples/service.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@
{% endnavtab %}
{% endif %}
{% endunless %}

{% unless page.terraform_examples == false %}
{% if hub_example.render_terraform? %}
{% navtab Konnect Terraform %}

{% include_cached plugins/hub-examples/terraform.html example=hub_example parent="service" %}

{% include_cached plugins/create-consumer/terraform.html hub_example=hub_example %}

{% endnavtab %}
{% endif %}
{% endunless %}
{% endnavtabs %}
38 changes: 38 additions & 0 deletions app/_includes/plugins/hub-examples/terraform.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<details markdown="1">
<summary><strong>Prerequisite:</strong> Configure your Personal Access Token</summary>

```hcl
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}

provider "konnect" {
personal_access_token = "kpat_YOUR_TOKEN"
server_url = "https://us.api.konghq.com/v2"
}
```
</details>

Add the following to your Terraform configuration to create a Konnect Gateway Plugin:

{% assign plugin_name = include.example.plugin_name | replace: "-","_" %}
```hcl
resource "konnect_gateway_plugin_{{ plugin_name }}" "my_{{ plugin_name }}" {
enabled = true

config = {
{%- for item in include.example.example.config %}
{{ item[0] }} = {{ item[1] | quote }}{%- endfor %}
}

control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id{% if include.parent %}
{{ include.parent }} = {
id = konnect_gateway_{{ include.parent }}.my_{{ include.parent }}.id
}
{%- endif %}
}
```
39 changes: 39 additions & 0 deletions app/_plugins/drops/plugins/hub_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@

# There are also oauth2, acl and mtls-auth but I don't have examples of how to use them yet
AUTH_PLUGINS = %w[basic-auth hmac-auth jwt key-auth].freeze
TERRAFORM_ENABLED_PLUGINS = %w[
key-auth
cors
rate-limiting
basic-auth
rate-limiting-advanced
openid-connect
jwt
prometheus
acl
request-termination
file-log
request-transformer
correlation-id
proxy-cache
request-transformer-advanced
response-transformer
response-transformer-advanced
ip-restriction
pre-function
oauth2
opentelemetry
ai-proxy
ai-prompt-guard
ai-prompt-template
ai-prompt-decorator
aws-lambda
jq
jwt-signer
saml
].freeze

module Jekyll
module Drops
Expand Down Expand Up @@ -31,6 +62,14 @@ def render_kubernetes?
@formats.include?(:kubernetes)
end

def render_terraform?
terraform_implemented? && @formats.include?(:terraform)
end

def terraform_implemented?
TERRAFORM_ENABLED_PLUGINS.include?(plugin_name)
end

def auth_plugin?
AUTH_PLUGINS.include?(plugin_name)
end
Expand Down
13 changes: 13 additions & 0 deletions app/_plugins/filters/quote_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module QuoteFilter
def quote(input)
if input.is_a?(Numeric)
input
else
"\"#{input}\""
end
end
end

Liquid::Template.register_filter(QuoteFilter)
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def hub_examples
schema: @release.schema,
example: @release.schema.example,
targets: ::Jekyll::InlinePluginExample::Config::TARGETS,
formats: %i[curl konnect yaml kubernetes]
formats: %i[curl konnect yaml kubernetes terraform]
)
end

Expand Down

0 comments on commit 3aa883d

Please sign in to comment.