Skip to content

Commit b5231bd

Browse files
authored
Add support for the Azure credentials resource (#1907)
* Add support for the Azure credentials resource
1 parent 1aa1b32 commit b5231bd

File tree

10 files changed

+969
-0
lines changed

10 files changed

+969
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_cloud_provider_azure_credential Data Source - terraform-provider-grafana"
4+
subcategory: "Cloud Provider"
5+
description: |-
6+
7+
---
8+
9+
# grafana_cloud_provider_azure_credential (Data Source)
10+
11+
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "grafana_cloud_provider_azure_credential" "test" {
17+
stack_id = "1"
18+
name = "test-name"
19+
client_id = "my-client-id"
20+
client_secret = "my-client-secret"
21+
tenant_id = "my-tenant-id"
22+
23+
resource_discovery_tag_filter {
24+
key = "key-1"
25+
value = "value-1"
26+
}
27+
resource_discovery_tag_filter {
28+
key = "key-2"
29+
value = "value-2"
30+
}
31+
}
32+
33+
34+
data "grafana_cloud_provider_azure_credential" "test" {
35+
stack_id = grafana_cloud_provider_azure_credential.test.stack_id
36+
resource_id = grafana_cloud_provider_azure_credential.test.resource_id
37+
}
38+
```
39+
40+
<!-- schema generated by tfplugindocs -->
41+
## Schema
42+
43+
### Required
44+
45+
- `resource_id` (String) The ID given by the Grafana Cloud Provider API to this Azure Credential resource.
46+
- `stack_id` (String) The StackID of the Grafana Cloud instance. Part of the Terraform Resource ID.
47+
48+
### Read-Only
49+
50+
- `client_id` (String) The client ID of the Azure Credential.
51+
- `client_secret` (String, Sensitive) The client secret of the Azure Credential.
52+
- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".
53+
- `name` (String) The name of the Azure Credential.
54+
- `resource_discovery_tag_filter` (Block List) The list of tag filters to apply to resources. (see [below for nested schema](#nestedblock--resource_discovery_tag_filter))
55+
- `tenant_id` (String) The tenant ID of the Azure Credential.
56+
57+
<a id="nestedblock--resource_discovery_tag_filter"></a>
58+
### Nested Schema for `resource_discovery_tag_filter`
59+
60+
Read-Only:
61+
62+
- `key` (String) The key of the tag filter.
63+
- `value` (String) The value of the tag filter.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_cloud_provider_azure_credential Resource - terraform-provider-grafana"
4+
subcategory: "Cloud Provider"
5+
description: |-
6+
7+
---
8+
9+
# grafana_cloud_provider_azure_credential (Resource)
10+
11+
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "grafana_cloud_provider_azure_credential" "test" {
17+
stack_id = "1"
18+
name = "test-name"
19+
client_id = "my-client-id"
20+
client_secret = "my-client-secret"
21+
tenant_id = "my-tenant-id"
22+
23+
resource_discovery_tag_filter {
24+
key = "key-1"
25+
value = "value-1"
26+
}
27+
28+
resource_discovery_tag_filter {
29+
key = "key-2"
30+
value = "value-2"
31+
}
32+
}
33+
```
34+
35+
<!-- schema generated by tfplugindocs -->
36+
## Schema
37+
38+
### Required
39+
40+
- `client_id` (String) The client ID of the Azure Credential.
41+
- `client_secret` (String, Sensitive) The client secret of the Azure Credential.
42+
- `name` (String) The name of the Azure Credential.
43+
- `stack_id` (String) The StackID of the Grafana Cloud instance. Part of the Terraform Resource ID.
44+
- `tenant_id` (String) The tenant ID of the Azure Credential.
45+
46+
### Optional
47+
48+
- `resource_discovery_tag_filter` (Block List) The list of tag filters to apply to resources. (see [below for nested schema](#nestedblock--resource_discovery_tag_filter))
49+
50+
### Read-Only
51+
52+
- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".
53+
- `resource_id` (String) The ID given by the Grafana Cloud Provider API to this AWS Account resource.
54+
55+
<a id="nestedblock--resource_discovery_tag_filter"></a>
56+
### Nested Schema for `resource_discovery_tag_filter`
57+
58+
Required:
59+
60+
- `key` (String) The key of the tag filter.
61+
- `value` (String) The value of the tag filter.
62+
63+
## Import
64+
65+
Import is supported using the following syntax:
66+
67+
```shell
68+
terraform import grafana_cloud_provider_azure_credential.name "{{ stack_id }}:{{ resource_id }}"
69+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
resource "grafana_cloud_provider_azure_credential" "test" {
2+
stack_id = "1"
3+
name = "test-name"
4+
client_id = "my-client-id"
5+
client_secret = "my-client-secret"
6+
tenant_id = "my-tenant-id"
7+
8+
resource_discovery_tag_filter {
9+
key = "key-1"
10+
value = "value-1"
11+
}
12+
resource_discovery_tag_filter {
13+
key = "key-2"
14+
value = "value-2"
15+
}
16+
}
17+
18+
19+
data "grafana_cloud_provider_azure_credential" "test" {
20+
stack_id = grafana_cloud_provider_azure_credential.test.stack_id
21+
resource_id = grafana_cloud_provider_azure_credential.test.resource_id
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform import grafana_cloud_provider_azure_credential.name "{{ stack_id }}:{{ resource_id }}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "grafana_cloud_provider_azure_credential" "test" {
2+
stack_id = "1"
3+
name = "test-name"
4+
client_id = "my-client-id"
5+
client_secret = "my-client-secret"
6+
tenant_id = "my-tenant-id"
7+
8+
resource_discovery_tag_filter {
9+
key = "key-1"
10+
value = "value-1"
11+
}
12+
13+
resource_discovery_tag_filter {
14+
key = "key-2"
15+
value = "value-2"
16+
}
17+
}

internal/common/cloudproviderapi/client.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,76 @@ func (c *Client) DeleteAWSCloudWatchScrapeJob(ctx context.Context, stackID strin
193193
return nil
194194
}
195195

196+
type AzureCredential struct {
197+
// ID is the unique identifier for the Azure credential in our systems.
198+
ID string `json:"id"`
199+
200+
// Name is the user-defined name for the Azure credential.
201+
Name string `json:"name"`
202+
203+
// TenantID is the Azure tenant ID.
204+
TenantID string `json:"tenant_id"`
205+
206+
// ClientID is the Azure client ID.
207+
ClientID string `json:"client_id"`
208+
209+
// ClientSecret is the Azure client secret.
210+
ClientSecret string `json:"client_secret"`
211+
212+
// StackID is the unique identifier for the stack in our systems.
213+
StackID string `json:"stack_id"`
214+
215+
// ResourceTagFilters is the list of Azure resource tag filters.
216+
ResourceTagFilters []TagFilter `json:"resource_tag_filters"`
217+
}
218+
219+
type TagFilter struct {
220+
Key string `json:"key"`
221+
Value string `json:"value"`
222+
}
223+
224+
func (c *Client) CreateAzureCredential(ctx context.Context, stackID string, credentialData AzureCredential) (AzureCredential, error) {
225+
path := fmt.Sprintf("/api/v2/stacks/%s/azure/credentials", stackID)
226+
respData := apiResponseWrapper[AzureCredential]{}
227+
err := c.doAPIRequest(ctx, http.MethodPost, path, &credentialData, &respData)
228+
if err != nil {
229+
return AzureCredential{}, fmt.Errorf("failed to create Azure credential: %w", err)
230+
}
231+
232+
return respData.Data, nil
233+
}
234+
235+
func (c *Client) GetAzureCredential(ctx context.Context, stackID string, credentialID string) (AzureCredential, error) {
236+
path := fmt.Sprintf("/api/v2/stacks/%s/azure/credentials/%s", stackID, credentialID)
237+
respData := apiResponseWrapper[AzureCredential]{}
238+
err := c.doAPIRequest(ctx, http.MethodGet, path, nil, &respData)
239+
if err != nil {
240+
return AzureCredential{}, fmt.Errorf("failed to get Azure credential: %w", err)
241+
}
242+
243+
return respData.Data, nil
244+
}
245+
246+
func (c *Client) UpdateAzureCredential(ctx context.Context, stackID string, accountID string, credentialData AzureCredential) (AzureCredential, error) {
247+
path := fmt.Sprintf("/api/v2/stacks/%s/azure/credentials/%s", stackID, accountID)
248+
respData := apiResponseWrapper[AzureCredential]{}
249+
err := c.doAPIRequest(ctx, http.MethodPut, path, &credentialData, &respData)
250+
if err != nil {
251+
return AzureCredential{}, fmt.Errorf("failed to update Azure credential: %w", err)
252+
}
253+
254+
return respData.Data, nil
255+
}
256+
257+
func (c *Client) DeleteAzureCredential(ctx context.Context, stackID string, credentialID string) error {
258+
path := fmt.Sprintf("/api/v2/stacks/%s/azure/credentials/%s", stackID, credentialID)
259+
err := c.doAPIRequest(ctx, http.MethodDelete, path, nil, nil)
260+
if err != nil {
261+
return fmt.Errorf("failed to delete Azure credential: %w", err)
262+
}
263+
return nil
264+
}
265+
196266
func (c *Client) doAPIRequest(ctx context.Context, method string, path string, body any, responseData any) error {
197267
var reqBodyBytes io.Reader
198268
if body != nil {

0 commit comments

Comments
 (0)