func (im *DefaultImportManager) ExportConfig(excludedOrgs map[string]string, excludedSpaces map[string]string) error {
	//Get all the users from the foundation
	uaaUsers, err := im.UAAMgr.ListUsers()
	if err != nil {
		lo.G.Error("Unable to retrieve users")
		return err
	}
	lo.G.Debugf("uaa user id map %v", uaaUsers)
	//Get all the orgs
	orgs, err := im.OrgManager.ListOrgs()
	if err != nil {
		lo.G.Errorf("Unable to retrieve orgs. Error : %s", err)
		return err
	}

	securityGroups, err := im.SecurityGroupManager.ListNonDefaultSecurityGroups()
	if err != nil {
		lo.G.Errorf("Unable to retrieve security groups. Error : %s", err)
		return err
	}

	defaultSecurityGroups, err := im.SecurityGroupManager.ListDefaultSecurityGroups()
	if err != nil {
		lo.G.Errorf("Unable to retrieve security groups. Error : %s", err)
		return err
	}

	isolationSegments, err := im.IsoSegmentManager.ListIsolationSegments()
	if err != nil {
		lo.G.Errorf("Unable to retrieve isolation segments. Error : %s", err)
		return err
	}
	configMgr := config.NewManager(im.ConfigDir)
	lo.G.Info("Trying to delete existing config directory")
	//Delete existing config directory
	err = configMgr.DeleteConfigIfExists()
	if err != nil {
		return err
	}
	//Create a brand new directory
	lo.G.Info("Trying to create new config folder")

	var uaaUserOrigin string
	for _, usr := range uaaUsers.List() {
		if usr.Origin != "" {
			uaaUserOrigin = usr.Origin
			break
		}
	}
	lo.G.Infof("Using UAA user origin: %s", uaaUserOrigin)
	err = configMgr.CreateConfigIfNotExists(uaaUserOrigin)
	if err != nil {
		return err
	}

	globalConfig, err := configMgr.GetGlobalConfig()
	if err != nil {
		return err
	}

	lo.G.Debugf("Orgs to process: %s", orgs)

	globalConfig.EnableServiceAccess = true
	for _, org := range orgs {
		orgName := org.Name
		if _, ok := excludedOrgs[orgName]; ok {
			lo.G.Infof("Skipping org: %s as it is ignored from export", orgName)
			continue
		}

		lo.G.Infof("Processing org: %s ", orgName)
		orgConfig := &config.OrgConfig{Org: orgName}
		//Add users
		im.addOrgUsers(orgConfig, uaaUsers, org.Guid)
		//Add Quota definition if applicable
		if org.QuotaDefinitionGuid != "" {
			orgQuota, err := org.Quota()
			if err != nil {
				return err
			}
			if orgQuota != nil {
				orgConfig.EnableOrgQuota = false
				orgConfig.NamedQuota = orgQuota.Name
				// orgConfig.MemoryLimit = config.ByteSize(orgQuota.MemoryLimit)
				// orgConfig.InstanceMemoryLimit = config.ByteSize(orgQuota.InstanceMemoryLimit)
				// orgConfig.TotalRoutes = config.AsString(orgQuota.TotalRoutes)
				// orgConfig.TotalServices = config.AsString(orgQuota.TotalServices)
				// orgConfig.PaidServicePlansAllowed = orgQuota.NonBasicServicesAllowed
				// orgConfig.TotalPrivateDomains = config.AsString(orgQuota.TotalPrivateDomains)
				// orgConfig.TotalReservedRoutePorts = config.AsString(orgQuota.TotalReservedRoutePorts)
				// orgConfig.TotalServiceKeys = config.AsString(orgQuota.TotalServiceKeys)
				// orgConfig.AppInstanceLimit = config.AsString(orgQuota.AppInstanceLimit)
				// orgConfig.AppTaskLimit = config.AsString(orgQuota.AppTaskLimit)

			}
		}
		if org.DefaultIsolationSegmentGuid != "" {
			for _, isosegment := range isolationSegments {
				if isosegment.GUID == org.DefaultIsolationSegmentGuid {
					orgConfig.DefaultIsoSegment = isosegment.Name
				}
			}
		}

		privatedomains, err := im.PrivateDomainManager.ListOrgSharedPrivateDomains(org.Guid)
		if err != nil {
			return err
		}
		for privatedomain, _ := range privatedomains {
			orgConfig.SharedPrivateDomains = append(orgConfig.SharedPrivateDomains, privatedomain)
		}

		privatedomains, err = im.PrivateDomainManager.ListOrgOwnedPrivateDomains(org.Guid)
		if err != nil {
			return err
		}
		for privatedomain, _ := range privatedomains {
			orgConfig.PrivateDomains = append(orgConfig.PrivateDomains, privatedomain)
		}

		orgConfig.ServiceAccess = make(map[string][]string)
		serviceInfo, err := im.ServiceAccessManager.ListServiceInfo()
		if err != nil {
			return err
		}
		for service, plans := range serviceInfo.AllPlans() {
			accessPlans := []string{}
			for _, plan := range plans {
				if plan.Public || plan.OrgHasAccess(org.Guid) {
					accessPlans = append(accessPlans, plan.Name)
				}
			}
			orgConfig.ServiceAccess[service] = accessPlans
		}

		spacesConfig := &config.Spaces{Org: orgConfig.Org, EnableDeleteSpaces: true}
		configMgr.AddOrgToConfig(orgConfig, spacesConfig)

		lo.G.Infof("Done creating org %s", orgConfig.Org)
		lo.G.Infof("Listing spaces for org %s", orgConfig.Org)
		spaces, _ := im.SpaceManager.ListSpaces(org.Guid)
		lo.G.Infof("Found %d Spaces for org %s", len(spaces), orgConfig.Org)

		spaceQuotas, err := im.QuotaManager.ListAllSpaceQuotasForOrg(org.Guid)
		if err != nil {
			return err
		}

		for _, spaceQuota := range spaceQuotas {
			if !im.doesSpaceExist(spaces, spaceQuota.Name) {
				err = configMgr.AddSpaceQuota(config.SpaceQuota{
					Org:                     org.Name,
					Name:                    spaceQuota.Name,
					AppInstanceLimit:        config.AsString(spaceQuota.AppInstanceLimit),
					TotalReservedRoutePorts: config.AsString(spaceQuota.TotalReservedRoutePorts),
					TotalServiceKeys:        config.AsString(spaceQuota.TotalServiceKeys),
					AppTaskLimit:            config.AsString(spaceQuota.AppTaskLimit),
					MemoryLimit:             config.ByteSize(spaceQuota.MemoryLimit),
					InstanceMemoryLimit:     config.ByteSize(spaceQuota.InstanceMemoryLimit),
					TotalRoutes:             config.AsString(spaceQuota.TotalRoutes),
					TotalServices:           config.AsString(spaceQuota.TotalServices),
					PaidServicePlansAllowed: spaceQuota.NonBasicServicesAllowed,
				})
				if err != nil {
					return err
				}
			}
		}

		for _, orgSpace := range spaces {
			spaceName := orgSpace.Name
			if _, ok := excludedSpaces[spaceName]; ok {
				lo.G.Infof("Skipping space: %s as it is ignored from export", spaceName)
				continue
			}
			lo.G.Infof("Processing space: %s", spaceName)

			spaceConfig := &config.SpaceConfig{Org: org.Name, Space: spaceName, EnableUnassignSecurityGroup: true}
			//Add users
			im.addSpaceUsers(spaceConfig, uaaUsers, orgSpace.Guid)
			//Add Quota definition if applicable
			if orgSpace.QuotaDefinitionGuid != "" {
				quota, err := orgSpace.Quota()
				if err != nil {
					return err
				}
				if quota != nil {
					if quota.Name == orgSpace.Name {
						spaceConfig.EnableSpaceQuota = true
						spaceConfig.MemoryLimit = config.ByteSize(quota.MemoryLimit)
						spaceConfig.InstanceMemoryLimit = config.ByteSize(quota.InstanceMemoryLimit)
						spaceConfig.TotalRoutes = config.AsString(quota.TotalRoutes)
						spaceConfig.TotalServices = config.AsString(quota.TotalServices)
						spaceConfig.PaidServicePlansAllowed = quota.NonBasicServicesAllowed
						spaceConfig.TotalReservedRoutePorts = config.AsString(quota.TotalReservedRoutePorts)
						spaceConfig.TotalServiceKeys = config.AsString(quota.TotalServiceKeys)
						spaceConfig.AppInstanceLimit = config.AsString(quota.AppInstanceLimit)
						spaceConfig.AppTaskLimit = config.AsString(quota.AppTaskLimit)
					} else {
						spaceConfig.NamedQuota = quota.Name
					}
				}
			} else {
				spaceConfig.MemoryLimit = orgConfig.MemoryLimit
				spaceConfig.InstanceMemoryLimit = orgConfig.InstanceMemoryLimit
				spaceConfig.TotalRoutes = orgConfig.TotalRoutes
				spaceConfig.TotalServices = orgConfig.TotalServices
				spaceConfig.PaidServicePlansAllowed = orgConfig.PaidServicePlansAllowed
				spaceConfig.TotalReservedRoutePorts = orgConfig.TotalReservedRoutePorts
				spaceConfig.TotalServiceKeys = orgConfig.TotalServiceKeys
				spaceConfig.AppInstanceLimit = orgConfig.AppInstanceLimit
				spaceConfig.AppTaskLimit = orgConfig.AppTaskLimit
			}

			if orgSpace.IsolationSegmentGuid != "" {
				for _, isosegment := range isolationSegments {
					if isosegment.GUID == orgSpace.IsolationSegmentGuid {
						spaceConfig.IsoSegment = isosegment.Name
					}
				}

			}
			if orgSpace.AllowSSH {
				spaceConfig.AllowSSH = true
			}

			spaceSGName := fmt.Sprintf("%s-%s", orgName, spaceName)
			if spaceSGNames, err := im.SecurityGroupManager.ListSpaceSecurityGroups(orgSpace.Guid); err == nil {
				for securityGroupName, _ := range spaceSGNames {
					lo.G.Infof("Adding named security group [%s] to space [%s]", securityGroupName, spaceName)
					if securityGroupName != spaceSGName {
						spaceConfig.ASGs = append(spaceConfig.ASGs, securityGroupName)
					}
				}
			}

			configMgr.AddSpaceToConfig(spaceConfig)

			if sgInfo, ok := securityGroups[spaceSGName]; ok {
				delete(securityGroups, spaceSGName)
				if rules, err := im.SecurityGroupManager.GetSecurityGroupRules(sgInfo.Guid); err == nil {
					configMgr.AddSecurityGroupToSpace(orgName, spaceName, rules)
				}
			}

		}
	}

	for sgName, sgInfo := range securityGroups {
		lo.G.Infof("Adding security group %s", sgName)
		if rules, err := im.SecurityGroupManager.GetSecurityGroupRules(sgInfo.Guid); err == nil {
			lo.G.Infof("Adding rules for %s", sgName)
			configMgr.AddSecurityGroup(sgName, rules)
		} else {
			lo.G.Error(err)
		}
	}

	for sgName, sgInfo := range defaultSecurityGroups {
		lo.G.Infof("Adding default security group %s", sgName)
		if sgInfo.Running {
			globalConfig.RunningSecurityGroups = append(globalConfig.RunningSecurityGroups, sgName)
		}
		if sgInfo.Staging {
			globalConfig.StagingSecurityGroups = append(globalConfig.StagingSecurityGroups, sgName)
		}
		if rules, err := im.SecurityGroupManager.GetSecurityGroupRules(sgInfo.Guid); err == nil {
			lo.G.Infof("Adding rules for %s", sgName)
			configMgr.AddDefaultSecurityGroup(sgName, rules)
		} else {
			lo.G.Error(err)
		}
	}

	orgQuotas, err := im.QuotaManager.Client.ListOrgQuotas()
	if err != nil {
		return err
	}

	for _, orgQuota := range orgQuotas {

		err = configMgr.AddOrgQuota(config.OrgQuota{
			Name:                    orgQuota.Name,
			AppInstanceLimit:        config.AsString(orgQuota.AppInstanceLimit),
			TotalPrivateDomains:     config.AsString(orgQuota.TotalPrivateDomains),
			TotalReservedRoutePorts: config.AsString(orgQuota.TotalReservedRoutePorts),
			TotalServiceKeys:        config.AsString(orgQuota.TotalServiceKeys),
			AppTaskLimit:            config.AsString(orgQuota.AppTaskLimit),
			MemoryLimit:             config.ByteSize(orgQuota.MemoryLimit),
			InstanceMemoryLimit:     config.ByteSize(orgQuota.InstanceMemoryLimit),
			TotalRoutes:             config.AsString(orgQuota.TotalRoutes),
			TotalServices:           config.AsString(orgQuota.TotalServices),
			PaidServicePlansAllowed: orgQuota.NonBasicServicesAllowed,
		})
		if err != nil {
			return err
		}
	}

	sharedDomains, err := im.SharedDomainManager.CFClient.ListSharedDomains()
	if err != nil {
		return err
	}

	routerGroups, err := im.SharedDomainManager.RoutingClient.RouterGroups()
	if err != nil {
		return err
	}
	globalConfig.EnableDeleteSharedDomains = true
	globalConfig.SharedDomains = make(map[string]config.SharedDomain)
	for _, sharedDomain := range sharedDomains {
		sharedDomainConfig := config.SharedDomain{
			Internal: sharedDomain.Internal,
		}
		if sharedDomain.RouterGroupGuid != "" {
			for _, routerGroup := range routerGroups {
				if routerGroup.Guid == sharedDomain.RouterGroupGuid {
					sharedDomainConfig.RouterGroup = routerGroup.Name
					continue
				}
			}
		}
		globalConfig.SharedDomains[sharedDomain.Name] = sharedDomainConfig
	}
	return configMgr.SaveGlobalConfig(globalConfig)
}
func (im *DefaultImportManager) ExportConfig(excludedOrgs map[string]string, excludedSpaces map[string]string) error {
	
	uaaUsers, err := im.UAAMgr.ListUsers()
	if err != nil {
		lo.G.Error("Unable to retrieve users")
		return err
	}
	lo.G.Debugf("uaa user id map %v", uaaUsers)
	
	orgs, err := im.OrgManager.ListOrgs()
	if err != nil {
		lo.G.Errorf("Unable to retrieve orgs. Error : %s", err)
		return err
	}

	securityGroups, err := im.SecurityGroupManager.ListNonDefaultSecurityGroups()
	if err != nil {
		lo.G.Errorf("Unable to retrieve security groups. Error : %s", err)
		return err
	}

	defaultSecurityGroups, err := im.SecurityGroupManager.ListDefaultSecurityGroups()
	if err != nil {
		lo.G.Errorf("Unable to retrieve security groups. Error : %s", err)
		return err
	}

	isolationSegments, err := im.IsoSegmentManager.ListIsolationSegments()
	if err != nil {
		lo.G.Errorf("Unable to retrieve isolation segments. Error : %s", err)
		return err
	}
	configMgr := config.NewManager(im.ConfigDir)
	lo.G.Info("Trying to delete existing config directory")
	
	err = configMgr.DeleteConfigIfExists()
	if err != nil {
		return err
	}
	
	lo.G.Info("Trying to create new config folder")

	var uaaUserOrigin string
	for _, usr := range uaaUsers.List() {
		if usr.Origin != "" {
			uaaUserOrigin = usr.Origin
			break
		}
	}
	lo.G.Infof("Using UAA user origin: %s", uaaUserOrigin)
	err = configMgr.CreateConfigIfNotExists(uaaUserOrigin)
	if err != nil {
		return err
	}

	globalConfig, err := configMgr.GetGlobalConfig()
	if err != nil {
		return err
	}

	lo.G.Debugf("Orgs to process: %s", orgs)

	globalConfig.EnableServiceAccess = true
	for _, org := range orgs {
		orgName := org.Name
		if _, ok := excludedOrgs[orgName]; ok {
			lo.G.Infof("Skipping org: %s as it is ignored from export", orgName)
			continue
		}

		lo.G.Infof("Processing org: %s ", orgName)
		orgConfig := &config.OrgConfig{Org: orgName}
		
		im.addOrgUsers(orgConfig, uaaUsers, org.Guid)
		
		if org.QuotaDefinitionGuid != "" {
			orgQuota, err := org.Quota()
			if err != nil {
				return err
			}
			if orgQuota != nil {
				orgConfig.EnableOrgQuota = false
				orgConfig.NamedQuota = orgQuota.Name
				
				
				
				
				
				
				
				
				
				

			}
		}
		if org.DefaultIsolationSegmentGuid != "" {
			for _, isosegment := range isolationSegments {
				if isosegment.GUID == org.DefaultIsolationSegmentGuid {
					orgConfig.DefaultIsoSegment = isosegment.Name
				}
			}
		}

		privatedomains, err := im.PrivateDomainManager.ListOrgSharedPrivateDomains(org.Guid)
		if err != nil {
			return err
		}
		for privatedomain, _ := range privatedomains {
			orgConfig.SharedPrivateDomains = append(orgConfig.SharedPrivateDomains, privatedomain)
		}

		privatedomains, err = im.PrivateDomainManager.ListOrgOwnedPrivateDomains(org.Guid)
		if err != nil {
			return err
		}
		for privatedomain, _ := range privatedomains {
			orgConfig.PrivateDomains = append(orgConfig.PrivateDomains, privatedomain)
		}

		orgConfig.ServiceAccess = make(map[string][]string)
		serviceInfo, err := im.ServiceAccessManager.ListServiceInfo()
		if err != nil {
			return err
		}
		for service, plans := range serviceInfo.AllPlans() {
			accessPlans := []string{}
			for _, plan := range plans {
				if plan.Public || plan.OrgHasAccess(org.Guid) {
					accessPlans = append(accessPlans, plan.Name)
				}
			}
			orgConfig.ServiceAccess[service] = accessPlans
		}

		spacesConfig := &config.Spaces{Org: orgConfig.Org, EnableDeleteSpaces: true}
		configMgr.AddOrgToConfig(orgConfig, spacesConfig)

		lo.G.Infof("Done creating org %s", orgConfig.Org)
		lo.G.Infof("Listing spaces for org %s", orgConfig.Org)
		spaces, _ := im.SpaceManager.ListSpaces(org.Guid)
		lo.G.Infof("Found %d Spaces for org %s", len(spaces), orgConfig.Org)

		spaceQuotas, err := im.QuotaManager.ListAllSpaceQuotasForOrg(org.Guid)
		if err != nil {
			return err
		}

		for _, spaceQuota := range spaceQuotas {
			if !im.doesSpaceExist(spaces, spaceQuota.Name) {
				err = configMgr.AddSpaceQuota(config.SpaceQuota{
					Org:                     org.Name,
					Name:                    spaceQuota.Name,
					AppInstanceLimit:        config.AsString(spaceQuota.AppInstanceLimit),
					TotalReservedRoutePorts: config.AsString(spaceQuota.TotalReservedRoutePorts),
					TotalServiceKeys:        config.AsString(spaceQuota.TotalServiceKeys),
					AppTaskLimit:            config.AsString(spaceQuota.AppTaskLimit),
					MemoryLimit:             config.ByteSize(spaceQuota.MemoryLimit),
					InstanceMemoryLimit:     config.ByteSize(spaceQuota.InstanceMemoryLimit),
					TotalRoutes:             config.AsString(spaceQuota.TotalRoutes),
					TotalServices:           config.AsString(spaceQuota.TotalServices),
					PaidServicePlansAllowed: spaceQuota.NonBasicServicesAllowed,
				})
				if err != nil {
					return err
				}
			}
		}

		for _, orgSpace := range spaces {
			spaceName := orgSpace.Name
			if _, ok := excludedSpaces[spaceName]; ok {
				lo.G.Infof("Skipping space: %s as it is ignored from export", spaceName)
				continue
			}
			lo.G.Infof("Processing space: %s", spaceName)

			spaceConfig := &config.SpaceConfig{Org: org.Name, Space: spaceName, EnableUnassignSecurityGroup: true}
			
			im.addSpaceUsers(spaceConfig, uaaUsers, orgSpace.Guid)
			
			if orgSpace.QuotaDefinitionGuid != "" {
				quota, err := orgSpace.Quota()
				if err != nil {
					return err
				}
				if quota != nil {
					if quota.Name == orgSpace.Name {
						spaceConfig.EnableSpaceQuota = true
						spaceConfig.MemoryLimit = config.ByteSize(quota.MemoryLimit)
						spaceConfig.InstanceMemoryLimit = config.ByteSize(quota.InstanceMemoryLimit)
						spaceConfig.TotalRoutes = config.AsString(quota.TotalRoutes)
						spaceConfig.TotalServices = config.AsString(quota.TotalServices)
						spaceConfig.PaidServicePlansAllowed = quota.NonBasicServicesAllowed
						spaceConfig.TotalReservedRoutePorts = config.AsString(quota.TotalReservedRoutePorts)
						spaceConfig.TotalServiceKeys = config.AsString(quota.TotalServiceKeys)
						spaceConfig.AppInstanceLimit = config.AsString(quota.AppInstanceLimit)
						spaceConfig.AppTaskLimit = config.AsString(quota.AppTaskLimit)
					} else {
						spaceConfig.NamedQuota = quota.Name
					}
				}
			} else {
				spaceConfig.MemoryLimit = orgConfig.MemoryLimit
				spaceConfig.InstanceMemoryLimit = orgConfig.InstanceMemoryLimit
				spaceConfig.TotalRoutes = orgConfig.TotalRoutes
				spaceConfig.TotalServices = orgConfig.TotalServices
				spaceConfig.PaidServicePlansAllowed = orgConfig.PaidServicePlansAllowed
				spaceConfig.TotalReservedRoutePorts = orgConfig.TotalReservedRoutePorts
				spaceConfig.TotalServiceKeys = orgConfig.TotalServiceKeys
				spaceConfig.AppInstanceLimit = orgConfig.AppInstanceLimit
				spaceConfig.AppTaskLimit = orgConfig.AppTaskLimit
			}

			if orgSpace.IsolationSegmentGuid != "" {
				for _, isosegment := range isolationSegments {
					if isosegment.GUID == orgSpace.IsolationSegmentGuid {
						spaceConfig.IsoSegment = isosegment.Name
					}
				}

			}
			if orgSpace.AllowSSH {
				spaceConfig.AllowSSH = true
			}

			spaceSGName := fmt.Sprintf("%s-%s", orgName, spaceName)
			if spaceSGNames, err := im.SecurityGroupManager.ListSpaceSecurityGroups(orgSpace.Guid); err == nil {
				for securityGroupName, _ := range spaceSGNames {
					lo.G.Infof("Adding named security group [%s] to space [%s]", securityGroupName, spaceName)
					if securityGroupName != spaceSGName {
						spaceConfig.ASGs = append(spaceConfig.ASGs, securityGroupName)
					}
				}
			}

			configMgr.AddSpaceToConfig(spaceConfig)

			if sgInfo, ok := securityGroups[spaceSGName]; ok {
				delete(securityGroups, spaceSGName)
				if rules, err := im.SecurityGroupManager.GetSecurityGroupRules(sgInfo.Guid); err == nil {
					configMgr.AddSecurityGroupToSpace(orgName, spaceName, rules)
				}
			}

		}
	}

	for sgName, sgInfo := range securityGroups {
		lo.G.Infof("Adding security group %s", sgName)
		if rules, err := im.SecurityGroupManager.GetSecurityGroupRules(sgInfo.Guid); err == nil {
			lo.G.Infof("Adding rules for %s", sgName)
			configMgr.AddSecurityGroup(sgName, rules)
		} else {
			lo.G.Error(err)
		}
	}

	for sgName, sgInfo := range defaultSecurityGroups {
		lo.G.Infof("Adding default security group %s", sgName)
		if sgInfo.Running {
			globalConfig.RunningSecurityGroups = append(globalConfig.RunningSecurityGroups, sgName)
		}
		if sgInfo.Staging {
			globalConfig.StagingSecurityGroups = append(globalConfig.StagingSecurityGroups, sgName)
		}
		if rules, err := im.SecurityGroupManager.GetSecurityGroupRules(sgInfo.Guid); err == nil {
			lo.G.Infof("Adding rules for %s", sgName)
			configMgr.AddDefaultSecurityGroup(sgName, rules)
		} else {
			lo.G.Error(err)
		}
	}

	orgQuotas, err := im.QuotaManager.Client.ListOrgQuotas()
	if err != nil {
		return err
	}

	for _, orgQuota := range orgQuotas {

		err = configMgr.AddOrgQuota(config.OrgQuota{
			Name:                    orgQuota.Name,
			AppInstanceLimit:        config.AsString(orgQuota.AppInstanceLimit),
			TotalPrivateDomains:     config.AsString(orgQuota.TotalPrivateDomains),
			TotalReservedRoutePorts: config.AsString(orgQuota.TotalReservedRoutePorts),
			TotalServiceKeys:        config.AsString(orgQuota.TotalServiceKeys),
			AppTaskLimit:            config.AsString(orgQuota.AppTaskLimit),
			MemoryLimit:             config.ByteSize(orgQuota.MemoryLimit),
			InstanceMemoryLimit:     config.ByteSize(orgQuota.InstanceMemoryLimit),
			TotalRoutes:             config.AsString(orgQuota.TotalRoutes),
			TotalServices:           config.AsString(orgQuota.TotalServices),
			PaidServicePlansAllowed: orgQuota.NonBasicServicesAllowed,
		})
		if err != nil {
			return err
		}
	}

	sharedDomains, err := im.SharedDomainManager.CFClient.ListSharedDomains()
	if err != nil {
		return err
	}

	routerGroups, err := im.SharedDomainManager.RoutingClient.RouterGroups()
	if err != nil {
		return err
	}
	globalConfig.EnableDeleteSharedDomains = true
	globalConfig.SharedDomains = make(map[string]config.SharedDomain)
	for _, sharedDomain := range sharedDomains {
		sharedDomainConfig := config.SharedDomain{
			Internal: sharedDomain.Internal,
		}
		if sharedDomain.RouterGroupGuid != "" {
			for _, routerGroup := range routerGroups {
				if routerGroup.Guid == sharedDomain.RouterGroupGuid {
					sharedDomainConfig.RouterGroup = routerGroup.Name
					continue
				}
			}
		}
		globalConfig.SharedDomains[sharedDomain.Name] = sharedDomainConfig
	}
	return configMgr.SaveGlobalConfig(globalConfig)
}
func ( im * DefaultImportManager ) ExportConfig ( excludedOrgs map [ string ] string , excludedSpaces map [ string ] string ) error { //Get all the users from the foundation uaaUsers , err := im . UAAMgr . ListUsers ( ) if err != nil { lo . G . Error ( " " ) return err } lo . G . Debugf ( " " , uaaUsers ) //Get all the orgs orgs , err := im . OrgManager . ListOrgs ( ) if err != nil { lo . G . Errorf ( " " , err ) return err } securityGroups , err := im . SecurityGroupManager . ListNonDefaultSecurityGroups ( ) if err != nil { lo . G . Errorf ( " " , err ) return err } defaultSecurityGroups , err := im . SecurityGroupManager . ListDefaultSecurityGroups ( ) if err != nil { lo . G . Errorf ( " " , err ) return err } isolationSegments , err := im . IsoSegmentManager . ListIsolationSegments ( ) if err != nil { lo . G . Errorf ( " " , err ) return err } configMgr := config . NewManager ( im . ConfigDir ) lo . G . Info ( " " ) //Delete existing config directory err = configMgr . DeleteConfigIfExists ( ) if err != nil { return err } //Create a brand new directory lo . G . Info ( " " ) var uaaUserOrigin string for _ , usr := range uaaUsers . List ( ) { if usr . Origin != " " { uaaUserOrigin = usr . Origin break } } lo . G . Infof ( " " , uaaUserOrigin ) err = configMgr . CreateConfigIfNotExists ( uaaUserOrigin ) if err != nil { return err } globalConfig , err := configMgr . GetGlobalConfig ( ) if err != nil { return err } lo . G . Debugf ( " " , orgs ) globalConfig . EnableServiceAccess = true for _ , org := range orgs { orgName := org . Name if _ , ok := excludedOrgs [ orgName ] ; ok { lo . G . Infof ( " " , orgName ) continue } lo . G . Infof ( " " , orgName ) orgConfig := & config . OrgConfig { Org : orgName } //Add users im . addOrgUsers ( orgConfig , uaaUsers , org . Guid ) //Add Quota definition if applicable if org . QuotaDefinitionGuid != " " { orgQuota , err := org . Quota ( ) if err != nil { return err } if orgQuota != nil { orgConfig . EnableOrgQuota = false orgConfig . NamedQuota = orgQuota . Name // orgConfig.MemoryLimit = config.ByteSize(orgQuota.MemoryLimit) // orgConfig.InstanceMemoryLimit = config.ByteSize(orgQuota.InstanceMemoryLimit) // orgConfig.TotalRoutes = config.AsString(orgQuota.TotalRoutes) // orgConfig.TotalServices = config.AsString(orgQuota.TotalServices) // orgConfig.PaidServicePlansAllowed = orgQuota.NonBasicServicesAllowed // orgConfig.TotalPrivateDomains = config.AsString(orgQuota.TotalPrivateDomains) // orgConfig.TotalReservedRoutePorts = config.AsString(orgQuota.TotalReservedRoutePorts) // orgConfig.TotalServiceKeys = config.AsString(orgQuota.TotalServiceKeys) // orgConfig.AppInstanceLimit = config.AsString(orgQuota.AppInstanceLimit) // orgConfig.AppTaskLimit = config.AsString(orgQuota.AppTaskLimit) } } if org . DefaultIsolationSegmentGuid != " " { for _ , isosegment := range isolationSegments { if isosegment . GUID == org . DefaultIsolationSegmentGuid { orgConfig . DefaultIsoSegment = isosegment . Name } } } privatedomains , err := im . PrivateDomainManager . ListOrgSharedPrivateDomains ( org . Guid ) if err != nil { return err } for privatedomain , _ := range privatedomains { orgConfig . SharedPrivateDomains = append ( orgConfig . SharedPrivateDomains , privatedomain ) } privatedomains , err = im . PrivateDomainManager . ListOrgOwnedPrivateDomains ( org . Guid ) if err != nil { return err } for privatedomain , _ := range privatedomains { orgConfig . PrivateDomains = append ( orgConfig . PrivateDomains , privatedomain ) } orgConfig . ServiceAccess = make ( map [ string ] [ ] string ) serviceInfo , err := im . ServiceAccessManager . ListServiceInfo ( ) if err != nil { return err } for service , plans := range serviceInfo . AllPlans ( ) { accessPlans := [ ] string { } for _ , plan := range plans { if plan . Public || plan . OrgHasAccess ( org . Guid ) { accessPlans = append ( accessPlans , plan . Name ) } } orgConfig . ServiceAccess [ service ] = accessPlans } spacesConfig := & config . Spaces { Org : orgConfig . Org , EnableDeleteSpaces : true } configMgr . AddOrgToConfig ( orgConfig , spacesConfig ) lo . G . Infof ( " " , orgConfig . Org ) lo . G . Infof ( " " , orgConfig . Org ) spaces , _ := im . SpaceManager . ListSpaces ( org . Guid ) lo . G . Infof ( " " , len ( spaces ) , orgConfig . Org ) spaceQuotas , err := im . QuotaManager . ListAllSpaceQuotasForOrg ( org . Guid ) if err != nil { return err } for _ , spaceQuota := range spaceQuotas { if ! im . doesSpaceExist ( spaces , spaceQuota . Name ) { err = configMgr . AddSpaceQuota ( config . SpaceQuota { Org : org . Name , Name : spaceQuota . Name , AppInstanceLimit : config . AsString ( spaceQuota . AppInstanceLimit ) , TotalReservedRoutePorts : config . AsString ( spaceQuota . TotalReservedRoutePorts ) , TotalServiceKeys : config . AsString ( spaceQuota . TotalServiceKeys ) , AppTaskLimit : config . AsString ( spaceQuota . AppTaskLimit ) , MemoryLimit : config . ByteSize ( spaceQuota . MemoryLimit ) , InstanceMemoryLimit : config . ByteSize ( spaceQuota . InstanceMemoryLimit ) , TotalRoutes : config . AsString ( spaceQuota . TotalRoutes ) , TotalServices : config . AsString ( spaceQuota . TotalServices ) , PaidServicePlansAllowed : spaceQuota . NonBasicServicesAllowed , } ) if err != nil { return err } } } for _ , orgSpace := range spaces { spaceName := orgSpace . Name if _ , ok := excludedSpaces [ spaceName ] ; ok { lo . G . Infof ( " " , spaceName ) continue } lo . G . Infof ( " " , spaceName ) spaceConfig := & config . SpaceConfig { Org : org . Name , Space : spaceName , EnableUnassignSecurityGroup : true } //Add users im . addSpaceUsers ( spaceConfig , uaaUsers , orgSpace . Guid ) //Add Quota definition if applicable if orgSpace . QuotaDefinitionGuid != " " { quota , err := orgSpace . Quota ( ) if err != nil { return err } if quota != nil { if quota . Name == orgSpace . Name { spaceConfig . EnableSpaceQuota = true spaceConfig . MemoryLimit = config . ByteSize ( quota . MemoryLimit ) spaceConfig . InstanceMemoryLimit = config . ByteSize ( quota . InstanceMemoryLimit ) spaceConfig . TotalRoutes = config . AsString ( quota . TotalRoutes ) spaceConfig . TotalServices = config . AsString ( quota . TotalServices ) spaceConfig . PaidServicePlansAllowed = quota . NonBasicServicesAllowed spaceConfig . TotalReservedRoutePorts = config . AsString ( quota . TotalReservedRoutePorts ) spaceConfig . TotalServiceKeys = config . AsString ( quota . TotalServiceKeys ) spaceConfig . AppInstanceLimit = config . AsString ( quota . AppInstanceLimit ) spaceConfig . AppTaskLimit = config . AsString ( quota . AppTaskLimit ) } else { spaceConfig . NamedQuota = quota . Name } } } else { spaceConfig . MemoryLimit = orgConfig . MemoryLimit spaceConfig . InstanceMemoryLimit = orgConfig . InstanceMemoryLimit spaceConfig . TotalRoutes = orgConfig . TotalRoutes spaceConfig . TotalServices = orgConfig . TotalServices spaceConfig . PaidServicePlansAllowed = orgConfig . PaidServicePlansAllowed spaceConfig . TotalReservedRoutePorts = orgConfig . TotalReservedRoutePorts spaceConfig . TotalServiceKeys = orgConfig . TotalServiceKeys spaceConfig . AppInstanceLimit = orgConfig . AppInstanceLimit spaceConfig . AppTaskLimit = orgConfig . AppTaskLimit } if orgSpace . IsolationSegmentGuid != " " { for _ , isosegment := range isolationSegments { if isosegment . GUID == orgSpace . IsolationSegmentGuid { spaceConfig . IsoSegment = isosegment . Name } } } if orgSpace . AllowSSH { spaceConfig . AllowSSH = true } spaceSGName := fmt . Sprintf ( " " , orgName , spaceName ) if spaceSGNames , err := im . SecurityGroupManager . ListSpaceSecurityGroups ( orgSpace . Guid ) ; err == nil { for securityGroupName , _ := range spaceSGNames { lo . G . Infof ( " " , securityGroupName , spaceName ) if securityGroupName != spaceSGName { spaceConfig . ASGs = append ( spaceConfig . ASGs , securityGroupName ) } } } configMgr . AddSpaceToConfig ( spaceConfig ) if sgInfo , ok := securityGroups [ spaceSGName ] ; ok { delete ( securityGroups , spaceSGName ) if rules , err := im . SecurityGroupManager . GetSecurityGroupRules ( sgInfo . Guid ) ; err == nil { configMgr . AddSecurityGroupToSpace ( orgName , spaceName , rules ) } } } } for sgName , sgInfo := range securityGroups { lo . G . Infof ( " " , sgName ) if rules , err := im . SecurityGroupManager . GetSecurityGroupRules ( sgInfo . Guid ) ; err == nil { lo . G . Infof ( " " , sgName ) configMgr . AddSecurityGroup ( sgName , rules ) } else { lo . G . Error ( err ) } } for sgName , sgInfo := range defaultSecurityGroups { lo . G . Infof ( " " , sgName ) if sgInfo . Running { globalConfig . RunningSecurityGroups = append ( globalConfig . RunningSecurityGroups , sgName ) } if sgInfo . Staging { globalConfig . StagingSecurityGroups = append ( globalConfig . StagingSecurityGroups , sgName ) } if rules , err := im . SecurityGroupManager . GetSecurityGroupRules ( sgInfo . Guid ) ; err == nil { lo . G . Infof ( " " , sgName ) configMgr . AddDefaultSecurityGroup ( sgName , rules ) } else { lo . G . Error ( err ) } } orgQuotas , err := im . QuotaManager . Client . ListOrgQuotas ( ) if err != nil { return err } for _ , orgQuota := range orgQuotas { err = configMgr . AddOrgQuota ( config . OrgQuota { Name : orgQuota . Name , AppInstanceLimit : config . AsString ( orgQuota . AppInstanceLimit ) , TotalPrivateDomains : config . AsString ( orgQuota . TotalPrivateDomains ) , TotalReservedRoutePorts : config . AsString ( orgQuota . TotalReservedRoutePorts ) , TotalServiceKeys : config . AsString ( orgQuota . TotalServiceKeys ) , AppTaskLimit : config . AsString ( orgQuota . AppTaskLimit ) , MemoryLimit : config . ByteSize ( orgQuota . MemoryLimit ) , InstanceMemoryLimit : config . ByteSize ( orgQuota . InstanceMemoryLimit ) , TotalRoutes : config . AsString ( orgQuota . TotalRoutes ) , TotalServices : config . AsString ( orgQuota . TotalServices ) , PaidServicePlansAllowed : orgQuota . NonBasicServicesAllowed , } ) if err != nil { return err } } sharedDomains , err := im . SharedDomainManager . CFClient . ListSharedDomains ( ) if err != nil { return err } routerGroups , err := im . SharedDomainManager . RoutingClient . RouterGroups ( ) if err != nil { return err } globalConfig . EnableDeleteSharedDomains = true globalConfig . SharedDomains = make ( map [ string ] config . SharedDomain ) for _ , sharedDomain := range sharedDomains { sharedDomainConfig := config . SharedDomain { Internal : sharedDomain . Internal , } if sharedDomain . RouterGroupGuid != " " { for _ , routerGroup := range routerGroups { if routerGroup . Guid == sharedDomain . RouterGroupGuid { sharedDomainConfig . RouterGroup = routerGroup . Name continue } } } globalConfig . SharedDomains [ sharedDomain . Name ] = sharedDomainConfig } return configMgr . SaveGlobalConfig ( globalConfig ) }
//ExportConfig Imports org and space configuration from an existing CF instance
//Entries part of excludedOrgs and excludedSpaces are not included in the import
ExportConfig Imports org and space configuration from an existing CF instance Entries part of excludedOrgs and excludedSpaces are not included in the import