Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the Azure credentials resource #1907

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
80 changes: 80 additions & 0 deletions docs/data-sources/cloud_provider_azure_credential.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_cloud_provider_azure_credential Data Source - terraform-provider-grafana"
subcategory: "Cloud Provider"
description: |-

---

# grafana_cloud_provider_azure_credential (Data Source)



## Example Usage

```terraform
resource "grafana_cloud_provider_azure_credential" "test" {
stack_id = "1"
name = "test-name"
client_id = "my-client-id"
client_secret = "my-client-secret"
tenant_id = "my-tenant-id"

resource_tag_filter {
key = "key-1"
value = "value-1"
}
resource_tag_filter {
key = "key-2"
value = "value-2"
}
}


data "grafana_cloud_provider_azure_credential" "test" {
stack_id = grafana_cloud_provider_azure_credential.test.stack_id
name = grafana_cloud_provider_azure_credential.test.name
client_id = grafana_cloud_provider_azure_credential.test.client_id
client_secret = grafana_cloud_provider_azure_credential.test.client_secret
tenant_id = grafana_cloud_provider_azure_credential.test.tenant_id
resource_id = grafana_cloud_provider_azure_credential.test.resource_id

resource_tag_filter {
key = grafana_cloud_provider_azure_credential.test.resource_tag_filter[0].key
value = grafana_cloud_provider_azure_credential.test.resource_tag_filter[0].value
}

resource_tag_filter {
key = grafana_cloud_provider_azure_credential.test.resource_tag_filter[1].key
value = grafana_cloud_provider_azure_credential.test.resource_tag_filter[1].value
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `client_id` (String) The client ID of the Azure Credential.
- `client_secret` (String, Sensitive) The client secret of the Azure Credential.
- `name` (String) The name of the Azure Credential.
- `resource_id` (String) The ID given by the Grafana Cloud Provider API to this Azure Credential resource.
- `stack_id` (String) The StackID of the Grafana Cloud instance. Part of the Terraform Resource ID.
- `tenant_id` (String) The tenant ID of the Azure Credential.

### Optional

- `resource_tag_filter` (Block List) The list of tag filters to apply to resources. (see [below for nested schema](#nestedblock--resource_tag_filter))

### Read-Only

- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".

<a id="nestedblock--resource_tag_filter"></a>
### Nested Schema for `resource_tag_filter`

Required:

- `key` (String) The key of the tag filter.
- `value` (String) The value of the tag filter.
69 changes: 69 additions & 0 deletions docs/resources/cloud_provider_azure_credential.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_cloud_provider_azure_credential Resource - terraform-provider-grafana"
subcategory: "Cloud Provider"
description: |-

---

# grafana_cloud_provider_azure_credential (Resource)



## Example Usage

```terraform
resource "grafana_cloud_provider_azure_credential" "test" {
stack_id = "1"
name = "test-name"
client_id = "my-client-id"
client_secret = "my-client-secret"
tenant_id = "my-tenant-id"

resource_tag_filter {
key = "key-1"
value = "value-1"
}

resource_tag_filter {
key = "key-2"
value = "value-2"
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `client_id` (String) The client ID of the Azure Credential.
- `client_secret` (String, Sensitive) The client secret of the Azure Credential.
- `name` (String) The name of the Azure Credential.
- `stack_id` (String) The StackID of the Grafana Cloud instance. Part of the Terraform Resource ID.
- `tenant_id` (String) The tenant ID of the Azure Credential.

### Optional

- `resource_tag_filter` (Block List) The list of tag filters to apply to resources. (see [below for nested schema](#nestedblock--resource_tag_filter))

### Read-Only

- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".
- `resource_id` (String) The ID given by the Grafana Cloud Provider API to this AWS Account resource.

<a id="nestedblock--resource_tag_filter"></a>
### Nested Schema for `resource_tag_filter`

Required:

- `key` (String) The key of the tag filter.
- `value` (String) The value of the tag filter.

## Import

Import is supported using the following syntax:

```shell
terraform import grafana_cloud_provider_azure_credential.name "{{ stack_id }}:{{ resource_id }}"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
resource "grafana_cloud_provider_azure_credential" "test" {
stack_id = "1"
name = "test-name"
client_id = "my-client-id"
client_secret = "my-client-secret"
tenant_id = "my-tenant-id"

resource_tag_filter {
key = "key-1"
value = "value-1"
}
resource_tag_filter {
key = "key-2"
value = "value-2"
}
}


data "grafana_cloud_provider_azure_credential" "test" {
stack_id = grafana_cloud_provider_azure_credential.test.stack_id
name = grafana_cloud_provider_azure_credential.test.name
client_id = grafana_cloud_provider_azure_credential.test.client_id
client_secret = grafana_cloud_provider_azure_credential.test.client_secret
tenant_id = grafana_cloud_provider_azure_credential.test.tenant_id
resource_id = grafana_cloud_provider_azure_credential.test.resource_id

resource_tag_filter {
key = grafana_cloud_provider_azure_credential.test.resource_tag_filter[0].key
value = grafana_cloud_provider_azure_credential.test.resource_tag_filter[0].value
}

resource_tag_filter {
key = grafana_cloud_provider_azure_credential.test.resource_tag_filter[1].key
value = grafana_cloud_provider_azure_credential.test.resource_tag_filter[1].value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import grafana_cloud_provider_azure_credential.name "{{ stack_id }}:{{ resource_id }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "grafana_cloud_provider_azure_credential" "test" {
stack_id = "1"
name = "test-name"
client_id = "my-client-id"
client_secret = "my-client-secret"
tenant_id = "my-tenant-id"

resource_tag_filter {
key = "key-1"
value = "value-1"
}

resource_tag_filter {
key = "key-2"
value = "value-2"
}
}
70 changes: 70 additions & 0 deletions internal/common/cloudproviderapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,76 @@ func (c *Client) DeleteAWSCloudWatchScrapeJob(ctx context.Context, stackID strin
return nil
}

type AzureCredential struct {
kgeckhart marked this conversation as resolved.
Show resolved Hide resolved
// ID is the unique identifier for the Azure credential in our systems.
ID string `json:"id"`

// Name is the user-defined name for the Azure credential.
Name string `json:"name"`

// TenantID is the Azure tenant ID.
TenantID string `json:"tenant_id"`

// ClientID is the Azure client ID.
ClientID string `json:"client_id"`

// ClientSecret is the Azure client secret.
ClientSecret string `json:"client_secret"`

// StackID is the unique identifier for the stack in our systems.
StackID string `json:"stack_id"`

// ResourceTagFilters is the list of Azure resource tag filters.
ResourceTagFilters []TagFilter `json:"resource_tag_filters"`
}

type TagFilter struct {
Key string `json:"key"`
Value string `json:"value"`
}

func (c *Client) CreateAzureCredential(ctx context.Context, stackID string, credentialData AzureCredential) (AzureCredential, error) {
path := fmt.Sprintf("/api/v2/stacks/%s/azure/credentials", stackID)
respData := apiResponseWrapper[AzureCredential]{}
err := c.doAPIRequest(ctx, http.MethodPost, path, &credentialData, &respData)
if err != nil {
return AzureCredential{}, fmt.Errorf("failed to create Azure credential: %w", err)
}

return respData.Data, nil
}

func (c *Client) GetAzureCredential(ctx context.Context, stackID string, credentialID string) (AzureCredential, error) {
path := fmt.Sprintf("/api/v2/stacks/%s/azure/credentials/%s", stackID, credentialID)
respData := apiResponseWrapper[AzureCredential]{}
err := c.doAPIRequest(ctx, http.MethodGet, path, nil, &respData)
if err != nil {
return AzureCredential{}, fmt.Errorf("failed to get Azure credential: %w", err)
}

return respData.Data, nil
}

func (c *Client) UpdateAzureCredential(ctx context.Context, stackID string, accountID string, credentialData AzureCredential) (AzureCredential, error) {
path := fmt.Sprintf("/api/v2/stacks/%s/azure/credentials/%s", stackID, accountID)
respData := apiResponseWrapper[AzureCredential]{}
err := c.doAPIRequest(ctx, http.MethodPut, path, &credentialData, &respData)
if err != nil {
return AzureCredential{}, fmt.Errorf("failed to update Azure credential: %w", err)
}

return respData.Data, nil
}

func (c *Client) DeleteAzureCredential(ctx context.Context, stackID string, credentialID string) error {
path := fmt.Sprintf("/api/v2/stacks/%s/azure/credentials/%s", stackID, credentialID)
err := c.doAPIRequest(ctx, http.MethodDelete, path, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete Azure credential: %w", err)
}
return nil
}

func (c *Client) doAPIRequest(ctx context.Context, method string, path string, body any, responseData any) error {
var reqBodyBytes io.Reader
if body != nil {
Expand Down
Loading