Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ resource "algolia_api_key" "example" {
description = "This is a example api key"
indexes = [algolia_index.example.name]
referers = ["https://algolia.com/\\*"]
query_parameters = ""
}
```

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/api_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ resource "algolia_api_key" "example" {
description = "This is a example api key"
indexes = ["*"]
referers = ["https://algolia.com/\\*"]
query_parameters = ""
}
```

Expand Down Expand Up @@ -49,6 +50,7 @@ The possible ACLs are:
### Optional

- `description` (String) Description of the API key.
- `query_parameters` (String) Specify the list of query parameters. You can force the query parameters for a query using the url string format.
- `expires_at` (String) Unix timestamp of the date at which the key expires. RFC3339 format. Will not expire per default.
- `indexes` (Set of String) List of targeted indices. You can target all indices starting with a prefix or ending with a suffix using the ‘*’ character. For example, “dev_*” matches all indices starting with “dev_” and “*_dev” matches all indices ending with “_dev”.
- `max_hits_per_query` (Number) Maximum number of hits this API key can retrieve in one call. This parameter can be used to protect you from attempts at retrieving your entire index contents by massively querying the index.
Expand Down
1 change: 1 addition & 0 deletions examples/resources/algolia_api_key/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ resource "algolia_api_key" "example" {
description = "This is a example api key"
indexes = ["*"]
referers = ["https://algolia.com/\\*"]
query_parameters = ""
}
17 changes: 17 additions & 0 deletions internal/provider/datautils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/algolia/algoliasearch-client-go/v3/algolia/transport"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
)

func setValues(d *schema.ResourceData, values map[string]interface{}) error {
Expand Down Expand Up @@ -46,3 +48,18 @@ func castInterfaceMap(m interface{}) map[string]interface{} {
}
return interfaceMap
}

func castKeyQueryParams(qp string) KeyQueryParams {

type QueryParameters KeyQueryParams

err = transport.URLDecode(
[]byte(qp),
&QueryParameters,
)
if err != nil {
return fmt.Errorf("cannot decode QueryParameters %q: %v", tmp.QueryParameters, err)
}

return QueryParameters
}
8 changes: 8 additions & 0 deletions internal/provider/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/algolia/algoliasearch-client-go/v3/algolia/transport"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -89,6 +90,11 @@ This parameter can be used to protect you from attempts at retrieving your entir
Optional: true,
Description: "List of referrers that can perform an operation. You can use the “*” (asterisk) character as a wildcard to match subdomains, or all pages of a website. For example, `\"https://algolia.com/\\*\"` matches all referrers starting with `\"https://algolia.com/\"`, and `\"\\*.algolia.com\"` matches all referrers ending with `\".algolia.com\"`. If you want to allow all possible referrers from the `algolia.com` domain, you can use `\"\\*algolia.com/\\*\"`.",
},
"query_parameters": {
Type: schema.TypeString,
Optional: true,
Description: "Specify the list of query parameters. You can force the query parameters for a query using the url string format.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -190,6 +196,7 @@ func refreshAPIKeyState(ctx context.Context, d *schema.ResourceData, m interface
"max_hits_per_query": key.MaxHitsPerQuery,
"max_queries_per_ip_per_hour": key.MaxQueriesPerIPPerHour,
"referers": key.Referers,
"query_parameters": transport.URLEncode(key.QueryParameters),
"description": key.Description,
"indexes": key.Indexes,
"created_at": key.CreatedAt.Unix(),
Expand Down Expand Up @@ -221,6 +228,7 @@ func mapToAPIKey(d *schema.ResourceData) search.Key {
MaxQueriesPerIPPerHour: d.Get("max_queries_per_ip_per_hour").(int),
Indexes: castStringSet(d.Get("indexes")),
Referers: castStringSet(d.Get("referers")),
QueryParameters: castKeyQueryParams(d.Get("query_parameters")),
Description: d.Get("description").(string),
}
}
3 changes: 3 additions & 0 deletions internal/provider/resource_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestAccResourceAPIKey(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "max_queries_per_ip_per_hour", "0"),
resource.TestCheckNoResourceAttr(resourceName, "indexes.0"),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "query_parameters", ""),
),
},
{
Expand All @@ -42,6 +43,7 @@ func TestAccResourceAPIKey(t *testing.T) {
testCheckResourceListAttr(resourceName, "indexes", []string{"dev_*"}),
testCheckResourceListAttr(resourceName, "referers", []string{"https://algolia.com/\\*"}),
resource.TestCheckResourceAttr(resourceName, "description", "This is a test api key"),
resource.TestCheckResourceAttr(resourceName, "query_parameters", "typoTolerance=strict&ignorePlurals=false"),
),
},
{
Expand Down Expand Up @@ -75,6 +77,7 @@ resource "algolia_api_key" "%s" {
indexes = ["dev_*"]
referers = ["https://algolia.com/\\*"]
description = "This is a test api key"
query_parameters = "typoTolerance=strict&ignorePlurals=false"
}`, name)
}

Expand Down