Skip to content

Commit

Permalink
Adding multiple data sources (#19628)
Browse files Browse the repository at this point in the history
  • Loading branch information
steweg authored Jan 3, 2023
1 parent b46e424 commit 861e637
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 0 deletions.
83 changes: 83 additions & 0 deletions internal/services/compute/marketplace_agreement_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package compute

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceMarketplaceAgreement() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceMarketplaceAgreementRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"offer": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

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

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

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

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

func dataSourceMarketplaceAgreementRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.MarketplaceAgreementsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewPlanID(subscriptionId, d.Get("publisher").(string), d.Get("offer").(string), d.Get("plan").(string))

log.Printf("[DEBUG] retrieving %s", id)

term, err := client.Get(ctx, id.AgreementName, id.OfferName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(term.Response) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving %s: %s", id, err)
}

d.SetId(id.ID())

if props := term.AgreementProperties; props != nil {
d.Set("license_text_link", props.LicenseTextLink)
d.Set("privacy_policy_link", props.PrivacyPolicyLink)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package compute_test

import (
"fmt"
"testing"

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

type MarketplaceAgreementDataSource struct{}

func TestAccDataSourceMarketplaceAgreement_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_marketplace_agreement", "test")
r := MarketplaceAgreementDataSource{}

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

func (MarketplaceAgreementDataSource) basic() string {
return fmt.Sprintf(`
%s
data "azurerm_marketplace_agreement" "test" {
publisher = "barracudanetworks"
offer = "waf"
plan = "hourly"
}
`, MarketplaceAgreementResource{}.basic("waf"))
}
1 change: 1 addition & 0 deletions internal/services/compute/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
"azurerm_image": dataSourceImage(),
"azurerm_images": dataSourceImages(),
"azurerm_disk_access": dataSourceDiskAccess(),
"azurerm_marketplace_agreement": dataSourceMarketplaceAgreement(),
"azurerm_platform_image": dataSourcePlatformImage(),
"azurerm_proximity_placement_group": dataSourceProximityPlacementGroup(),
"azurerm_shared_image_gallery": dataSourceSharedImageGallery(),
Expand Down
1 change: 1 addition & 0 deletions internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
"azurerm_network_service_tags": dataSourceNetworkServiceTags(),
"azurerm_subnet": dataSourceSubnet(),
"azurerm_virtual_hub": dataSourceVirtualHub(),
"azurerm_virtual_hub_route_table": dataSourceVirtualHubRouteTable(),
"azurerm_virtual_network_gateway": dataSourceVirtualNetworkGateway(),
"azurerm_virtual_network_gateway_connection": dataSourceVirtualNetworkGatewayConnection(),
"azurerm_virtual_network": dataSourceVirtualNetwork(),
Expand Down
128 changes: 128 additions & 0 deletions internal/services/network/virtual_hub_route_table_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package network

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceVirtualHubRouteTable() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceVirtualHubRouteTableRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.HubRouteTableID(id)
return err
}),

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: networkValidate.HubRouteTableName,
},

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"virtual_hub_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: networkValidate.VirtualHubName,
},

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

"labels": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

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

"destinations": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

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

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

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

func dataSourceVirtualHubRouteTableRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.HubRouteTableClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewHubRouteTableID(subscriptionId, d.Get("resource_group_name").(string), d.Get("virtual_hub_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.VirtualHubName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.SetId(id.ID())

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("virtual_hub_name", id.VirtualHubName)
d.Set("virtual_hub_id", parse.NewVirtualHubID(id.SubscriptionId, id.ResourceGroup, id.VirtualHubName).ID())

if props := resp.HubRouteTableProperties; props != nil {
d.Set("labels", utils.FlattenStringSlice(props.Labels))

if err := d.Set("route", flattenVirtualHubRouteTableHubRoutes(props.Routes)); err != nil {
return fmt.Errorf("setting `route`: %+v", err)
}
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package network_test

import (
"fmt"
"testing"

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

type VirtualHubRouteTableDataSource struct{}

func TestAccDataSourceAzureRMVirtualHubRouteTable_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_virtual_hub_route_table", "test")
r := VirtualHubRouteTableDataSource{}

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

func (VirtualHubRouteTableDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_virtual_hub_route_table" "test" {
name = azurerm_virtual_hub_route_table.test.name
resource_group_name = azurerm_virtual_hub.test.resource_group_name
virtual_hub_name = azurerm_virtual_hub.test.name
}
`, VirtualHubRouteTableResource{}.basic(data))
}
47 changes: 47 additions & 0 deletions website/docs/d/marketplace_agreement.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "Compute"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_marketplace_agreement"
description: |-
Gets information about an existing Marketplace Agreement.
---

# azurerm_marketplace_agreement

Uses this data source to access information about an existing Marketplace Agreement.

## Example Usage

```hcl
data "azurerm_marketplace_agreement" "barracuda" {
publisher = "barracudanetworks"
offer = "waf"
plan = "hourly"
}
output "azurerm_marketplace_agreement_id" {
value = data.azurerm_marketplace_agreement.id
}
```

## Argument Reference

The following arguments are supported:

* `offer` - The Offer of the Marketplace Image.

* `plan` - The Plan of the Marketplace Image.

* `publisher` - The Publisher of the Marketplace Image.

## Attributes Reference

The following attributes are exported:

* `id` - The ID of the Marketplace Agreement.

## 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 Marketplace Agreement.
Loading

0 comments on commit 861e637

Please sign in to comment.