Skip to content

Commit

Permalink
Merge pull request #75 from ading1977/set-displayname (#76)
Browse files Browse the repository at this point in the history
Allow setting display name for group policies
  • Loading branch information
ading1977 authored Sep 20, 2019
1 parent 5d8ddb9 commit 15606b6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 31 deletions.
110 changes: 81 additions & 29 deletions pkg/builder/group/policy_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ func sellerGroupConstraint(constraintType proto.GroupDTO_ConstraintType, constra

type buyerSellerPolicyData struct {
policyId string
displayName string
constraintType proto.GroupDTO_ConstraintType
buyerData *BuyerPolicyData
sellerData *SellerPolicyData
}

type buyerBuyerPolicyData struct {
policyId string
displayName string
constraintType proto.GroupDTO_ConstraintType
buyerData *BuyerPolicyData
sellerType proto.EntityDTO_EntityType
sellerTypePtr *proto.EntityDTO_EntityType
}

type PlacePolicyBuilder struct {
Expand Down Expand Up @@ -63,6 +65,11 @@ func Place(policyId string) *PlacePolicyBuilder {
return placePolicy
}

func (place *PlacePolicyBuilder) WithDisplayName(displayName string) *PlacePolicyBuilder {
place.displayName = displayName
return place
}

func (place *PlacePolicyBuilder) WithBuyers(buyers *BuyerPolicyData) *PlacePolicyBuilder {
place.buyerData = buyers
return place
Expand All @@ -87,6 +94,11 @@ func DoNotPlace(policyId string) *DoNotPlacePolicyBuilder {
return doNotPlace
}

func (doNotPlace *DoNotPlacePolicyBuilder) WithDisplayName(displayName string) *DoNotPlacePolicyBuilder {
doNotPlace.displayName = displayName
return doNotPlace
}

func (doNotPlace *DoNotPlacePolicyBuilder) WithBuyers(buyers *BuyerPolicyData) *DoNotPlacePolicyBuilder {
doNotPlace.buyerData = buyers
return doNotPlace
Expand All @@ -113,11 +125,24 @@ func PlaceTogether(policyId string) *PlaceTogetherPolicyBuilder {
return placeTogetherPolicy
}

func (placeTogether *PlaceTogetherPolicyBuilder) WithBuyers(buyers *BuyerPolicyData) *PlaceTogetherPolicyBuilder {
func (placeTogether *PlaceTogetherPolicyBuilder) WithDisplayName(
displayName string) *PlaceTogetherPolicyBuilder {
placeTogether.displayName = displayName
return placeTogether
}

func (placeTogether *PlaceTogetherPolicyBuilder) WithBuyers(
buyers *BuyerPolicyData) *PlaceTogetherPolicyBuilder {
placeTogether.buyerData = buyers
return placeTogether
}

func (placeTogether *PlaceTogetherPolicyBuilder) OnSellerType(
sellerType proto.EntityDTO_EntityType) *PlaceTogetherPolicyBuilder {
placeTogether.sellerTypePtr = &sellerType
return placeTogether
}

func (placeTogether *PlaceTogetherPolicyBuilder) Build() ([]*proto.GroupDTO, error) {
return buildBuyerBuyerPolicyGroup(placeTogether.buyerBuyerPolicyData)
}
Expand All @@ -132,23 +157,30 @@ func DoNotPlaceTogether(policyId string) *DoNotPlaceTogetherPolicyBuilder {
return doNotPlaceTogether
}

func (doNotPlace *DoNotPlaceTogetherPolicyBuilder) WithBuyers(buyers *BuyerPolicyData) *DoNotPlaceTogetherPolicyBuilder {
doNotPlace.buyerData = buyers
return doNotPlace
func (doNotPlaceTogether *DoNotPlaceTogetherPolicyBuilder) WithDisplayName(
displayName string) *DoNotPlaceTogetherPolicyBuilder {
doNotPlaceTogether.displayName = displayName
return doNotPlaceTogether
}

func (doNotPlace *DoNotPlaceTogetherPolicyBuilder) OnSellerType(sellerType proto.EntityDTO_EntityType) *DoNotPlaceTogetherPolicyBuilder {
doNotPlace.sellerType = sellerType
return doNotPlace
func (doNotPlaceTogether *DoNotPlaceTogetherPolicyBuilder) WithBuyers(
buyers *BuyerPolicyData) *DoNotPlaceTogetherPolicyBuilder {
doNotPlaceTogether.buyerData = buyers
return doNotPlaceTogether
}

func (doNotPlaceTogether *DoNotPlaceTogetherPolicyBuilder) OnSellerType(
sellerType proto.EntityDTO_EntityType) *DoNotPlaceTogetherPolicyBuilder {
doNotPlaceTogether.sellerTypePtr = &sellerType
return doNotPlaceTogether
}

func (doNotPlace *DoNotPlaceTogetherPolicyBuilder) Build() ([]*proto.GroupDTO, error) {
return buildBuyerBuyerPolicyGroup(doNotPlace.buyerBuyerPolicyData)
func (doNotPlaceTogether *DoNotPlaceTogetherPolicyBuilder) Build() ([]*proto.GroupDTO, error) {
return buildBuyerBuyerPolicyGroup(doNotPlaceTogether.buyerBuyerPolicyData)
}

////========================================================================
func buildBuyerSellerPolicyGroup(policyData *buyerSellerPolicyData) ([]*proto.GroupDTO, error) {

if policyData.buyerData == nil {
return nil, fmt.Errorf("[buildBuyerSellerPolicyGroup] Buyer group data not set")
}
Expand All @@ -159,7 +191,12 @@ func buildBuyerSellerPolicyGroup(policyData *buyerSellerPolicyData) ([]*proto.Gr
var groupDTOs []*proto.GroupDTO

// Buyer group and constraints
buyerGroup, err := createPolicyBuyerGroup(policyData.policyId, policyData.constraintType, policyData.buyerData, policyData.sellerData)
buyerGroup, err := createPolicyBuyerGroup(
policyData.policyId,
policyData.displayName,
policyData.constraintType,
policyData.buyerData,
policyData.sellerData)
if err != nil {
return []*proto.GroupDTO{}, err
} else {
Expand All @@ -172,7 +209,11 @@ func buildBuyerSellerPolicyGroup(policyData *buyerSellerPolicyData) ([]*proto.Gr
}

// Seller group and constraints
sellerGroup, err := createPolicySellerGroup(policyData.policyId, policyData.constraintType, policyData.sellerData)
sellerGroup, err := createPolicySellerGroup(
policyData.policyId,
policyData.displayName,
policyData.constraintType,
policyData.sellerData)
if err != nil {
return []*proto.GroupDTO{}, err
} else {
Expand All @@ -188,13 +229,15 @@ func buildBuyerSellerPolicyGroup(policyData *buyerSellerPolicyData) ([]*proto.Gr
}

// Set up buyer group for a policy
func createPolicyBuyerGroup(policyId string, constraintType proto.GroupDTO_ConstraintType,
func createPolicyBuyerGroup(policyId string, displayName string, constraintType proto.GroupDTO_ConstraintType,
buyerData *BuyerPolicyData, sellerData *SellerPolicyData) (*AbstractConstraintGroupBuilder, error) {

var buyerGroup *AbstractBuilder
if buyerData.entityTypePtr == nil {
return nil, fmt.Errorf("Buyer entity type is not set")
return nil, fmt.Errorf("buyer entity type is not set for policy %s", policyId)
}
if sellerData.entityTypePtr == nil {
return nil, fmt.Errorf("seller entity type is not set for policy %s", policyId)
}
var buyerGroup *AbstractBuilder
entityType := *buyerData.entityTypePtr
if buyerData.entities != nil {
buyerGroup = StaticGroup(policyId).
Expand All @@ -205,7 +248,7 @@ func createPolicyBuyerGroup(policyId string, constraintType proto.GroupDTO_Const
OfType(entityType).
MatchingEntities(buyerData.matchingBuyers)
} else {
return nil, fmt.Errorf("Buyer group member data missing")
return nil, fmt.Errorf("buyer group member data is missing for policy %s", policyId)
}

// constraint info
Expand All @@ -214,8 +257,9 @@ func createPolicyBuyerGroup(policyId string, constraintType proto.GroupDTO_Const
if buyerData.atMost > 0 {
constraintInfoBuilder.AtMostBuyers(buyerData.atMost)
}
if sellerData != nil && sellerData.entityTypePtr != nil {
constraintInfoBuilder.WithSellerType(*sellerData.entityTypePtr)
constraintInfoBuilder.WithSellerType(*sellerData.entityTypePtr)
if displayName != "" {
constraintInfoBuilder.WithDisplayName(displayName)
}

buyerConstraintGroup := &AbstractConstraintGroupBuilder{
Expand All @@ -227,12 +271,12 @@ func createPolicyBuyerGroup(policyId string, constraintType proto.GroupDTO_Const
}

// Set up seller group for a policy
func createPolicySellerGroup(policyId string, constraintType proto.GroupDTO_ConstraintType,
func createPolicySellerGroup(policyId string, displayName string, constraintType proto.GroupDTO_ConstraintType,
sellerData *SellerPolicyData) (*AbstractConstraintGroupBuilder, error) {

var sellerGroup *AbstractBuilder
if sellerData.entityTypePtr == nil {
return nil, fmt.Errorf("Seller entity type is not set")
return nil, fmt.Errorf("seller entity type is not set for policy %s", policyId)
}
entityType := *sellerData.entityTypePtr
if sellerData.entities != nil {
Expand All @@ -244,13 +288,15 @@ func createPolicySellerGroup(policyId string, constraintType proto.GroupDTO_Cons
OfType(entityType).
MatchingEntities(sellerData.matchingBuyers)
} else {
return nil, fmt.Errorf("Seller group member data missing")
return nil, fmt.Errorf("seller group member data is missing for policy %s", policyId)
}

// constraint info
var constraintInfoBuilder *ConstraintInfoBuilder
constraintInfoBuilder = sellerGroupConstraint(constraintType, policyId)

if displayName != "" {
constraintInfoBuilder.WithDisplayName(displayName)
}
sellerConstraintGroup := &AbstractConstraintGroupBuilder{
AbstractBuilder: sellerGroup,
ConstraintInfoBuilder: constraintInfoBuilder,
Expand All @@ -260,20 +306,26 @@ func createPolicySellerGroup(policyId string, constraintType proto.GroupDTO_Cons
}

func buildBuyerBuyerPolicyGroup(policyData *buyerBuyerPolicyData) ([]*proto.GroupDTO, error) {

if policyData.buyerData == nil {
return nil, fmt.Errorf("[buildBuyerBuyerPolicyGroup] Buyer group data not set")
return nil, fmt.Errorf("[buildBuyerBuyerPolicyGroup] Buyer group data is not set")
}
if policyData.sellerTypePtr == nil {
return nil, fmt.Errorf("[buildBuyerBuyerPolicyGroup] Seller entity type is not set")
}

var groupDTOs []*proto.GroupDTO

// Buyer group and constraints
sellerData := &SellerPolicyData{
groupData: &groupData{
entityTypePtr: &policyData.sellerType,
entityTypePtr: policyData.sellerTypePtr,
},
}
buyerGroup, err := createPolicyBuyerGroup(policyData.policyId, policyData.constraintType, policyData.buyerData, sellerData)
buyerGroup, err := createPolicyBuyerGroup(
policyData.policyId,
policyData.displayName,
policyData.constraintType,
policyData.buyerData,
sellerData)
if err != nil {
return []*proto.GroupDTO{}, err
} else {
Expand Down
6 changes: 4 additions & 2 deletions pkg/builder/group/policy_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ func TestPlaceTogetherPolicy(t *testing.T) {
SetProperty("3333-")

placeTogetherPolicyBuilder := PlaceTogether(id).
WithBuyers(DynamicBuyers(SelectedBy(selectionSpec1).and(selectionSpec2)).OfType(eType))
WithBuyers(DynamicBuyers(SelectedBy(selectionSpec1).and(selectionSpec2)).OfType(eType)).
OnSellerType(proto.EntityDTO_CONTAINER_POD)

groupDTOList, _ := assertPlaceTogetherPolicyConditions(t, placeTogetherPolicyBuilder)
for _, groupDTO := range groupDTOList {
Expand All @@ -318,7 +319,8 @@ func TestDoNotPlaceTogetherPolicy(t *testing.T) {
fmt.Printf("%++v\n", buyerData)

doNotPlaceTogetherPolicyBuilder := DoNotPlaceTogether(id).
WithBuyers(StaticBuyers([]string{"container1", "container2"}).OfType(eType))
WithBuyers(StaticBuyers([]string{"container1", "container2"}).OfType(eType)).
OnSellerType(proto.EntityDTO_CONTAINER_POD)

groupDTOList, _ := assertDoNotPlaceTogetherPolicyConditions(t, doNotPlaceTogetherPolicyBuilder)
for _, groupDTO := range groupDTOList {
Expand Down

0 comments on commit 15606b6

Please sign in to comment.