Skip to content

Commit

Permalink
add azurerm_nginx_certificate data source
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetsarna committed Jan 22, 2024
1 parent 0abf413 commit b7e20b7
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
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 fmt.Errorf("error parsing NGINX deployment ID %s: %+v", deploymentId, 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("reading %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.ToString(prop.KeyVirtualPath)
output.KeyVaultSecretId = pointer.ToString(prop.KeyVaultSecretId)
output.CertificateVirtualPath = pointer.ToString(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

0 comments on commit b7e20b7

Please sign in to comment.