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 azurerm_nginx_certificate data source #24577

Merged
merged 1 commit into from
Jan 23, 2024
Merged
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
118 changes: 118 additions & 0 deletions internal/services/nginx/nginx_certificate_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package nginx

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/nginx/2023-04-01/nginxcertificate"
"github.com/hashicorp/go-azure-sdk/resource-manager/nginx/2023-04-01/nginxdeployment"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type CertificateDataSourceModel struct {
Name string `tfschema:"name"`
NginxDeploymentId string `tfschema:"nginx_deployment_id"`
KeyVirtualPath string `tfschema:"key_virtual_path"`
CertificateVirtualPath string `tfschema:"certificate_virtual_path"`
KeyVaultSecretId string `tfschema:"key_vault_secret_id"`
}

type CertificateDataSource struct{}

var _ sdk.DataSource = CertificateDataSource{}

func (m CertificateDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"nginx_deployment_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: nginxdeployment.ValidateNginxDeploymentID,
},
}
}

func (m CertificateDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"key_virtual_path": {
Type: pluginsdk.TypeString,
Computed: true,
},

"certificate_virtual_path": {
Type: pluginsdk.TypeString,
Computed: true,
},

"key_vault_secret_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

func (m CertificateDataSource) ModelObject() interface{} {
return &CertificateDataSourceModel{}
}

func (m CertificateDataSource) ResourceType() string {
return "azurerm_nginx_certificate"
}

func (m CertificateDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Nginx.NginxCertificate
var model CertificateDataSourceModel
if err := metadata.Decode(&model); err != nil {
return err
}
deploymentId, err := nginxdeployment.ParseNginxDeploymentID(model.NginxDeploymentId)
if err != nil {
return err
}
id := nginxcertificate.NewCertificateID(
deploymentId.SubscriptionId,
deploymentId.ResourceGroupName,
deploymentId.NginxDeploymentName,
model.Name,
)
result, err := client.CertificatesGet(ctx, id)
if err != nil {
if response.WasNotFound(result.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

output := CertificateDataSourceModel{
Name: id.CertificateName,
NginxDeploymentId: deploymentId.ID(),
}

if model := result.Model; model != nil {
prop := result.Model.Properties
output.KeyVirtualPath = pointer.From(prop.KeyVirtualPath)
output.KeyVaultSecretId = pointer.From(prop.KeyVaultSecretId)
output.CertificateVirtualPath = pointer.From(prop.CertificateVirtualPath)
}

metadata.SetID(id)
return metadata.Encode(&output)
},
}
}
41 changes: 41 additions & 0 deletions internal/services/nginx/nginx_certificate_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package nginx_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type NginxCertificateDataSource struct{}

func TestAccNginxCertificateDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_nginx_certificate", "test")
r := NginxCertificateDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("certificate_virtual_path").Exists(),
check.That(data.ResourceName).Key("key_vault_secret_id").Exists(),
check.That(data.ResourceName).Key("key_virtual_path").Exists(),
),
},
})
}

func (d NginxCertificateDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_nginx_certificate" "test" {
name = azurerm_nginx_certificate.test.name
nginx_deployment_id = azurerm_nginx_deployment.test.id
}
`, CertificateResource{}.basic(data))
}
1 change: 1 addition & 0 deletions internal/services/nginx/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (r Registration) WebsiteCategories() []string {
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{
DeploymentDataSource{},
CertificateDataSource{},
}
}

Expand Down
50 changes: 50 additions & 0 deletions website/docs/d/nginx_certificate.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
subcategory: "Nginx"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_nginx_certificate"
description: |-
Gets information about an existing Nginx Certificate.
---

# Data Source: azurerm_nginx_certificate

Use this data source to access information about an existing Nginx Certificate.

## Example Usage

```hcl
data "azurerm_nginx_certificate" "example" {
name = "existing"
nginx_deployment_id = azurerm_nginx_deployment.example.id
}

output "id" {
value = data.azurerm_nginx_certificate.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of this Nginx Certificate.

* `nginx_deployment_id` - (Required) The ID of the Nginx Deployment that this certificate is associated with.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Nginx Certificate.

* `certificate_virtual_path` - The path to the certificate file of this certificate.

* `key_virtual_path` - The path to the key file of this certificate.

* `key_vault_secret_id` - The ID of the Key Vault Secret for this certificate.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Nginx Certificate.
4 changes: 2 additions & 2 deletions website/docs/r/nginx_certificate.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ subcategory: "Nginx"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_nginx_certificate"
description: |-
Manages a Certificate for an NGinx Deployment.
Manages a Certificate for an Nginx Deployment.
---

# azurerm_nginx_certificate
Expand Down Expand Up @@ -127,7 +127,7 @@ The following arguments are supported:

* `nginx_deployment_id` - (Required) The ID of the Nginx Deployment that this Certificate should be associated with. Changing this forces a new Nginx Certificate to be created.

* `certificate_virtual_path` - (Required) Specify the path to the cert file of this certificate.
* `certificate_virtual_path` - (Required) Specify the path to the certificate file of this certificate.

* `key_virtual_path` - (Required) Specify the path to the key file of this certificate.

Expand Down
Loading