securitygroup/securitygroup.go
func (m *DefaultManager) CreateApplicationSecurityGroups() error {
	spaceConfigs, err := m.Cfg.GetSpaceConfigs()
	if err != nil {
		return err
	}
	sgs, err := m.ListNonDefaultSecurityGroups()
	if err != nil {
		return err
	}

	for _, input := range spaceConfigs {
		space, err := m.SpaceManager.FindSpace(input.Org, input.Space)
		if err != nil {
			return err
		}

		existingSpaceSecurityGroups, err := m.ListSpaceSecurityGroups(space.Guid)
		if err != nil {
			return errors.Wrapf(err, "Unabled to list existing space security groups for org/space [%s/%s]", input.Org, input.Space)
		}
		lo.G.Debugf("Existing space security groups %+v", existingSpaceSecurityGroups)
		// iterate through and assign named security groups to the space - ensuring that they are up to date is
		// done elsewhere.
		for _, securityGroupName := range input.ASGs {
			if sgInfo, ok := sgs[securityGroupName]; ok {
				if _, ok := existingSpaceSecurityGroups[securityGroupName]; !ok {
					err := m.AssignSecurityGroupToSpace(space, sgInfo)
					if err != nil {
						return err
					}
				} else {
					delete(existingSpaceSecurityGroups, securityGroupName)
				}
			} else {
				return fmt.Errorf("Security group [%s] does not exist", securityGroupName)
			}
		}

		spaceSecurityGroupName := fmt.Sprintf("%s-%s", input.Org, input.Space)
		if input.EnableSecurityGroup {
			var sgInfo cfclient.SecGroup
			var ok bool
			if sgInfo, ok = sgs[spaceSecurityGroupName]; ok {
				changed, err := m.hasSecurityGroupChanged(sgInfo, input.GetSecurityGroupContents())
				if err != nil {
					return errors.Wrapf(err, "Checking if security group %s has changed", spaceSecurityGroupName)
				}
				if changed {
					if err := m.UpdateSecurityGroup(sgInfo, input.GetSecurityGroupContents()); err != nil {
						return err
					}
				}
			} else {
				securityGroup, err := m.CreateSecurityGroup(spaceSecurityGroupName, input.GetSecurityGroupContents())
				if err != nil {
					return err
				}
				sgInfo = *securityGroup
			}
			if _, ok := existingSpaceSecurityGroups[spaceSecurityGroupName]; !ok {
				err := m.AssignSecurityGroupToSpace(space, sgInfo)
				if err != nil {
					return err
				}
			} else {
				delete(existingSpaceSecurityGroups, spaceSecurityGroupName)
			}
		}

		if input.EnableUnassignSecurityGroup {
			lo.G.Debugf("Existing space security groups after %+v", existingSpaceSecurityGroups)
			for sgName, _ := range existingSpaceSecurityGroups {
				if sgInfo, ok := sgs[sgName]; ok {
					err := m.UnassignSecurityGroupToSpace(space, sgInfo)
					if err != nil {
						return err
					}
				} else {
					return fmt.Errorf("Security group [%s] does not exist", sgName)
				}
			}
		}
	}
	return nil
}
func (m *DefaultManager) CreateApplicationSecurityGroups() error {
	spaceConfigs, err := m.Cfg.GetSpaceConfigs()
	if err != nil {
		return err
	}
	sgs, err := m.ListNonDefaultSecurityGroups()
	if err != nil {
		return err
	}

	for _, input := range spaceConfigs {
		space, err := m.SpaceManager.FindSpace(input.Org, input.Space)
		if err != nil {
			return err
		}

		existingSpaceSecurityGroups, err := m.ListSpaceSecurityGroups(space.Guid)
		if err != nil {
			return errors.Wrapf(err, "Unabled to list existing space security groups for org/space [%s/%s]", input.Org, input.Space)
		}
		lo.G.Debugf("Existing space security groups %+v", existingSpaceSecurityGroups)
		// iterate through and assign named security groups to the space - ensuring that they are up to date is
		// done elsewhere.
		for _, securityGroupName := range input.ASGs {
			if sgInfo, ok := sgs[securityGroupName]; ok {
				if _, ok := existingSpaceSecurityGroups[securityGroupName]; !ok {
					err := m.AssignSecurityGroupToSpace(space, sgInfo)
					if err != nil {
						return err
					}
				} else {
					delete(existingSpaceSecurityGroups, securityGroupName)
				}
			} else {
				return fmt.Errorf("Security group [%s] does not exist", securityGroupName)
			}
		}

		spaceSecurityGroupName := fmt.Sprintf("%s-%s", input.Org, input.Space)
		if input.EnableSecurityGroup {
			var sgInfo cfclient.SecGroup
			var ok bool
			if sgInfo, ok = sgs[spaceSecurityGroupName]; ok {
				changed, err := m.hasSecurityGroupChanged(sgInfo, input.GetSecurityGroupContents())
				if err != nil {
					return errors.Wrapf(err, "Checking if security group %s has changed", spaceSecurityGroupName)
				}
				if changed {
					if err := m.UpdateSecurityGroup(sgInfo, input.GetSecurityGroupContents()); err != nil {
						return err
					}
				}
			} else {
				securityGroup, err := m.CreateSecurityGroup(spaceSecurityGroupName, input.GetSecurityGroupContents())
				if err != nil {
					return err
				}
				sgInfo = *securityGroup
			}
			if _, ok := existingSpaceSecurityGroups[spaceSecurityGroupName]; !ok {
				err := m.AssignSecurityGroupToSpace(space, sgInfo)
				if err != nil {
					return err
				}
			} else {
				delete(existingSpaceSecurityGroups, spaceSecurityGroupName)
			}
		}

		if input.EnableUnassignSecurityGroup {
			lo.G.Debugf("Existing space security groups after %+v", existingSpaceSecurityGroups)
			for sgName, _ := range existingSpaceSecurityGroups {
				if sgInfo, ok := sgs[sgName]; ok {
					err := m.UnassignSecurityGroupToSpace(space, sgInfo)
					if err != nil {
						return err
					}
				} else {
					return fmt.Errorf("Security group [%s] does not exist", sgName)
				}
			}
		}
	}
	return nil
}
func ( m * DefaultManager ) CreateApplicationSecurityGroups ( ) error { spaceConfigs , err := m . Cfg . GetSpaceConfigs ( ) if err != nil { return err } sgs , err := m . ListNonDefaultSecurityGroups ( ) if err != nil { return err } for _ , input := range spaceConfigs { space , err := m . SpaceManager . FindSpace ( input . Org , input . Space ) if err != nil { return err } existingSpaceSecurityGroups , err := m . ListSpaceSecurityGroups ( space . Guid ) if err != nil { return errors . Wrapf ( err , " " , input . Org , input . Space ) } lo . G . Debugf ( " " , existingSpaceSecurityGroups ) // iterate through and assign named security groups to the space - ensuring that they are up to date is // done elsewhere. for _ , securityGroupName := range input . ASGs { if sgInfo , ok := sgs [ securityGroupName ] ; ok { if _ , ok := existingSpaceSecurityGroups [ securityGroupName ] ; ! ok { err := m . AssignSecurityGroupToSpace ( space , sgInfo ) if err != nil { return err } } else { delete ( existingSpaceSecurityGroups , securityGroupName ) } } else { return fmt . Errorf ( " " , securityGroupName ) } } spaceSecurityGroupName := fmt . Sprintf ( " " , input . Org , input . Space ) if input . EnableSecurityGroup { var sgInfo cfclient . SecGroup var ok bool if sgInfo , ok = sgs [ spaceSecurityGroupName ] ; ok { changed , err := m . hasSecurityGroupChanged ( sgInfo , input . GetSecurityGroupContents ( ) ) if err != nil { return errors . Wrapf ( err , " " , spaceSecurityGroupName ) } if changed { if err := m . UpdateSecurityGroup ( sgInfo , input . GetSecurityGroupContents ( ) ) ; err != nil { return err } } } else { securityGroup , err := m . CreateSecurityGroup ( spaceSecurityGroupName , input . GetSecurityGroupContents ( ) ) if err != nil { return err } sgInfo = * securityGroup } if _ , ok := existingSpaceSecurityGroups [ spaceSecurityGroupName ] ; ! ok { err := m . AssignSecurityGroupToSpace ( space , sgInfo ) if err != nil { return err } } else { delete ( existingSpaceSecurityGroups , spaceSecurityGroupName ) } } if input . EnableUnassignSecurityGroup { lo . G . Debugf ( " " , existingSpaceSecurityGroups ) for sgName , _ := range existingSpaceSecurityGroups { if sgInfo , ok := sgs [ sgName ] ; ok { err := m . UnassignSecurityGroupToSpace ( space , sgInfo ) if err != nil { return err } } else { return fmt . Errorf ( " " , sgName ) } } } } return nil }
//CreateApplicationSecurityGroups -
CreateApplicationSecurityGroups -