-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'nginx_configuration_data_source' data source
- Loading branch information
1 parent
97a9f83
commit edab341
Showing
4 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
internal/services/nginx/nginx_configuration_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// 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/nginxconfiguration" | ||
"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" | ||
) | ||
|
||
type ConfigurationDataSourceModel struct { | ||
NginxDeploymentId string `tfschema:"nginx_deployment_id"` | ||
ConfigFile []ConfigFile `tfschema:"config_file"` | ||
ProtectedFile []ProtectedFile `tfschema:"protected_file"` | ||
PackageData string `tfschema:"package_data"` | ||
RootFile string `tfschema:"root_file"` | ||
} | ||
|
||
type ConfigurationDataSource struct{} | ||
|
||
var _ sdk.DataSource = ConfigurationDataSource{} | ||
|
||
func (m ConfigurationDataSource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"nginx_deployment_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: nginxdeployment.ValidateNginxDeploymentID, | ||
}, | ||
} | ||
} | ||
|
||
func (m ConfigurationDataSource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"config_file": { | ||
Type: pluginsdk.TypeSet, | ||
Computed: true, | ||
Elem: &pluginsdk.Resource{ | ||
Schema: map[string]*pluginsdk.Schema{ | ||
"content": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"virtual_path": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"protected_file": { | ||
Type: pluginsdk.TypeSet, | ||
Computed: true, | ||
Elem: &pluginsdk.Resource{ | ||
Schema: map[string]*pluginsdk.Schema{ | ||
"content": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
|
||
"virtual_path": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"package_data": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"root_file": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
} | ||
} | ||
|
||
func (m ConfigurationDataSource) ModelObject() interface{} { | ||
return &ConfigurationDataSourceModel{} | ||
} | ||
|
||
func (m ConfigurationDataSource) ResourceType() string { | ||
return "azurerm_nginx_configuration" | ||
} | ||
|
||
func (m ConfigurationDataSource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Nginx.NginxConfiguration | ||
var model ConfigurationDataSourceModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return err | ||
} | ||
deploymentId, err := nginxdeployment.ParseNginxDeploymentID(model.NginxDeploymentId) | ||
if err != nil { | ||
return err | ||
} | ||
id := nginxconfiguration.NewConfigurationID( | ||
deploymentId.SubscriptionId, | ||
deploymentId.ResourceGroupName, | ||
deploymentId.NginxDeploymentName, | ||
defaultConfigurationName, | ||
) | ||
result, err := client.ConfigurationsGet(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 := ConfigurationDataSourceModel{ | ||
NginxDeploymentId: deploymentId.ID(), | ||
} | ||
|
||
if model := result.Model; model != nil { | ||
prop := result.Model.Properties | ||
output.RootFile = pointer.From(prop.RootFile) | ||
if prop.Package != nil && prop.Package.Data != nil { | ||
output.PackageData = pointer.From(prop.Package.Data) | ||
} | ||
|
||
if files := prop.Files; files != nil { | ||
for _, file := range *files { | ||
output.ConfigFile = append(output.ConfigFile, ConfigFile{ | ||
Content: pointer.From(file.Content), | ||
VirtualPath: pointer.From(file.VirtualPath), | ||
}) | ||
} | ||
} | ||
|
||
if files := prop.ProtectedFiles; files != nil { | ||
for _, file := range *files { | ||
output.ProtectedFile = append(output.ProtectedFile, ProtectedFile{ | ||
Content: pointer.From(file.Content), | ||
VirtualPath: pointer.From(file.VirtualPath), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
metadata.SetID(id) | ||
return metadata.Encode(&output) | ||
}, | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
internal/services/nginx/nginx_configuration_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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 NginxConfigurationDataSource struct{} | ||
|
||
func TestAccNginxConfigurationDataSource_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_nginx_configuration", "test") | ||
r := NginxConfigurationDataSource{} | ||
|
||
data.DataSourceTest(t, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key("root_file").Exists(), | ||
check.That(data.ResourceName).Key("config_file").Exists(), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (d NginxConfigurationDataSource) basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_nginx_configuration" "test" { | ||
nginx_deployment_id = azurerm_nginx_deployment.test.id | ||
} | ||
`, ConfigurationResource{}.basic(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
subcategory: "Nginx" | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: Data Source: azurerm_nginx_configuration" | ||
description: |- | ||
Gets information about an existing Nginx Configuration. | ||
--- | ||
|
||
# Data Source: azurerm_nginx_configuration | ||
|
||
Use this data source to access information about an existing Nginx Configuration. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_nginx_configuration" "example" { | ||
nginx_deployment_id = "TODO" | ||
} | ||
output "id" { | ||
value = data.azurerm_nginx_configuration.example.id | ||
} | ||
``` | ||
|
||
## Arguments Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `nginx_deployment_id` - (Required) The ID of the Nginx Deployment. | ||
|
||
## Attributes Reference | ||
|
||
In addition to the Arguments listed above - the following Attributes are exported: | ||
|
||
* `id` - The ID of the Nginx Configuration. | ||
|
||
* `config_file` - A `config_file` block as defined below. | ||
|
||
* `package_data` - The package data for this configuration. | ||
|
||
* `root_file` - The root file path of this Nginx Configuration. | ||
|
||
--- | ||
|
||
A `config_file` block exports the following: | ||
|
||
* `content` - The base-64 encoded contents of this configuration file. | ||
|
||
* `virtual_path` - The path of this configuration file. | ||
|
||
## 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 Configuration. |