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

APIM: Delete subscriptions when deleting products #3467

Merged
merged 7 commits into from
Oct 27, 2023
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
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"ms-azuretools.vscode-docker",
"redhat.vscode-yaml",
"ms-kubernetes-tools.vscode-kubernetes-tools",
"task.vscode-task"
"task.vscode-task",
"github.vscode-pull-request-github"
],
"settings": {
"terminal.integrated.defaultProfile.linux": "bash",
Expand Down
75 changes: 75 additions & 0 deletions v2/api/apimanagement/customizations/product_extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package customizations

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement"
"github.com/go-logr/logr"
"github.com/pkg/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/conversion"

storage "github.com/Azure/azure-service-operator/v2/api/apimanagement/v1api20220801storage"
"github.com/Azure/azure-service-operator/v2/internal/genericarmclient"
"github.com/Azure/azure-service-operator/v2/internal/resolver"
"github.com/Azure/azure-service-operator/v2/internal/util/to"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/extensions"
)

var _ extensions.Deleter = &ProductExtension{}

func (extension *ProductExtension) Delete(
ctx context.Context,
log logr.Logger,
resolver *resolver.Resolver,
armClient *genericarmclient.GenericClient,
obj genruntime.ARMMetaObject,
next extensions.DeleteFunc) (ctrl.Result, error) {

typedObj, ok := obj.(*storage.Product)
if !ok {
return ctrl.Result{}, errors.Errorf("cannot run on unknown resource type %T, expected *apiManagement.Product", obj)
}

// Type assert that we are the hub type. This will fail to compile if
// the hub type has been changed but this extension has not been updated to match
var _ conversion.Hub = typedObj

productName := typedObj.GetName()
id, err := genruntime.GetAndParseResourceID(typedObj)
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to get the ARM ResourceId for %s", productName)
}

if id.Parent == nil {
return ctrl.Result{}, errors.Wrapf(err, ". APIM Product had no parent ID: %s", id.String())
}
parentName := id.Parent.Name

// Using armClient.ClientOptions() here ensures we share the same HTTP connection, so this is not opening a new
// connection each time through
clientFactory, err := armapimanagement.NewClientFactory(id.SubscriptionID, armClient.Creds(), armClient.ClientOptions())
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to create new apimClient")
}

// This is a synchronous operation
_, err = clientFactory.NewProductClient().Delete(
ross-p-smith marked this conversation as resolved.
Show resolved Hide resolved
ctx,
id.ResourceGroupName,
parentName,
productName,
"*",
&armapimanagement.ProductClientDeleteOptions{
DeleteSubscriptions: to.Ptr(true),
})
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to delete product %q", productName)
}

return next(ctx, log, resolver, armClient, obj)
}
21 changes: 10 additions & 11 deletions v2/internal/controllers/crd_apimanagement_20220801_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ func Test_ApiManagement_20220801_CRUD(t *testing.T) {
PublisherEmail: to.Ptr("ASO@testing.com"),
PublisherName: to.Ptr("ASOTesting"),
Sku: &sku,
Restore: to.Ptr(true),
},
}

// APIM takes a long time to provision. When you are authoring tests in this file. I
// recommend you change this line to CreateResourceAndWaitWithoutCleanup.
tc.CreateResourcesAndWait(&service)
tc.CreateResourceAndWait(&service)

tc.Expect(service.Status.Id).ToNot(BeNil())

Expand Down Expand Up @@ -100,13 +99,12 @@ func Test_ApiManagement_20220801_CRUD(t *testing.T) {
APIM_Policy_CRUD(tc, &service)
},
},
// TODO: https://github.com/Azure/azure-service-operator/issues/3408
// testcommon.Subtest{
// Name: "APIM Product CRUD",
// Test: func(tc *testcommon.KubePerTestContext) {
// APIM_Product_CRUD(tc, &service)
// },
// },
testcommon.Subtest{
Name: "APIM Product CRUD",
Test: func(tc *testcommon.KubePerTestContext) {
APIM_Product_CRUD(tc, &service)
},
},
testcommon.Subtest{
Name: "APIM Api CRUD",
Test: func(tc *testcommon.KubePerTestContext) {
Expand Down Expand Up @@ -266,8 +264,9 @@ func APIM_Product_CRUD(tc *testcommon.KubePerTestContext, service client.Object)
tc.Expect(product.Status).ToNot(BeNil())
tc.Expect(product.Status.Id).ToNot(BeNil())

// The product would have created a subscription, so we need to delete that first
// Pass in deleteSubscription = true
// The product would have created a subscription automatically,
// we have had to extend (products_extensions.go) to pass the following option
// deleteSubscription = true

defer tc.DeleteResourceAndWait(&product)

Expand Down
Loading