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

r/azurerm_api_management_api_subscription: support for api_id #12025

Merged
merged 5 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ func resourceApiManagementSubscription() *pluginsdk.Resource {
ValidateFunc: validation.StringIsNotEmpty,
},

// TODO this now sets the scope property - either a scope block needs adding or additional properties `api_id` and maybe `all_apis`
"product_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validate.ProductID,
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validate.ProductID,
ConflictsWith: []string{"api_id"},
},

"api_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validate.ApiID,
ConflictsWith: []string{"product_id"},
},

"state": {
Expand Down Expand Up @@ -138,14 +146,25 @@ func resourceApiManagementSubscriptionCreateUpdate(d *pluginsdk.ResourceData, me
}

displayName := d.Get("display_name").(string)
productId := d.Get("product_id").(string)
productId, productSet := d.GetOk("product_id")
apiId, apiSet := d.GetOk("api_id")
state := d.Get("state").(string)
allowTracing := d.Get("allow_tracing").(bool)

var scope string
switch {
case productSet:
scope = productId.(string)
case apiSet:
scope = apiId.(string)
default:
scope = "all_apis"
}

params := apimanagement.SubscriptionCreateParameters{
SubscriptionCreateParameterProperties: &apimanagement.SubscriptionCreateParameterProperties{
DisplayName: utils.String(displayName),
Scope: utils.String(productId),
Scope: utils.String(scope),
State: apimanagement.SubscriptionState(state),
AllowTracing: utils.Bool(allowTracing),
},
Expand Down Expand Up @@ -207,14 +226,22 @@ func resourceApiManagementSubscriptionRead(d *pluginsdk.ResourceData, meta inter
d.Set("display_name", props.DisplayName)
d.Set("state", string(props.State))
productId := ""
apiId := ""
if *props.Scope != "" {
// the scope is either a product or api id or "all_apis" constant
parseId, err := parse.ProductID(*props.Scope)
if err != nil {
return fmt.Errorf("parsing product id %q: %+v", *props.Scope, err)
if err == nil {
productId = parseId.ID()
} else {
parsedApiId, err := parse.ApiID(*props.Scope)
if err != nil {
return fmt.Errorf("parsing scope into product/ api id %q: %+v", *props.Scope, err)
}
apiId = parsedApiId.ID()
}
productId = parseId.ID()
}
d.Set("product_id", productId)
d.Set("api_id", apiId)
d.Set("user_id", props.OwnerID)
d.Set("allow_tracing", props.AllowTracing)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ func TestAccApiManagementSubscription_withoutUser(t *testing.T) {
})
}

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

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withApiId(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("api_id").Exists(),
check.That(data.ResourceName).Key("product_id").HasValue(""),
check.That(data.ResourceName).Key("user_id").HasValue(""),
),
},
data.ImportStep(),
})
}

func (ApiManagementSubscriptionResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.SubscriptionID(state.ID)
if err != nil {
Expand Down Expand Up @@ -232,6 +250,30 @@ resource "azurerm_api_management_subscription" "test" {
`, r.template(data))
}

func (ApiManagementSubscriptionResource) withApiId(data acceptance.TestData) string {
template := ApiManagementSubscriptionResource{}.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_api_management_api" "test" {
name = "TestApi"
resource_group_name = azurerm_api_management.test.resource_group_name
api_management_name = azurerm_api_management.test.name
revision = "1"
protocols = ["https"]
display_name = "Test API"
path = "test"
}

resource "azurerm_api_management_subscription" "test" {
resource_group_name = azurerm_api_management.test.resource_group_name
api_management_name = azurerm_api_management.test.name
api_id = azurerm_api_management_api.test.id
display_name = "Butter Parser API Enterprise Edition"
}
`, template)
}

func (ApiManagementSubscriptionResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
8 changes: 7 additions & 1 deletion website/docs/r/api_management_subscription.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ The following arguments are supported:

* `resource_group_name` - (Required) The name of the Resource Group in which the API Management Service exists. Changing this forces a new resource to be created.

* `product_id` - (Required) The ID of the Product which should be assigned to this Subscription. Changing this forces a new resource to be created.
* `product_id` - (Optional) The ID of the Product which should be assigned to this Subscription. Changing this forces a new resource to be created.

-> **Info:** Only one of `product_id` and `api_id` can be set. If both are missing `all_apis` scope is used for the subscription.

* `user_id` - (Optional) The ID of the User which should be assigned to this Subscription. Changing this forces a new resource to be created.

* `api_id` - (Optional) The ID of the API which should be assigned to this Subscription. Changing this forces a new resource to be created.

-> **Info:** Only one of `product_id` and `api_id` can be set. If both are missing `all_apis` scope is used for the subscription.

---

* `state` - (Optional) The state of this Subscription. Possible values are `active`, `cancelled`, `expired`, `rejected`, `submitted` and `suspended`. Defaults to `submitted`.
Expand Down