Skip to content

Commit

Permalink
add azurerm_nginx_deployment data source
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetsarna committed Jan 13, 2024
1 parent d4d0c76 commit ced4838
Show file tree
Hide file tree
Showing 5 changed files with 436 additions and 3 deletions.
277 changes: 277 additions & 0 deletions internal/services/nginx/nginx_deployment_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
// 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-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"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 DeploymentDataSourceModel struct {
ResourceGroupName string `tfschema:"resource_group_name"`
Name string `tfschema:"name"`
NginxVersion string `tfschema:"nginx_version"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
Sku string `tfschema:"sku"`
ManagedResourceGroup string `tfschema:"managed_resource_group"`
Location string `tfschema:"location"`
Capacity int64 `tfschema:"capacity"`
DiagnoseSupportEnabled bool `tfschema:"diagnose_support_enabled"`
Email string `tfschema:"email"`
IpAddress string `tfschema:"ip_address"`
LoggingStorageAccount []LoggingStorageAccount `tfschema:"logging_storage_account"`
FrontendPublic []FrontendPublic `tfschema:"frontend_public"`
FrontendPrivate []FrontendPrivate `tfschema:"frontend_private"`
NetworkInterface []NetworkInterface `tfschema:"network_interface"`
Tags map[string]string `tfschema:"tags"`
}

type DeploymentDataSource struct{}

var _ sdk.DataSource = DeploymentDataSource{}

func (m DeploymentDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
}
}

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

"identity": commonschema.SystemOrUserAssignedIdentityComputed(),

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

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

"location": commonschema.LocationComputed(),

"capacity": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"diagnose_support_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

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

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

"logging_storage_account": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

"frontend_public": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"ip_address": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},

"frontend_private": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"ip_address": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

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

"network_interface": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"subnet_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"tags": commonschema.TagsDataSource(),
}
}

func (m DeploymentDataSource) ModelObject() interface{} {
return &DeploymentDataSourceModel{}
}

func (m DeploymentDataSource) ResourceType() string {
return "azurerm_nginx_deployment"
}

func (m DeploymentDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Nginx.NginxDeployment
subscriptionId := metadata.Client.Account.SubscriptionId
var model DeploymentDataSourceModel
if err := metadata.Decode(&model); err != nil {
return err
}
id := nginxdeployment.NewNginxDeploymentID(subscriptionId, model.ResourceGroupName, model.Name)
result, err := client.DeploymentsGet(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 := DeploymentDataSourceModel{
Name: id.NginxDeploymentName,
ResourceGroupName: id.ResourceGroupName,
}

if model := result.Model; model != nil {
output.Location = pointer.ToString(model.Location)
if tags := model.Tags; tags != nil {
output.Tags = pointer.ToMapOfStringStrings(model.Tags)
}
if model.Sku != nil {
output.Sku = model.Sku.Name
}
flattenedIdentity, err := identity.FlattenSystemAndUserAssignedMapToModel(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %v", err)
}
output.Identity = *flattenedIdentity
if props := model.Properties; props != nil {
output.IpAddress = pointer.ToString(props.IPAddress)
output.ManagedResourceGroup = pointer.ToString(props.ManagedResourceGroup)
output.NginxVersion = pointer.ToString(props.NginxVersion)
output.DiagnoseSupportEnabled = pointer.ToBool(props.EnableDiagnosticsSupport)

if props.Logging != nil && props.Logging.StorageAccount != nil {
output.LoggingStorageAccount = []LoggingStorageAccount{
{
Name: pointer.ToString(props.Logging.StorageAccount.AccountName),
ContainerName: pointer.ToString(props.Logging.StorageAccount.ContainerName),
},
}
}

if profile := props.NetworkProfile; profile != nil {
if frontend := profile.FrontEndIPConfiguration; frontend != nil {
if publicIps := frontend.PublicIPAddresses; publicIps != nil && len(*publicIps) > 0 {
output.FrontendPublic = append(output.FrontendPublic, FrontendPublic{})
for _, ip := range *publicIps {
output.FrontendPublic[0].IpAddress = append(output.FrontendPublic[0].IpAddress, pointer.ToString(ip.Id))
}
}

if privateIPs := frontend.PrivateIPAddresses; privateIPs != nil && len(*privateIPs) > 0 {
for _, ip := range *privateIPs {
method := ""
if ip.PrivateIPAllocationMethod != nil {
method = string(*ip.PrivateIPAllocationMethod)
}

output.FrontendPrivate = append(output.FrontendPrivate, FrontendPrivate{
IpAddress: pointer.ToString(ip.PrivateIPAddress),
AllocationMethod: method,
SubnetId: pointer.ToString(ip.SubnetId),
})
}
}
}

if netIf := profile.NetworkInterfaceConfiguration; netIf != nil {
output.NetworkInterface = []NetworkInterface{
{SubnetId: pointer.ToString(netIf.SubnetId)},
}
}
}

if scaling := props.ScalingProperties; scaling != nil {
output.Capacity = pointer.ToInt64(props.ScalingProperties.Capacity)
}

if userProfile := props.UserProfile; userProfile != nil && userProfile.PreferredEmail != nil {
output.Email = pointer.ToString(props.UserProfile.PreferredEmail)
}
}
}

metadata.SetID(id)
return metadata.Encode(&output)
},
}
}
44 changes: 44 additions & 0 deletions internal/services/nginx/nginx_deployment_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 NginxDeploymentDataSource struct{}

func TestAccNginxDeploymentDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_nginx_deployment", "test")
r := NginxDeploymentDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("location").Exists(),
check.That(data.ResourceName).Key("nginx_version").Exists(),
check.That(data.ResourceName).Key("sku").Exists(),
check.That(data.ResourceName).Key("capacity").Exists(),
check.That(data.ResourceName).Key("managed_resource_group").Exists(),
check.That(data.ResourceName).Key("ip_address").Exists(),
),
},
})
}

func (d NginxDeploymentDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_nginx_deployment" "test" {
name = azurerm_nginx_deployment.test.name
resource_group_name = azurerm_nginx_deployment.test.resource_group_name
}
`, DeploymentResource{}.basic(data))
}
4 changes: 3 additions & 1 deletion internal/services/nginx/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func (r Registration) WebsiteCategories() []string {

// DataSources ...
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
DeploymentDataSource{},
}
}

// Resources ...
Expand Down
Loading

0 comments on commit ced4838

Please sign in to comment.