Skip to content

Commit d318c94

Browse files
feat(modules): adding module to retrieve api keys using a external graphql provider (#2728)
Co-authored-by: pranav-new-relic <[email protected]>
1 parent bd4ab81 commit d318c94

File tree

6 files changed

+247
-0
lines changed

6 files changed

+247
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Module: Create Access Keys and Fetch Access keys:
2+
3+
## Overview
4+
This module may be used to create a user or ingest key using the `newrelic_api_access_key` resource, and fetch the created key, by performing a NerdGraph query under the hood, using the ID of the key created via the resource to fetch the created key.
5+
6+
### Outputs
7+
The following output values are provided by the module:
8+
9+
* `key`: The actual API key.
10+
* `name`: The name of the key.
11+
* `type`: The type of API key.
12+
* `ingest_type`: The type of ingest (applicable only for key_type = INGEST).
13+
14+
15+
### Example usage #1 (USER)
16+
```terraform
17+
module "create_access_keys" {
18+
source = "../examples/modules/newrelic_api_access_key_extended"
19+
20+
create_access_keys_service = {
21+
api_key = "NRAK-XXXXXXXXXX"
22+
newrelic_account_id = "12345678"
23+
name = "Access key for DemoApp"
24+
key_type = "USER"
25+
user_id = 12345623445
26+
}
27+
}
28+
29+
output "required_attributes" {
30+
value = module.create_access_keys.required_attributes
31+
}
32+
```
33+
### Example usage #2 (INGEST-LICENSE)
34+
```terraform
35+
module "create_access_keys" {
36+
source = "../examples/modules/newrelic_api_access_key_extended"
37+
38+
create_access_keys_service = {
39+
api_key = "NRAK-XXXXXXXXXX"
40+
newrelic_account_id = "12345678"
41+
name = "DemoApp"
42+
key_type = "USER"
43+
ingest_type = "LICENSE"
44+
}
45+
}
46+
47+
output "required_attributes" {
48+
value = module.create_access_keys.required_attributes
49+
}
50+
```
51+
### Example usage #3 (INGEST-BROWSER)
52+
```terraform
53+
module "create_access_keys" {
54+
source = "../examples/modules/newrelic_api_access_key_extended"
55+
56+
create_access_keys_service = {
57+
api_key = "NRAK-XXXXXXXXXX"
58+
newrelic_account_id = "12345678"
59+
name = "DemoApp"
60+
key_type = "USER"
61+
ingest_type = "BROWSER"
62+
}
63+
}
64+
65+
output "required_attributes" {
66+
value = module.create_access_keys.required_attributes
67+
}
68+
```
69+
70+
## Overview
71+
This module may be used to fetch a user or ingest key, using the ID of the key. Note that the ID of a key can be copied from the New Relic One UI, and is also exported by the newrelic_api_access_key resource in the New Relic Terraform Provider, if the key is created using this resource.
72+
73+
### Outputs
74+
The following output values are provided by the module:
75+
76+
* `key`: The actual API key
77+
* `name`: The name of the key.
78+
* `type`: The type of API key
79+
* `ingest_type`: The type of ingest (applicable only for key_type = INGEST).
80+
81+
82+
### Example usage
83+
```terraform
84+
module "fetch_access_keys" {
85+
source = "../examples/modules/newrelic_api_access_key_extended"
86+
87+
fetch_access_keys_service = {
88+
api_key = "NRAK-XXXXXXXXXXXXXXXX"
89+
key_id = "DWEGHFF327532576931786356532327538273"
90+
key_type = "INGEST"
91+
}
92+
}
93+
94+
output "required_attributes" {
95+
value = module.fetch_access_keys.required_attributes
96+
}
97+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
provider "graphql" {
3+
url = var.fetch_access_keys_service.graphiql_url
4+
headers = {
5+
"Content-Type" = "application/json"
6+
"API-Key" = var.fetch_access_keys_service.api_key != "" ? var.fetch_access_keys_service.api_key : var.create_access_keys_service.api_key
7+
}
8+
}
9+
10+
data "graphql_query" "basic_query" {
11+
query_variables = {
12+
"id" = var.fetch_access_keys_service.key_id
13+
"key_type" = var.fetch_access_keys_service.key_type
14+
}
15+
query = <<EOF
16+
query getUser($id: ID!, $key_type: ApiAccessKeyType!) {
17+
actor {
18+
apiAccess {
19+
key(id: $id, keyType: $key_type) {
20+
key
21+
name
22+
type
23+
... on ApiAccessIngestKey {
24+
ingestType
25+
}
26+
}
27+
}
28+
}
29+
}
30+
EOF
31+
count = local.is_resource_created ? 0 : 1
32+
}
33+
34+
resource "newrelic_api_access_key" "api_access_key" {
35+
count = var.create_access_keys_service.newrelic_account_id != "" ? 1 : 0
36+
account_id = var.create_access_keys_service.newrelic_account_id
37+
key_type = var.create_access_keys_service.key_type
38+
name = "${var.create_access_keys_service.key_type != "USER" ? "APM " : "" }${var.create_access_keys_service.key_type}${var.create_access_keys_service.key_type != "USER" ? "-" : "" }${var.create_access_keys_service.ingest_type} Key for ${var.create_access_keys_service.name}"
39+
notes = var.create_access_keys_service.notes
40+
user_id = var.create_access_keys_service.key_type == "USER" ? var.create_access_keys_service.user_id : null
41+
ingest_type = var.create_access_keys_service.key_type == "INGEST" ? var.create_access_keys_service.ingest_type : null
42+
}
43+
44+
data "graphql_query" "query_with_id" {
45+
query_variables = {
46+
"id" = newrelic_api_access_key.api_access_key[0].id
47+
"key_type" = var.create_access_keys_service.key_type
48+
}
49+
query = <<EOF
50+
query getUser($id: ID!, $key_type: ApiAccessKeyType!) {
51+
actor {
52+
apiAccess {
53+
key(id: $id, keyType: $key_type) {
54+
key
55+
name
56+
type
57+
... on ApiAccessIngestKey {
58+
ingestType
59+
}
60+
}
61+
}
62+
}
63+
}
64+
EOF
65+
depends_on = [newrelic_api_access_key.api_access_key]
66+
count = local.is_resource_created ? 1 : 0
67+
}
68+
69+
70+
71+
72+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
output "required_attributes" {
3+
value = {
4+
"key": local.key,
5+
"name": local.name,
6+
"key_type": local.type,
7+
"ingest_type": local.ingestType
8+
}
9+
}
10+
11+
output "key_id" {
12+
value = length(newrelic_api_access_key.api_access_key) > 0 ? newrelic_api_access_key.api_access_key[0].id : null
13+
}
14+
15+
output "key" {
16+
value = length(newrelic_api_access_key.api_access_key) > 0 ? newrelic_api_access_key.api_access_key[0].key : null
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
required_providers {
3+
newrelic = {
4+
source = "newrelic/newrelic"
5+
}
6+
graphql = {
7+
source = "sullivtr/graphql"
8+
}
9+
}
10+
}
11+
12+
provider "newrelic" {
13+
region = "US" # US or EU
14+
}
15+
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
locals {
2+
response = local.is_resource_created ? jsondecode(data.graphql_query.query_with_id[0].query_response): jsondecode(data.graphql_query.basic_query[0].query_response)
3+
key = local.response["data"]["actor"]["apiAccess"]["key"]["key"]
4+
name = local.response["data"]["actor"]["apiAccess"]["key"]["name"]
5+
type = local.response["data"]["actor"]["apiAccess"]["key"]["type"]
6+
ingestType = lookup(local.response["data"]["actor"]["apiAccess"]["key"],"ingestType",null)
7+
is_resource_created = var.create_access_keys_service.newrelic_account_id != ""
8+
}
9+
10+
variable "fetch_access_keys_service" {
11+
description = "The service is to get api keys"
12+
type = object({
13+
api_key = string
14+
key_id = string
15+
key_type = string
16+
graphiql_url = optional(string,"https://api.newrelic.com/graphql")
17+
})
18+
default = {
19+
api_key = ""
20+
key_id = "XXXX"
21+
key_type = "XXXX"
22+
}
23+
}
24+
25+
variable "create_access_keys_service" {
26+
description = "The service is to create api keys"
27+
type = object({
28+
api_key = string
29+
newrelic_account_id = string
30+
name = optional(string,"New API Key")
31+
key_type = string
32+
ingest_type = optional(string,"")
33+
notes = optional(string,"API Key created using the newrelic_api_access_key Terraform resource")
34+
user_id = optional(string,null)
35+
})
36+
default = {
37+
api_key = ""
38+
newrelic_account_id = ""
39+
key_type = "INGEST"
40+
}
41+
}

website/docs/r/api_access_key.html.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ For example:
6464
```
6565
$ terraform import newrelic_api_access_key.foobar "1234567:INGEST"
6666
```
67+
## Extended Usage
68+
This module may be used to create a user or ingest key using the `create_access_keys_service` resource, and fetch the created key using `fetch_access_keys_service`, by performing a NerdGraph query under the hood, using the ID of the key created via the resource to fetch the created key.
69+
Please refer
70+
[create access keys and fetch access keys](https://github.com/newrelic/terraform-provider-newrelic/blob/main/examples/modules/golden-signal-alerts-new/README.md) for more info.

0 commit comments

Comments
 (0)