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

New Data Source: azurerm_api_management_subscription #27824

Merged
merged 8 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion contributing/topics/guide-new-data-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (ResourceGroupExampleDataSource) Attributes() map[string]*pluginsdk.Schema
}

func (ResourceGroupExampleDataSource) ModelObject() interface{} {
return &ResourceGroupExampleDataSourceModel
return &ResourceGroupExampleDataSourceModel{}
}

func (ResourceGroupExampleDataSource) ResourceType() string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package apimanagement

import (
"context"
"fmt"
"strings"
"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-sdk/resource-manager/apimanagement/2022-08-01/api"
"github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2022-08-01/product"
"github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2022-08-01/subscription"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ sdk.DataSource = ApiManagementSubscriptionDataSource{}

type ApiManagementSubscriptionDataSource struct{}

type ApiManagementSubscriptionDataSourceModel struct {
ApiManagementName string `tfschema:"api_management_name"`
ResourceGroupName string `tfschema:"resource_group_name"`
SubscriptionId string `tfschema:"subscription_id"`
AllowTracing bool `tfschema:"allow_tracing"`
ApiId string `tfschema:"api_id"`
DisplayName string `tfschema:"display_name"`
PrimaryKey string `tfschema:"primary_key"`
ProductId string `tfschema:"product_id"`
SecondaryKey string `tfschema:"secondary_key"`
State string `tfschema:"state"`
UserId string `tfschema:"user_id"`
}

func (ApiManagementSubscriptionDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"api_management_name": schemaz.SchemaApiManagementDataSourceName(),

"resource_group_name": commonschema.ResourceGroupName(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's replace these with the resource's ID since we can infer the resource group from it

Suggested change
"api_management_name": schemaz.SchemaApiManagementDataSourceName(),
"resource_group_name": commonschema.ResourceGroupName(),
"api_management_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: apimanagementservice.ValidateServiceID,
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the changes, and also updated the doc.


"subscription_id": schemaz.SchemaApiManagementChildDataSourceName(),
}
}

func (ApiManagementSubscriptionDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{

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

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

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

"primary_key": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
},

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

"secondary_key": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},

"state": {
Type: pluginsdk.TypeString,
Computed: true,
},
"user_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

func (ApiManagementSubscriptionDataSource) ModelObject() interface{} {
return &ApiManagementSubscriptionDataSourceModel{}
}

func (ApiManagementSubscriptionDataSource) ResourceType() string {
return "azurerm_api_management_subscription"
}

func (ApiManagementSubscriptionDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{

Timeout: 5 * time.Minute,

CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.ApiManagement.SubscriptionsClient
subscriptionId := metadata.Client.Account.SubscriptionId

var state ApiManagementSubscriptionDataSourceModel

if err := metadata.Decode(&state); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
id := subscription.NewSubscriptions2ID(subscriptionId, state.ResourceGroupName, state.ApiManagementName, state.SubscriptionId)

resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

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

metadata.SetID(id)

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
productId := ""
apiId := ""
// check if the subscription is for all apis or a specific product/ api
if props.Scope != "" && !strings.HasSuffix(props.Scope, "/apis") {
// the scope is either a product or api id
parseId, err := product.ParseProductIDInsensitively(props.Scope)
if err == nil {
productId = parseId.ID()
} else {
parsedApiId, err := api.ParseApiIDInsensitively(props.Scope)
if err != nil {
return fmt.Errorf("parsing scope into product/ api id %q: %+v", props.Scope, err)
}
apiId = parsedApiId.ID()
}
}
state.AllowTracing = pointer.From(props.AllowTracing)
state.ApiId = apiId
state.DisplayName = pointer.From(props.DisplayName)
state.ProductId = productId
CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
state.State = string(props.State)
state.UserId = pointer.From(props.OwnerId)
}
}

// Primary and secondary keys must be got from this additional api
keyResp, err := client.ListSecrets(ctx, id)
if err != nil {
return fmt.Errorf("listing Subscription %q Primary and Secondary Keys (API Management Service %q / Resource Group %q): %+v", id.SubscriptionId, id.ServiceName, id.ResourceGroupName, err)
CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
}
if model := keyResp.Model; model != nil {
state.SecondaryKey = pointer.From(model.SecondaryKey)
state.PrimaryKey = pointer.From(model.PrimaryKey)
}

return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package apimanagement_test

import (
"fmt"
"testing"

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

type ApiManagementSubscriptionDataSource struct{}

func TestAccDataSourceApiManagementSubscription_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_api_management_subscription", "test")
r := ApiManagementSubscriptionDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("subscription_id").HasValue("test-subscription"),
check.That(data.ResourceName).Key("display_name").HasValue("Test Subscription"),
check.That(data.ResourceName).Key("allow_tracing").HasValue("true"),
check.That(data.ResourceName).Key("state").HasValue("active"),
),
},
})
}

func (ApiManagementSubscriptionDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "accTestRG-%d"
location = "%s"
}

resource "azurerm_api_management" "test" {
name = "acctestAM-%d"
publisher_name = "pub1"
publisher_email = "pub1@email.com"
sku_name = "Consumption_0"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_api_management_subscription" "test" {
resource_group_name = azurerm_resource_group.test.name
api_management_name = azurerm_api_management.test.name
subscription_id = "test-subscription"
allow_tracing = true
display_name = "Test Subscription"
state = "active"
}

data "azurerm_api_management_subscription" "test" {
resource_group_name = azurerm_api_management_subscription.test.resource_group_name
api_management_name = azurerm_api_management_subscription.test.api_management_name
subscription_id = azurerm_api_management_subscription.test.subscription_id
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
6 changes: 5 additions & 1 deletion internal/services/apimanagement/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type Registration struct{}

var _ sdk.TypedServiceRegistration = Registration{}
var _ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{}

func (r Registration) AssociatedGitHubLabel() string {
Expand Down Expand Up @@ -93,8 +94,11 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
}
}

// DataSources returns a list of Data Sources supported by this Service
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
ApiManagementSubscriptionDataSource{},
}
}

func (r Registration) Resources() []sdk.Resource {
Expand Down
63 changes: 63 additions & 0 deletions website/docs/d/api_management_subscription.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
subcategory: "API Management"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_api_management_subscription"
description: |-
Gets information about an existing API Management Subscription.
---

# Data Source: azurerm_api_management_subscription

Use this data source to access information about an existing API Management Subscription.

## Example Usage

```hcl
data "azurerm_api_management_subscription" "example" {
resource_group_name = "example-rg"
subscription_id = "example-subscription"
api_management_name = "example-apim"
}

output "id" {
value = data.azurerm_api_management_subscription.example.subscription_id
}
```

## Arguments Reference

The following arguments are supported:

* `api_management_name` - (Required) The Name of the API Management Service in which this Subscription exists.

* `resource_group_name` - (Required) The Name of the Resource Group in which the API Management Service exists.

* `subscription_id` - (Required) The Identifier for the API Management Subscription.

## Attributes Reference

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

* `id` - The ID of the API Management Subscription.

* `allow_tracing` - Indicates whether tracing is enabled.

* `api_id` - The ID of the API assigned to this Subscription.

* `display_name` - The display name of this Subscription.

* `primary_key` - The primary key for this subscription.

* `product_id` - The ID of the Product assigned to this Subscription.

* `secondary_key` - The secondary key for this subscription.

* `state` - The state of this Subscription.

* `user_id` - The ID of the User assigned to this Subscription.

## 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 API Management Subscription.
Loading