Skip to content

Commit 6918038

Browse files
authored
Add PDC network and PDC network token resources (#1975)
* Add PDC network and PDC network token resources - Add resource lister function that will return a slice of resources * Support PDC in data sources * Add examples and generate docs * Regen * Update data source property name * Add PDC network data sources * Rename resources for clarity - Tidy up property names * Some more naming updates * Update docs * Fix pdc network Id check * Update docs and add test * Docs update * Add reference to access policies naming reqs * Minor updates for type names and docs * Fix replace * Fix warning * Suppress pdc diffs * Remove resource lister * Remove type
1 parent 60edf0c commit 6918038

File tree

16 files changed

+883
-0
lines changed

16 files changed

+883
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_cloud_private_data_source_connect_networks Data Source - terraform-provider-grafana"
4+
subcategory: "Cloud"
5+
description: |-
6+
Fetches Private Data source Connect networks from Grafana Cloud.
7+
Official documentation https://grafana.com/docs/grafana-cloud/account-management/authentication-and-permissions/access-policies/API documentation https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#list-access-policies
8+
Required access policy scopes:
9+
accesspolicies:read
10+
---
11+
12+
# grafana_cloud_private_data_source_connect_networks (Data Source)
13+
14+
Fetches Private Data source Connect networks from Grafana Cloud.
15+
16+
* [Official documentation](https://grafana.com/docs/grafana-cloud/account-management/authentication-and-permissions/access-policies/)
17+
* [API documentation](https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#list-access-policies)
18+
19+
Required access policy scopes:
20+
21+
* accesspolicies:read
22+
23+
24+
25+
<!-- schema generated by tfplugindocs -->
26+
## Schema
27+
28+
### Optional
29+
30+
- `name_filter` (String) If set, only private data source connect networks with the specified name will be returned. This is faster than filtering in Terraform.
31+
- `region_filter` (String) If set, only private data source connect networks in the specified region will be returned. This is faster than filtering in Terraform.
32+
33+
### Read-Only
34+
35+
- `id` (String) The ID of this datasource. This is an internal identifier used by the provider to track this datasource.
36+
- `private_data_source_connect_networks` (Set of Object) (see [below for nested schema](#nestedatt--private_data_source_connect_networks))
37+
38+
<a id="nestedatt--private_data_source_connect_networks"></a>
39+
### Nested Schema for `private_data_source_connect_networks`
40+
41+
Read-Only:
42+
43+
- `display_name` (String)
44+
- `id` (String)
45+
- `name` (String)
46+
- `region` (String)
47+
- `status` (String)

docs/data-sources/data_source.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ data "grafana_data_source" "from_uid" {
5959
- `id` (String) The ID of this resource.
6060
- `is_default` (Boolean) Whether to set the data source as default. This should only be `true` to a single data source.
6161
- `json_data_encoded` (String) Serialized JSON string containing the json data. This attribute can be used to pass configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI. Note that keys in this map are usually camelCased.
62+
- `private_data_source_connect_network_id` (String) (Can only be used with data sources in Grafana Cloud) The ID of the Private Data source Connect network to use with this data source.
6263
- `type` (String) The data source type. Must be one of the supported data source keywords.
6364
- `url` (String) The URL for the data source. The type of URL required varies depending on the chosen data source type.
6465
- `username` (String) (Required by some data source types) The username to use to authenticate to the data source.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_cloud_private_data_source_connect_network Resource - terraform-provider-grafana"
4+
subcategory: "Cloud"
5+
description: |-
6+
Official documentation https://grafana.com/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/API documentation https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#create-an-access-policy
7+
Required access policy scopes:
8+
accesspolicies:readaccesspolicies:writeaccesspolicies:delete
9+
---
10+
11+
# grafana_cloud_private_data_source_connect_network (Resource)
12+
13+
* [Official documentation](https://grafana.com/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/)
14+
* [API documentation](https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#create-an-access-policy)
15+
16+
Required access policy scopes:
17+
18+
* accesspolicies:read
19+
* accesspolicies:write
20+
* accesspolicies:delete
21+
22+
## Example Usage
23+
24+
```terraform
25+
data "grafana_cloud_stack" "current" {
26+
slug = "<your slug>"
27+
}
28+
29+
resource "grafana_cloud_private_data_source_connect_network" "test" {
30+
region = "us"
31+
name = "my-pdc"
32+
display_name = "My PDC"
33+
stack_identifier = data.grafana_cloud_stack.current.id
34+
}
35+
36+
resource "grafana_cloud_private_data_source_connect_network_token" "test" {
37+
pdc_network_id = grafana_cloud_private_data_source_connect_network.test.pdc_network_id
38+
region = grafana_cloud_private_data_source_connect_network.test.region
39+
name = "my-pdc-token"
40+
display_name = "My PDC Token"
41+
}
42+
```
43+
44+
<!-- schema generated by tfplugindocs -->
45+
## Schema
46+
47+
### Required
48+
49+
- `name` (String) Name of the PDC network.**Note:** The name must be lowercase and can contain hyphens or underscores. See full requirements here: https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#request-body
50+
- `region` (String) The region where your stack is deployed. Use the instances list API to get the region for your instance - use the regionSlug property: https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#list-stacks
51+
- `stack_identifier` (String) The identifier of the stack.
52+
53+
### Optional
54+
55+
- `display_name` (String) Display name of the PDC network. Defaults to the name.
56+
57+
### Read-Only
58+
59+
- `created_at` (String) Creation date of the private data source connect network.
60+
- `id` (String) The ID of this resource.
61+
- `pdc_network_id` (String) ID of the private data source connect network.
62+
- `updated_at` (String) Last update date of the private data source connect network.
63+
64+
## Import
65+
66+
Import is supported using the following syntax:
67+
68+
```shell
69+
terraform import grafana_cloud_private_data_source_connect_network.name "{{ region }}:{{ policyId }}"
70+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_cloud_private_data_source_connect_network_token Resource - terraform-provider-grafana"
4+
subcategory: "Cloud"
5+
description: |-
6+
Official documentation https://grafana.com/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/API documentation https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#create-a-token
7+
Required access policy scopes:
8+
accesspolicies:readaccesspolicies:writeaccesspolicies:delete
9+
---
10+
11+
# grafana_cloud_private_data_source_connect_network_token (Resource)
12+
13+
* [Official documentation](https://grafana.com/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/)
14+
* [API documentation](https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#create-a-token)
15+
16+
Required access policy scopes:
17+
18+
* accesspolicies:read
19+
* accesspolicies:write
20+
* accesspolicies:delete
21+
22+
## Example Usage
23+
24+
```terraform
25+
data "grafana_cloud_stack" "current" {
26+
slug = "<your slug>"
27+
}
28+
29+
resource "grafana_cloud_private_data_source_connect_network" "test" {
30+
region = "us"
31+
name = "my-pdc"
32+
display_name = "My PDC"
33+
stack_identifier = data.grafana_cloud_stack.current.id
34+
}
35+
36+
resource "grafana_cloud_private_data_source_connect_network_token" "test" {
37+
pdc_network_id = grafana_cloud_private_data_source_connect_network.test.pdc_network_id
38+
region = grafana_cloud_private_data_source_connect_network.test.region
39+
name = "my-pdc-token"
40+
display_name = "My PDC Token"
41+
}
42+
```
43+
44+
<!-- schema generated by tfplugindocs -->
45+
## Schema
46+
47+
### Required
48+
49+
- `name` (String) Name of the private data source network token.
50+
- `pdc_network_id` (String) ID of the private data source network for which to create a token.
51+
- `region` (String) Region of the private data source network. Should be set to the same region as the private data source network. Use the region list API to get the list of available regions: https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#list-regions.
52+
53+
### Optional
54+
55+
- `display_name` (String) Display name of the private data source network token. Defaults to the name.
56+
- `expires_at` (String) Expiration date of the private data source network token. Does not expire by default.
57+
58+
### Read-Only
59+
60+
- `created_at` (String) Creation date of the private data source network token.
61+
- `id` (String) The ID of this resource.
62+
- `token` (String, Sensitive)
63+
- `updated_at` (String) Last update date of the private data source network token.
64+
65+
## Import
66+
67+
Import is supported using the following syntax:
68+
69+
```shell
70+
terraform import grafana_cloud_private_data_source_connect_network_token.name "{{ region }}:{{ tokenId }}"
71+
```

docs/resources/data_source.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ resource "grafana_data_source" "prometheus" {
116116
- `is_default` (Boolean) Whether to set the data source as default. This should only be `true` to a single data source. Defaults to `false`.
117117
- `json_data_encoded` (String) Serialized JSON string containing the json data. This attribute can be used to pass configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI. Note that keys in this map are usually camelCased.
118118
- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
119+
- `private_data_source_connect_network_id` (String) (Can only be used with data sources in Grafana Cloud) The ID of the Private Data source Connect network to use with this data source. Defaults to ``.
119120
- `secure_json_data_encoded` (String, Sensitive) Serialized JSON string containing the secure json data. This attribute can be used to pass secure configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI. Note that keys in this map are usually camelCased.
120121
- `uid` (String) Unique identifier. If unset, this will be automatically generated.
121122
- `url` (String) The URL for the data source. The type of URL required varies depending on the chosen data source type.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform import grafana_cloud_private_data_source_connect_network.name "{{ region }}:{{ policyId }}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
data "grafana_cloud_stack" "current" {
2+
slug = "<your slug>"
3+
}
4+
5+
resource "grafana_cloud_private_data_source_connect_network" "test" {
6+
region = "us"
7+
name = "my-pdc"
8+
display_name = "My PDC"
9+
stack_identifier = data.grafana_cloud_stack.current.id
10+
}
11+
12+
resource "grafana_cloud_private_data_source_connect_network_token" "test" {
13+
pdc_network_id = grafana_cloud_private_data_source_connect_network.test.pdc_network_id
14+
region = grafana_cloud_private_data_source_connect_network.test.region
15+
name = "my-pdc-token"
16+
display_name = "My PDC Token"
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform import grafana_cloud_private_data_source_connect_network_token.name "{{ region }}:{{ tokenId }}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
data "grafana_cloud_stack" "current" {
2+
slug = "<your slug>"
3+
}
4+
5+
resource "grafana_cloud_private_data_source_connect_network" "test" {
6+
region = "us"
7+
name = "my-pdc"
8+
display_name = "My PDC"
9+
stack_identifier = data.grafana_cloud_stack.current.id
10+
}
11+
12+
resource "grafana_cloud_private_data_source_connect_network_token" "test" {
13+
pdc_network_id = grafana_cloud_private_data_source_connect_network.test.pdc_network_id
14+
region = grafana_cloud_private_data_source_connect_network.test.region
15+
name = "my-pdc-token"
16+
display_name = "My PDC Token"
17+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package cloud
2+
3+
import (
4+
"context"
5+
6+
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
7+
"github.com/hashicorp/terraform-plugin-framework/attr"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
)
13+
14+
var dataSourcePrivateDataSourceConnectNetworksName = "grafana_cloud_private_data_source_connect_networks"
15+
16+
func datasourcePrivateDataSourceConnectNetworks() *common.DataSource {
17+
return common.NewDataSource(
18+
common.CategoryCloud,
19+
dataSourcePrivateDataSourceConnectNetworksName,
20+
&PDCNetworksDataSource{},
21+
)
22+
}
23+
24+
type PDCNetworksDataSource struct {
25+
basePluginFrameworkDataSource
26+
}
27+
28+
func (r *PDCNetworksDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
29+
resp.TypeName = dataSourcePrivateDataSourceConnectNetworksName
30+
}
31+
32+
func (r *PDCNetworksDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
33+
resp.Schema = schema.Schema{
34+
MarkdownDescription: `
35+
Fetches Private Data source Connect networks from Grafana Cloud.
36+
37+
* [Official documentation](https://grafana.com/docs/grafana-cloud/account-management/authentication-and-permissions/access-policies/)
38+
* [API documentation](https://grafana.com/docs/grafana-cloud/developer-resources/api-reference/cloud-api/#list-access-policies)
39+
40+
Required access policy scopes:
41+
42+
* accesspolicies:read`,
43+
Attributes: map[string]schema.Attribute{
44+
"id": schema.StringAttribute{
45+
Computed: true,
46+
Description: "The ID of this datasource. This is an internal identifier used by the provider to track this datasource.",
47+
},
48+
"region_filter": schema.StringAttribute{
49+
Optional: true,
50+
Description: "If set, only private data source connect networks in the specified region will be returned. This is faster than filtering in Terraform.",
51+
},
52+
"name_filter": schema.StringAttribute{
53+
Optional: true,
54+
Description: "If set, only private data source connect networks with the specified name will be returned. This is faster than filtering in Terraform.",
55+
},
56+
"private_data_source_connect_networks": schema.SetAttribute{
57+
Computed: true,
58+
ElementType: types.ObjectType{
59+
AttrTypes: map[string]attr.Type{
60+
"id": types.StringType,
61+
"region": types.StringType,
62+
"name": types.StringType,
63+
"display_name": types.StringType,
64+
"status": types.StringType,
65+
},
66+
},
67+
},
68+
},
69+
}
70+
}
71+
72+
type PDCNetworksDataSourcePolicyModel struct {
73+
ID types.String `tfsdk:"id"`
74+
Region types.String `tfsdk:"region"`
75+
Name types.String `tfsdk:"name"`
76+
DisplayName types.String `tfsdk:"display_name"`
77+
Status types.String `tfsdk:"status"`
78+
}
79+
80+
type PDCNetworksDataSourceModel struct {
81+
ID types.String `tfsdk:"id"`
82+
NameFilter types.String `tfsdk:"name_filter"`
83+
RegionFilter types.String `tfsdk:"region_filter"`
84+
PrivateDataSourceNetworks []PDCNetworksDataSourcePolicyModel `tfsdk:"private_data_source_networks"`
85+
}
86+
87+
func (r *PDCNetworksDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
88+
// Read Terraform state data into the model
89+
var data PDCNetworksDataSourceModel
90+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
91+
92+
var regions []string
93+
if data.RegionFilter.ValueString() != "" {
94+
regions = append(regions, data.RegionFilter.ValueString())
95+
} else {
96+
apiResp, _, err := r.client.StackRegionsAPI.GetStackRegions(ctx).Execute()
97+
if err != nil {
98+
resp.Diagnostics = diag.Diagnostics{diag.NewErrorDiagnostic("Failed to get stack regions", err.Error())}
99+
return
100+
}
101+
for _, region := range apiResp.Items {
102+
regions = append(regions, region.FormattedApiStackRegionAnyOf.Slug)
103+
}
104+
}
105+
106+
data.PrivateDataSourceNetworks = []PDCNetworksDataSourcePolicyModel{}
107+
for _, region := range regions {
108+
apiResp, _, err := r.client.AccesspoliciesAPI.GetAccessPolicies(ctx).Region(region).Execute()
109+
if err != nil {
110+
resp.Diagnostics = diag.Diagnostics{diag.NewErrorDiagnostic("Failed to get access policies", err.Error())}
111+
return
112+
}
113+
for _, policy := range apiResp.Items {
114+
if data.NameFilter.ValueString() != "" && data.NameFilter.ValueString() != policy.Name {
115+
continue
116+
}
117+
data.PrivateDataSourceNetworks = append(data.PrivateDataSourceNetworks, PDCNetworksDataSourcePolicyModel{
118+
ID: types.StringValue(*policy.Id),
119+
Region: types.StringValue(region),
120+
Name: types.StringValue(policy.Name),
121+
DisplayName: types.StringValue(*policy.DisplayName),
122+
Status: types.StringValue(*policy.Status),
123+
})
124+
}
125+
}
126+
data.ID = types.StringValue(data.RegionFilter.ValueString() + "-" + data.NameFilter.ValueString()) // Unique ID
127+
128+
// Save data into Terraform state
129+
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
130+
}

0 commit comments

Comments
 (0)