func (e *subscriptionExternal) Delete(ctx context.Context, mg resource.Managed) error {
	cr, ok := mg.(*v1alpha1.Subscription)
	if !ok {
		return errors.New(errNotSubscription)
	}
	_, err := e.ps.Projects.Subscriptions.Delete(subscription.GetFullyQualifiedName(e.projectID,
		meta.GetExternalName(cr))).Context(ctx).Do()
	return errors.Wrap(resource.Ignore(gcp.IsErrorNotFound, err), errDeleteSubscription)
}
Delete initiates an deletion of the external resource.
package pubsub
import (
	"context"
	"time"
	"github.com/google/go-cmp/cmp"
	pubsub "google.golang.org/api/pubsub/v1"
	"k8s.io/client-go/util/workqueue"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
	"github.com/crossplane/crossplane-runtime/pkg/errors"
	"github.com/crossplane/crossplane-runtime/pkg/event"
	"github.com/crossplane/crossplane-runtime/pkg/logging"
	"github.com/crossplane/crossplane-runtime/pkg/meta"
	"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
	"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
	"github.com/crossplane/crossplane-runtime/pkg/resource"
	"github.com/crossplane/provider-gcp/apis/pubsub/v1alpha1"
	gcp "github.com/crossplane/provider-gcp/pkg/clients"
	"github.com/crossplane/provider-gcp/pkg/clients/subscription"
)
const (
	errNotSubscription        = "managed resource is not of type Subscription"
	errGetSubscription        = "cannot get Subscription"
	errUpdateSubscription     = "cannot update Subscription"
	errKubeUpdateSubscription = "cannot update Subscription custom resource"
	errCreateSubscription     = "cannot create Subscription"
	errDeleteSubscription     = "cannot delete Subscription"
)
func SetupSubscription(mgr ctrl.Manager, l logging.Logger, rl workqueue.RateLimiter, poll time.Duration) error {
	name := managed.ControllerName(v1alpha1.SubscriptionGroupKind)
	return ctrl.NewControllerManagedBy(mgr).
		Named(name).
		WithOptions(controller.Options{
			RateLimiter: ratelimiter.NewDefaultManagedRateLimiter(rl),
		}).
		For(&v1alpha1.Subscription{}).
		Complete(managed.NewReconciler(mgr,
			resource.ManagedKind(v1alpha1.SubscriptionGroupVersionKind),
			managed.WithExternalConnecter(&subscriptionConnector{client: mgr.GetClient()}),
			managed.WithPollInterval(poll),
			managed.WithLogger(l.WithValues("controller", name)),
			managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name)))))
}
type subscriptionConnector struct {
	client client.Client
}
func (c *subscriptionConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
	projectID, opts, err := gcp.GetAuthInfo(ctx, c.client, mg)
	if err != nil {
		return nil, err
	}
	s, err := pubsub.NewService(ctx, opts)
	if err != nil {
		return nil, errors.Wrap(err, errNewClient)
	}
	return &subscriptionExternal{projectID: projectID, client: c.client, ps: s}, nil
}
type subscriptionExternal struct {
	projectID string
	client    client.Client
	ps        *pubsub.Service
}
func (e *subscriptionExternal) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {
	cr, ok := mg.(*v1alpha1.Subscription)
	if !ok {
		return managed.ExternalObservation{}, errors.New(errNotSubscription)
	}
	s, err := e.ps.Projects.Subscriptions.Get(subscription.GetFullyQualifiedName(e.projectID, meta.GetExternalName(cr))).Context(ctx).Do()
	if err != nil {
		return managed.ExternalObservation{}, errors.Wrap(resource.Ignore(gcp.IsErrorNotFound, err), errGetSubscription)
	}
	currentSpec := cr.Spec.ForProvider.DeepCopy()
	subscription.LateInitialize(&cr.Spec.ForProvider, *s)
	if !cmp.Equal(currentSpec, &cr.Spec.ForProvider) {
		if err := e.client.Update(ctx, cr); err != nil {
			return managed.ExternalObservation{}, errors.Wrap(err, errKubeUpdateSubscription)
		}
	}
	cr.SetConditions(xpv1.Available())
	return managed.ExternalObservation{
		ResourceExists:   true,
		ResourceUpToDate: subscription.IsUpToDate(e.projectID, cr.Spec.ForProvider, *s),
	}, nil
}
func (e *subscriptionExternal) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
	cr, ok := mg.(*v1alpha1.Subscription)
	if !ok {
		return managed.ExternalCreation{}, errors.New(errNotSubscription)
	}
	cr.SetConditions(xpv1.Creating())
	_, err := e.ps.Projects.Subscriptions.Create(subscription.GetFullyQualifiedName(e.projectID, meta.GetExternalName(cr)),
		subscription.GenerateSubscription(e.projectID, meta.GetExternalName(cr), cr.Spec.ForProvider)).Context(ctx).Do()
	return managed.ExternalCreation{}, errors.Wrap(err, errCreateSubscription)
}
func (e *subscriptionExternal) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) {
	cr, ok := mg.(*v1alpha1.Subscription)
	if !ok {
		return managed.ExternalUpdate{}, errors.New(errNotSubscription)
	}
	s, err := e.ps.Projects.Subscriptions.Get(subscription.GetFullyQualifiedName(e.projectID, meta.GetExternalName(cr))).Context(ctx).Do()
	if err != nil {
		return managed.ExternalUpdate{}, errors.Wrap(err, errGetSubscription)
	}
	_, err = e.ps.Projects.Subscriptions.Patch(subscription.GetFullyQualifiedName(e.projectID, meta.GetExternalName(cr)),
		subscription.GenerateUpdateRequest(meta.GetExternalName(cr), cr.Spec.ForProvider, *s)).Context(ctx).Do()
	return managed.ExternalUpdate{}, errors.Wrap(err, errUpdateSubscription)
}