You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: coordinator.go
    path of file: ./repos/erda/internal/apps/msp/resource/deploy/coordinator
    
    the code of the file until where you have to start completion: // Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License 
    
func (p *provider) UnDeploy(resourceId string) error {
	// judge if is tenant
	// call scheduler if need
	// mark tmc_instance as deleted
	// delete sub resources recursive if any
	// delete request relation record

	tmcInstance, tenant, tmc, dice, err := p.defaultHandler.GetInstanceById(resourceId)
	if err != nil {
		return err
	}
	if tmcInstance == nil && tenant == nil {
		return fmt.Errorf("resource not found")
	}

	handler := p.findHandler(tmc)
	if handler == nil {
		return fmt.Errorf("handler not found")
	}

	if tmc.DeployMode == handlers.DeployModePAAS {
		if dice != nil && dice.Services != nil && len(dice.Services) > 0 {
			handler.UnDeploy(tmcInstance)
		}
		handler.DeleteTmcInstance(tmcInstance, handlers.TmcInstanceStatusDeleted)
	} else {
		clusterConfig, err := handler.GetClusterConfig(tmcInstance.Az)
		if err != nil {
			return err
		}
		handler.DeleteTenant(tenant, tmcInstance, clusterConfig)
	}

	parentId := tmcInstance.ID
	if tenant != nil {
		parentId = tenant.ID
	}

	childIds := handler.GetRelationResourceIds(parentId)
	for _, childId := range childIds {
		p.UnDeploy(childId)
		_ = handler.DeleteRequestRelation(parentId, childId)
	}

	return nil
}